[Opensrf-commits] r1011 - in branches/new-json2: include/opensrf src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Jul 8 13:42:23 EDT 2007


Author: erickson
Date: 2007-07-08 13:38:02 -0400 (Sun, 08 Jul 2007)
New Revision: 1011

Modified:
   branches/new-json2/include/opensrf/string_array.h
   branches/new-json2/include/opensrf/utils.h
   branches/new-json2/src/libopensrf/Makefile.json
   branches/new-json2/src/libopensrf/osrf_hash.c
   branches/new-json2/src/libopensrf/osrf_json_parser.c
   branches/new-json2/src/libopensrf/string_array.c
Log:
created an osrfList-backed version of the string_array code. added a buffer_reset macro.  make the standalon json module buildable

Modified: branches/new-json2/include/opensrf/string_array.h
===================================================================
--- branches/new-json2/include/opensrf/string_array.h	2007-07-07 01:24:43 UTC (rev 1010)
+++ branches/new-json2/include/opensrf/string_array.h	2007-07-08 17:38:02 UTC (rev 1011)
@@ -2,17 +2,20 @@
 
 #include <opensrf/utils.h>
 #include <opensrf/log.h>
+#include <opensrf/osrf_list.h>
 
-#define STRING_ARRAY_MAX_SIZE 1024
+#define STRING_ARRAY_MAX_SIZE 4096
 
 #ifndef STRING_ARRAY_H
 #define STRING_ARRAY_H
 
+#define OSRF_STRING_ARRAY_FREE(arr)\
+    if(arr) {osrfListFree(arr->list); free(arr);}
+        
+
 struct string_array_struct {
-		char** array;	
-		int size;
-		int arr_size;
-		int total_string_size;
+    osrfList* list;
+    int size;
 };
 typedef struct string_array_struct string_array;
 typedef struct string_array_struct osrfStringArray;

Modified: branches/new-json2/include/opensrf/utils.h
===================================================================
--- branches/new-json2/include/opensrf/utils.h	2007-07-07 01:24:43 UTC (rev 1010)
+++ branches/new-json2/include/opensrf/utils.h	2007-07-08 17:38:02 UTC (rev 1011)
@@ -64,6 +64,10 @@
 		}\
 	}while(0)
 
+#define OSRF_BUFFER_RESET(gb) \
+    memset(gb->buf, 0, gb->size);\
+    gb->n_used = 0;
+
 	
 
 

Modified: branches/new-json2/src/libopensrf/Makefile.json
===================================================================
--- branches/new-json2/src/libopensrf/Makefile.json	2007-07-07 01:24:43 UTC (rev 1010)
+++ branches/new-json2/src/libopensrf/Makefile.json	2007-07-08 17:38:02 UTC (rev 1011)
@@ -1,13 +1,24 @@
 #-DOSRF_JSON_ALLOW_COMMENTS 
+
+# ------------------------------------------------------------------
+# To build a standalone version of libosrf_json, something 
+# like the following should work:
+# $ CFLAGS="-fPIC -I /usr/include/libxml2 -I ../../include" \
+# 	OSRF_INC="../../include/opensrf" LDLIBS="-lxml2" \
+# 	make -f Makefile.json standalone
+# ------------------------------------------------------------------
 TARGETS = osrf_json_object.o osrf_json_parser.o osrf_json_tools.o osrf_legacy_json.o osrf_json_xml.o
 
+# these are only needed when compiling the standalone version
+EXT_TARGETS = osrf_list.o osrf_hash.o utils.o log.o md5.o string_array.o
+
 all:	$(TARGETS)
 	if [ ! -z "$(OSRF_LEGACY_JSON)" ]; then \
 		$(CC) -shared -W1 $(LDFLAGS) $(TARGETS) -o $(TMPDIR)/libobjson.so;\
 	fi;
 
-standalone:
-	$(CC) -shared -W1 $(LDFLAGS) $(LDLIBS) $(TARGETS) -o libosrf_json.so
+standalone: $(TARGETS) $(EXT_TARGETS)
+	$(CC) -shared -W1 $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(TARGETS) $(EXT_TARGETS) -o libosrf_json.so
 
 osrf_json_object.o:	osrf_json_object.c $(OSRF_INC)/osrf_json.h $(OSRF_INC)/osrf_json_utils.h
 osrf_json_parser.o:	osrf_json_parser.c $(OSRF_INC)/osrf_json.h $(OSRF_INC)/osrf_json_utils.h
@@ -15,3 +26,15 @@
 osrf_legacy_json.o:	osrf_legacy_json.c $(OSRF_INC)/osrf_json.h $(OSRF_INC)/osrf_json_utils.h
 osrf_json_xml.o:	osrf_json_xml.c $(OSRF_INC)/osrf_json.h $(OSRF_INC)/osrf_json_xml.h
 
