File 0001-Add-g_option_context_parse_utf8.patch of Package mingw32-glib2

From 474525e0ad3fdb9febf72bbb07a421816d40108c Mon Sep 17 00:00:00 2001
From: Hib Eris <hib@hiberis.nl>
Date: Wed, 6 Feb 2013 09:41:19 +0100
Subject: [PATCH 1/4] Add g_option_context_parse_utf8()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Based on the patches by Christian Persch, Tor Lillqvist and Krzysztof KosiƄski.

https://bugzilla.gnome.org/show_bug.cgi?id=522131
---
 docs/reference/glib/glib-sections.txt |   1 +
 glib/goption.c                        | 141 ++++++++++++++++++++++++++--------
 glib/goption.h                        |   6 ++
 3 files changed, 116 insertions(+), 32 deletions(-)

diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index d9b8dca..b7915d3 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -1175,6 +1175,7 @@ g_option_context_set_translate_func
 g_option_context_set_translation_domain
 g_option_context_free
 g_option_context_parse
+g_option_context_parse_utf8
 g_option_context_set_help_enabled
 g_option_context_get_help_enabled
 g_option_context_set_ignore_unknown_options
diff --git a/glib/goption.c b/glib/goption.c
index 0a22f6f..76013f5 100644
--- a/glib/goption.c
+++ b/glib/goption.c
@@ -241,6 +241,12 @@ static void free_changes_list (GOptionContext *context,
                                gboolean        revert);
 static void free_pending_nulls (GOptionContext *context,
                                 gboolean        perform_nulls);
+static gboolean
+option_context_parse_internal (GOptionContext   *context,
+                               gboolean          utf8_flag,
+                               gint             *argc,
+                               gchar          ***argv,
+                               GError          **error);
 
 
 static int
@@ -1130,12 +1136,37 @@ add_pending_null (GOptionContext *context,
   context->pending_nulls = g_list_prepend (context->pending_nulls, n);
 }
 
