[Opensrf-commits] r1689 - in trunk: include/opensrf src/libopensrf (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Tue Mar 31 14:30:19 EDT 2009


Author: scottmk
Date: 2009-03-31 14:30:10 -0400 (Tue, 31 Mar 2009)
New Revision: 1689

Modified:
   trunk/include/opensrf/osrf_json.h
   trunk/include/opensrf/osrf_list.h
   trunk/src/libopensrf/osrf_json_object.c
   trunk/src/libopensrf/osrf_list.c
Log:
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.


Modified: trunk/include/opensrf/osrf_json.h
===================================================================
--- trunk/include/opensrf/osrf_json.h	2009-03-31 12:45:53 UTC (rev 1688)
+++ trunk/include/opensrf/osrf_json.h	2009-03-31 18:30:10 UTC (rev 1689)
@@ -284,11 +284,16 @@
  */
 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);
 

Modified: trunk/include/opensrf/osrf_list.h
===================================================================
--- trunk/include/opensrf/osrf_list.h	2009-03-31 12:45:53 UTC (rev 1688)
+++ trunk/include/opensrf/osrf_list.h	2009-03-31 18:30:10 UTC (rev 1689)
@@ -113,6 +113,15 @@
 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
   @param addr The pointer connected to the list item we're to find

Modified: trunk/src/libopensrf/osrf_json_object.c
===================================================================
--- trunk/src/libopensrf/osrf_json_object.c	2009-03-31 12:45:53 UTC (rev 1688)
+++ trunk/src/libopensrf/osrf_json_object.c	2009-03-31 18:30:10 UTC (rev 1689)
@@ -352,7 +352,7 @@
 			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 @@
 }
 
 
+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);

Modified: trunk/src/libopensrf/osrf_list.c
===================================================================
--- trunk/src/libopensrf/osrf_list.c	2009-03-31 12:45:53 UTC (rev 1688)
+++ trunk/src/libopensrf/osrf_list.c	2009-03-31 18:30:10 UTC (rev 1689)
@@ -107,7 +107,17 @@
 	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;
 	int index;



More information about the opensrf-commits mailing list