corefile.h: Move filename utilities to path.h

This commit is contained in:
AJR 2022-09-25 22:08:17 -04:00
parent 7e61dc3a3d
commit 6252b49c47
25 changed files with 125 additions and 94 deletions

View File

@ -20,6 +20,7 @@
#include "corestr.h"
#include "opresolv.h"
#include "path.h"
#include "zippath.h"
#include <algorithm>

View File

@ -10,6 +10,7 @@
#include "emuopts.h"
#include "corestr.h"
#include "path.h"
#include "zippath.h"

View File

@ -15,6 +15,7 @@
#include "hashfile.h"
#include "corestr.h"
#include "path.h"
#include <stack>

View File

@ -16,6 +16,7 @@
#include "corestr.h"
#include "coreutil.h"
#include "path.h"
#include "osdepend.h"
#include "uismall.fh"

View File

@ -17,6 +17,7 @@
#include "video/rgbutil.h"
#include "corestr.h"
#include "path.h"
#include "unicode.h"
#include <cctype>

View File

@ -19,6 +19,7 @@
#include "screen.h"
#include "corestr.h"
#include "path.h"
#include "png.h"
#include "xmlfile.h"

View File

@ -31,6 +31,7 @@
#include "chd.h"
#include "corestr.h"
#include "path.h"
#include "unzip.h"
#include "xmlfile.h"

View File

@ -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 <cctype>
#include <stack>

View File

@ -14,6 +14,7 @@
#include "softlist_dev.h"
#include "jedparse.h"
#include "path.h"
#include "unzip.h"

View File

@ -22,6 +22,7 @@
#include "corestr.h"
#include "osdepend.h"
#include "path.h"
#include <algorithm>
#include <iterator>

View File

@ -17,6 +17,7 @@
#include "ui/ui.h"
#include "ui/utils.h"
#include "path.h"
#include "zippath.h"
#include <cstring>

View File

@ -20,6 +20,7 @@
#include "softlist_dev.h"
#include "corestr.h"
#include "path.h"
#include <algorithm>
#include <cstring>

View File

@ -29,6 +29,8 @@
#include "romload.h"
#include "uiinput.h"
#include "path.h"
#include <algorithm>
#include <cstring>
#include <fstream>

View File

@ -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 <algorithm>

View File

@ -14,6 +14,8 @@
#include "emuopts.h"
#include "inputdev.h"
#include "path.h"
namespace ui {

View File

@ -12,13 +12,10 @@
#include "coretmpl.h"
#include "osdcore.h"
#include "path.h"
#include "unicode.h"
#include "vecstream.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstring>
#include <iterator>
#include <limits>
@ -1057,71 +1054,3 @@ std::error_condition core_file::load(std::string_view filename, std::vector<uint
}
} // namespace util
/***************************************************************************
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 (tolower((uint8_t)filename[--namelen]) != tolower((uint8_t)extension[--extlen]))
matches = false;
}
return matches;
}

View File

@ -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

View File

@ -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 <algorithm>
#include <cctype>
#include <iterator>
/***************************************************************************
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;
}

View File

@ -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 <string>
#include <string_view>
#include <utility>
@ -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

View File

@ -11,6 +11,7 @@
#include "zippath.h"
#include "corestr.h"
#include "path.h"
#include "unzip.h"
#include <algorithm>

View File

@ -15,6 +15,7 @@
#include "corefile.h"
#include "hashing.h"
#include "md5.h"
#include "path.h"
#include "strformat.h"
#include "vbiparse.h"

View File

@ -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"

View File

@ -16,9 +16,9 @@
#include "formats/imageutl.h"
#include "corefile.h"
#include "corestr.h"
#include "opresolv.h"
#include "path.h"
#include <cstdarg>
#include <cstdio>

View File

@ -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"

View File

@ -14,6 +14,7 @@
#include "corefile.h"
#include "ioprocs.h"
#include "path.h"
#include "unzip.h"
#include <cassert>