From 122f950d57a953962c0791c70b7bd13abc036882 Mon Sep 17 00:00:00 2001 From: LPuehringerStudent Date: Sun, 15 Mar 2026 22:31:27 +0100 Subject: [PATCH] Fix unicode path handling in codegen (#126) On Windows, std::ifstream and std::ofstream don't handle Unicode paths correctly when constructed from std::filesystem::path because they convert to narrow strings using the system code page. This commit adds cross-platform helper functions openInputFile() and openOutputFile() that: - On Windows: use path.wstring() to properly handle Unicode characters - On other platforms: use the standard path constructor Fixes #126 --- codegen/src/Main.cpp | 2 +- codegen/src/Shared.hpp | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/codegen/src/Main.cpp b/codegen/src/Main.cpp index 44defd573..2824da0db 100644 --- a/codegen/src/Main.cpp +++ b/codegen/src/Main.cpp @@ -133,7 +133,7 @@ int main(int argc, char** argv) try { if (auto sdkPath = std::getenv("GEODE_SDK")) { auto versionPath = std::filesystem::path(sdkPath) / "VERSION"; if (std::filesystem::exists(versionPath)) { - std::ifstream versionFile(versionPath); + auto versionFile = openInputFile(versionPath); std::string version; std::getline(versionFile, version); codegen::sdkVersion = codegen::Version::fromString(version); diff --git a/codegen/src/Shared.hpp b/codegen/src/Shared.hpp index 66d7fb952..f1417e363 100644 --- a/codegen/src/Shared.hpp +++ b/codegen/src/Shared.hpp @@ -8,8 +8,42 @@ #include #include +#ifdef _WIN32 +#include +#include +#include +#endif + using std::istreambuf_iterator; +// Cross-platform helper to open a file stream with Unicode path support +// On Windows, we need to use wide strings for proper Unicode support +#ifdef _WIN32 +inline std::ifstream openInputFile(std::filesystem::path const& path) { + std::ifstream file; + file.open(path.wstring()); + return file; +} + +inline std::ofstream openOutputFile(std::filesystem::path const& path) { + std::ofstream file; + file.open(path.wstring()); + return file; +} +#else +inline std::ifstream openInputFile(std::filesystem::path const& path) { + std::ifstream file; + file.open(path); + return file; +} + +inline std::ofstream openOutputFile(std::filesystem::path const& path) { + std::ofstream file; + file.open(path); + return file; +} +#endif + #ifdef _MSC_VER #pragma warning(disable : 4996) #endif @@ -32,15 +66,13 @@ std::string generateInlineSources(Root const& root, std::filesystem::path const& // returns true if the file contents were different (overwritten), false otherwise inline bool writeFile(std::filesystem::path const& writePath, std::string const& output) { - std::ifstream readfile; + auto readfile = openInputFile(writePath); readfile >> std::noskipws; - readfile.open(writePath); std::string data((std::istreambuf_iterator(readfile)), std::istreambuf_iterator()); readfile.close(); if (data != output) { - std::ofstream writefile; - writefile.open(writePath); + auto writefile = openOutputFile(writePath); writefile << output; writefile.close();