[gimp] Fix a lot of stupidity in gimp_item_tree_uniquefy_name()



commit 2cb6669d0122e023f048d747a08c3a9593b495c2
Author: Michael Natterer <mitch gimp org>
Date:   Sun Feb 7 23:56:40 2010 +0100

    Fix a lot of stupidity in gimp_item_tree_uniquefy_name()
    
    Don't write to the string returned by gimp_object_get_name(). Pull
    most of the code out of the inner loop (which also means to allocate
    only one instead of three strings in the inner loop). Don't use the
    object as storage for the name that gets generated in the inner loop.
    Also, a space can't hurt so it's now "Foo #1" instead of "Foo#1".

 app/core/gimpitemtree.c |   33 +++++++++++++++++++++------------
 1 files changed, 21 insertions(+), 12 deletions(-)
---
diff --git a/app/core/gimpitemtree.c b/app/core/gimpitemtree.c
index 97710d2..af9217c 100644
--- a/app/core/gimpitemtree.c
+++ b/app/core/gimpitemtree.c
@@ -600,40 +600,49 @@ gimp_item_tree_uniquefy_name (GimpItemTree *tree,
       gimp_object_set_name (GIMP_OBJECT (item), new_name);
     }
 
-  while (g_hash_table_lookup (private->name_hash,
-                              gimp_object_get_name (item)))
+  if (g_hash_table_lookup (private->name_hash,
+                           gimp_object_get_name (item)))
     {
-      gchar *name   = g_strdup (gimp_object_get_name (item));
-      gchar *ext    = strrchr (name, '#');
-      gint   number = 0;
+      gchar *name     = g_strdup (gimp_object_get_name (item));
+      gchar *ext      = strrchr (name, '#');
+      gchar *new_name = NULL;
+      gint   number   = 0;
 
       if (ext)
         {
-          gchar *ext_str;
+          gchar ext_str[8];
 
           number = atoi (ext + 1);
 
-          ext_str = g_strdup_printf ("%d", number);
+          g_snprintf (ext_str, sizeof (ext_str), "%d", number);
 
           /*  check if the extension really is of the form "#<n>"  */
           if (! strcmp (ext_str, ext + 1))
             {
+              if (ext > name && *(ext - 1) == ' ')
+                ext--;
+
               *ext = '\0';
             }
           else
             {
               number = 0;
             }
-
-          g_free (ext_str);
         }
 
-      number++;
+      do
+        {
+          number++;
+
+          g_free (new_name);
 
-      gimp_object_take_name (GIMP_OBJECT (item),
-                             g_strdup_printf ("%s#%d", name, number));
+          new_name = g_strdup_printf ("%s #%d", name, number);
+        }
+      while (g_hash_table_lookup (private->name_hash, new_name));
 
       g_free (name);
+
+      gimp_object_take_name (GIMP_OBJECT (item), new_name);
     }
 
   g_hash_table_insert (private->name_hash,



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