diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-03-03 15:12:45 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-03-03 15:12:45 +0100 |
| commit | 1594c96a327ae418b193d19d8d6c80b89cc74430 (patch) | |
| tree | b17a5e997526e48115c2f511bfb513982a23367a | |
| parent | 4cf0d29a1ad11e4da4da677a3859d0844e5f8bd5 (diff) | |
unicode file path support
| -rw-r--r-- | src/main.cpp | 2 | ||||
| -rw-r--r-- | src/main_windows.cpp | 18 |
2 files changed, 14 insertions, 6 deletions
diff --git a/src/main.cpp b/src/main.cpp index df1dfe2..0e6f396 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -140,7 +140,7 @@ static void* _ts_search_thread(void* args) { if (read_cursor >= new_result->files.length) continue; found_file* f = (found_file*)array_at(&new_result->files, read_cursor); - file_content content = platform_read_file(f->path, "rb"); + file_content content = platform_read_file(f->path, "rb, ccs=UTF-8"); _ts_search_file(f, content, new_result); diff --git a/src/main_windows.cpp b/src/main_windows.cpp index 3a1d2e8..42d989b 100644 --- a/src/main_windows.cpp +++ b/src/main_windows.cpp @@ -227,8 +227,16 @@ file_content platform_read_file(char *path, const char *mode) result.content = 0; result.content_length = 0; result.file_error = 0; + + // convert utf8 to wchar path. + wchar_t wchar_buffer[MAX_INPUT_LENGTH]; + MultiByteToWideChar(CP_UTF8, 0, path, -1, (wchar_t*)wchar_buffer, MAX_INPUT_LENGTH); - FILE *file = fopen(path, mode); + const size_t cSize = strlen(mode)+1; + wchar_t* wc = new wchar_t[cSize]; + mbstowcs (wc, mode, cSize); + + FILE *file = _wfopen(wchar_buffer, wc); if (!file) { if (errno == EMFILE) @@ -287,7 +295,7 @@ file_content platform_read_file(char *path, const char *mode) static void *_list_files_thread(void *args) { search_result *info = (search_result *)args; - platform_list_files_block(info); + platform_list_files_block(info, nullptr); info->done_finding_files = true; return 0; } @@ -303,7 +311,7 @@ void platform_list_files_block(search_result* result, wchar_t* start_dir) // Utf8 to wchar str wchar_t* search_dir = (wchar_t*)malloc(MAX_INPUT_LENGTH); if (start_dir == nullptr) { - MultiByteToWideChar(CP_UTF8, 0, result->directory_to_search, strlen(result->directory_to_search), search_dir, MAX_INPUT_LENGTH); + MultiByteToWideChar(CP_UTF8, 0, result->directory_to_search, -1, search_dir, MAX_INPUT_LENGTH); } else { wcscpy(search_dir, start_dir); @@ -311,8 +319,8 @@ void platform_list_files_block(search_result* result, wchar_t* start_dir) // Append wildcard wchar_t* search_dir_fix = (wchar_t*)malloc(MAX_INPUT_LENGTH); - wcscpy(search_dir_fix, search_dir); - wcscat(search_dir_fix, L"\\*"); + wcscpy_s(search_dir_fix, MAX_INPUT_LENGTH, search_dir); + wcscat_s(search_dir_fix, MAX_INPUT_LENGTH, L"\\*"); WIN32_FIND_DATAW file_info; HANDLE handle = FindFirstFileW(search_dir_fix, &file_info); |
