diff --git a/include/sourcepp/string/String.h b/include/sourcepp/string/String.h index 4ea03b62e..f4016bf43 100644 --- a/include/sourcepp/string/String.h +++ b/include/sourcepp/string/String.h @@ -30,8 +30,8 @@ void toUpper(std::string& input); [[nodiscard]] std::string padNumber(int number, int width, char pad = '0'); -void normalizeSlashes(std::string& path, bool stripTerminalSlashes = true); +void normalizeSlashes(std::string& path, bool stripSlashPrefix = false, bool stripSlashSuffix = true); -void denormalizeSlashes(std::string& path, bool stripTerminalSlashes = true); +void denormalizeSlashes(std::string& path, bool stripSlashPrefix = false, bool stripSlashSuffix = true); } // namespace sourcepp::string diff --git a/src/sourcepp/string/String.cpp b/src/sourcepp/string/String.cpp index d4da221f5..748bf299d 100644 --- a/src/sourcepp/string/String.cpp +++ b/src/sourcepp/string/String.cpp @@ -71,26 +71,22 @@ std::string string::padNumber(int number, int width, char pad) { return std::string(width - std::min(width, numStr.length()), pad) + numStr; } -void string::normalizeSlashes(std::string& path, bool stripTerminalSlashes) { +void string::normalizeSlashes(std::string& path, bool stripSlashPrefix, bool stripSlashSuffix) { std::replace(path.begin(), path.end(), '\\', '/'); - if (stripTerminalSlashes) { - if (path.starts_with('/')) { - path = path.substr(1); - } - if (path.ends_with('/')) { - path.pop_back(); - } + if (stripSlashPrefix && path.starts_with('/')) { + path = path.substr(1); + } + if (stripSlashSuffix && path.ends_with('/')) { + path.pop_back(); } } -void string::denormalizeSlashes(std::string& path, bool stripTerminalSlashes) { +void string::denormalizeSlashes(std::string& path, bool stripSlashPrefix, bool stripSlashSuffix) { std::replace(path.begin(), path.end(), '/', '\\'); - if (stripTerminalSlashes) { - if (path.starts_with('\\')) { - path = path.substr(1); - } - if (path.ends_with('\\')) { - path.pop_back(); - } + if (stripSlashPrefix && path.starts_with('\\')) { + path = path.substr(1); + } + if (stripSlashSuffix && path.ends_with('\\')) { + path.pop_back(); } } diff --git a/src/vpkpp/format/BSP.cpp b/src/vpkpp/format/BSP.cpp index 84a9779e3..621243f59 100644 --- a/src/vpkpp/format/BSP.cpp +++ b/src/vpkpp/format/BSP.cpp @@ -110,7 +110,7 @@ std::unique_ptr BSP::open(const std::string& path, PackFileOptions opt Entry entry = createNewEntry(); entry.path = fileInfo->filename; - string::normalizeSlashes(entry.path); + string::normalizeSlashes(entry.path, true); if (!bsp->isCaseSensitive()) { string::toLower(entry.path); } @@ -121,7 +121,7 @@ std::unique_ptr BSP::open(const std::string& path, PackFileOptions opt entry.crc32 = fileInfo->crc; auto parentDir = std::filesystem::path{entry.path}.parent_path().string(); - string::normalizeSlashes(parentDir); + string::normalizeSlashes(parentDir, true); if (!bsp->isCaseSensitive()) { string::toLower(parentDir); } diff --git a/src/vpkpp/format/GCF.cpp b/src/vpkpp/format/GCF.cpp index a0b87524d..0bc60f50e 100644 --- a/src/vpkpp/format/GCF.cpp +++ b/src/vpkpp/format/GCF.cpp @@ -130,7 +130,7 @@ std::unique_ptr GCF::open(const std::string& path, PackFileOptions opt dirname.insert(0, current_filename_entry.filename); } Entry gcfEntry = createNewEntry(); - string::normalizeSlashes(dirname); + string::normalizeSlashes(dirname, true); if (!gcf->isCaseSensitive()) { string::toLower(dirname); string::toLower(entry.filename); diff --git a/src/vpkpp/format/GMA.cpp b/src/vpkpp/format/GMA.cpp index 0713ce108..ebb7bdb94 100644 --- a/src/vpkpp/format/GMA.cpp +++ b/src/vpkpp/format/GMA.cpp @@ -47,7 +47,7 @@ std::unique_ptr GMA::open(const std::string& path, PackFileOptions opt Entry entry = createNewEntry(); reader.read(entry.path); - string::normalizeSlashes(entry.path); + string::normalizeSlashes(entry.path, true); if (!gma->isCaseSensitive()) { string::toLower(entry.path); } @@ -66,7 +66,7 @@ std::unique_ptr GMA::open(const std::string& path, PackFileOptions opt } for (const auto& entry : entries) { auto parentDir = std::filesystem::path{entry.path}.parent_path().string(); - string::normalizeSlashes(parentDir); + string::normalizeSlashes(parentDir, true); if (!gma->isCaseSensitive()) { string::toLower(parentDir); } diff --git a/src/vpkpp/format/GRP.cpp b/src/vpkpp/format/GRP.cpp index 892629db0..5274880c7 100644 --- a/src/vpkpp/format/GRP.cpp +++ b/src/vpkpp/format/GRP.cpp @@ -42,7 +42,7 @@ std::unique_ptr GRP::open(const std::string& path, PackFileOptions opt Entry entry = createNewEntry(); reader.read(entry.path, GRP_FILENAME_MAX_SIZE); - string::normalizeSlashes(entry.path); + string::normalizeSlashes(entry.path, true); if (!grp->isCaseSensitive()) { string::toLower(entry.path); } diff --git a/src/vpkpp/format/PAK.cpp b/src/vpkpp/format/PAK.cpp index a29483686..1f67f2b20 100644 --- a/src/vpkpp/format/PAK.cpp +++ b/src/vpkpp/format/PAK.cpp @@ -41,7 +41,7 @@ std::unique_ptr PAK::open(const std::string& path, PackFileOptions opt Entry entry = createNewEntry(); reader.read(entry.path, PAK_FILENAME_MAX_SIZE); - string::normalizeSlashes(entry.path); + string::normalizeSlashes(entry.path, true); if (!pak->isCaseSensitive()) { string::toLower(entry.path); } @@ -50,7 +50,7 @@ std::unique_ptr PAK::open(const std::string& path, PackFileOptions opt entry.length = reader.read(); auto parentDir = std::filesystem::path{entry.path}.parent_path().string(); - string::normalizeSlashes(parentDir); + string::normalizeSlashes(parentDir, true); if (!pak->isCaseSensitive()) { string::toLower(parentDir); } diff --git a/src/vpkpp/format/PCK.cpp b/src/vpkpp/format/PCK.cpp index 24e7ad994..cc634184b 100644 --- a/src/vpkpp/format/PCK.cpp +++ b/src/vpkpp/format/PCK.cpp @@ -99,7 +99,7 @@ std::unique_ptr PCK::open(const std::string& path, PackFileOptions opt if (entry.path.starts_with(PCK_PATH_PREFIX)) { entry.path = entry.path.substr(PCK_PATH_PREFIX.length()); } - string::normalizeSlashes(entry.path); + string::normalizeSlashes(entry.path, true); if (!pck->isCaseSensitive()) { string::toLower(entry.path); } @@ -113,7 +113,7 @@ std::unique_ptr PCK::open(const std::string& path, PackFileOptions opt } auto parentDir = std::filesystem::path{entry.path}.parent_path().string(); - string::normalizeSlashes(parentDir); + string::normalizeSlashes(parentDir, true); if (!pck->entries.contains(parentDir)) { pck->entries[parentDir] = {}; } diff --git a/src/vpkpp/format/VPK.cpp b/src/vpkpp/format/VPK.cpp index 9ce16e171..d84cc7e4a 100644 --- a/src/vpkpp/format/VPK.cpp +++ b/src/vpkpp/format/VPK.cpp @@ -94,7 +94,7 @@ std::unique_ptr VPK::createFromDirectoryProcedural(const std::string& std::string entryPath; try { entryPath = std::filesystem::absolute(file.path()).string().substr(std::filesystem::absolute(contentPath).string().length()); - string::normalizeSlashes(entryPath); + string::normalizeSlashes(entryPath, true); } catch (const std::exception&) { continue; // Likely a Unicode error, unsupported filename } @@ -508,7 +508,7 @@ bool VPK::bake(const std::string& outputDir_, const Callback& callback) { // Helper const auto getArchiveFilename = [this](const std::string& filename_, int archiveIndex) { std::string out{filename_ + '_' + string::padNumber(archiveIndex, 3) + (::isFPX(this) ? FPX_EXTENSION : VPK_EXTENSION).data()}; - string::normalizeSlashes(out, false); + string::normalizeSlashes(out); return out; }; diff --git a/src/vpkpp/format/ZIP.cpp b/src/vpkpp/format/ZIP.cpp index 1fca47403..dde6ec3f7 100644 --- a/src/vpkpp/format/ZIP.cpp +++ b/src/vpkpp/format/ZIP.cpp @@ -54,7 +54,7 @@ std::unique_ptr ZIP::open(const std::string& path, PackFileOptions opt Entry entry = createNewEntry(); entry.path = fileInfo->filename; - string::normalizeSlashes(entry.path); + string::normalizeSlashes(entry.path, true); if (!zip->isCaseSensitive()) { string::toLower(entry.path); } @@ -65,7 +65,7 @@ std::unique_ptr ZIP::open(const std::string& path, PackFileOptions opt entry.crc32 = fileInfo->crc; auto parentDir = std::filesystem::path{entry.path}.parent_path().string(); - string::normalizeSlashes(parentDir); + string::normalizeSlashes(parentDir, true); if (!zip->isCaseSensitive()) { string::toLower(parentDir); }