gimp r26834 - in trunk: . app/widgets



Author: mitch
Date: Tue Sep  2 11:30:28 2008
New Revision: 26834
URL: http://svn.gnome.org/viewvc/gimp?rev=26834&view=rev

Log:
2008-09-02  Michael Natterer  <mitch gimp org>

	* app/widgets/gimpselectiondata.c (gimp_selection_data_get_name):
	return a const string, no need to strdup it since it's only used
	temporarily in this file.

	(gimp_selection_data_get_image)
	(gimp_selection_data_get_component)
	(gimp_selection_data_get_item)
	(gimp_selection_data_get_object): changed accordingly. Move
	variables to local scopes and simplify.



Modified:
   trunk/ChangeLog
   trunk/app/widgets/gimpselectiondata.c

Modified: trunk/app/widgets/gimpselectiondata.c
==============================================================================
--- trunk/app/widgets/gimpselectiondata.c	(original)
+++ trunk/app/widgets/gimpselectiondata.c	Tue Sep  2 11:30:28 2008
@@ -50,14 +50,15 @@
 
 /*  local function prototypes  */
 
-static gchar      * gimp_selection_data_get_name   (GtkSelectionData *selection);
-static GimpObject * gimp_selection_data_get_object (GtkSelectionData *selection,
-                                                    GimpContainer    *container,
-                                                    GimpObject       *additional);
-static gchar      * gimp_unescape_uri_string       (const char       *escaped,
-                                                    int               len,
-                                                    const char       *illegal_escaped_characters,
-                                                    gboolean          ascii_must_not_be_escaped);
+static const gchar * gimp_selection_data_get_name   (GtkSelectionData *selection,
+                                                     const gchar      *strfunc);
+static GimpObject  * gimp_selection_data_get_object (GtkSelectionData *selection,
+                                                     GimpContainer    *container,
+                                                     GimpObject       *additional);
+static gchar       * gimp_unescape_uri_string       (const char       *escaped,
+                                                     int               len,
+                                                     const char       *illegal_escaped_characters,
+                                                     gboolean          ascii_must_not_be_escaped);
 
 
 /*  public functions  */
@@ -369,27 +370,26 @@
 gimp_selection_data_get_image (GtkSelectionData *selection,
                                Gimp             *gimp)
 {
-  GimpImage *image = NULL;
-  gchar     *str;
-  gint       pid;
-  gint       ID;
+  const gchar *str;
 
   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
   g_return_val_if_fail (selection != NULL, NULL);
 
-  str = gimp_selection_data_get_name (selection);
-  if (! str)
-    return NULL;
+  str = gimp_selection_data_get_name (selection, G_STRFUNC);
 
-  if (sscanf (str, "%i:%i", &pid, &ID) == 2 &&
-      pid == get_pid ())
+  if (str)
     {
-      image = gimp_image_get_by_ID (gimp, ID);
-    }
+      gint pid;
+      gint ID;
 
-  g_free (str);
+      if (sscanf (str, "%i:%i", &pid, &ID) == 2 &&
+          pid == get_pid ())
+        {
+          return gimp_image_get_by_ID (gimp, ID);
+        }
+    }
 
-  return image;
+  return NULL;
 }
 
 void
@@ -416,11 +416,7 @@
                                    Gimp             *gimp,
                                    GimpChannelType  *channel)
 {
-  GimpImage *image = NULL;
-  gchar     *str;
-  gint       pid;
-  gint       ID;
-  gint       ch;
+  const gchar *str;
 
   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
   g_return_val_if_fail (selection != NULL, NULL);
@@ -428,22 +424,27 @@
   if (channel)
     *channel = 0;
 
-  str = gimp_selection_data_get_name (selection);
-  if (! str)
-    return NULL;
+  str = gimp_selection_data_get_name (selection, G_STRFUNC);
 
-  if (sscanf (str, "%i:%i:%i", &pid, &ID, &ch) == 3 &&
-      pid == get_pid ())
+  if (str)
     {
-      image = gimp_image_get_by_ID (gimp, ID);
+      gint pid;
+      gint ID;
+      gint ch;
 
-      if (image && channel)
-        *channel = ch;
-    }
+      if (sscanf (str, "%i:%i:%i", &pid, &ID, &ch) == 3 &&
+          pid == get_pid ())
+        {
+          GimpImage *image = gimp_image_get_by_ID (gimp, ID);
 
-  g_free (str);
+          if (image && channel)
+            *channel = ch;
 
-  return image;
+          return image;
+        }
+    }
+
+  return NULL;
 }
 
 void
