From 824a70414ae74075f5840372207d808f229af380 Mon Sep 17 00:00:00 2001 From: miker Date: Sat, 12 Apr 2008 02:50:12 +0000 Subject: [PATCH] Patch from Scott McKellar: These patches add two new functions to the osrfHash routines, and apply them in several modules. THe purpose is to eliminate the access of an osrfHash's internals by the client code, so that the internals can be replaced more readily. Those internals haven't changed yet, but they will. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1310 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/osrf_hash.h | 4 ++++ src/libopensrf/osrf_hash.c | 7 ++++++- src/libopensrf/osrf_json_object.c | 8 ++++---- src/router/osrf_router.c | 12 ++++++------ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/opensrf/osrf_hash.h b/include/opensrf/osrf_hash.h index 5cc3bb5..edf8d45 100644 --- a/include/opensrf/osrf_hash.h +++ b/include/opensrf/osrf_hash.h @@ -27,6 +27,10 @@ typedef struct _osrfHashIteratorStruct osrfHashIterator; */ osrfHash* osrfNewHash(); +/** Installs a callback function for freeing stored items + */ +void osrfHashSetCallback( osrfHash* hash, void (*callback) (char* key, void* item) ); + /** Sets the given key with the given item if "freeItem" is defined and an item already exists at the given location, diff --git a/src/libopensrf/osrf_hash.c b/src/libopensrf/osrf_hash.c index e99dda3..47cc0ee 100644 --- a/src/libopensrf/osrf_hash.c +++ b/src/libopensrf/osrf_hash.c @@ -57,7 +57,12 @@ static unsigned int osrfHashMakeKey(char* str) { num = (__h & (OSRF_HASH_LIST_SIZE-1));\ } while(0) - +/** Installs a callback function for freeing stored items + */ +void osrfHashSetCallback( osrfHash* hash, void (*callback) (char* key, void* item) ) +{ + if( hash ) hash->freeItem = callback; +} /* returns the index of the item and points l to the sublist the item * lives in if the item and points n to the hashnode the item diff --git a/src/libopensrf/osrf_json_object.c b/src/libopensrf/osrf_json_object.c index 28c3fd0..cadceb1 100644 --- a/src/libopensrf/osrf_json_object.c +++ b/src/libopensrf/osrf_json_object.c @@ -37,7 +37,7 @@ GNU General Public License for more details. _obj_->type = newtype;\ if( newtype == JSON_HASH && _obj_->value.h == NULL ) { \ _obj_->value.h = osrfNewHash(); \ - _obj_->value.h->freeItem = _jsonFreeHashItem; \ + osrfHashSetCallback( _obj_->value.h, _jsonFreeHashItem ); \ } else if( newtype == JSON_ARRAY && _obj_->value.l == NULL ) { \ _obj_->value.l = osrfNewList(); \ _obj_->value.l->freeItem = _jsonFreeListItem;\ @@ -242,7 +242,7 @@ unsigned long jsonObjectSetKey( jsonObject* o, const char* key, jsonObject* newo JSON_INIT_CLEAR(o, JSON_HASH); newo->parent = o; osrfHashSet( o->value.h, newo, key ); - o->size = o->value.h->size; + o->size = osrfHashGetCount(o->value.h); return o->size; } @@ -322,7 +322,7 @@ static void add_json_to_buffer( const jsonObject* obj, growing_buffer * buf ) { while( (item = osrfHashIteratorNext(itr)) ) { if(i++ > 0) OSRF_BUFFER_ADD(buf, ","); - buffer_fadd(buf, "\"%s\":", itr->current); + buffer_fadd(buf, "\"%s\":", osrfHashIteratorKey(itr)); add_json_to_buffer( item, buf ); } @@ -362,7 +362,7 @@ jsonObject* jsonIteratorNext(jsonIterator* itr) { if(!itr->hashItr) return NULL; jsonObject* item = osrfHashIteratorNext(itr->hashItr); free(itr->key); - itr->key = strdup(itr->hashItr->current); + itr->key = strdup( osrfHashIteratorKey(itr->hashItr) ); return item; } else { return jsonObjectGetIndex( itr->obj, itr->index++ ); diff --git a/src/router/osrf_router.c b/src/router/osrf_router.c index dbeea1f..4a888d9 100644 --- a/src/router/osrf_router.c +++ b/src/router/osrf_router.c @@ -76,7 +76,7 @@ osrfRouter* osrfNewRouter( router->classes = osrfNewHash(); - router->classes->freeItem = &osrfRouterClassFree; + osrfHashSetCallback(router->classes, &osrfRouterClassFree); router->connection = client_init( domain, port, NULL, 0 ); @@ -128,7 +128,7 @@ void osrfRouterRun( osrfRouter* router ) { while( (class = osrfHashIteratorNext(itr)) ) { - const char* classname = itr->current; + const char* classname = osrfHashIteratorKey(itr); if( classname && (class = osrfRouterFindClass( router, classname )) ) { @@ -292,7 +292,7 @@ static osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* clas osrfRouterClass* class = safe_malloc(sizeof(osrfRouterClass)); class->nodes = osrfNewHash(); class->itr = osrfNewHashIterator(class->nodes); - class->nodes->freeItem = &osrfRouterNodeFree; + osrfHashSetCallback(class->nodes, &osrfRouterNodeFree); class->router = router; class->connection = client_init( router->domain, router->port, NULL, 0 ); @@ -574,7 +574,7 @@ static int _osrfRouterFillFDSet( osrfRouter* router, fd_set* set ) { osrfHashIterator* itr = osrfNewHashIterator(router->classes); while( (class = osrfHashIteratorNext(itr)) ) { - const char* classname = itr->current; + const char* classname = osrfHashIteratorKey(itr); if( classname && (class = osrfRouterFindClass( router, classname )) ) { sockid = class->ROUTER_SOCKFD; @@ -732,7 +732,7 @@ static int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* m while( (class = osrfHashIteratorNext(class_itr)) ) { jsonObject* class_res = jsonNewObjectType(JSON_HASH); - const char* classname = class_itr->current; + const char* classname = osrfHashIteratorKey(class_itr); osrfHashIterator* node_itr = osrfNewHashIterator(class->nodes); while( (node = osrfHashIteratorNext(node_itr)) ) { @@ -756,7 +756,7 @@ static int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* m while( (class = osrfHashIteratorNext(class_itr)) ) { count = 0; - const char* classname = class_itr->current; + const char* classname = osrfHashIteratorKey(class_itr); osrfHashIterator* node_itr = osrfNewHashIterator(class->nodes); while( (node = osrfHashIteratorNext(node_itr)) ) { -- 2.11.0