Skip to content

Commit

Permalink
fix(sourcepp): change default string::normalizeSlashes behavior to fi…
Browse files Browse the repository at this point in the history
…x default linux behavior stripping the first slash from an absolute path
  • Loading branch information
craftablescience committed Jul 10, 2024
1 parent fff4bb9 commit a94d4a5
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 32 deletions.
4 changes: 2 additions & 2 deletions include/sourcepp/string/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 12 additions & 16 deletions src/sourcepp/string/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,22 @@ std::string string::padNumber(int number, int width, char pad) {
return std::string(width - std::min<std::string::size_type>(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();
}
}
4 changes: 2 additions & 2 deletions src/vpkpp/format/BSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ std::unique_ptr<PackFile> 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);
}
Expand All @@ -121,7 +121,7 @@ std::unique_ptr<PackFile> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/vpkpp/format/GCF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ std::unique_ptr<PackFile> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/format/GMA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ std::unique_ptr<PackFile> 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);
}
Expand All @@ -66,7 +66,7 @@ std::unique_ptr<PackFile> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/vpkpp/format/GRP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ std::unique_ptr<PackFile> 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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/format/PAK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::unique_ptr<PackFile> 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);
}
Expand All @@ -50,7 +50,7 @@ std::unique_ptr<PackFile> PAK::open(const std::string& path, PackFileOptions opt
entry.length = reader.read<uint32_t>();

auto parentDir = std::filesystem::path{entry.path}.parent_path().string();
string::normalizeSlashes(parentDir);
string::normalizeSlashes(parentDir, true);
if (!pak->isCaseSensitive()) {
string::toLower(parentDir);
}
Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/format/PCK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ std::unique_ptr<PackFile> 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);
}
Expand All @@ -113,7 +113,7 @@ std::unique_ptr<PackFile> 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] = {};
}
Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/format/VPK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std::unique_ptr<PackFile> 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
}
Expand Down Expand Up @@ -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;
};

Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/format/ZIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::unique_ptr<PackFile> 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);
}
Expand All @@ -65,7 +65,7 @@ std::unique_ptr<PackFile> 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);
}
Expand Down

0 comments on commit a94d4a5

Please sign in to comment.