From 58f411d591fb6f33e611a6706c2411731aec92aa Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sat, 13 Sep 2025 08:45:41 +0300 Subject: [PATCH 1/2] refactor(utils): modernize path conversions and errors --- .../logit_cpp/logit/utils/encoding_utils.hpp | 22 ++++++++++++++++ include/logit_cpp/logit/utils/path_utils.hpp | 26 +++++++------------ 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/include/logit_cpp/logit/utils/encoding_utils.hpp b/include/logit_cpp/logit/utils/encoding_utils.hpp index c7888cf..0e28314 100644 --- a/include/logit_cpp/logit/utils/encoding_utils.hpp +++ b/include/logit_cpp/logit/utils/encoding_utils.hpp @@ -34,6 +34,28 @@ namespace logit { return ansi_string; } + /// \brief Converts a UTF-8 string to a wide UTF-16 string (Windows-specific). + /// \param utf8 The UTF-8 encoded string. + /// \return The converted wide string. + inline std::wstring utf8_to_wstring(const std::string& utf8) { + int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0); + if (n_len == 0) return {}; + std::wstring wide_string(n_len - 1, L'\0'); + MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide_string[0], n_len); + return wide_string; + } + + /// \brief Converts a wide UTF-16 string to a UTF-8 string (Windows-specific). + /// \param wide The UTF-16 encoded string. + /// \return The converted UTF-8 string. + inline std::string wstring_to_utf8(const std::wstring& wide) { + int n_len = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, NULL, 0, NULL, NULL); + if (n_len == 0) return {}; + std::string utf8(n_len - 1, '\0'); + WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, &utf8[0], n_len, NULL, NULL); + return utf8; + } + } // namespace logit #endif // defined(_WIN32) diff --git a/include/logit_cpp/logit/utils/path_utils.hpp b/include/logit_cpp/logit/utils/path_utils.hpp index 5d1881d..daa3b87 100644 --- a/include/logit_cpp/logit/utils/path_utils.hpp +++ b/include/logit_cpp/logit/utils/path_utils.hpp @@ -13,12 +13,12 @@ #include #endif +#include "encoding_utils.hpp" + #ifdef _WIN32 // For Windows systems #include #include -#include -#include #elif defined(__APPLE__) // For macOS systems #include @@ -46,10 +46,7 @@ namespace logit { inline std::string get_exec_dir() { return "./"; } - inline std::vector get_list_files(const std::string&) { - std::cerr << "get_list_files is not supported under Emscripten" << std::endl; - return {}; - } + inline std::vector get_list_files(const std::string&) = delete; inline std::string get_file_name(const std::string& file_path) { size_t pos = file_path.find_last_of("/\\"); @@ -61,9 +58,7 @@ namespace logit { return file_path; } - inline void create_directories(const std::string&) { - std::cerr << "create_directories is not supported under Emscripten" << std::endl; - } + inline void create_directories(const std::string&) = delete; inline bool is_file(const std::string& path) { size_t dot_pos = path.find_last_of('.'); @@ -102,8 +97,7 @@ namespace logit { } // Convert from std::wstring (UTF-16) to std::string (UTF-8) - std::wstring_convert> converter; - return converter.to_bytes(exe_path); + return wstring_to_utf8(exe_path); # elif defined(__APPLE__) uint32_t size = 0; _NSGetExecutablePath(nullptr, &size); @@ -149,7 +143,6 @@ namespace logit { std::vector list_files; # ifdef _WIN32 // Use wide versions of functions to correctly handle non-ASCII characters. - std::wstring_convert> converter; std::wstring wsearch_path; // If the path is empty, use the current directory. @@ -158,7 +151,7 @@ namespace logit { GetCurrentDirectoryW(MAX_PATH, buffer); wsearch_path = buffer; } else { - wsearch_path = converter.from_bytes(path); + wsearch_path = utf8_to_wstring(path); } // Ensure there is a trailing separator. @@ -182,11 +175,11 @@ namespace logit { if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // Recursively process subdirectories. - std::vector sub_files = get_list_files(converter.to_bytes(wfull_path)); + std::vector sub_files = get_list_files(wstring_to_utf8(wfull_path)); list_files.insert(list_files.end(), sub_files.begin(), sub_files.end()); } else { // Add the found file. - list_files.push_back(converter.to_bytes(wfull_path)); + list_files.push_back(wstring_to_utf8(wfull_path)); } } while (FindNextFileW(hFind, &fd)); FindClose(hFind); @@ -267,8 +260,7 @@ namespace logit { void create_directories(const std::string& path) { # ifdef _WIN32 // Convert UTF-8 string to wide string for Windows - std::wstring_convert> converter; - std::wstring wide_path = converter.from_bytes(path); + std::wstring wide_path = utf8_to_wstring(path); std::filesystem::path dir(wide_path); # else std::filesystem::path dir = std::filesystem::u8path(path); From acfb69befbbccce051e3ec6782ed294aa2f25061 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sat, 13 Sep 2025 08:58:34 +0300 Subject: [PATCH 2/2] chore: update path_utils.hpp --- include/logit_cpp/logit/utils/path_utils.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/logit_cpp/logit/utils/path_utils.hpp b/include/logit_cpp/logit/utils/path_utils.hpp index daa3b87..a7299d3 100644 --- a/include/logit_cpp/logit/utils/path_utils.hpp +++ b/include/logit_cpp/logit/utils/path_utils.hpp @@ -13,8 +13,6 @@ #include #endif -#include "encoding_utils.hpp" - #ifdef _WIN32 // For Windows systems #include