Skip to content

Commit

Permalink
Fix regex issues in texture path cleanup by removing whitespace chara…
Browse files Browse the repository at this point in the history
…cters #49

Remove whitespace characters like \n, \r and blanks from the start and end of texture paths first.
  • Loading branch information
ousnius committed Oct 4, 2024
1 parent 3d6ee9f commit 13feec0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/NifUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ inline bool is_relative_path(std::string_view path) noexcept {
}
}

// Helper to trim whitespace characters including newlines from the start and end of a string
void trim_whitespace(std::string& str);

// Convenience wrapper for std::find
template<typename Container, typename Value = typename Container::value>
auto find(Container& cont, Value&& val) {
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(sources
Factory.cpp
Geometry.cpp
NifFile.cpp
NifUtil.cpp
Nodes.cpp
Objects.cpp
Particles.cpp
Expand Down
3 changes: 3 additions & 0 deletions src/NifFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,9 @@ void NifFile::TrimTexturePaths() {
if (tex.empty())
return tex;

// Trim whitespace characters (including newlines)
trim_whitespace(tex);

// Replace multiple slashes or forward slashes with one backslash
tex = std::regex_replace(tex, std::regex("/+|\\\\+"), "\\");

Expand Down
34 changes: 34 additions & 0 deletions src/NifUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
nifly
C++ NIF library for the Gamebryo/NetImmerse File Format
See the included GPLv3 LICENSE file
*/

#pragma once

#include "NifUtil.hpp"

namespace nifly {

void trim_whitespace(std::string& str) {
if (str.empty())
return;

std::string::size_type i, j;
i = 0;

while (i < str.size() && isspace(str[i]))
++i;

if (i == str.size())
str.clear();

j = str.size() - 1;

while (isspace(str[j]))
--j;

str = str.substr(i, j - i + 1);
}

} // namespace nifly
24 changes: 24 additions & 0 deletions tests/TestNifFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ TEST_CASE("Load and save static file (SE)", "[NifFile]") {
REQUIRE(CompareBinaryFiles(fileOutput, fileExpected));
}

TEST_CASE("Trim texture paths", "[NifFile]") {
constexpr auto fileName = "TestNifFile_Static_SE";
std::string fileInput = folderInput + "/" + fileName + nifSuffix;

NifFile nif;
REQUIRE(nif.Load(fileInput) == 0);

auto shapes = nif.GetShapes();
REQUIRE(!shapes.empty());

auto shader = nif.GetShader(shapes.front());
REQUIRE(shader);
REQUIRE(shader->HasTextureSet());

auto textureSet = nif.GetHeader().GetBlock<BSShaderTextureSet>(shader->TextureSetRef());
REQUIRE(textureSet);
REQUIRE(textureSet->textures.size() == 9);

textureSet->textures[0].get() = " \\Data\\\\Textures//white.dds\r\n ";

nif.TrimTexturePaths();
REQUIRE(textureSet->textures[0] == "textures\\white.dds");
}

TEST_CASE("Load and save static file (FO4)", "[NifFile]") {
constexpr auto fileName = "TestNifFile_Static_FO4";
const auto [fileInput, fileOutput, fileExpected] = GetNifFileTuple(fileName);
Expand Down

0 comments on commit 13feec0

Please sign in to comment.