Skip to content

Commit

Permalink
thcrap: fix fn_for_build
Browse files Browse the repository at this point in the history
  • Loading branch information
32th-System committed Mar 25, 2023
1 parent c1ba2c4 commit 6b47a26
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
15 changes: 12 additions & 3 deletions thcrap/src/patchfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,21 @@ int file_write(const char *fn, const void *file_buffer, size_t file_size)

TH_CALLER_FREE char* fn_for_build(const char *fn)
{
if (!fn) return nullptr;
std::string_view build_view = runconfig_build_get_view();
if (!build_view.empty()) {
const size_t fn_length = PtrDiffStrlen(strchr(fn, '.'), fn);
return strdup_cat({ fn, fn_length + 1 }, build_view, fn + fn_length);
const char* fn_offset = strrchr(fn, '/');
if(!fn_offset) fn_offset = strrchr(fn, '\\');
if (!fn_offset) fn_offset = fn;

if (const char* dot_offset = strchr(fn_offset, '.')) {
const size_t fn_length = PtrDiffStrlen(dot_offset, fn);
return strdup_cat({ fn, fn_length + 1 }, build_view, fn + fn_length);
} else {
return strdup_cat(fn, ".", build_view);
}
}
return NULL;
return nullptr;
}

TH_CALLER_FREE char* fn_for_game(const char *fn)
Expand Down
26 changes: 26 additions & 0 deletions thcrap_test/src/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "thcrap.h"
#include "gtest/gtest.h"

TEST(StackTest, fn_for_filename)
{
ASSERT_EQ(fn_for_build("abc.def/g.txt"), nullptr);

runconfig_build_set("v1.00b");

char* test1 = fn_for_build("abc.def/g.txt");
char* test2 = fn_for_build("abc/def.g.txt");
char* test3 = fn_for_build("abc/def");
char* test4 = fn_for_build("abc.txt");

ASSERT_STREQ(test1, "abc.def/g.v1.00b.txt");
ASSERT_STREQ(test2, "abc/def.v1.00b.g.txt");
ASSERT_STREQ(test3, "abc/def.v1.00b");
ASSERT_STREQ(test4, "abc.v1.00b.txt");

ASSERT_EQ(fn_for_build(nullptr), nullptr);

thcrap_free(test1);
thcrap_free(test2);
thcrap_free(test3);
thcrap_free(test4);
}
1 change: 1 addition & 0 deletions thcrap_test/thcrap_test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<ClCompile Include="src\patchfile.cpp" />
<ClCompile Include="src\plugin.cpp" />
<ClCompile Include="src\search_for_games.cpp" />
<ClCompile Include="src\stack.cpp" />
<ClCompile Include="src\thcrap_tasofro_pl_ed.cpp" />
<ClCompile Include="src\win32_utf8.cpp" />
</ItemGroup>
Expand Down

0 comments on commit 6b47a26

Please # to comment.