Merge pull request #1081 from npwoods/core_file_changes

Core file changes
This commit is contained in:
Vas Crabb 2016-07-12 10:57:42 +10:00 committed by GitHub
commit 52c452a207
5 changed files with 29 additions and 29 deletions

View File

@ -148,12 +148,9 @@ menu_file_create::menu_file_create(mame_ui_manager &mui, render_container &conta
{
m_image = image;
m_ok = true;
auto const sep = current_file.rfind(PATH_SEPARATOR);
m_filename.reserve(1024);
m_filename = sep != std::string::npos
? current_file.substr(sep + strlen(PATH_SEPARATOR), current_file.size() - sep - strlen(PATH_SEPARATOR))
: current_file;
m_filename = core_filename_extract_base(current_file);
}

View File

@ -1270,19 +1270,21 @@ core_file::core_file()
assumptions about path separators
-------------------------------------------------*/
std::string core_filename_extract_base(const char *name, bool strip_extension)
std::string core_filename_extract_base(const std::string &name, bool strip_extension)
{
/* find the start of the name */
const char *start = name + strlen(name);
while (start > name && !util::is_directory_separator(start[-1]))
start--;
// find the start of the basename
auto const start = std::find_if(name.rbegin(), name.rend(), [](char c) { return util::is_directory_separator(c); });
/* copy the rest into an astring */
std::string result(start);
// find the end of the basename
auto const chop_position = strip_extension
? std::find(name.rbegin(), start, '.')
: start;
auto const end = (chop_position != start)
? chop_position + 1
: name.rbegin();
/* chop the extension if present */
if (strip_extension)
result = result.substr(0, result.find_last_of('.'));
// copy the result into an string
std::string result(start.base(), end.base());
return result;
}
@ -1292,19 +1294,20 @@ std::string core_filename_extract_base(const char *name, bool strip_extension)
filename end with the specified extension?
-------------------------------------------------*/
int core_filename_ends_with(const char *filename, const char *extension)
bool core_filename_ends_with(const std::string &filename, const std::string &extension)
{
int namelen = strlen(filename);
int extlen = strlen(extension);
int matches = TRUE;
auto namelen = filename.length();
auto extlen = extension.length();
/* work backwards checking for a match */
while (extlen > 0)
// first if the extension is bigger than the name, we definitely don't match
bool matches = namelen >= extlen;
// work backwards checking for a match
while (matches && extlen > 0 && namelen > 0)
{
if (tolower((UINT8)filename[--namelen]) != tolower((UINT8)extension[--extlen]))
{
matches = FALSE;
break;
}
matches = false;
}
return matches;
}

View File

@ -140,10 +140,10 @@ protected:
/* ----- filename utilities ----- */
/* extract the base part of a filename (remove extensions and paths) */
std::string core_filename_extract_base(const char *name, bool strip_extension = false);
std::string core_filename_extract_base(const std::string &name, bool strip_extension = false);
/* true if the given filename ends with a particular extension */
int core_filename_ends_with(const char *filename, const char *extension);
bool core_filename_ends_with(const std::string &filename, const std::string &extension);
#endif // MAME_LIB_UTIL_COREFILE_H

View File

@ -2432,11 +2432,11 @@ static void do_extract_cd(parameters_t &params)
const cdrom_track_info &trackinfo = toc->tracks[tracknum];
if (mode == MODE_GDI)
{
output_track_metadata(mode, *output_toc_file, tracknum, trackinfo, core_filename_extract_base(trackbin_name.c_str()).c_str(), discoffs, outputoffs);
output_track_metadata(mode, *output_toc_file, tracknum, trackinfo, core_filename_extract_base(trackbin_name).c_str(), discoffs, outputoffs);
}
else
{
output_track_metadata(mode, *output_toc_file, tracknum, trackinfo, core_filename_extract_base(output_bin_file_str->c_str()).c_str(), discoffs, outputoffs);
output_track_metadata(mode, *output_toc_file, tracknum, trackinfo, core_filename_extract_base(*output_bin_file_str).c_str(), discoffs, outputoffs);
}
// If this is bin/cue output and the CHD contains subdata, warn the user and don't include

View File

@ -411,7 +411,7 @@ static int recurse_dir(int srcrootlen, int dstrootlen, std::string &srcdir, std:
// make sure we care, first
file_type type = FILE_TYPE_INVALID;
for (auto & elem : extension_lookup)
if (core_filename_ends_with(curlist->name.c_str(), elem.extension))
if (core_filename_ends_with(curlist->name, elem.extension))
{
type = elem.type;
break;