created an osrfList-backed version of the string_array code. added a buffer_reset...
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Sun, 8 Jul 2007 17:38:02 +0000 (17:38 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Sun, 8 Jul 2007 17:38:02 +0000 (17:38 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/branches/new-json2@1011 9efc2488-bf62-4759-914b-345cdb29e865

include/opensrf/string_array.h
include/opensrf/utils.h
src/libopensrf/Makefile.json
src/libopensrf/osrf_hash.c
src/libopensrf/osrf_json_parser.c
src/libopensrf/string_array.c

index 77b3de0..70b0038 100644 (file)
@@ -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;
index 7bbade0..c6832cd 100644 (file)
@@ -64,6 +64,10 @@ GNU General Public License for more details.
                }\
        }while(0)
 
+#define OSRF_BUFFER_RESET(gb) \
+    memset(gb->buf, 0, gb->size);\
+    gb->n_used = 0;
+
        
 
 
index 7642cc6..8de20a6 100644 (file)
@@ -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_json_tools.o:   osrf_json_tools.c $(OSRF_INC)/osrf_json.h $(OSRF_INC)/osrf_js
 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
+
index e00f09c..5447466 100644 (file)
@@ -193,7 +193,7 @@ void osrfHashFree( osrfHash* hash ) {
        }
 
        osrfListFree(hash->hash);
-       osrfStringArrayFree(hash->keys);
+    OSRF_STRING_ARRAY_FREE(hash->keys);
        free(hash);
 }
 
@@ -201,7 +201,6 @@ void osrfHashFree( osrfHash* hash ) {
 
 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 @@ void* osrfHashIteratorNext( osrfHashIterator* itr ) {
        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* osrfHashIteratorNext( osrfHashIterator* itr ) {
 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;
index c78710b..9ef2574 100644 (file)
@@ -70,7 +70,7 @@ int _jsonParserError( jsonParserContext* ctx, char* err, ... ) {
        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 @@ int _jsonParserHandleUnicode( jsonParserContext* ctx ) {
 
        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 @@ int _jsonParserHandleMatch( jsonParserContext* ctx, int type ) {
        }
 
        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 @@ int _jsonParserHandleString( jsonParserContext* ctx ) {
 
                                }
 
-                               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 @@ int _jsonParserHandleNumber( jsonParserContext* ctx ) {
        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 */
index 3197718..7bb07c1 100644 (file)
@@ -8,18 +8,11 @@ string_array* init_string_array(int size) {
        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 osrfStringArrayAdd(osrfStringArray* arr, char* string) {
 
 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 @@ int osrfStringArrayContains( osrfStringArray* arr, char* string ) {
 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--;
 }