[Opensrf-commits] r1568 - in trunk: include/opensrf src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Jan 5 09:27:40 EST 2009


Author: scottmk
Date: 2009-01-05 09:27:39 -0500 (Mon, 05 Jan 2009)
New Revision: 1568

Modified:
   trunk/include/opensrf/utils.h
   trunk/src/libopensrf/utils.c
Log:
Add a new function buffer_add_n(), and the corresponding
function-like macro OSRF_BUFFER_ADD_N.

These facilities append a specified number of characters from
an input string to a growing_buffer.  They are intended for 
situations where the length of the input string is already known,
or where the input string is not nul-terminated in the right
place.


Modified: trunk/include/opensrf/utils.h
===================================================================
--- trunk/include/opensrf/utils.h	2008-12-30 19:22:07 UTC (rev 1567)
+++ trunk/include/opensrf/utils.h	2009-01-05 14:27:39 UTC (rev 1568)
@@ -66,6 +66,21 @@
 		}\
 	} while(0)
 
+#define OSRF_BUFFER_ADD_N(gb, data, n) \
+	do {\
+		growing_buffer* gb__ = gb; \
+		const char* data__ = data; \
+		size_t n__ = n; \
+		if(gb__ && data__) {\
+			int tl__ = n__ + gb__->n_used;\
+			if( tl__ < gb__->size ) {\
+				memcpy( gb__->buf + gb__->n_used, data__, n__ ); \
+				gb__->buf[tl__] = '\0'; \
+				gb__->n_used = tl__; \
+} else { buffer_add_n(gb__, data__, n__); }\
+}\
+} while(0)
+
 #define OSRF_BUFFER_ADD_CHAR(gb, c)\
 	do {\
 		if(gb) {\
@@ -193,6 +208,7 @@
 //int buffer_addchar(growing_buffer* gb, char c);
 
 int buffer_add(growing_buffer* gb, const char* c);
+int buffer_add_n(growing_buffer* gb, const char* data, size_t n);
 int buffer_fadd(growing_buffer* gb, const char* format, ... );
 int buffer_reset( growing_buffer* gb);
 char* buffer_data( const growing_buffer* gb);

Modified: trunk/src/libopensrf/utils.c
===================================================================
--- trunk/src/libopensrf/utils.c	2008-12-30 19:22:07 UTC (rev 1567)
+++ trunk/src/libopensrf/utils.c	2009-01-05 14:27:39 UTC (rev 1568)
@@ -247,7 +247,29 @@
 	return total_len;
 }
 
+/** Append a specified number of characters to a growing_buffer.
+    If the characters so appended include an embedded nul, the results
+    are likely to be unhappy.
+*/
+int buffer_add_n(growing_buffer* gb, const char* data, size_t n) {
+	if(!(gb && data)) return 0;
 
+	if(n == 0) return 0;
+
+	int total_len = n + gb->n_used;
+
+	if( total_len >= gb->size ) {
+		if( buffer_expand( gb, total_len ) )
+			return -1;
+	}
+
+	memcpy( gb->buf + gb->n_used, data, n );
+	gb->buf[total_len] = '\0';
+	gb->n_used = total_len;
+	return total_len;
+}
+
+
 int buffer_reset( growing_buffer *gb){
 	if( gb == NULL ) { return -1; }
 	if( gb->buf == NULL ) { return -1; }



More information about the opensrf-commits mailing list