From 0657b852024cf2560581326894793a552e40d275 Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Sat, 31 Dec 2022 11:09:49 +0100 Subject: [PATCH] Use GLib to convert between UTF-8 and UTF-16 --- app/main.c | 23 ++++++----------------- libgimp/gimp.c | 23 ++++++----------------- libgimpbase/gimputils.c | 22 ++++++---------------- 3 files changed, 18 insertions(+), 50 deletions(-) diff --git a/app/main.c b/app/main.c index c5a5416414..6a99f52bb9 100644 --- a/app/main.c +++ b/app/main.c @@ -625,29 +625,18 @@ main (int argc, const gchar *install_dir; gchar *bin_dir; LPWSTR w_bin_dir; - int n; w_bin_dir = NULL; install_dir = gimp_installation_directory (); bin_dir = g_build_filename (install_dir, "bin", NULL); - n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, - bin_dir, -1, NULL, 0); - if (n == 0) - goto out; - - w_bin_dir = g_malloc_n (n + 1, sizeof (wchar_t)); - n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, - bin_dir, -1, - w_bin_dir, (n + 1) * sizeof (wchar_t)); - if (n == 0) - goto out; - - SetDllDirectoryW (w_bin_dir); - - out: + w_bin_dir = g_utf8_to_utf16 (bin_dir, -1, NULL, NULL, NULL); if (w_bin_dir) - g_free (w_bin_dir); + { + SetDllDirectoryW (w_bin_dir); + g_free (w_bin_dir); + } + g_free (bin_dir); } diff --git a/libgimp/gimp.c b/libgimp/gimp.c index 63f9c3cd8b..dc3dc77875 100644 --- a/libgimp/gimp.c +++ b/libgimp/gimp.c @@ -213,29 +213,18 @@ gimp_main (GType plug_in_type, const gchar *install_dir; gchar *bin_dir; LPWSTR w_bin_dir; - int n; w_bin_dir = NULL; install_dir = gimp_installation_directory (); bin_dir = g_build_filename (install_dir, "bin", NULL); - n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, - bin_dir, -1, NULL, 0); - if (n == 0) - goto out; - - w_bin_dir = g_malloc_n (n + 1, sizeof (wchar_t)); - n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, - bin_dir, -1, - w_bin_dir, (n + 1) * sizeof (wchar_t)); - if (n == 0) - goto out; - - SetDllDirectoryW (w_bin_dir); - - out: + w_bin_dir = g_utf8_to_utf16 (bin_dir, -1, NULL, NULL, NULL); if (w_bin_dir) - g_free (w_bin_dir); + { + SetDllDirectoryW (w_bin_dir); + g_free (w_bin_dir); + } + g_free (bin_dir); } diff --git a/libgimpbase/gimputils.c b/libgimpbase/gimputils.c index 227efd5b95..85f26c2e3d 100644 --- a/libgimpbase/gimputils.c +++ b/libgimpbase/gimputils.c @@ -377,7 +377,6 @@ gimp_file_show_in_file_manager (GFile *file, { gboolean ret; char *filename; - int n; LPWSTR w_filename = NULL; ITEMIDLIST *pidl = NULL; @@ -385,7 +384,8 @@ gimp_file_show_in_file_manager (GFile *file, /* Calling this function multiple times should do no harm, but it is easier to put this here as it needs linking against ole32. */ - CoInitialize (NULL); + if (FAILED (CoInitialize (NULL))) + return ret; filename = g_file_get_path (file); if (!filename) @@ -395,20 +395,8 @@ gimp_file_show_in_file_manager (GFile *file, goto out; } - n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, - filename, -1, NULL, 0); - if (n == 0) - { - g_set_error_literal (error, G_FILE_ERROR, 0, - _("Error converting UTF-8 filename to wide char")); - goto out; - } - - w_filename = g_malloc_n (n + 1, sizeof (wchar_t)); - n = MultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, - filename, -1, - w_filename, (n + 1) * sizeof (wchar_t)); - if (n == 0) + w_filename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); + if (!w_filename) { g_set_error_literal (error, G_FILE_ERROR, 0, _("Error converting UTF-8 filename to wide char")); @@ -432,6 +420,8 @@ gimp_file_show_in_file_manager (GFile *file, g_free (w_filename); g_free (filename); + CoUninitialize (); + return ret; }