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:
AJR 2021-01-06 15:17:44 -05:00
parent 979b96b6cd
commit 29e96c84a8
2 changed files with 44 additions and 97 deletions

View File

@ -46,21 +46,16 @@ int is_path_separator(char c)
// 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
std::string::size_type i = 0;
// skip drive letter
if (isalpha(path[i]) && (path[i + 1] == ':'))
i += 2;
if (path.length() >= 2 && isalpha(path[0]) && (path[1] == ':'))
path.remove_prefix(2);
// skip path separators
while (is_path_separator(path[i]))
i++;
return path[i] == '\0';
return path.find_first_not_of(PATH_SEPARATOR) == std::string_view::npos;
}
@ -72,8 +67,7 @@ bool is_root(std::string const &path)
bool is_7z_file(std::string const &path)
{
auto const s = path.rfind('.');
return (std::string::npos != s) && !core_stricmp(path.c_str() + s, ".7z");
return core_filename_ends_with(path, ".7z");
}
@ -84,8 +78,7 @@ bool is_7z_file(std::string const &path)
bool is_zip_file(std::string const &path)
{
auto const s = path.rfind('.');
return (std::string::npos != s) && !core_stricmp(path.c_str() + s, ".zip");
return core_filename_ends_with(path, ".7zip");
}
@ -119,7 +112,7 @@ bool is_zip_path_separator(char c)
// 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
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
// -------------------------------------------------
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())
{
std::string::size_type i = 0, j = 0;
std::string_view::size_type i = 0, j = 0;
char c1, c2;
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
// -------------------------------------------------
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::size_type pos;
std::string_view::size_type pos;
// skip over trailing path separators
pos = length ? (length - 1) : std::string::npos;
while ((pos > 0) && (pos != std::string::npos) && is_path_separator(path[pos]))
pos--;
pos = path.find_last_not_of(PATH_SEPARATOR);
// return endpos
if (endpos != nullptr)
*endpos = pos;
// now skip until we find a path separator
while ((pos != std::string::npos) && !is_path_separator(path[pos]))
pos = (pos > 0) ? pos - 1 : std::string::npos;
while ((pos != std::string_view::npos) && !is_path_separator(path[pos]))
pos = (pos > 0) ? pos - 1 : std::string_view::npos;
// return beginpos
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
// -------------------------------------------------
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();
@ -240,10 +230,10 @@ osd_file::error zippath_resolve(const char *path, osd::directory::entry::entry_t
do
{
// trim the path of trailing path separators
auto i = apath.length();
while ((i > 1) && is_path_separator(apath[i - 1]))
i--;
apath.resize(i);
auto i = apath.find_last_not_of(PATH_SEPARATOR);
if (i == std::string::npos)
break;
apath = apath.substr(0, i + 1);
apath_trimmed = apath;
// 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_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]))
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
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:
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
// -------------------------------------------------
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
{
@ -581,7 +571,7 @@ osd_file::error zippath_directory::open(std::string const &path, ptr &directory)
osd::directory::entry::entry_type entry_type;
archive_file::ptr zipfile;
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)
return err;
@ -598,7 +588,7 @@ osd_file::error zippath_directory::open(std::string const &path, ptr &directory)
else
{
// 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)
return osd_file::error::FAILURE;
@ -627,12 +617,12 @@ zippath_directory::~zippath_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);
if (pos != std::string::npos)
if (pos != std::string_view::npos)
dst = path.substr(0, pos + 1);
else
dst.clear();
@ -645,7 +635,7 @@ std::string &zippath_parent(std::string &dst, const std::string &path)
// 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;
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&amp;
*/
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
// -------------------------------------------------
@ -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.
*
@ -771,7 +721,7 @@ std::string zippath_combine(const std::string &path1, const std::string &path2)
* @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;
archive_file::error ziperr;
@ -787,7 +737,7 @@ osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, u
/* loop through */
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? */
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;
}
if (subpath.length() > 0)
if (!subpath.empty())
header = zippath_find_sub_path(*zip, subpath, entry_type);
else
header = zip->first_file();
@ -820,7 +770,7 @@ osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, u
goto done;
/* update subpath, if appropriate */
if (subpath.length() == 0)
if (subpath.empty())
subpath.assign(zip->current_name());
/* we're done */
@ -828,8 +778,8 @@ osd_file::error zippath_fopen(const std::string &filename, uint32_t openflags, u
}
}
if (subpath.length() == 0)
filerr = util::core_file::open(filename, openflags, file);
if (subpath.empty())
filerr = util::core_file::open(std::string(filename), openflags, file);
else
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);
/* append to the sub path */
if (subpath.length() > 0)
if (!subpath.empty())
{
std::string temp2;
mainpath = mainpath.substr(temp.length());
@ -872,7 +822,7 @@ done:
if (filerr == osd_file::error::NONE)
{
revised_path = alloc_fullpath;
if (subpath.length() > 0)
if (!subpath.empty())
revised_path.append(PATH_SEPARATOR).append(subpath);
}
}

View File

@ -17,6 +17,7 @@
#include "unzip.h"
#include <string>
#include <string_view>
namespace util {
@ -31,7 +32,7 @@ public:
typedef std::unique_ptr<zippath_directory> ptr;
// 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
virtual ~zippath_directory();
@ -52,12 +53,8 @@ public:
// ----- path operations -----
// retrieves the parent directory
std::string &zippath_parent(std::string &dst, const std::string &path);
std::string zippath_parent(const std::string &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);
std::string &zippath_parent(std::string &dst, std::string_view path);
std::string zippath_parent(std::string_view path);
// combines two paths
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 -----
// 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