[glib] g_option_context_parse_strv: use UTF-8 on Windows



commit 3352293ab5b93d8607dc61a99195ea09aa7bf423
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Jan 12 12:10:19 2014 -0500

    g_option_context_parse_strv: use UTF-8 on Windows
    
    Add another difference to the freshly-added g_option_context_parse_strv:
    now, on Windows, its arguments on to be in UTF-8 instead of the argv[]
    encoding (from the system codepage).
    
    The documentation teases g_win32_get_command_line() which is a new
    GLib-filename-encoding-based function that will be added in a following
    commit.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=722025

 glib/goption.c |   37 +++++++++++++++++++++++++++++++++----
 1 files changed, 33 insertions(+), 4 deletions(-)
---
diff --git a/glib/goption.c b/glib/goption.c
index 0001e70..e66dc14 100644
--- a/glib/goption.c
+++ b/glib/goption.c
@@ -1158,7 +1158,14 @@ parse_arg (GOptionContext *context,
       {
         gchar *data;
 
+#if G_OS_WIN32
+        if (!context->strv_mode)
+          data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        else
+          data = g_strdup (value);
+#else
         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+#endif
 
         if (!data)
           return FALSE;
@@ -1177,7 +1184,14 @@ parse_arg (GOptionContext *context,
       {
         gchar *data;
 
+#if G_OS_WIN32
+        if (!context->strv_mode)
+          data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        else
+          data = g_strdup (value);
+#else
         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+#endif
 
         if (!data)
           return FALSE;
@@ -1210,7 +1224,10 @@ parse_arg (GOptionContext *context,
         gchar *data;
 
 #ifdef G_OS_WIN32
-        data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        if (!context->strv_mode)
+          data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        else
+          data = g_strdup (value);
 
         if (!data)
           return FALSE;
@@ -1233,7 +1250,10 @@ parse_arg (GOptionContext *context,
         gchar *data;
 
 #ifdef G_OS_WIN32
-        data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        if (!context->strv_mode)
+          data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        else
+          data = g_strdup (value);
 
         if (!data)
           return FALSE;
@@ -1290,7 +1310,10 @@ parse_arg (GOptionContext *context,
         else if (entry->flags & G_OPTION_FLAG_FILENAME)
           {
 #ifdef G_OS_WIN32
-            data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+            if (!context->strv_mode)
+              data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+            else
+              data = g_strdup (value);
 #else
             data = g_strdup (value);
 #endif
@@ -2472,7 +2495,8 @@ g_option_context_get_description (GOptionContext *context)
 /**
  * g_option_context_parse_strv:
  * @context: a #GOptionContext
- * @arguments: (inout) (array null-terminated=1): a pointer to the command line arguments
+ * @arguments: (inout) (array null-terminated=1): a pointer to the
+ *    command line arguments (which must be in UTF-8 on Windows)
  * @error: a return location for errors
  *
  * Parses the command line arguments.
@@ -2484,6 +2508,11 @@ g_option_context_get_description (GOptionContext *context)
  * In particular, strings that are removed from the arguments list will
  * be freed using g_free().
  *
+ * On Windows, the strings are expected to be in UTF-8.  This is in
+ * contrast to g_option_context_parse() which expects them to be in the
+ * system codepage, which is how they are passed as @argv to main().
+ * See g_win32_get_command_line() for a solution.
+ *
  * This function is useful if you are trying to use #GOptionContext with
  * #GApplication.
  *


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]