diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2024-09-10 01:51:28 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2024-09-10 11:44:49 +0200 |
commit | 62ccd355f060d29a46346e08086c7be6de57cdd8 (patch) | |
tree | 9f139887eb7ef8f29a2471a0e358acdf47f9afa1 | |
parent | 4a4ffd9379ad247f0a303aa58d297c93dce0adcd (diff) | |
download | sciteco-62ccd355f060d29a46346e08086c7be6de57cdd8.tar.gz |
win32: fixed opening and saving UTF-8 filenames (refs #5)
* The default ANSI versions of the Win32 API calls worked only as
long as we used the ANSI subset of UTF-8 in filenames.
* There is g_win32_locale_filename_from_utf8(), but it's not guaranteed
to derive an unique filename.
* Therefore we define UNICODE and convert between UTF-8 and UTF-16
(Windows' native Unicode encoding).
-rw-r--r-- | src/file-utils.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/file-utils.c b/src/file-utils.c index a49974c..0909d7a 100644 --- a/src/file-utils.c +++ b/src/file-utils.c @@ -28,6 +28,7 @@ #ifdef HAVE_WINDOWS_H #define WIN32_LEAN_AND_MEAN +#define UNICODE #include <windows.h> #endif @@ -56,26 +57,35 @@ G_STATIC_ASSERT(INVALID_FILE_ATTRIBUTES == TECO_FILE_INVALID_ATTRIBUTES); teco_file_attributes_t teco_file_get_attributes(const gchar *filename) { - return GetFileAttributes((LPCTSTR)filename); + g_autofree gunichar2 *filename_utf16 = g_utf8_to_utf16(filename, -1, NULL, NULL, NULL); + return filename_utf16 ? GetFileAttributesW(filename_utf16) + : TECO_FILE_INVALID_ATTRIBUTES; } void teco_file_set_attributes(const gchar *filename, teco_file_attributes_t attrs) { - SetFileAttributes((LPCTSTR)filename, attrs); + g_autofree gunichar2 *filename_utf16 = g_utf8_to_utf16(filename, -1, NULL, NULL, NULL); + if (filename_utf16) + SetFileAttributesW(filename_utf16, attrs); } gchar * teco_file_get_absolute_path(const gchar *path) { + if (!path) + return NULL; + g_autofree gunichar2 *path_utf16 = g_utf8_to_utf16(path, -1, NULL, NULL, NULL); TCHAR buf[MAX_PATH]; - return path && GetFullPathName(path, sizeof(buf), buf, NULL) ? g_strdup(buf) : NULL; + return path_utf16 && GetFullPathNameW(path_utf16, G_N_ELEMENTS(buf), buf, NULL) + ? g_utf16_to_utf8(buf, -1, NULL, NULL, NULL) : NULL; } gboolean teco_file_is_visible(const gchar *path) { - return !(GetFileAttributes((LPCTSTR)path) & FILE_ATTRIBUTE_HIDDEN); + g_autofree gunichar2 *path_utf16 = g_utf8_to_utf16(path, -1, NULL, NULL, NULL); + return path_utf16 && !(GetFileAttributesW(path_utf16) & FILE_ATTRIBUTE_HIDDEN); } #else /* !G_OS_WIN32 */ @@ -83,7 +93,7 @@ teco_file_is_visible(const gchar *path) teco_file_attributes_t teco_file_get_attributes(const gchar *filename) { - struct stat buf; + GStatBuf buf; return g_stat(filename, &buf) ? TECO_FILE_INVALID_ATTRIBUTES : buf.st_mode; } |