[Patch] Add a new function g_string_chunk_n_insert() ?



This is for glib->gstring.

The attached patch added a function g_string_chunk_n_insert().

The function is the same as g_string_chunk_insert() except the new function
also takes the max number of bytes to insert. The inserted string is
always null-terminated. The source need not to be null-terminated.

It is useful for construction of a bunch of strings from 
a big buffer, e.g. mmapped files. Otherwise, string segments
have to be explicitly created before insertions. Result in a
lot of malloc's.

-- 
Best regard
hashao
Index: gstring.h
===================================================================
RCS file: /cvs/gnome/glib/Attic/gstring.h,v
retrieving revision 1.3
diff -u -r1.3 gstring.h
--- gstring.h	2001/03/07 14:46:41	1.3
+++ gstring.h	2001/12/23 10:49:37
@@ -46,6 +46,9 @@
 void	      g_string_chunk_free	   (GStringChunk *chunk);
 gchar*	      g_string_chunk_insert	   (GStringChunk *chunk,
 					    const gchar	 *string);
+gchar*	      g_string_chunk_n_insert	   (GStringChunk *chunk,
+					    const gchar	 *string,
+					    gsize nbyte);
 gchar*	      g_string_chunk_insert_const  (GStringChunk *chunk,
 					    const gchar	 *string);
 
Index: gstring.c
===================================================================
RCS file: /cvs/gnome/glib/Attic/gstring.c,v
retrieving revision 1.22
diff -u -r1.22 gstring.c
--- gstring.c	2001/03/07 14:46:41	1.22
+++ gstring.c	2001/12/23 10:49:37
@@ -169,6 +169,42 @@
 }
 
 gchar*
+g_string_chunk_n_insert (GStringChunk *fchunk,
+		       const gchar  *string, gsize nbyte)
+{
+  GRealStringChunk *chunk = (GRealStringChunk*) fchunk;
+  gint len = nbyte + 1;   /* +1 for the terminating \0. */
+  char* pos;
+
+  g_return_val_if_fail (chunk != NULL, NULL);
+
+  if ((chunk->storage_next + len + 1) > chunk->this_size)
+    {
+      gint new_size = chunk->default_size;
+
+      while (new_size < len+1)
+	new_size <<= 1;
+
+      chunk->storage_list = g_slist_prepend (chunk->storage_list,
+					     g_new (char, new_size));
+
+      chunk->this_size = new_size;
+      chunk->storage_next = 0;
+    }
+
+  pos = ((char *) chunk->storage_list->data) + chunk->storage_next;
+
+  *(pos+len) = '\0';
+
+  strncpy (pos, string, len - 1);
+  len = strlen (pos);
+
+  chunk->storage_next += len + 1;
+
+  return pos;
+}
+
+gchar*
 g_string_chunk_insert_const (GStringChunk *fchunk,
 			     const gchar  *string)
 {
Index: testglib.c
===================================================================
RCS file: /cvs/gnome/glib/Attic/testglib.c,v
retrieving revision 1.50
diff -u -r1.50 testglib.c
--- testglib.c	2001/04/20 17:08:56	1.50
+++ testglib.c	2001/12/23 10:49:42
@@ -741,6 +741,20 @@
       if (strcmp ("hi pete", tmp_string) != 0)
 	g_error ("string chunks are broken.\n");
     }
+  tmp_string = g_string_chunk_n_insert (string_chunk, "hi pete", 8);
+
+  if (strcmp ("hi pete", tmp_string) != 0)
+    g_error ("string chunks are broken.\n");
+
+  tmp_string = g_string_chunk_n_insert (string_chunk, "hi pete", 7);
+
+  if (strcmp ("hi pete", tmp_string) != 0)
+    g_error ("string chunks are broken.\n");
+
+  tmp_string = g_string_chunk_n_insert (string_chunk, "hi pete", 6);
+
+  if (strcmp ("hi pet", tmp_string) != 0)
+    g_error ("string chunks are broken.\n");
 
   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
 


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