+
+osrf_list.o:	osrf_list.c $(OSRF_INC)/osrf_list.h
+osrf_hash.o:	osrf_hash.c $(OSRF_INC)/osrf_hash.h
+utils.o:	utils.c $(OSRF_INC)/utils.h
+md5.o:	md5.c $(OSRF_INC)/md5.h
+log.o:	log.c $(OSRF_INC)/log.h
+string_array.o:	string_array.c $(OSRF_INC)/string_array.h
+
+
+clean:
+	rm -f osrf_json*.o osrf_legacy_json.o libosrf_json.so
+

Modified: branches/new-json2/src/libopensrf/osrf_hash.c
===================================================================
--- branches/new-json2/src/libopensrf/osrf_hash.c	2007-07-07 01:24:43 UTC (rev 1010)
+++ branches/new-json2/src/libopensrf/osrf_hash.c	2007-07-08 17:38:02 UTC (rev 1011)
@@ -193,7 +193,7 @@
 	}
 
 	osrfListFree(hash->hash);
-	osrfStringArrayFree(hash->keys);
+    OSRF_STRING_ARRAY_FREE(hash->keys);
 	free(hash);
 }
 
@@ -201,7 +201,6 @@
 
 osrfHashIterator* osrfNewHashIterator( osrfHash* hash ) {
 	if(!hash) return NULL;
-	//osrfHashIterator* itr = safe_malloc(sizeof(osrfHashIterator));
 	osrfHashIterator* itr;
 	OSRF_MALLOC(itr, sizeof(osrfHashIterator));
 	itr->hash = hash;
@@ -214,8 +213,7 @@
 	if(!(itr && itr->hash)) return NULL;
 	if( itr->currentIdx >= itr->keys->size ) return NULL;
 	free(itr->current);
-	itr->current = strdup(
-			osrfStringArrayGetString(itr->keys, itr->currentIdx++));
+	itr->current = strdup(osrfStringArrayGetString(itr->keys, itr->currentIdx++));
 	char* val = osrfHashGet( itr->hash, itr->current );
 	return val;
 }
@@ -223,14 +221,12 @@
 void osrfHashIteratorFree( osrfHashIterator* itr ) {
 	if(!itr) return;
 	free(itr->current);
-	//osrfStringArrayFree(itr->keys);
 	free(itr);
 }
 
 void osrfHashIteratorReset( osrfHashIterator* itr ) {
 	if(!itr) return;
 	free(itr->current);
-	//osrfStringArrayFree(itr->keys);
 	itr->keys = osrfHashKeysInc(itr->hash);
 	itr->currentIdx = 0;
 	itr->current = NULL;

Modified: branches/new-json2/src/libopensrf/osrf_json_parser.c
===================================================================
--- branches/new-json2/src/libopensrf/osrf_json_parser.c	2007-07-07 01:24:43 UTC (rev 1010)
+++ branches/new-json2/src/libopensrf/osrf_json_parser.c	2007-07-08 17:38:02 UTC (rev 1011)
@@ -70,7 +70,7 @@
 	if( ctx->handler->handleError ) {
 		VA_LIST_TO_STRING(err);
 		int pre	= ctx->index - 15;
-		int post	= ctx->index + 15;
+		int post= ctx->index + 15;
 		while( pre < 0 ) pre++;
 		while( post >= ctx->chunksize ) post--;
 		int l = post - pre;
@@ -136,7 +136,7 @@
 
 	JSON_STATE_REMOVE(ctx, JSON_STATE_IN_UTF);
 	JSON_STATE_REMOVE(ctx, JSON_STATE_IN_ESCAPE);
-	buffer_reset(ctx->utfbuf);
+	OSRF_BUFFER_RESET(ctx->utfbuf);
 	return 0;
 }
 
@@ -225,7 +225,7 @@
 	}
 
 	ctx->index--; /* set it back to the index of the final sequence character */
-	buffer_reset(ctx->buffer);
+	OSRF_BUFFER_RESET(ctx->buffer);
 	JSON_STATE_REMOVE(ctx, JSON_STATE_IN_NULL);
 	JSON_STATE_REMOVE(ctx, JSON_STATE_IN_TRUE);
 	JSON_STATE_REMOVE(ctx, JSON_STATE_IN_FALSE);
@@ -288,7 +288,7 @@
 
 				}
 
-				buffer_reset(ctx->buffer); /* flush the buffer and states */
+				OSRF_BUFFER_RESET(ctx->buffer); /* flush the buffer and states */
 				JSON_STATE_REMOVE(ctx, JSON_STATE_IN_STRING);
 				JSON_STATE_REMOVE(ctx, JSON_STATE_IN_KEY);
 				break;
@@ -323,7 +323,7 @@
 	if(err && err[0] != '\0') 
 		return _jsonParserError(ctx, "Invalid number sequence");
 	JSON_STATE_REMOVE(ctx, JSON_STATE_IN_NUMBER);
-	buffer_reset(ctx->buffer);
+	OSRF_BUFFER_RESET(ctx->buffer);
 	if(ctx->handler->handleNumber)
 		ctx->handler->handleNumber( ctx->userData, d );
 	ctx->index--; /* scooch back to the first non-digit number */

