C:/Users/vincent/Data/Perso/dev/ocilib/ocilib/src/list.c

00001 /*
00002    +----------------------------------------------------------------------+   
00003    |                                                                      |
00004    |                     OCILIB - C Driver for Oracle                     |
00005    |                                                                      |
00006    |                      (C Wrapper for Oracle OCI)                      |
00007    |                                                                      |
00008    +----------------------------------------------------------------------+
00009    |                      Website : http://www.ocilib.net                 |
00010    +----------------------------------------------------------------------+
00011    |               Copyright (c) 2007-2009 Vincent ROGIER                 |
00012    +----------------------------------------------------------------------+
00013    | This library is free software; you can redistribute it and/or        |
00014    | modify it under the terms of the GNU Lesser General Public           |
00015    | License as published by the Free Software Foundation; either         |
00016    | version 2 of the License, or (at your option) any later version.     |
00017    |                                                                      |
00018    | This library is distributed in the hope that it will be useful,      |
00019    | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
00020    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
00021    | Lesser General Public License for more details.                      |
00022    |                                                                      |
00023    | You should have received a copy of the GNU Lesser General Public     |
00024    | License along with this library; if not, write to the Free           |
00025    | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   |
00026    +----------------------------------------------------------------------+
00027    |          Author: Vincent ROGIER <vince.rogier@gmail.com>             |
00028    +----------------------------------------------------------------------+ 
00029 */
00030 
00031 /* ------------------------------------------------------------------------ *
00032  * $Id: list.c, v 3.4.0 2009-07-30 17:40 Vince $
00033  * ------------------------------------------------------------------------ */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ************************************************************************ *
00038  *                             PRIVATE FUNCTIONS
00039  * ************************************************************************ */
00040 
00041 /* ------------------------------------------------------------------------ *
00042  * OCI_ListCreate
00043  * ------------------------------------------------------------------------ */
00044 
00045 OCI_List * OCI_ListCreate(int type)
00046 {
00047     OCI_List *list = NULL;
00048     
00049     /* allocate list */
00050 
00051     list = (OCI_List *) OCI_MemAlloc(OCI_IPC_LIST, sizeof(*list), 1, TRUE);
00052 
00053     /* create a mutex on multithreaded environments */
00054 
00055     if (list != NULL)
00056     {
00057         list->type = type;
00058 
00059          if (OCI_LIB_THREADED)
00060          {
00061              list->mutex = OCI_MutexCreateInternal();
00062 
00063             if (list->mutex == NULL)
00064                 OCI_FREE(list);
00065          }
00066     }
00067 
00068     return list;
00069 }
00070 
00071 /* ------------------------------------------------------------------------ *
00072  * OCI_ListFree
00073  * ------------------------------------------------------------------------ */
00074 
00075 boolean OCI_ListFree(OCI_List *list)
00076 {
00077     boolean res = TRUE;
00078 
00079     OCI_CHECK(list == NULL, FALSE);
00080 
00081     OCI_ListClear(list);
00082 
00083     if (list->mutex != NULL)
00084         res = OCI_MutexFree(list->mutex);
00085 
00086     OCI_FREE(list);
00087     
00088     return res;
00089 }
00090 
00091 /* ------------------------------------------------------------------------ *
00092  * OCI_ListCreateItem
00093  * ------------------------------------------------------------------------ */
00094 
00095 OCI_Item * OCI_ListCreateItem(int type, int size)
00096 {
00097     OCI_Item *item  = NULL;
00098 
00099     /* allocate list item entry */
00100 
00101     item = (OCI_Item *) OCI_MemAlloc(OCI_IPC_LIST_ITEM, sizeof(*item), 1, TRUE);
00102 
00103     if (item != NULL)
00104     {
00105         /* allocate item data buffer */
00106 
00107         item->data = (void *) OCI_MemAlloc(type, size, 1, TRUE);
00108 
00109         if (item->data == NULL)
00110             OCI_FREE(item);
00111     }
00112 
00113     return item;
00114 }
00115 
00116 /* ------------------------------------------------------------------------ *
00117  * OCI_ListAppend
00118  * ------------------------------------------------------------------------ */
00119 
00120 OCI_Item * OCI_ListAppend(OCI_List *list, int size)
00121 {
00122     OCI_Item *item = NULL;
00123     OCI_Item *temp = NULL;
00124 
00125     OCI_CHECK(list == NULL, NULL);
00126 
00127     item = OCI_ListCreateItem(list->type, size);
00128 
00129     OCI_CHECK(item == NULL, FALSE);
00130 
00131     if (list->mutex != NULL)
00132         OCI_MutexAcquire(list->mutex);
00133 
00134     temp = list->head;
00135 
00136     while (temp != NULL && temp->next)
00137     {
00138         temp = temp->next;
00139     }
00140 
00141     if (temp != NULL)
00142         temp->next = item;
00143     else
00144         list->head = item;
00145 
00146     list->count++;
00147 
00148     if (list->mutex != NULL)
00149         OCI_MutexRelease(list->mutex);
00150 
00151     return item;
00152 }
00153 
00154 /* ------------------------------------------------------------------------ *
00155  * OCI_ListClear
00156  * ------------------------------------------------------------------------ */
00157 
00158 boolean OCI_ListClear(OCI_List *list)
00159 {
00160     OCI_Item *item = NULL;
00161     OCI_Item *temp = NULL;
00162 
00163     OCI_CHECK(list == NULL, FALSE);
00164 
00165     if (list->mutex != NULL)
00166         OCI_MutexAcquire(list->mutex);
00167 
00168     /* walk along the list to free item's buffer */
00169 
00170     item = list->head;
00171 
00172     while (item != NULL)
00173     {
00174         temp  = item;
00175         item  = item->next;
00176 
00177         /* free data */
00178 
00179         OCI_FREE(temp->data);
00180         OCI_FREE(temp);
00181     }
00182 
00183     list->head  = NULL;
00184     list->count = 0;
00185 
00186     if (list->mutex != NULL)
00187         OCI_MutexRelease(list->mutex);
00188 
00189     return TRUE;
00190 }
00191 
00192 /* ------------------------------------------------------------------------ *
00193  * OCI_ListForEach
00194  * ------------------------------------------------------------------------ */
00195 
00196 boolean OCI_ListForEach(OCI_List *list, boolean (*proc)(void *))
00197 {
00198     OCI_Item *item = NULL;
00199 
00200     OCI_CHECK(list == NULL, FALSE);
00201 
00202     if (list->mutex != NULL)
00203         OCI_MutexAcquire(list->mutex);
00204 
00205     item = list->head;
00206 
00207     /* for each item in the list, execute the given callback */
00208 
00209     while (item != NULL)
00210     {
00211         proc(item->data);
00212         item = item->next;
00213     }
00214 
00215     if (list->mutex != NULL)
00216         OCI_MutexRelease(list->mutex);
00217 
00218     return TRUE;
00219 }
00220 
00221 /* ------------------------------------------------------------------------ *
00222  * OCI_ListRemove
00223  * ------------------------------------------------------------------------ */
00224 
00225 boolean OCI_ListRemove(OCI_List *list, void *data)
00226 {
00227     OCI_Item *item = NULL;
00228     OCI_Item *temp = NULL;
00229 
00230     OCI_CHECK(list == NULL, FALSE);
00231     OCI_CHECK(data == NULL, FALSE);
00232 
00233     if (list->mutex != NULL)
00234         OCI_MutexAcquire(list->mutex);
00235 
00236     item = list->head;
00237 
00238     while (item != NULL)
00239     {
00240         if (item->data == data)
00241         {
00242             if (temp) temp->next = item->next;
00243 
00244             /* if item was the first entry, readjust the first list
00245                entry to next element */
00246 
00247             if (item == list->head) list->head = item->next;
00248 
00249             OCI_FREE(item);
00250 
00251             break;
00252         }
00253 
00254         temp = item;
00255         item = item->next;
00256     }
00257 
00258     list->count--;
00259 
00260     if (list->mutex != NULL)
00261         OCI_MutexRelease(list->mutex);
00262 
00263     return TRUE;
00264 }

Generated on Thu Jul 30 17:41:52 2009 for OCILIB (C Driver for Oracle) by  doxygen 1.5.4