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

svn at svn.open-ils.org svn at svn.open-ils.org
Sun Nov 15 11:25:37 EST 2009


Author: scottmk
Date: 2009-11-15 11:25:34 -0500 (Sun, 15 Nov 2009)
New Revision: 1851

Modified:
   trunk/include/opensrf/osrf_list.h
   trunk/src/libopensrf/osrf_list.c
Log:
Created a new function osrfListClear() to take an existing
osrfList and make it empty.

M    include/opensrf/osrf_list.h
M    src/libopensrf/osrf_list.c


Modified: trunk/include/opensrf/osrf_list.h
===================================================================
--- trunk/include/opensrf/osrf_list.h	2009-11-14 18:39:32 UTC (rev 1850)
+++ trunk/include/opensrf/osrf_list.h	2009-11-15 16:25:34 UTC (rev 1851)
@@ -88,6 +88,8 @@
 
 void osrfListFree( osrfList* list );
 
+void osrfListClear( osrfList* list );
+
 void* osrfListRemove( osrfList* list, unsigned int position );
 
 void* osrfListExtract( osrfList* list, unsigned int position );

Modified: trunk/src/libopensrf/osrf_list.c
===================================================================
--- trunk/src/libopensrf/osrf_list.c	2009-11-14 18:39:32 UTC (rev 1850)
+++ trunk/src/libopensrf/osrf_list.c	2009-11-15 16:25:34 UTC (rev 1851)
@@ -1,5 +1,5 @@
 /**
-	@file osrf_list.c	
+	@file osrf_list.c
 	@brief Implementation of osrfList, sort of like a vector class.
 */
 
@@ -32,8 +32,8 @@
 	OSRF_MALLOC(list, sizeof(osrfList));
 	list->size = 0;
 	list->freeItem = NULL;
-    if( size <= 0 ) size = 16;
-	list->arrsize	= size;
+	if( size <= 0 ) size = 16;
+	list->arrsize = size;
 	OSRF_MALLOC( list->arrlist, list->arrsize * sizeof(void*) );
 
 	// Nullify all pointers in the array
@@ -78,7 +78,7 @@
 int osrfListPushFirst( osrfList* list, void* item ) {
 	if(!(list && item)) return -1;
 	int i;
-	for( i = 0; i < list->size; i++ ) 
+	for( i = 0; i < list->size; i++ )
 		if(!list->arrlist[i]) break;
 	osrfListSet( list, item, i );
 	return list->size;
@@ -107,7 +107,7 @@
 
 	int newsize = list->arrsize;
 
-	while( position >= newsize ) 
+	while( position >= newsize )
 		newsize += OSRF_LIST_INC_SIZE;
 
 	if( newsize > list->arrsize ) { /* expand the list if necessary */
@@ -115,7 +115,7 @@
 		OSRF_MALLOC(newarr, newsize * sizeof(void*));
 
 		// Copy the old pointers, and nullify the new ones
-		
+
 		int i;
 		for( i = 0; i < list->arrsize; i++ )
 			newarr[i] = list->arrlist[i];
@@ -161,7 +161,7 @@
 	if( list->freeItem ) {
 		int i; void* val;
 		for( i = 0; i < list->size; i++ ) {
-			if( (val = list->arrlist[i]) ) 
+			if( (val = list->arrlist[i]) )
 				list->freeItem(val);
 		}
 	}
@@ -171,6 +171,26 @@
 }
 
 /**
+	@brief Make an osrfList empty.
+	@param list Pointer to the osrfList to be cleared.
+
+	Delete every item in the list.  If a callback function is defined for freeing the items,
+	call it for every item.
+*/
+void osrfListClear( osrfList* list ) {
+	if(!list) return;
+
+	unsigned int i;
+	for( i = 0; i < list->size; ++i ) {
+		if( list->freeItem && list->arrlist[ i ] )
+			list->freeItem( list->arrlist[ i ] );
+		list->arrlist[ i ] = NULL;
+	}
+
+	list->size = 0;
+}
+
+/**
 	@brief Remove the pointer from a specified position.
 	@param list A pointer to the osrfList.
 	@param position A zero-based index identifying the pointer to be removed.
@@ -236,7 +256,7 @@
 	if(!(list && addr)) return -1;
 	int index;
 	for( index = 0; index < list->size; index++ ) {
-		if( list->arrlist[index] == addr ) 
+		if( list->arrlist[index] == addr )
 			return index;
 	}
 	return -1;
@@ -361,7 +381,7 @@
 /**
 	@brief Install a default item-freeing callback function (the ANSI standard free() function).
 	@param list A pointer to the osrfList where the callback function will be installed.
- */
+*/
 void osrfListSetDefaultFree( osrfList* list ) {
 	if(!list) return;
 	list->freeItem = free;



More information about the opensrf-commits mailing list