From c913fb92e62180b3c3250f33460fb3af26147781 Mon Sep 17 00:00:00 2001 From: scottmk Date: Tue, 31 Mar 2009 18:30:10 +0000 Subject: [PATCH] 1. Create a new osrfListExtract function, which removes an item from an osrfList without destroying it, and returns a pointer to the item thus removed. 2. Create a new jsonObjectExtractIndex, which removes a specified entry in a JSON_ARRAY, and returns a pointer to it, without destroying it. 3. In osrf_json.h: Corrected an inaccurate comment about jsonObjectRemoveIndex(). Contrary to the original comment, this function does not shift other objects down to fill the gap. git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1689 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/osrf_json.h | 9 +++++++-- include/opensrf/osrf_list.h | 9 +++++++++ src/libopensrf/osrf_json_object.c | 10 +++++++++- src/libopensrf/osrf_list.c | 10 ++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/opensrf/osrf_json.h b/include/opensrf/osrf_json.h index 8ac7d70..0c67699 100644 --- a/include/opensrf/osrf_json.h +++ b/include/opensrf/osrf_json.h @@ -284,11 +284,16 @@ jsonObject* jsonObjectGetIndex( const jsonObject* obj, unsigned long index ); */ unsigned long jsonObjectSetIndex(jsonObject* dest, unsigned long index, jsonObject* newObj); -/* removes the object at the given index and, if more items exist, - * re-indexes (shifts down by 1) the rest of the objects in the array +/* removes and deallocates the object at the given index, replacing + it with a NULL pointer */ unsigned long jsonObjectRemoveIndex(jsonObject* dest, unsigned long index); +/* removes (but does not deallocate) the object at the given index, + * replacing it with a NULL pointer; returns a pointer to the object removed + */ +jsonObject* jsonObjectExtractIndex(jsonObject* dest, unsigned long index); + /* removes (and deallocates) the object with key 'key' if it exists */ unsigned long jsonObjectRemoveKey( jsonObject* dest, const char* key); diff --git a/include/opensrf/osrf_list.h b/include/opensrf/osrf_list.h index aeaf357..06145b4 100644 --- a/include/opensrf/osrf_list.h +++ b/include/opensrf/osrf_list.h @@ -112,6 +112,15 @@ void osrfListFree( osrfList* list ); */ void* osrfListRemove( osrfList* list, unsigned int position ); +/** + Removes the list item at the given index, without freeing it + @param list The list + @param position The position of the item to remove + @return A pointer to the item extracted, or NULL + if there is nothing to extract + */ +void* osrfListExtract( osrfList* list, unsigned int position ); + /** Finds the list item whose void* is the same as the one passed in @param list The list diff --git a/src/libopensrf/osrf_json_object.c b/src/libopensrf/osrf_json_object.c index 1eddd9c..75785d7 100644 --- a/src/libopensrf/osrf_json_object.c +++ b/src/libopensrf/osrf_json_object.c @@ -352,7 +352,7 @@ static void add_json_to_buffer( const jsonObject* obj, while( (item = osrfHashIteratorNext(itr)) ) { if(i++ > 0) OSRF_BUFFER_ADD_CHAR(buf, ','); OSRF_BUFFER_ADD_CHAR(buf, '"'); - buffer_append_utf8(buf, osrfHashIteratorKey(itr)); + buffer_append_utf8(buf, osrfHashIteratorKey(itr)); OSRF_BUFFER_ADD(buf, "\":"); add_json_to_buffer( item, buf, do_classname, second_pass ); } @@ -440,6 +440,14 @@ unsigned long jsonObjectRemoveIndex(jsonObject* dest, unsigned long index) { } +jsonObject* jsonObjectExtractIndex(jsonObject* dest, unsigned long index) { + if( dest && dest->type == JSON_ARRAY ) { + return osrfListExtract(dest->value.l, index); + } else + return NULL; +} + + unsigned long jsonObjectRemoveKey( jsonObject* dest, const char* key) { if( dest && key && dest->type == JSON_HASH ) { osrfHashRemove(dest->value.h, key); diff --git a/src/libopensrf/osrf_list.c b/src/libopensrf/osrf_list.c index a95691b..16090ce 100644 --- a/src/libopensrf/osrf_list.c +++ b/src/libopensrf/osrf_list.c @@ -107,6 +107,16 @@ void* osrfListRemove( osrfList* list, unsigned int position ) { return olditem; } +void* osrfListExtract( osrfList* list, unsigned int position ) { + if(!list || position >= list->size || position < 0) return NULL; + + void* olditem = list->arrlist[position]; + list->arrlist[position] = NULL; + + if( position == list->size - 1 ) list->size--; + return olditem; +} + int osrfListFind( const osrfList* list, void* addr ) { if(!(list && addr)) return -1; -- 2.43.2