From 09b3fbf52cbaf756b0e3e55773161aa53290b1d0 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Sun, 10 Jul 2016 23:38:44 -0400 Subject: [PATCH 1/8] C++-ification of some corefile static functions --- src/lib/util/corefile.cpp | 16 ++++++++-------- src/lib/util/corefile.h | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lib/util/corefile.cpp b/src/lib/util/corefile.cpp index 09d99c2072b..01c539680a2 100644 --- a/src/lib/util/corefile.cpp +++ b/src/lib/util/corefile.cpp @@ -1270,11 +1270,11 @@ 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])) + const char *start = name.c_str() + name.length(); + while (start > name.c_str() && !util::is_directory_separator(start[-1])) start--; /* copy the rest into an astring */ @@ -1292,17 +1292,17 @@ 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(); + bool matches = true; /* work backwards checking for a match */ while (extlen > 0) if (tolower((UINT8)filename[--namelen]) != tolower((UINT8)extension[--extlen])) { - matches = FALSE; + matches = false; break; } diff --git a/src/lib/util/corefile.h b/src/lib/util/corefile.h index 8f71832411f..5fa6d5af9a7 100644 --- a/src/lib/util/corefile.h +++ b/src/lib/util/corefile.h @@ -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 From e1f3bc9b8a875dd211c4b60e54fb4f7f17c1dafc Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Sun, 10 Jul 2016 23:43:16 -0400 Subject: [PATCH 2/8] Now using core_filename_extract_base() in menu_file_create ctor --- src/frontend/mame/ui/filecreate.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/frontend/mame/ui/filecreate.cpp b/src/frontend/mame/ui/filecreate.cpp index e54692e600f..07f0abdf9c5 100644 --- a/src/frontend/mame/ui/filecreate.cpp +++ b/src/frontend/mame/ui/filecreate.cpp @@ -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); } From 96066caca0b5f078de529bb0c53971b7c1f4d552 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Mon, 11 Jul 2016 07:24:02 -0400 Subject: [PATCH 3/8] Removed some c_str() calls that are no longer necessary --- src/tools/chdman.cpp | 4 ++-- src/tools/src2html.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tools/chdman.cpp b/src/tools/chdman.cpp index a2f154bcfc6..9150048f02b 100644 --- a/src/tools/chdman.cpp +++ b/src/tools/chdman.cpp @@ -2432,11 +2432,11 @@ static void do_extract_cd(parameters_t ¶ms) 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 diff --git a/src/tools/src2html.cpp b/src/tools/src2html.cpp index 9d89a7d220f..e64fa12a162 100644 --- a/src/tools/src2html.cpp +++ b/src/tools/src2html.cpp @@ -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; From 130e05cc4eb41119e56dee22aeb7f8126a8206ea Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Mon, 11 Jul 2016 08:26:49 -0400 Subject: [PATCH 4/8] Fixed stupid compilation error --- src/tools/chdman.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/chdman.cpp b/src/tools/chdman.cpp index 9150048f02b..86b406a4393 100644 --- a/src/tools/chdman.cpp +++ b/src/tools/chdman.cpp @@ -2436,7 +2436,7 @@ static void do_extract_cd(parameters_t ¶ms) } else { - output_track_metadata(mode, *output_toc_file, tracknum, trackinfo, core_filename_extract_base(output_bin_file_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 From 2bd5932b42b56741dee27bf25bdac4158aaa6696 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Mon, 11 Jul 2016 08:27:03 -0400 Subject: [PATCH 5/8] Deeper C++-ification --- src/lib/util/corefile.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/lib/util/corefile.cpp b/src/lib/util/corefile.cpp index 01c539680a2..725316b0dff 100644 --- a/src/lib/util/corefile.cpp +++ b/src/lib/util/corefile.cpp @@ -1272,17 +1272,21 @@ core_file::core_file() std::string core_filename_extract_base(const std::string &name, bool strip_extension) { - /* find the start of the name */ - const char *start = name.c_str() + name.length(); - while (start > name.c_str() && !util::is_directory_separator(start[-1])) + // find the start of the basename + auto start = name.end(); + while (start != name.begin() && !util::is_directory_separator(start[-1])) start--; - /* copy the rest into an astring */ - std::string result(start); + // find the end of the basename + auto chop_position = strip_extension + ? name.find_last_of('.') + : std::string::npos; + auto end = chop_position != std::string::npos + ? name.begin() + chop_position + : name.end(); - /* 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, end); return result; } From 0e4cb93bc58d8b598bee9ff4ea6e5949853fadeb Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Mon, 11 Jul 2016 10:20:56 -0400 Subject: [PATCH 6/8] Adopting std::find() and std::find_if() in core_filename_extract_base() --- src/lib/util/corefile.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/lib/util/corefile.cpp b/src/lib/util/corefile.cpp index 725316b0dff..0fe87f15982 100644 --- a/src/lib/util/corefile.cpp +++ b/src/lib/util/corefile.cpp @@ -1273,20 +1273,18 @@ core_file::core_file() std::string core_filename_extract_base(const std::string &name, bool strip_extension) { // find the start of the basename - auto start = name.end(); - while (start != name.begin() && !util::is_directory_separator(start[-1])) - start--; + auto const start = std::find_if(name.rbegin(), name.rend(), [](char c) { return util::is_directory_separator(c); }); // find the end of the basename - auto chop_position = strip_extension - ? name.find_last_of('.') - : std::string::npos; - auto end = chop_position != std::string::npos - ? name.begin() + chop_position - : name.end(); + auto const chop_position = strip_extension + ? std::find(name.rbegin(), start, '.') + : start; + auto const end = (chop_position != start) + ? chop_position + 1 + : name.rbegin(); // copy the result into an string - std::string result(start, end); + std::string result(start.base(), end.base()); return result; } From 32230ae6485f76c7d0745c3e981a6aa14d4e0be0 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Mon, 11 Jul 2016 10:46:36 -0400 Subject: [PATCH 7/8] Adding a check for reverse overflow when extensions is longer than filename --- src/lib/util/corefile.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/util/corefile.cpp b/src/lib/util/corefile.cpp index 0fe87f15982..5c530b41f30 100644 --- a/src/lib/util/corefile.cpp +++ b/src/lib/util/corefile.cpp @@ -1300,8 +1300,8 @@ bool core_filename_ends_with(const std::string &filename, const std::string &ext auto extlen = extension.length(); bool matches = true; - /* work backwards checking for a match */ - while (extlen > 0) + // work backwards checking for a match + while (extlen > 0 && namelen > 0) if (tolower((UINT8)filename[--namelen]) != tolower((UINT8)extension[--extlen])) { matches = false; From 210d5cb4513b7ba67b6990624cf19fee1ee0a9f9 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Mon, 11 Jul 2016 18:05:23 -0400 Subject: [PATCH 8/8] Fixed issue that could cause core_filename_ends_with() to return true when extension.size() > filename.size() --- src/lib/util/corefile.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lib/util/corefile.cpp b/src/lib/util/corefile.cpp index 5c530b41f30..c06426015f7 100644 --- a/src/lib/util/corefile.cpp +++ b/src/lib/util/corefile.cpp @@ -1298,15 +1298,16 @@ bool core_filename_ends_with(const std::string &filename, const std::string &ext { auto namelen = filename.length(); auto extlen = extension.length(); - bool matches = true; + // 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 (extlen > 0 && namelen > 0) + while (matches && extlen > 0 && namelen > 0) + { if (tolower((UINT8)filename[--namelen]) != tolower((UINT8)extension[--extlen])) - { matches = false; - break; - } + } return matches; }