Patch for concatenating arrays of strings



	I have found that there is no function to concatenate an string array,
so I have made it. I send the patch and I hope it to be useful.

	Thanks.

-- 
Xabier Rodriguez Calvar <xrcalvar igalia com>
Igalia, S.L.
Index: glib/gstrfuncs.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gstrfuncs.c,v
retrieving revision 1.100
diff -u -r1.100 gstrfuncs.c
--- glib/gstrfuncs.c	1 Jul 2003 22:15:33 -0000	1.100
+++ glib/gstrfuncs.c	11 Sep 2003 14:21:13 -0000
@@ -242,6 +242,44 @@
   return concat;
 }
 
+
+/**
+ * g_strconcat_array:
+ * @string_array: a NULL-terminated string array
+ * 
+ * Copies each string in the array inside the a new one
+ * 
+ * Returns: a new string as result of concatenation of the
+ * whole array
+ */
+
+gchar *
+g_strconcat_array(const gchar **string_array)
+{
+  gsize  size = 1;
+  gint   index;
+  gchar *concat;
+  gchar *ptr;
+
+  g_return_val_if_fail(string_array == NULL, NULL);
+
+  for (index = 0; string_array[index] != NULL; index++)
+    {
+      size += strlen(string_array[index]);
+    }
+  
+  concat = g_new(gchar, size);
+  ptr = concat;
+
+  for (index = 0; string_array[index] != NULL, index++)
+    {
+      ptr = g_stpcpy(ptr, string_array[index]);
+    }
+ 
+  return concat;
+}
+
+
 /**
  * g_strtod:
  * @nptr:    the string to convert to a numeric value.
Index: glib/gstrfuncs.h
===================================================================
RCS file: /cvs/gnome/glib/glib/gstrfuncs.h,v
retrieving revision 1.19
diff -u -r1.19 gstrfuncs.h
--- glib/gstrfuncs.h	12 Oct 2002 19:37:48 -0000	1.19
+++ glib/gstrfuncs.h	11 Sep 2003 14:21:13 -0000
@@ -179,24 +179,25 @@
 /* String utility functions that return a newly allocated string which
  * ought to be freed with g_free from the caller at some point.
  */
-gchar*	              g_strdup	       (const gchar *str);
-gchar*	              g_strdup_printf  (const gchar *format,
-					...) G_GNUC_PRINTF (1, 2);
-gchar*	              g_strdup_vprintf (const gchar *format,
-					va_list      args);
-gchar*	              g_strndup	       (const gchar *str,
-					gsize        n);  
-gchar*	              g_strnfill       (gsize        length,  
-					gchar        fill_char);
-gchar*	              g_strconcat      (const gchar *string1,
-					...); /* NULL terminated */
-gchar*                g_strjoin	       (const gchar  *separator,
-					...); /* NULL terminated */
+gchar*	              g_strdup	        (const gchar *str);
+gchar*	              g_strdup_printf   (const gchar *format,
+					 ...) G_GNUC_PRINTF (1, 2);
+gchar*	              g_strdup_vprintf  (const gchar *format,
+					 va_list      args);
+gchar*	              g_strndup	        (const gchar *str,
+				 	 gsize        n);  
+gchar*	              g_strnfill        (gsize        length,  
+				 	 gchar        fill_char);
+gchar*	              g_strconcat       (const gchar *string1,
+					 ...); /* NULL terminated */
+gchar*                g_strconcat_array (const gchar **string_array)
+gchar*                g_strjoin	        (const gchar  *separator,
+					 ...); /* NULL terminated */
 /* Make a copy of a string interpreting C string -style escape
  * sequences. Inverse of g_strescape. The recognized sequences are \b
  * \f \n \r \t \\ \" and the octal format.
  */
-gchar*                g_strcompress    (const gchar *source);
+gchar*                g_strcompress     (const gchar *source);
 
 /* Copy a string escaping nonprintable characters like in C strings.
  * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
@@ -206,11 +207,11 @@
  * Luckily this function wasn't used much, using NULL as second parameter
  * provides mostly identical semantics.
  */
-gchar*                g_strescape      (const gchar *source,
-					const gchar *exceptions);
+gchar*                g_strescape       (const gchar *source,
+				 	 const gchar *exceptions);
 
-gpointer              g_memdup	       (gconstpointer mem,
-					guint	       byte_size);
+gpointer              g_memdup	        (gconstpointer mem,
+					 guint	       byte_size);
 
 /* NULL terminated string arrays.
  * g_strsplit() splits up string into max_tokens tokens at delim and
@@ -220,16 +221,16 @@
  * g_strfreev() frees the array itself and all of its strings.
  * g_strdupv() copies a NULL-terminated array of strings
  */
-gchar**	              g_strsplit       (const gchar  *string,
-					const gchar  *delimiter,
-					gint          max_tokens);
-gchar*                g_strjoinv       (const gchar  *separator,
-					gchar       **str_array);
-void                  g_strfreev       (gchar       **str_array);
-gchar**               g_strdupv        (gchar       **str_array);
+gchar**	              g_strsplit        (const gchar  *string,
+					 const gchar  *delimiter,
+					 gint          max_tokens);
+gchar*                g_strjoinv        (const gchar  *separator,
+					 gchar       **str_array);
+void                  g_strfreev        (gchar       **str_array);
+gchar**               g_strdupv         (gchar       **str_array);
 
-gchar*                g_stpcpy         (gchar        *dest,
-                                        const char   *src);
+gchar*                g_stpcpy          (gchar        *dest,
+                                         const char   *src);
 
 G_END_DECLS
 


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