Fix the GObject Visual Studio Projects
[platform/upstream/glib.git] / glib / goption.c
index 7f8af97..0a22f6f 100644 (file)
@@ -272,16 +272,11 @@ _g_utf8_strwidth (const gchar *p)
   return len;
 }
 
-
-GQuark
-g_option_error_quark (void)
-{
-  return g_quark_from_static_string ("g-option-context-error-quark");
-}
+G_DEFINE_QUARK (g-option-context-error-quark, g_option_error)
 
 /**
  * g_option_context_new:
- * @parameter_string: a string which is displayed in
+ * @parameter_string: (allow-none): a string which is displayed in
  *    the first line of <option>--help</option> output, after the
  *    usage summary
  *    <literal><replaceable>programname</replaceable> [OPTION...]</literal>
@@ -562,10 +557,12 @@ g_option_context_add_main_entries (GOptionContext      *context,
 }
 
 static gint
-calculate_max_length (GOptionGroup *group)
+calculate_max_length (GOptionGroup *group,
+                      GHashTable   *aliases)
 {
   GOptionEntry *entry;
   gint i, len, max_length;
+  const gchar *long_name;
 
   max_length = 0;
 
@@ -576,7 +573,10 @@ calculate_max_length (GOptionGroup *group)
       if (entry->flags & G_OPTION_FLAG_HIDDEN)
         continue;
 
-      len = _g_utf8_strwidth (entry->long_name);
+      long_name = g_hash_table_lookup (aliases, &entry->long_name);
+      if (!long_name)
+        long_name = entry->long_name;
+      len = _g_utf8_strwidth (long_name);
 
       if (entry->short_name)
         len += 4;
@@ -594,9 +594,11 @@ static void
 print_entry (GOptionGroup       *group,
              gint                max_length,
              const GOptionEntry *entry,
-             GString            *string)
+             GString            *string,
+             GHashTable         *aliases)
 {
   GString *str;
+  const gchar *long_name;
 
   if (entry->flags & G_OPTION_FLAG_HIDDEN)
     return;
@@ -604,12 +606,16 @@ print_entry (GOptionGroup       *group,
   if (entry->long_name[0] == 0)
     return;
 
+  long_name = g_hash_table_lookup (aliases, &entry->long_name);
+  if (!long_name)
+    long_name = entry->long_name;
+
   str = g_string_new (NULL);
 
   if (entry->short_name)
-    g_string_append_printf (str, "  -%c, --%s", entry->short_name, entry->long_name);
+    g_string_append_printf (str, "  -%c, --%s", entry->short_name, long_name);
   else
-    g_string_append_printf (str, "  --%s", entry->long_name);
+    g_string_append_printf (str, "  --%s", long_name);
 
   if (entry->arg_description)
     g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
@@ -639,6 +645,8 @@ group_has_visible_entries (GOptionContext *context,
 
       if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
         continue;
+      if (entry->long_name[0] == 0) /* ignore rest entry */
+        continue;
       if (!(entry->flags & reject_filter))
         return TRUE;
     }
@@ -647,7 +655,7 @@ group_has_visible_entries (GOptionContext *context,
 }
 
 static gboolean