Modified: branches/new-json2/src/libopensrf/string_array.c
===================================================================
--- branches/new-json2/src/libopensrf/string_array.c	2007-07-07 01:24:43 UTC (rev 1010)
+++ branches/new-json2/src/libopensrf/string_array.c	2007-07-08 17:38:02 UTC (rev 1011)
@@ -8,18 +8,11 @@
 	if(size > STRING_ARRAY_MAX_SIZE)
 		osrfLogError( OSRF_LOG_MARK, "init_string_array size is too large");
 
-	/*
-	string_array* arr = 
-		(string_array*) safe_malloc(sizeof(string_array));
-		*/
 	string_array* arr;
 	OSRF_MALLOC( arr, sizeof(string_array));
-
-	//arr->array = (char**) safe_malloc(size * sizeof(char*));
-	OSRF_MALLOC(arr->array, size * sizeof(char*));
-
+    arr->list = osrfNewListSize(size);
+    osrfListSetDefaultFree(arr->list);
 	arr->size = 0;
-	arr->arr_size = size;
 	return arr;
 }
 
@@ -30,65 +23,37 @@
 
 void string_array_add(string_array* arr, char* str) {
 	if(arr == NULL || str == NULL ) return;
-	if( strlen(str) < 1 ) return;
-
-	arr->size++;
-
 	if( arr->size > STRING_ARRAY_MAX_SIZE ) 
 		osrfLogError( OSRF_LOG_MARK, "string_array_add size is too large");
-
-	/* if necessary, double capacity */
-	if(arr->size >= arr->arr_size) {
-		arr->arr_size *= 2;
-		//char** tmp = (char**) safe_malloc(arr->arr_size * sizeof(char*));
-		char** tmp;
-		OSRF_MALLOC( tmp, arr->arr_size * sizeof(char*));
-		int i;
-
-		/* copy the string pointers over */
-		for( i = 0; i!= arr->size; i++ ) 
-			tmp[i] = arr->array[i];
-
-		free(arr->array);
-		arr->array = tmp;
-	}
-
-	arr->array[arr->size - 1] = strdup(str);
+    osrfListPush(arr->list, strdup(str));
+    arr->size = arr->list->size;
 }
 
 char* osrfStringArrayGetString(osrfStringArray* arr, int index) {
-	return string_array_get_string(arr, index);
+    return OSRF_LIST_GET_INDEX(arr->list, index);
 }
 
 char* string_array_get_string(string_array* arr, int index) {
-	if(!arr || index < 0 || index >= arr->size ) return NULL;
-	return arr->array[index]; 
+    return OSRF_LIST_GET_INDEX(arr->list, index);
 }
 
 
 void osrfStringArrayFree(osrfStringArray* arr) {
-	string_array_destroy(arr);
+    OSRF_STRING_ARRAY_FREE(arr);
 }
 
 void string_array_destroy(string_array* arr) {
-	if(arr) {
-		int i = 0;
-		while( i < arr->size ) free(arr->array[i++]);
-		free(arr->array);
-		free(arr);
-	}
+    OSRF_STRING_ARRAY_FREE(arr);
 }
 
 
 int osrfStringArrayContains( osrfStringArray* arr, char* string ) {
 	if(!(arr && string)) return 0;
-	
 	int i;
-	for( i = 0; i != arr->size; i++ ) {
-		char* str = osrfStringArrayGetString(arr, i);
-		if(str) {
-			if(!strcmp(str, string)) return 1;
-		}
+	for( i = 0; i < arr->size; i++ ) {
+        char* str = OSRF_LIST_GET_INDEX(arr->list, i);
+		if(str && !strcmp(str, string)) 
+            return 1;
 	}
 
 	return 0;
@@ -97,19 +62,29 @@
 void osrfStringArrayRemove( osrfStringArray* arr, char* tstr) {
 	if(!(arr && tstr)) return;
 	int i;
-	for( i = 0; i != arr->size; i++ ) {
-		char* str = osrfStringArrayGetString(arr, i);
-		if(str) {
-			if(!strcmp(str, tstr)) {
-				free(arr->array[i]);
-				arr->array[i] = NULL;
-				break;
-			}
+    char* str;
+
+	for( i = 0; i < arr->size; i++ ) {
+        /* find and remove the string */
+        str = OSRF_LIST_GET_INDEX(arr->list, i);
+		if(str && !strcmp(str, tstr)) {
+            osrfListRemove(arr->list, i);
+			break;
 		}
 	}
-	for( ; i != arr->size; i++ ) 
-		arr->array[i] = arr->array[i+1];
 
+    /* disable automatic item freeing on delete and shift
+     * items up in the array to fill in the gap
+     */
+    arr->list->freeItem = NULL;
+	for( ; i < arr->size - 1; i++ ) 
+        osrfListSet(arr->list, OSRF_LIST_GET_INDEX(arr->list, i+1) , i);
+
+    /* remove the last item since it was shifted up */
+    osrfListRemove(arr->list, i);
+
+    /* re-enable automatic item freeing in delete */
+    osrfListSetDefaultFree(arr->list);
 	arr->size--;
 }
 



More information about the opensrf-commits mailing list