Skip to content

Commit aea71e4

Browse files
authored
refactor(utils): modernize path conversions and errors
2 parents 0e9c7f6 + acfb69b commit aea71e4

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

include/logit_cpp/logit/utils/encoding_utils.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ namespace logit {
3434
return ansi_string;
3535
}
3636

37+
/// \brief Converts a UTF-8 string to a wide UTF-16 string (Windows-specific).
38+
/// \param utf8 The UTF-8 encoded string.
39+
/// \return The converted wide string.
40+
inline std::wstring utf8_to_wstring(const std::string& utf8) {
41+
int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
42+
if (n_len == 0) return {};
43+
std::wstring wide_string(n_len - 1, L'\0');
44+
MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide_string[0], n_len);
45+
return wide_string;
46+
}
47+
48+
/// \brief Converts a wide UTF-16 string to a UTF-8 string (Windows-specific).
49+
/// \param wide The UTF-16 encoded string.
50+
/// \return The converted UTF-8 string.
51+
inline std::string wstring_to_utf8(const std::wstring& wide) {
52+
int n_len = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, NULL, 0, NULL, NULL);
53+
if (n_len == 0) return {};
54+
std::string utf8(n_len - 1, '\0');
55+
WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, &utf8[0], n_len, NULL, NULL);
56+
return utf8;
57+
}
58+
3759
} // namespace logit
3860
#endif // defined(_WIN32)
3961

include/logit_cpp/logit/utils/path_utils.hpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
// For Windows systems
1818
#include <direct.h>
1919
#include <windows.h>
20-
#include <locale>
21-
#include <codecvt>
2220
#elif defined(__APPLE__)
2321
// For macOS systems
2422
#include <mach-o/dyld.h>
@@ -46,10 +44,7 @@ namespace logit {
4644

4745
inline std::string get_exec_dir() { return "./"; }
4846

49-
inline std::vector<std::string> get_list_files(const std::string&) {
50-
std::cerr << "get_list_files is not supported under Emscripten" << std::endl;
51-
return {};
52-
}
47+
inline std::vector<std::string> get_list_files(const std::string&) = delete;
5348

5449
inline std::string get_file_name(const std::string& file_path) {
5550
size_t pos = file_path.find_last_of("/\\");
@@ -61,9 +56,7 @@ namespace logit {
6156
return file_path;
6257
}
6358

64-
inline void create_directories(const std::string&) {
65-
std::cerr << "create_directories is not supported under Emscripten" << std::endl;
66-
}
59+
inline void create_directories(const std::string&) = delete;
6760

6861
inline bool is_file(const std::string& path) {
6962
size_t dot_pos = path.find_last_of('.');
@@ -102,8 +95,7 @@ namespace logit {
10295
}
10396

10497
// Convert from std::wstring (UTF-16) to std::string (UTF-8)
105-
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
106-
return converter.to_bytes(exe_path);
98+
return wstring_to_utf8(exe_path);
10799
# elif defined(__APPLE__)
108100
uint32_t size = 0;
109101
_NSGetExecutablePath(nullptr, &size);
@@ -149,7 +141,6 @@ namespace logit {
149141
std::vector<std::string> list_files;
150142
# ifdef _WIN32
151143
// Use wide versions of functions to correctly handle non-ASCII characters.
152-
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
153144
std::wstring wsearch_path;
154145

155146
// If the path is empty, use the current directory.
@@ -158,7 +149,7 @@ namespace logit {
158149
GetCurrentDirectoryW(MAX_PATH, buffer);
159150
wsearch_path = buffer;
160151
} else {
161-
wsearch_path = converter.from_bytes(path);
152+
wsearch_path = utf8_to_wstring(path);
162153
}
163154

164155
// Ensure there is a trailing separator.
@@ -182,11 +173,11 @@ namespace logit {
182173

183174
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
184175
// Recursively process subdirectories.
185-
std::vector<std::string> sub_files = get_list_files(converter.to_bytes(wfull_path));
176+
std::vector<std::string> sub_files = get_list_files(wstring_to_utf8(wfull_path));
186177
list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
187178
} else {
188179
// Add the found file.
189-
list_files.push_back(converter.to_bytes(wfull_path));
180+
list_files.push_back(wstring_to_utf8(wfull_path));
190181
}
191182
} while (FindNextFileW(hFind, &fd));
192183
FindClose(hFind);
@@ -267,8 +258,7 @@ namespace logit {
267258
void create_directories(const std::string& path) {
268259
# ifdef _WIN32
269260
// Convert UTF-8 string to wide string for Windows
270-
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
271-
std::wstring wide_path = converter.from_bytes(path);
261+
std::wstring wide_path = utf8_to_wstring(path);
272262
std::filesystem::path dir(wide_path);
273263
# else
274264
std::filesystem::path dir = std::filesystem::u8path(path);

0 commit comments

Comments
 (0)