-group_list_has_visible_entires (GOptionContext *context,
+group_list_has_visible_entries (GOptionContext *context,
                                 GList          *group_list,
                                 gboolean       main_entries)
 {
@@ -715,10 +723,11 @@ g_option_context_get_help (GOptionContext *context,
                            GOptionGroup   *group)
 {
   GList *list;
-  gint max_length, len;
+  gint max_length = 0, len;
   gint i;
   GOptionEntry *entry;
   GHashTable *shadow_map;
+  GHashTable *aliases;
   gboolean seen[256];
   const gchar *rest_description;
   GString *string;
@@ -766,6 +775,7 @@ g_option_context_get_help (GOptionContext *context,
 
   memset (seen, 0, sizeof (gboolean) * 256);
   shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
+  aliases = g_hash_table_new_full (NULL, NULL, NULL, g_free);
 
   if (context->main_group)
     {
@@ -792,7 +802,10 @@ g_option_context_get_help (GOptionContext *context,
           entry = &g->entries[i];
           if (g_hash_table_lookup (shadow_map, entry->long_name) &&
               !(entry->flags & G_OPTION_FLAG_NOALIAS))
-            entry->long_name = g_strdup_printf ("%s-%s", g->name, entry->long_name);
+            {
+              g_hash_table_insert (aliases, &entry->long_name,
+                                   g_strdup_printf ("%s-%s", g->name, entry->long_name));
+            }
           else
             g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
 
@@ -809,17 +822,20 @@ g_option_context_get_help (GOptionContext *context,
 
   list = context->groups;
 
-  max_length = _g_utf8_strwidth ("-?, --help");
-
-  if (list)
+  if (context->help_enabled)
     {
-      len = _g_utf8_strwidth ("--help-all");
-      max_length = MAX (max_length, len);
+      max_length = _g_utf8_strwidth ("-?, --help");
+
+      if (list)
+        {
+          len = _g_utf8_strwidth ("--help-all");
+          max_length = MAX (max_length, len);
+        }
     }
 
   if (context->main_group)
     {
-      len = calculate_max_length (context->main_group);
+      len = calculate_max_length (context->main_group, aliases);
       max_length = MAX (max_length, len);
     }
 
@@ -827,12 +843,15 @@ g_option_context_get_help (GOptionContext *context,
     {
       GOptionGroup *g = list->data;
 
-      /* First, we check the --help-<groupname> options */
-      len = _g_utf8_strwidth ("--help-") + _g_utf8_strwidth (g->name);
-      max_length = MAX (max_length, len);
+      if (context->help_enabled)
+        {
+          /* First, we check the --help-<groupname> options */
+          len = _g_utf8_strwidth ("--help-") + _g_utf8_strwidth (g->name);
+          max_length = MAX (max_length, len);
+        }
 
       /* Then we go through the entries */
-      len = calculate_max_length (g);
+      len = calculate_max_length (g, aliases);
       max_length = MAX (max_length, len);
 
       list = list->next;
@@ -841,7 +860,7 @@ g_option_context_get_help (GOptionContext *context,
   /* Add a bit of padding */
   max_length += 4;
 
-  if (!group)
+  if (!group && context->help_enabled)
     {
       list = context->groups;
 
@@ -881,7 +900,7 @@ g_option_context_get_help (GOptionContext *context,
           g_string_append (string, TRANSLATE (group, group->description));
           g_string_append (string, "\n");
           for (i = 0; i < group->n_entries; i++)
-            print_entry (group, max_length, &group->entries[i], string);
+            print_entry (group, max_length, &group->entries[i], string, aliases);
           g_string_append (string, "\n");
         }
     }
@@ -901,7 +920,7 @@ g_option_context_get_help (GOptionContext *context,
               g_string_append (string, "\n");
               for (i = 0; i < g->n_entries; i++)
                 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
-                  print_entry (g, max_length, &g->entries[i], string);
+                  print_entry (g, max_length, &g->entries[i], string, aliases);
 
               g_string_append (string, "\n");
             }
@@ -913,7 +932,7 @@ g_option_context_get_help (GOptionContext *context,
   /* Print application options if --help or --help-all has been specified */
   if ((main_help || !group) &&
       (group_has_visible_entries (context, context->main_group, TRUE) ||
-       group_list_has_visible_entires (context, context->groups, TRUE)))
+       group_list_has_visible_entries (context, context->groups, TRUE)))
     {
       list = context->groups;
 
@@ -922,7 +941,7 @@ g_option_context_get_help (GOptionContext *context,
       if (context->main_group)
         for (i = 0; i < context->main_group->n_entries; i++)
           print_entry (context->main_group, max_length,
-                       &context->main_group->entries[i], string);
+                       &context->main_group->entries[i], string, aliases);
 
       while (list != NULL)
         {
@@ -931,7 +950,7 @@ g_option_context_get_help (GOptionContext *context,
           /* Print main entries from other groups */
           for (i = 0; i < g->n_entries; i++)
             if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
-              print_entry (g, max_length, &g->entries[i], string);
+              print_entry (g, max_length, &g->entries[i], string, aliases);
 
           list = list->next;
         }
@@ -945,6 +964,8 @@ g_option_context_get_help (GOptionContext *context,
       g_string_append (string, "\n");
     }
 
+  g_hash_table_destroy (aliases);
+
   return g_string_free (string, FALSE);
 }
 
@@ -1679,7 +1700,7 @@ platform_get_argv0 (void)
 
   cmdline = (char **) realloc (cmdline, len);
 
-  if (sysctl (mib, nitems (mib), cmdline, &len, NULL, 0) == -1)
+  if (sysctl (mib, G_N_ELEMENTS (mib), cmdline, &len, NULL, 0) == -1)
     {
       g_free (cmdline);
       return NULL;