mirror of
https://github.com/holub/mame
synced 2025-07-03 00:56:03 +03:00
zippath.cpp: Checkpoint
- Adopt std::string_view as the input parameter type for most functions. (This necessitates some explicit copying to std::string since other APIs have not been updated yet.) - Remove zippath_parent_basename, whose implementation was utterly broken and fortunately unused.
This commit is contained in:
parent
979b96b6cd
commit
29e96c84a8
@ -46,21 +46,16 @@ int is_path_separator(char c)
|
|||||||
// is_root - tests to see if this path is the root
|
// is_root - tests to see if this path is the root
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
bool is_root(std::string const &path)
|
bool is_root(std::string_view path)
|
||||||
{
|
{
|
||||||
// FIXME: get rid of the assumption that paths are DOS-like
|
// FIXME: get rid of the assumption that paths are DOS-like
|
||||||
|
|
||||||
std::string::size_type i = 0;
|
|
||||||
|
|
||||||
// skip drive letter
|
// skip drive letter
|
||||||
if (isalpha(path[i]) && (path[i + 1] == ':'))
|
if (path.length() >= 2 && isalpha(path[0]) && (path[1] == ':'))
|
||||||
i += 2;
|
path.remove_prefix(2);
|
||||||
|
|
||||||
// skip path separators
|
// skip path separators
|
||||||
while (is_path_separator(path[i]))
|
return path.find_first_not_of(PATH_SEPARATOR) == std::string_view::npos;
|
||||||
i++;
|
|
||||||
|
|
||||||
return path[i] == '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -72,8 +67,7 @@ bool is_root(std::string const &path)
|
|||||||
|
|
||||||
bool is_7z_file(std::string const &path)
|
bool is_7z_file(std::string const &path)
|
||||||
{
|
{
|
||||||
auto const s = path.rfind('.');
|
return core_filename_ends_with(path, ".7z");
|
||||||
return (std::string::npos != s) && !core_stricmp(path.c_str() + s, ".7z");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -84,8 +78,7 @@ bool is_7z_file(std::string const &path)
|
|||||||
|
|
||||||
bool is_zip_file(std::string const &path)
|
bool is_zip_file(std::string const &path)
|
||||||
{
|
{
|
||||||
auto const s = path.rfind('.');
|
return core_filename_ends_with(path, ".7zip");
|
||||||
return (std::string::npos != s) && !core_stricmp(path.c_str() + s, ".zip");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -119,7 +112,7 @@ bool is_zip_path_separator(char c)
|
|||||||
// character, normalizing separators as '/'
|
// character, normalizing separators as '/'
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
char next_path_char(std::string const &s, std::string::size_type &pos)
|
char next_path_char(std::string_view s, std::string_view::size_type &pos)
|
||||||
{
|
{
|
||||||
// skip over any initial separators
|
// skip over any initial separators
|
||||||
if (pos == 0)
|
if (pos == 0)
|
||||||
@ -158,11 +151,11 @@ char next_path_char(std::string const &s, std::string::size_type &pos)
|
|||||||
// type of a sub path in a zip file
|
// type of a sub path in a zip file
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
int zippath_find_sub_path(archive_file &zipfile, std::string const &subpath, osd::directory::entry::entry_type &type)
|
int zippath_find_sub_path(archive_file &zipfile, std::string_view subpath, osd::directory::entry::entry_type &type)
|
||||||
{
|
{
|
||||||
for (int header = zipfile.first_file(); header >= 0; header = zipfile.next_file())
|
for (int header = zipfile.first_file(); header >= 0; header = zipfile.next_file())
|
||||||
{
|
{
|
||||||
std::string::size_type i = 0, j = 0;
|
std::string_view::size_type i = 0, j = 0;
|
||||||
char c1, c2;
|
char c1, c2;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@ -196,23 +189,20 @@ int zippath_find_sub_path(archive_file &zipfile, std::string const &subpath, osd
|
|||||||
// parse_parent_path - parses out the parent path
|
// parse_parent_path - parses out the parent path
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
void parse_parent_path(const std::string &path, std::string::size_type *beginpos, std::string::size_type *endpos)
|
void parse_parent_path(std::string_view path, std::string_view::size_type *beginpos, std::string_view::size_type *endpos)
|
||||||
{
|
{
|
||||||
std::string::size_type length = path.length();
|
std::string_view::size_type pos;
|
||||||
std::string::size_type pos;
|
|
||||||
|
|
||||||
// skip over trailing path separators
|
// skip over trailing path separators
|
||||||
pos = length ? (length - 1) : std::string::npos;
|
pos = path.find_last_not_of(PATH_SEPARATOR);
|
||||||
while ((pos > 0) && (pos != std::string::npos) && is_path_separator(path[pos]))
|
|
||||||
pos--;
|
|
||||||
|
|
||||||
// return endpos
|
// return endpos
|
||||||
if (endpos != nullptr)
|
if (endpos != nullptr)
|
||||||
*endpos = pos;
|
*endpos = pos;
|
||||||
|
|
||||||
// now skip until we find a path separator
|
// now skip until we find a path separator
|
||||||
while ((pos != std::string::npos) && !is_path_separator(path[pos]))
|
while ((pos != std::string_view::npos) && !is_path_separator(path[pos]))
|
||||||
pos = (pos > 0) ? pos - 1 : std::string::npos;
|
pos = (pos > 0) ? pos - 1 : std::string_view::npos;
|
||||||
|
|
||||||
// return beginpos
|
// return beginpos
|
||||||
if (beginpos != nullptr)
|
if (beginpos != nullptr)
|
||||||
@ -225,7 +215,7 @@ void parse_parent_path(const std::string &path, std::string::size_type *beginpos
|
|||||||
// true path and ZIP entry components
|
// true path and ZIP entry components
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
osd_file::error zippath_resolve(const char *path, osd::directory::entry::entry_type &entry_type, archive_file::ptr &zipfile, std::string &newpath)
|
osd_file::error zippath_resolve(std::string_view path, osd::directory::entry::entry_type &entry_type, archive_file::ptr &zipfile, std::string &newpath)
|
||||||
{
|
{
|
||||||
newpath.clear();
|
newpath.clear();
|
||||||
|
|
||||||
@ -240,10 +230,10 @@ osd_file::error zippath_resolve(const char *path, osd::directory::entry::entry_t
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
// trim the path of trailing path separators
|
// trim the path of trailing path separators
|
||||||
auto i = apath.length();
|
auto i = apath.find_last_not_of(PATH_SEPARATOR);
|
||||||
while ((i > 1) && is_path_separator(apath[i - 1]))
|
if (i == std::string::npos)
|
||||||
i--;
|
break;
|
||||||
apath.resize(i);
|
apath = apath.substr(0, i + 1);
|
||||||
apath_trimmed = apath;
|
apath_trimmed = apath;
|
||||||
|
|
||||||
// stat the path
|
// stat the path
|
||||||
@ -274,10 +264,10 @@ osd_file::error zippath_resolve(const char *path, osd::directory::entry::entry_t
|
|||||||
((is_zip_file(apath_trimmed) && (archive_file::open_zip(apath_trimmed, zipfile) == archive_file::error::NONE)) ||
|
((is_zip_file(apath_trimmed) && (archive_file::open_zip(apath_trimmed, zipfile) == archive_file::error::NONE)) ||
|
||||||
(is_7z_file(apath_trimmed) && (archive_file::open_7z(apath_trimmed, zipfile) == archive_file::error::NONE))))
|
(is_7z_file(apath_trimmed) && (archive_file::open_7z(apath_trimmed, zipfile) == archive_file::error::NONE))))
|
||||||
{
|
{
|
||||||
auto i = strlen(path + apath.length());
|
auto i = path.length() - apath.length();
|
||||||
while ((i > 0) && is_zip_path_separator(path[apath.length() + i - 1]))
|
while ((i > 0) && is_zip_path_separator(path[apath.length() + i - 1]))
|
||||||
i--;
|
i--;
|
||||||
newpath.assign(path + apath.length(), i);
|
newpath.assign(path, apath.length(), i);
|
||||||
|
|
||||||
// this was a true ZIP path - attempt to identify the type of path
|
// this was a true ZIP path - attempt to identify the type of path
|
||||||
zippath_find_sub_path(*zipfile, newpath, current_entry_type);
|
zippath_find_sub_path(*zipfile, newpath, current_entry_type);
|
||||||
@ -524,10 +514,10 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class filesystem_diectory_impl : public zippath_directory_base
|
class filesystem_directory_impl : public zippath_directory_base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
filesystem_diectory_impl(std::string const &path) : zippath_directory_base(!is_root(path)), m_directory(osd::directory::open(path))
|
filesystem_directory_impl(std::string_view path) : zippath_directory_base(!is_root(path)), m_directory(osd::directory::open(std::string(path)))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -573,7 +563,7 @@ private:
|
|||||||
// zippath_directory::open - opens a directory
|
// zippath_directory::open - opens a directory
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
osd_file::error zippath_directory::open(std::string const &path, ptr &directory)
|
osd_file::error zippath_directory::open(std::string_view path, ptr &directory)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -581,7 +571,7 @@ osd_file::error zippath_directory::open(std::string const &path, ptr &directory)
|
|||||||
osd::directory::entry::entry_type entry_type;
|
osd::directory::entry::entry_type entry_type;
|
||||||
archive_file::ptr zipfile;
|
archive_file::ptr zipfile;
|
||||||
std::string zipprefix;
|
std::string zipprefix;
|
||||||
osd_file::error const err = zippath_resolve(path.c_str(), entry_type, zipfile, zipprefix);
|
osd_file::error const err = zippath_resolve(path, entry_type, zipfile, zipprefix);
|
||||||
if (osd_file::error::NONE != err)
|
if (osd_file::error::NONE != err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
@ -598,7 +588,7 @@ osd_file::error zippath_directory::open(std::string const &path, ptr &directory)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// a conventional directory
|
// a conventional directory
|
||||||
std::unique_ptr<filesystem_diectory_impl> result(new filesystem_diectory_impl(path));
|
std::unique_ptr<filesystem_directory_impl> result(new filesystem_directory_impl(path));
|
||||||
if (!*result)
|
if (!*result)
|
||||||
return osd_file::error::FAILURE;
|
return osd_file::error::FAILURE;
|
||||||
|
|
||||||
@ -627,12 +617,12 @@ zippath_directory::~zippath_directory()
|
|||||||
// zippath_parent - retrieves the parent directory
|
// zippath_parent - retrieves the parent directory
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
std::string &zippath_parent(std::string &dst, const std::string &path)
|
std::string &zippath_parent(std::string &dst, std::string_view path)
|
||||||
{
|
{
|
||||||
std::string::size_type pos;
|
std::string_view::size_type pos;
|
||||||
parse_parent_path(path, &pos, nullptr);
|
parse_parent_path(path, &pos, nullptr);
|
||||||
|
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string_view::npos)
|
||||||
dst = path.substr(0, pos + 1);
|
dst = path.substr(0, pos + 1);
|
||||||
else
|
else
|
||||||
dst.clear();
|
dst.clear();
|
||||||
@ -645,7 +635,7 @@ std::string &zippath_parent(std::string &dst, const std::string &path)
|
|||||||
// zippath_parent - retrieves the parent directory
|
// zippath_parent - retrieves the parent directory
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
std::string zippath_parent(const std::string &path)
|
std::string zippath_parent(std::string_view path)
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
zippath_parent(result, path);
|
zippath_parent(result, path);
|
||||||
@ -654,46 +644,6 @@ std::string zippath_parent(const std::string &path)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// -------------------------------------------------
|
|
||||||
// zippath_parent_basename - retrieves the parent
|
|
||||||
// directory basename
|
|
||||||
// -------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn std::string &zippath_parent_basename(std::string &dst, const std::string &path)
|
|
||||||
*
|
|
||||||
* @brief Zippath parent basename.
|
|
||||||
*
|
|
||||||
* @param [in,out] dst Destination for the.
|
|
||||||
* @param path Full pathname of the file.
|
|
||||||
*
|
|
||||||
* @return A std::string&
|
|
||||||
*/
|
|
||||||
|
|
||||||
std::string &zippath_parent_basename(std::string &dst, const std::string &path)
|
|
||||||
{
|
|
||||||
std::string::size_type beginpos, endpos;
|
|
||||||
parse_parent_path(path, &beginpos, &endpos);
|
|
||||||
dst.copy((char*)(path.c_str() + beginpos + 1), endpos - beginpos);
|
|
||||||
return dst;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// -------------------------------------------------
|
|
||||||
// zippath_parent_basename - retrieves the parent
|
|
||||||
// directory basename
|
|
||||||
// -------------------------------------------------
|
|
||||||
|
|
||||||
std::string zippath_parent_basename(const std::string &path)
|
|
||||||
{
|
|
||||||
std::string result;
|
|
||||||
zippath_parent_basename(result, path);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
// zippath_combine - combines two paths
|
// zippath_combine - combines two paths
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
@ -759,7 +709,7 @@ std::string zippath_combine(const std::string &path1, const std::string &path2)
|
|||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, util::core_file::ptr &file, std::string &revised_path)
|
* @fn osd_file::error zippath_fopen(std::string_view filename, uint32_t openflags, util::core_file::ptr &file, std::string &revised_path)
|
||||||
*
|
*
|
||||||
* @brief Zippath fopen.
|
* @brief Zippath fopen.
|
||||||
*
|
*
|
||||||
@ -771,7 +721,7 @@ std::string zippath_combine(const std::string &path1, const std::string &path2)
|
|||||||
* @return A osd_file::error.
|
* @return A osd_file::error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, util::core_file::ptr &file, std::string &revised_path)
|
osd_file::error zippath_fopen(std::string_view filename, uint32_t openflags, util::core_file::ptr &file, std::string &revised_path)
|
||||||
{
|
{
|
||||||
osd_file::error filerr = osd_file::error::NOT_FOUND;
|
osd_file::error filerr = osd_file::error::NOT_FOUND;
|
||||||
archive_file::error ziperr;
|
archive_file::error ziperr;
|
||||||
@ -787,7 +737,7 @@ osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, u
|
|||||||
|
|
||||||
/* loop through */
|
/* loop through */
|
||||||
while((file == nullptr) && (mainpath.length() > 0)
|
while((file == nullptr) && (mainpath.length() > 0)
|
||||||
&& ((openflags == OPEN_FLAG_READ) || (subpath.length() == 0)))
|
&& ((openflags == OPEN_FLAG_READ) || subpath.empty()))
|
||||||
{
|
{
|
||||||
/* is the mainpath a ZIP path? */
|
/* is the mainpath a ZIP path? */
|
||||||
if (is_zip_file(mainpath) || is_7z_file(mainpath))
|
if (is_zip_file(mainpath) || is_7z_file(mainpath))
|
||||||
@ -803,7 +753,7 @@ osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, u
|
|||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subpath.length() > 0)
|
if (!subpath.empty())
|
||||||
header = zippath_find_sub_path(*zip, subpath, entry_type);
|
header = zippath_find_sub_path(*zip, subpath, entry_type);
|
||||||
else
|
else
|
||||||
header = zip->first_file();
|
header = zip->first_file();
|
||||||
@ -820,7 +770,7 @@ osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, u
|
|||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* update subpath, if appropriate */
|
/* update subpath, if appropriate */
|
||||||
if (subpath.length() == 0)
|
if (subpath.empty())
|
||||||
subpath.assign(zip->current_name());
|
subpath.assign(zip->current_name());
|
||||||
|
|
||||||
/* we're done */
|
/* we're done */
|
||||||
@ -828,8 +778,8 @@ osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, u
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subpath.length() == 0)
|
if (subpath.empty())
|
||||||
filerr = util::core_file::open(filename, openflags, file);
|
filerr = util::core_file::open(std::string(filename), openflags, file);
|
||||||
else
|
else
|
||||||
filerr = osd_file::error::NOT_FOUND;
|
filerr = osd_file::error::NOT_FOUND;
|
||||||
|
|
||||||
@ -840,7 +790,7 @@ osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, u
|
|||||||
auto temp = zippath_parent(mainpath);
|
auto temp = zippath_parent(mainpath);
|
||||||
|
|
||||||
/* append to the sub path */
|
/* append to the sub path */
|
||||||
if (subpath.length() > 0)
|
if (!subpath.empty())
|
||||||
{
|
{
|
||||||
std::string temp2;
|
std::string temp2;
|
||||||
mainpath = mainpath.substr(temp.length());
|
mainpath = mainpath.substr(temp.length());
|
||||||
@ -872,7 +822,7 @@ done:
|
|||||||
if (filerr == osd_file::error::NONE)
|
if (filerr == osd_file::error::NONE)
|
||||||
{
|
{
|
||||||
revised_path = alloc_fullpath;
|
revised_path = alloc_fullpath;
|
||||||
if (subpath.length() > 0)
|
if (!subpath.empty())
|
||||||
revised_path.append(PATH_SEPARATOR).append(subpath);
|
revised_path.append(PATH_SEPARATOR).append(subpath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
#include "unzip.h"
|
#include "unzip.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
@ -31,7 +32,7 @@ public:
|
|||||||
typedef std::unique_ptr<zippath_directory> ptr;
|
typedef std::unique_ptr<zippath_directory> ptr;
|
||||||
|
|
||||||
// opens a directory
|
// opens a directory
|
||||||
static osd_file::error open(std::string const &path, ptr &directory);
|
static osd_file::error open(std::string_view path, ptr &directory);
|
||||||
|
|
||||||
// closes a directory
|
// closes a directory
|
||||||
virtual ~zippath_directory();
|
virtual ~zippath_directory();
|
||||||
@ -52,12 +53,8 @@ public:
|
|||||||
// ----- path operations -----
|
// ----- path operations -----
|
||||||
|
|
||||||
// retrieves the parent directory
|
// retrieves the parent directory
|
||||||
std::string &zippath_parent(std::string &dst, const std::string &path);
|
std::string &zippath_parent(std::string &dst, std::string_view path);
|
||||||
std::string zippath_parent(const std::string &path);
|
std::string zippath_parent(std::string_view path);
|
||||||
|
|
||||||
// retrieves the parent directory basename
|
|
||||||
std::string &zippath_parent_basename(std::string &dst, const std::string &path);
|
|
||||||
std::string zippath_parent_basename(const std::string &path);
|
|
||||||
|
|
||||||
// combines two paths
|
// combines two paths
|
||||||
std::string &zippath_combine(std::string &dst, const std::string &path1, const std::string &path2);
|
std::string &zippath_combine(std::string &dst, const std::string &path1, const std::string &path2);
|
||||||
@ -67,7 +64,7 @@ std::string zippath_combine(const std::string &path1, const std::string &path2);
|
|||||||
// ----- file operations -----
|
// ----- file operations -----
|
||||||
|
|
||||||
// opens a zip path file
|
// opens a zip path file
|
||||||
osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, util::core_file::ptr &file, std::string &revised_path);
|
osd_file::error zippath_fopen(std::string_view filename, uint32_t openflags, util::core_file::ptr &file, std::string &revised_path);
|
||||||
|
|
||||||
} // namespace util
|
} // namespace util
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user