+static gchar *
+get_string_arg (const gchar *value,
+                gboolean     utf8_flag,
+                GError     **error)
+{
+  if (utf8_flag)
+    return g_strdup (value);
+
+  return g_locale_to_utf8 (value, -1, NULL, NULL, error);
+}
+
+static gchar *
+get_filename_arg (const gchar *value,
+                  gboolean     utf8_flag,
+                  GError     **error)
+{
+#ifdef G_OS_WIN32
+  if (!utf8_flag)
+    return g_locale_to_utf8 (value, -1, NULL, NULL, error);
+#endif
+
+  return g_strdup (value);
+}
+
 static gboolean
 parse_arg (GOptionContext *context,
            GOptionGroup   *group,
            GOptionEntry   *entry,
            const gchar    *value,
            const gchar    *option_name,
+           gboolean        utf8_flag,
            GError        **error)
 
 {
@@ -1157,7 +1188,7 @@ parse_arg (GOptionContext *context,
       {
         gchar *data;
 
-        data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        data = get_string_arg (value, utf8_flag, error);
 
         if (!data)
           return FALSE;
@@ -1176,7 +1207,7 @@ parse_arg (GOptionContext *context,
       {
         gchar *data;
 
-        data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        data = get_string_arg (value, utf8_flag, error);
 
         if (!data)
           return FALSE;
@@ -1208,14 +1239,11 @@ parse_arg (GOptionContext *context,
       {
         gchar *data;
 
-#ifdef G_OS_WIN32
-        data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        data = get_filename_arg (value, utf8_flag, error);
 
         if (!data)
           return FALSE;
-#else
-        data = g_strdup (value);
-#endif
+
         change = get_change (context, G_OPTION_ARG_FILENAME,
                              entry->arg_data);
         g_free (change->allocated.str);
@@ -1231,14 +1259,11 @@ parse_arg (GOptionContext *context,
       {
         gchar *data;
 
-#ifdef G_OS_WIN32
-        data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+        data = get_filename_arg (value, utf8_flag, error);
 
         if (!data)
           return FALSE;
-#else
-        data = g_strdup (value);
-#endif
+
         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
                              entry->arg_data);
 
@@ -1287,15 +1312,9 @@ parse_arg (GOptionContext *context,
         else if (entry->flags & G_OPTION_FLAG_NO_ARG)
           data = NULL;
         else if (entry->flags & G_OPTION_FLAG_FILENAME)
-          {
-#ifdef G_OS_WIN32
-            data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
-#else
-            data = g_strdup (value);
-#endif
-          }
+          data = get_string_arg (value, utf8_flag, error);
         else
-          data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
+          data = get_string_arg (value, utf8_flag, error);
 
         if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
             !data)
@@ -1361,6 +1380,7 @@ parse_short_option (GOptionContext *context,
                     gint            idx,
                     gint           *new_idx,
                     gchar           arg,
+                    gboolean        utf8_flag,
                     gint           *argc,
                     gchar        ***argv,
                     GError        **error,
@@ -1423,7 +1443,7 @@ parse_short_option (GOptionContext *context,
             }
 
           if (!parse_arg (context, group, &group->entries[j],
-                          value, option_name, error))
+                          value, option_name, utf8_flag, error))
             {
               g_free (option_name);
               return FALSE;
@@ -1443,6 +1463,7 @@ parse_long_option (GOptionContext *context,
                    gint           *idx,
                    gchar          *arg,
                    gboolean        aliased,
+                   gboolean        utf8_flag,
                    gint           *argc,
                    gchar        ***argv,
                    GError        **error,
@@ -1466,7 +1487,7 @@ parse_long_option (GOptionContext *context,
 
           option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
           retval = parse_arg (context, group, &group->entries[j],
-                              NULL, option_name, error);
+                              NULL, option_name, utf8_flag, error);
           g_free (option_name);
 
           add_pending_null (context, &((*argv)[*idx]), NULL);
@@ -1503,7 +1524,7 @@ parse_long_option (GOptionContext *context,
                         {
                           gboolean retval;
                           retval = parse_arg (context, group, &group->entries[j],
-                                              NULL, option_name, error);
+                                              NULL, option_name, utf8_flag, error);
                           *parsed = TRUE;
                           g_free (option_name);
                           return retval;
@@ -1520,7 +1541,7 @@ parse_long_option (GOptionContext *context,
                 {
                     gboolean retval;
                     retval = parse_arg (context, group, &group->entries[j],
-                                        NULL, option_name, error);
+                                        NULL, option_name, utf8_flag, error);
                     *parsed = TRUE;
                     g_free (option_name);
                     return retval;
@@ -1535,7 +1556,7 @@ parse_long_option (GOptionContext *context,
                 }
 
               if (!parse_arg (context, group, &group->entries[j],
-                              value, option_name, error))
+                              value, option_name, utf8_flag, error))
                 {
                   g_free (option_name);
                   return FALSE;
@@ -1553,6 +1574,7 @@ parse_long_option (GOptionContext *context,
 static gboolean
 parse_remaining_arg (GOptionContext *context,
                      GOptionGroup   *group,
+                     gboolean        utf8_flag,
                      gint           *idx,
                      gint           *argc,
                      gchar        ***argv,
@@ -1575,7 +1597,7 @@ parse_remaining_arg (GOptionContext *context,
 
       add_pending_null (context, &((*argv)[*idx]), NULL);
 
-      if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
+      if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", utf8_flag, error))
         return FALSE;
 
       *parsed = TRUE;
@@ -1759,6 +1781,16 @@ g_option_context_parse (GOptionContext   *context,
                         gchar          ***argv,
                         GError          **error)
 {
+  return option_context_parse_internal (context, FALSE, argc, argv, error);
+}
+
+static gboolean
+option_context_parse_internal (GOptionContext   *context,
+                               gboolean          utf8_flag,
+                               gint             *argc,
+                               gchar          ***argv,
+                               GError          **error)
+{
   gint i, j, k;
   GList *list;
 
@@ -1855,7 +1887,7 @@ g_option_context_parse (GOptionContext   *context,
 
                   if (context->main_group &&
                       !parse_long_option (context, context->main_group, &i, arg,
-                                          FALSE, argc, argv, error, &parsed))
+                                          FALSE, utf8_flag, argc, argv, error, &parsed))
                     goto fail;
 
                   if (parsed)
@@ -1868,7 +1900,7 @@ g_option_context_parse (GOptionContext   *context,
                       GOptionGroup *group = list->data;
 
                       if (!parse_long_option (context, group, &i, arg,
-                                              FALSE, argc, argv, error, &parsed))
+                                              FALSE, utf8_flag, argc, argv, error, &parsed))
                         goto fail;
 
                       if (parsed)
@@ -1893,7 +1925,7 @@ g_option_context_parse (GOptionContext   *context,
                           if (strncmp (group->name, arg, dash - arg) == 0)
                             {
                               if (!parse_long_option (context, group, &i, dash + 1,
-                                                      TRUE, argc, argv, error, &parsed))
+                                                      TRUE, utf8_flag, argc, argv, error, &parsed))
                                 goto fail;
 
                               if (parsed)
@@ -1924,7 +1956,7 @@ g_option_context_parse (GOptionContext   *context,
                       parsed = FALSE;
                       if (context->main_group &&
                           !parse_short_option (context, context->main_group,
-                                               i, &new_i, arg[j],
+                                               i, &new_i, arg[j], utf8_flag,
                                                argc, argv, error, &parsed))
                         goto fail;
                       if (!parsed)
@@ -1935,7 +1967,7 @@ g_option_context_parse (GOptionContext   *context,
                             {
                               GOptionGroup *group = list->data;
                               if (!parse_short_option (context, group, i, &new_i, arg[j],
-                                                       argc, argv, error, &parsed))
+                                                       utf8_flag, argc, argv, error, &parsed))
                                 goto fail;
                               if (parsed)
                                 break;
@@ -1990,7 +2022,7 @@ g_option_context_parse (GOptionContext   *context,
             {
               /* Collect remaining args */
               if (context->main_group &&
-                  !parse_remaining_arg (context, context->main_group, &i,
+                  !parse_remaining_arg (context, context->main_group, utf8_flag, &i,
                                         argc, argv, error, &parsed))
                 goto fail;
 
@@ -2078,6 +2110,51 @@ g_option_context_parse (GOptionContext   *context,
 }
 
 /**
+ * g_option_context_parse_utf8:
+ * @context: a #GOptionContext
+ * @argc: (inout) (allow-none): a pointer to the number of strings in @argv
+ * @argv: (inout) (array length=argc) (allow-none) (element-type utf-8): a pointer to an array of UTF-8 strings
+ * @error: a return location for errors
+ *
+ * A variant of g_option_context_parse() that treats @argv as an
+ * array of UTF-8 strings.
+ *
+ * This function is suitable for use with the wide arguments array
+ * returned from g_win32_get_wargv().
+ *
+ * Note that arguments for %G_OPTION_ARG_FILENAME and
+ * %G_OPTION_ARG_FILENAME_ARRAY options, and %G_OPTION_ARG_CALLBACK
+ * options with %G_OPTION_FLAG_FILENAME will be passed the unchanged
+ * UTF-8 strings, and not strings converted to filename encoding
+ * (see g_filename_from_utf8()).
+ *
+ * Return value: %TRUE if the parsing was successful,
+ *               %FALSE if an error occurred
+ *
+ * Since: 2.36
+ **/
+gboolean
+g_option_context_parse_utf8 (GOptionContext   *context,
+                             gint             *argc,
+                             gchar          ***argv,
+                             GError          **error)
+{
+  gint i;
+
+  for (i = 0; i < *argc; ++i)
+    {
+      if (!g_utf8_validate ((*argv)[i], -1, NULL))
+        {
+          g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
+                       "Argument is not UTF-8");
+          return FALSE;
+        }
+    }
+
+  return option_context_parse_internal (context, TRUE, argc, argv, error);
+}
+
+/**
  * g_option_group_new:
  * @name: the name for the option group, this is used to provide
  *   help for the options in this group with <option>--help-</option>@name
diff --git a/glib/goption.h b/glib/goption.h
index ee01377..8950430 100644
--- a/glib/goption.h
+++ b/glib/goption.h
@@ -342,6 +342,12 @@ gboolean        g_option_context_parse            (GOptionContext      *context,
 						   gint                *argc,
 						   gchar             ***argv,
 						   GError             **error);
+GLIB_AVAILABLE_IN_2_36
+gboolean	g_option_context_parse_utf8       (GOptionContext      *context,
+						   gint                *argc,
+						   gchar             ***argv,
+						   GError             **error);
+
 GLIB_AVAILABLE_IN_ALL
 void            g_option_context_set_translate_func (GOptionContext     *context,
 						     GTranslateFunc      func,
-- 
1.8.1.2

openSUSE Build Service is sponsored by