@@ -467,27 +468,26 @@
 gimp_selection_data_get_item (GtkSelectionData *selection,
                               Gimp             *gimp)
 {
-  GimpItem *item = NULL;
-  gchar    *str;
-  gint      pid;
-  gint      ID;
+  const gchar *str;
 
   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
   g_return_val_if_fail (selection != NULL, NULL);
 
-  str = gimp_selection_data_get_name (selection);
-  if (! str)
-    return NULL;
+  str = gimp_selection_data_get_name (selection, G_STRFUNC);
 
-  if (sscanf (str, "%i:%i", &pid, &ID) == 2 &&
-      pid == get_pid ())
+  if (str)
     {
-      item = gimp_item_get_by_ID (gimp, ID);
-    }
+      gint pid;
+      gint ID;
 
-  g_free (str);
+      if (sscanf (str, "%i:%i", &pid, &ID) == 2 &&
+          pid == get_pid ())
+        {
+          return gimp_item_get_by_ID (gimp, ID);
+        }
+    }
 
-  return item;
+  return NULL;
 }
 
 void
@@ -630,24 +630,24 @@
 
 /*  private functions  */
 
-static gchar *
-gimp_selection_data_get_name (GtkSelectionData *selection)
+static const gchar *
+gimp_selection_data_get_name (GtkSelectionData *selection,
+                              const gchar      *strfunc)
 {
-  gchar *name;
+  const gchar *name;
 
   if ((selection->format != 8) || (selection->length < 1))
     {
-      g_warning ("Received invalid selection data");
+      g_warning ("%s: received invalid selection data", strfunc);
       return NULL;
     }
 
-  name = g_strndup ((const gchar *) selection->data, selection->length);
+  name = (const gchar *) selection->data;
 
   if (! g_utf8_validate (name, -1, NULL))
     {
-      g_warning ("Received invalid selection data "
-                 "(doesn't validate as UTF-8)!");
-      g_free (name);
+      g_warning ("%s: received invalid selection data "
+                 "(doesn't validate as UTF-8)", strfunc);
       return NULL;
     }
 
@@ -661,42 +661,43 @@
                                 GimpContainer    *container,
                                 GimpObject       *additional)
 {
-  GimpObject *object = NULL;
-  gchar      *str;
-  gint        pid;
-  gpointer    object_addr;
-  gint        name_offset = 0;
+  const gchar *str;
 
-  str = gimp_selection_data_get_name (selection);
-  if (! str)
-    return NULL;
+  str = gimp_selection_data_get_name (selection, G_STRFUNC);
 
-  if (sscanf (str, "%i:%p:%n", &pid, &object_addr, &name_offset) >= 2 &&
-      pid == get_pid () && name_offset > 0)
+  if (str)
     {
-      gchar *name = str + name_offset;
-
-      GIMP_LOG (DND, "pid = %d, addr = %p, name = '%s'",
-                pid, object_addr, name);
+      gint     pid;
+      gpointer object_addr;
+      gint     name_offset = 0;
 
-      if (additional &&
-          strcmp (name, gimp_object_get_name (additional)) == 0 &&
-          object_addr == (gpointer) additional)
+      if (sscanf (str, "%i:%p:%n", &pid, &object_addr, &name_offset) >= 2 &&
+          pid == get_pid () && name_offset > 0)
         {
-          object = additional;
-        }
-      else
-        {
-          object = gimp_container_get_child_by_name (container, name);
+          const gchar *name = str + name_offset;
+
+          GIMP_LOG (DND, "pid = %d, addr = %p, name = '%s'",
+                    pid, object_addr, name);
 
-          if (object && object_addr != (gpointer) object)
-            object = NULL;
+          if (additional &&
+              strcmp (name, gimp_object_get_name (additional)) == 0 &&
+              object_addr == (gpointer) additional)
+            {
+              return additional;
+            }
+          else
+            {
+              GimpObject *object;
+
+              object = gimp_container_get_child_by_name (container, name);
+
+              if (object_addr == (gpointer) object)
+                return object;
+            }
         }
     }
 
-  g_free (str);
-
-  return object;
+  return NULL;
 }
 
 /*  the next two functions are straight cut'n'paste from glib/glib/gconvert.c,



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