From 6252b49c4754620e641d18bc4f8e620a570e4e66 Mon Sep 17 00:00:00 2001 From: AJR Date: Sun, 25 Sep 2022 22:08:17 -0400 Subject: corefile.h: Move filename utilities to path.h --- src/emu/diimage.cpp | 1 + src/emu/dislot.cpp | 1 + src/emu/emuopts.cpp | 1 + src/emu/rendfont.cpp | 1 + src/emu/validity.cpp | 1 + src/emu/video.cpp | 1 + src/frontend/mame/clifront.cpp | 1 + src/frontend/mame/mameopts.cpp | 4 +- src/frontend/mame/media_ident.cpp | 1 + src/frontend/mame/ui/custui.cpp | 1 + src/frontend/mame/ui/filecreate.cpp | 1 + src/frontend/mame/ui/inifile.cpp | 1 + src/frontend/mame/ui/miscmenu.cpp | 2 + src/frontend/mame/ui/selsoft.cpp | 4 +- src/frontend/mame/ui/state.cpp | 2 + src/lib/util/corefile.cpp | 71 -------------------------------- src/lib/util/corefile.h | 16 -------- src/lib/util/path.cpp | 81 ++++++++++++++++++++++++++++++++++++- src/lib/util/path.h | 19 ++++++++- src/lib/util/zippath.cpp | 1 + src/tools/chdman.cpp | 1 + src/tools/floptool.cpp | 2 +- src/tools/imgtool/imgtool.cpp | 2 +- src/tools/imgtool/main.cpp | 2 +- src/tools/imgtool/stream.cpp | 1 + 25 files changed, 125 insertions(+), 94 deletions(-) diff --git a/src/emu/diimage.cpp b/src/emu/diimage.cpp index f60fa032dd8..016f577f071 100644 --- a/src/emu/diimage.cpp +++ b/src/emu/diimage.cpp @@ -20,6 +20,7 @@ #include "corestr.h" #include "opresolv.h" +#include "path.h" #include "zippath.h" #include diff --git a/src/emu/dislot.cpp b/src/emu/dislot.cpp index 868b0ebf103..06ff055e293 100644 --- a/src/emu/dislot.cpp +++ b/src/emu/dislot.cpp @@ -10,6 +10,7 @@ #include "emuopts.h" #include "corestr.h" +#include "path.h" #include "zippath.h" diff --git a/src/emu/emuopts.cpp b/src/emu/emuopts.cpp index 5e0e5735a2e..827b7249e07 100644 --- a/src/emu/emuopts.cpp +++ b/src/emu/emuopts.cpp @@ -15,6 +15,7 @@ #include "hashfile.h" #include "corestr.h" +#include "path.h" #include diff --git a/src/emu/rendfont.cpp b/src/emu/rendfont.cpp index ed60987e455..998e825b61d 100644 --- a/src/emu/rendfont.cpp +++ b/src/emu/rendfont.cpp @@ -16,6 +16,7 @@ #include "corestr.h" #include "coreutil.h" +#include "path.h" #include "osdepend.h" #include "uismall.fh" diff --git a/src/emu/validity.cpp b/src/emu/validity.cpp index f76ea7d24fb..1d0db1c421c 100644 --- a/src/emu/validity.cpp +++ b/src/emu/validity.cpp @@ -17,6 +17,7 @@ #include "video/rgbutil.h" #include "corestr.h" +#include "path.h" #include "unicode.h" #include diff --git a/src/emu/video.cpp b/src/emu/video.cpp index bccaac58a00..8f9e9e3623a 100644 --- a/src/emu/video.cpp +++ b/src/emu/video.cpp @@ -19,6 +19,7 @@ #include "screen.h" #include "corestr.h" +#include "path.h" #include "png.h" #include "xmlfile.h" diff --git a/src/frontend/mame/clifront.cpp b/src/frontend/mame/clifront.cpp index 9e04f1ed77b..6faa41dec41 100644 --- a/src/frontend/mame/clifront.cpp +++ b/src/frontend/mame/clifront.cpp @@ -31,6 +31,7 @@ #include "chd.h" #include "corestr.h" +#include "path.h" #include "unzip.h" #include "xmlfile.h" diff --git a/src/frontend/mame/mameopts.cpp b/src/frontend/mame/mameopts.cpp index bbdf8a692b4..c6031308592 100644 --- a/src/frontend/mame/mameopts.cpp +++ b/src/frontend/mame/mameopts.cpp @@ -15,10 +15,12 @@ #include "fileio.h" #include "screen.h" #include "softlist_dev.h" -#include "zippath.h" #include "hashfile.h" #include "clifront.h" +#include "path.h" +#include "zippath.h" + #include #include diff --git a/src/frontend/mame/media_ident.cpp b/src/frontend/mame/media_ident.cpp index 7e6665bc765..d09858198ea 100644 --- a/src/frontend/mame/media_ident.cpp +++ b/src/frontend/mame/media_ident.cpp @@ -14,6 +14,7 @@ #include "softlist_dev.h" #include "jedparse.h" +#include "path.h" #include "unzip.h" diff --git a/src/frontend/mame/ui/custui.cpp b/src/frontend/mame/ui/custui.cpp index 090f657169a..febb6a89472 100644 --- a/src/frontend/mame/ui/custui.cpp +++ b/src/frontend/mame/ui/custui.cpp @@ -22,6 +22,7 @@ #include "corestr.h" #include "osdepend.h" +#include "path.h" #include #include diff --git a/src/frontend/mame/ui/filecreate.cpp b/src/frontend/mame/ui/filecreate.cpp index 44d50108b8c..675ddc93ea6 100644 --- a/src/frontend/mame/ui/filecreate.cpp +++ b/src/frontend/mame/ui/filecreate.cpp @@ -17,6 +17,7 @@ #include "ui/ui.h" #include "ui/utils.h" +#include "path.h" #include "zippath.h" #include diff --git a/src/frontend/mame/ui/inifile.cpp b/src/frontend/mame/ui/inifile.cpp index 55c7da2d919..c4217f93fa3 100644 --- a/src/frontend/mame/ui/inifile.cpp +++ b/src/frontend/mame/ui/inifile.cpp @@ -20,6 +20,7 @@ #include "softlist_dev.h" #include "corestr.h" +#include "path.h" #include #include diff --git a/src/frontend/mame/ui/miscmenu.cpp b/src/frontend/mame/ui/miscmenu.cpp index 0733f554506..d043157dbea 100644 --- a/src/frontend/mame/ui/miscmenu.cpp +++ b/src/frontend/mame/ui/miscmenu.cpp @@ -29,6 +29,8 @@ #include "romload.h" #include "uiinput.h" +#include "path.h" + #include #include #include diff --git a/src/frontend/mame/ui/selsoft.cpp b/src/frontend/mame/ui/selsoft.cpp index c15fcff7a26..7255db8427f 100644 --- a/src/frontend/mame/ui/selsoft.cpp +++ b/src/frontend/mame/ui/selsoft.cpp @@ -16,7 +16,6 @@ #include "ui/inifile.h" #include "ui/selector.h" -#include "corestr.h" #include "drivenum.h" #include "emuopts.h" #include "fileio.h" @@ -25,6 +24,9 @@ #include "softlist_dev.h" #include "uiinput.h" #include "luaengine.h" + +#include "corestr.h" +#include "path.h" #include "unicode.h" #include diff --git a/src/frontend/mame/ui/state.cpp b/src/frontend/mame/ui/state.cpp index 6aa5d3c6fa4..635b980c6ba 100644 --- a/src/frontend/mame/ui/state.cpp +++ b/src/frontend/mame/ui/state.cpp @@ -14,6 +14,8 @@ #include "emuopts.h" #include "inputdev.h" +#include "path.h" + namespace ui { diff --git a/src/lib/util/corefile.cpp b/src/lib/util/corefile.cpp index 6bb6a043a04..4fed0e0a28b 100644 --- a/src/lib/util/corefile.cpp +++ b/src/lib/util/corefile.cpp @@ -12,13 +12,10 @@ #include "coretmpl.h" #include "osdcore.h" -#include "path.h" #include "unicode.h" #include "vecstream.h" -#include #include -#include #include #include #include @@ -1057,71 +1054,3 @@ std::error_condition core_file::load(std::string_view filename, std::vector= extlen; - - // work backwards checking for a match - while (matches && extlen > 0 && namelen > 0) - { - if (tolower((uint8_t)filename[--namelen]) != tolower((uint8_t)extension[--extlen])) - matches = false; - } - - return matches; -} diff --git a/src/lib/util/corefile.h b/src/lib/util/corefile.h index c2731a1c4ed..6a7d14ea439 100644 --- a/src/lib/util/corefile.h +++ b/src/lib/util/corefile.h @@ -100,20 +100,4 @@ public: } // namespace util -/*************************************************************************** - FUNCTION PROTOTYPES -***************************************************************************/ - -/* ----- filename utilities ----- */ - -// extract the base part of a filename (remove extensions and paths) -std::string_view core_filename_extract_base(std::string_view name, bool strip_extension = false) noexcept; - -// extracts the file extension from a filename -std::string_view core_filename_extract_extension(std::string_view filename, bool strip_period = false) noexcept; - -// true if the given filename ends with a particular extension -bool core_filename_ends_with(std::string_view filename, std::string_view extension) noexcept; - - #endif // MAME_LIB_UTIL_COREFILE_H diff --git a/src/lib/util/path.cpp b/src/lib/util/path.cpp index 1b8a2f424dd..bdfb7f4443f 100644 --- a/src/lib/util/path.cpp +++ b/src/lib/util/path.cpp @@ -1,3 +1,82 @@ // license:BSD-3-Clause -// copyright-holders:Vas Crabb +// copyright-holders:Aaron Giles, Nathan Woods +/*************************************************************************** + + path.cpp + + Path and filename utilities. + +***************************************************************************/ + #include "path.h" + +#include +#include +#include + + +/*************************************************************************** + FILENAME UTILITIES +***************************************************************************/ + +// ------------------------------------------------- +// core_filename_extract_base - extract the base +// name from a filename; note that this makes +// assumptions about path separators +// ------------------------------------------------- + +std::string_view core_filename_extract_base(std::string_view name, bool strip_extension) noexcept +{ + // find the start of the basename + auto const start = std::find_if(name.rbegin(), name.rend(), &util::is_directory_separator); + if (start == name.rbegin()) + return std::string_view(); + + // find the end of the basename + auto const chop_position = strip_extension + ? std::find(name.rbegin(), start, '.') + : start; + auto const end = ((chop_position != start) && (std::next(chop_position) != start)) + ? std::next(chop_position) + : name.rbegin(); + + return std::string_view(&*start.base(), end.base() - start.base()); +} + + +// ------------------------------------------------- +// core_filename_extract_extension +// ------------------------------------------------- + +std::string_view core_filename_extract_extension(std::string_view filename, bool strip_period) noexcept +{ + auto loc = filename.find_last_of('.'); + if (loc != std::string_view::npos) + return filename.substr(loc + (strip_period ? 1 : 0)); + else + return std::string_view(); +} + + +// ------------------------------------------------- +// core_filename_ends_with - does the given +// filename end with the specified extension? +// ------------------------------------------------- + +bool core_filename_ends_with(std::string_view filename, std::string_view extension) noexcept +{ + auto namelen = filename.length(); + auto extlen = extension.length(); + + // 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 (std::tolower(uint8_t(filename[--namelen])) != std::tolower(uint8_t(extension[--extlen]))) + matches = false; + } + + return matches; +} diff --git a/src/lib/util/path.h b/src/lib/util/path.h index 8638bcb332b..204c3503bbf 100644 --- a/src/lib/util/path.h +++ b/src/lib/util/path.h @@ -1,5 +1,5 @@ // license:BSD-3-Clause -// copyright-holders:Vas Crabb +// copyright-holders:Vas Crabb, Aaron Giles /*************************************************************************** path.h @@ -13,6 +13,7 @@ #include "osdfile.h" // for PATH_SEPARATOR #include +#include #include @@ -80,4 +81,20 @@ inline std::string path_concat(T &&first, U &&... more) } // namespace util + +/*************************************************************************** + FUNCTION PROTOTYPES +***************************************************************************/ + +/* ----- filename utilities ----- */ + +// extract the base part of a filename (remove extensions and paths) +std::string_view core_filename_extract_base(std::string_view name, bool strip_extension = false) noexcept; + +// extracts the file extension from a filename +std::string_view core_filename_extract_extension(std::string_view filename, bool strip_period = false) noexcept; + +// true if the given filename ends with a particular extension +bool core_filename_ends_with(std::string_view filename, std::string_view extension) noexcept; + #endif // MAME_LIB_UTIL_PATH_H diff --git a/src/lib/util/zippath.cpp b/src/lib/util/zippath.cpp index 49cced27ff1..a5bb2cae8af 100644 --- a/src/lib/util/zippath.cpp +++ b/src/lib/util/zippath.cpp @@ -11,6 +11,7 @@ #include "zippath.h" #include "corestr.h" +#include "path.h" #include "unzip.h" #include diff --git a/src/tools/chdman.cpp b/src/tools/chdman.cpp index e35f742b740..e24d5a58588 100644 --- a/src/tools/chdman.cpp +++ b/src/tools/chdman.cpp @@ -15,6 +15,7 @@ #include "corefile.h" #include "hashing.h" #include "md5.h" +#include "path.h" #include "strformat.h" #include "vbiparse.h" diff --git a/src/tools/floptool.cpp b/src/tools/floptool.cpp index 4ca0c39b1ca..eaf43d0861e 100644 --- a/src/tools/floptool.cpp +++ b/src/tools/floptool.cpp @@ -8,9 +8,9 @@ #include "image_handler.h" -#include "corefile.h" #include "corestr.h" #include "ioprocs.h" +#include "path.h" #include "strformat.h" #include "osdcomm.h" diff --git a/src/tools/imgtool/imgtool.cpp b/src/tools/imgtool/imgtool.cpp index 7bb230d1e38..445fba33ebb 100644 --- a/src/tools/imgtool/imgtool.cpp +++ b/src/tools/imgtool/imgtool.cpp @@ -16,9 +16,9 @@ #include "formats/imageutl.h" -#include "corefile.h" #include "corestr.h" #include "opresolv.h" +#include "path.h" #include #include diff --git a/src/tools/imgtool/main.cpp b/src/tools/imgtool/main.cpp index d8a166e247f..8dd6affbc43 100644 --- a/src/tools/imgtool/main.cpp +++ b/src/tools/imgtool/main.cpp @@ -13,9 +13,9 @@ #include "main.h" #include "modules.h" -#include "corefile.h" #include "corestr.h" #include "opresolv.h" +#include "path.h" #include "strformat.h" #include "unicode.h" diff --git a/src/tools/imgtool/stream.cpp b/src/tools/imgtool/stream.cpp index 5bd654af3d7..bd9ea3bd286 100644 --- a/src/tools/imgtool/stream.cpp +++ b/src/tools/imgtool/stream.cpp @@ -14,6 +14,7 @@ #include "corefile.h" #include "ioprocs.h" +#include "path.h" #include "unzip.h" #include -- cgit v1.2.3