[Opensrf-commits] r1163 - in trunk: include/opensrf src/libopensrf
svn at svn.open-ils.org
svn at svn.open-ils.org
Sun Dec 9 10:34:03 EST 2007
Author: miker
Date: 2007-12-09 10:13:50 -0500 (Sun, 09 Dec 2007)
New Revision: 1163
Modified:
trunk/include/opensrf/osrf_json.h
trunk/src/libopensrf/osrf_json_parser.c
Log:
Patch from Scott McKellar to clean up const-iness and simplify the parser internals
Modified: trunk/include/opensrf/osrf_json.h
===================================================================
--- trunk/include/opensrf/osrf_json.h 2007-12-08 19:31:57 UTC (rev 1162)
+++ trunk/include/opensrf/osrf_json.h 2007-12-09 15:13:50 UTC (rev 1163)
@@ -81,7 +81,7 @@
growing_buffer* buffer; /* used to hold JSON strings, number, true, false, and null sequences */
growing_buffer* utfbuf; /* holds the current unicode characters */
void* userData; /* opaque user pointer. we ignore this */
- struct jsonParserHandlerStruct* handler; /* the event handler struct */
+ const struct jsonParserHandlerStruct* handler; /* the event handler struct */
};
typedef struct jsonParserContextStruct jsonParserContext;
@@ -132,7 +132,7 @@
* and ignored by the parser
* @return An allocated parser context, NULL on error
*/
-jsonParserContext* jsonNewParser( jsonParserHandler* handler, void* userData);
+jsonParserContext* jsonNewParser( const jsonParserHandler* handler, void* userData);
/**
* Deallocates a parser context
Modified: trunk/src/libopensrf/osrf_json_parser.c
===================================================================
--- trunk/src/libopensrf/osrf_json_parser.c 2007-12-08 19:31:57 UTC (rev 1162)
+++ trunk/src/libopensrf/osrf_json_parser.c 2007-12-09 15:13:50 UTC (rev 1163)
@@ -22,7 +22,7 @@
static void (*jsonClientErrorCallback) (const char*) = NULL;
/* these are the handlers for our internal parser */
-static jsonParserHandler jsonInternalParserHandlerStruct = {
+static const jsonParserHandler jsonInternalParserHandler = {
_jsonHandleStartObject,
_jsonHandleObjectKey,
_jsonHandleEndObject,
@@ -34,19 +34,34 @@
_jsonHandleNumber,
_jsonHandleError
};
-static jsonParserHandler*
- jsonInternalParserHandler = &jsonInternalParserHandlerStruct;
+static jsonParserContext staticContext;
+static int staticContextInUse = 0; // boolean
-jsonParserContext* jsonNewParser( jsonParserHandler* handler, void* userData) {
+static jsonInternalParser staticParser;
+static int staticParserInUse = 0; // boolean
+
+jsonParserContext* jsonNewParser( const jsonParserHandler* handler, void* userData) {
jsonParserContext* ctx;
- OSRF_MALLOC(ctx, sizeof(jsonParserContext));
+
+ // Use the static instance of jsonParserContext,
+ // if it's available
+
+ if( staticContextInUse )
+ OSRF_MALLOC(ctx, sizeof(jsonParserContext));
+ else {
+ ctx = &staticContext;
+ staticContextInUse = 1;
+ }
+
ctx->stateStack = osrfNewList();
ctx->buffer = buffer_init(512);
ctx->utfbuf = buffer_init(5);
ctx->handler = handler;
ctx->state = 0;
ctx->index = 0;
+ ctx->chunksize = 0;
+ ctx->flags = 0;
ctx->chunk = NULL;
ctx->userData = userData;
return ctx;
@@ -57,7 +72,14 @@
buffer_free(ctx->buffer);
buffer_free(ctx->utfbuf);
osrfListFree(ctx->stateStack);
- free(ctx);
+
+ // if the jsonParserContext was allocated
+ // dynamically, then free it
+
+ if( &staticContext == ctx )
+ staticContextInUse = 0;
+ else
+ free(ctx);
}
@@ -500,10 +522,22 @@
jsonInternalParser* _jsonNewInternalParser() {
jsonInternalParser* p;
- OSRF_MALLOC(p, sizeof(jsonInternalParser));
- p->ctx = jsonNewParser( jsonInternalParserHandler, p );
+
+ // Use the static instance of jsonInternalParser,
+ // if it's available
+
+ if( staticParserInUse )
+ OSRF_MALLOC(p, sizeof(jsonInternalParser));
+ else {
+ p = &staticParser;
+ staticParserInUse = 1;
+ }
+
+ p->ctx = jsonNewParser( &jsonInternalParserHandler, p );
p->obj = NULL;
+ p->current = NULL;
p->lastkey = NULL;
+ p->handleError = NULL;
return p;
}
@@ -511,7 +545,14 @@
if(!p) return;
jsonParserFree(p->ctx);
free(p->lastkey);
- free(p);
+
+ // if the jsonInternalParser was allocated
+ // dynamically, then free it
+
+ if( &staticParser == p )
+ staticParserInUse = 0;
+ else
+ free(p);
}
static jsonObject* _jsonParseStringImpl(const char* str, void (*errorHandler) (const char*) ) {
More information about the opensrf-commits
mailing list