Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

sf_sdlist.c

Go to the documentation of this file.
00001 /* $Id$ */
00002 /*
00003 ** Copyright (C) 2002 Martin Roesch <roesch@sourcefire.com>
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 
00021 #include "sf_sdlist.h"
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00026 
00027 /* Function: int sf_sdlist_init(sfSDlist *list, void (*destroy)(void *data))
00028  * 
00029  * Purpose: initialize an dlist
00030  * Args: list - pointer to a dlist structure
00031  *       destroy - free function ( use NULL for none )
00032  * Returns:
00033  *     1 on failure , 0 on success
00034  */ 
00035 
00036 int sf_sdlist_init(sfSDList *list, void (*destroy)(void *data))
00037 {
00038     list->destroy = destroy;
00039     list->size = 0;
00040     list->head = NULL;
00041     list->tail = NULL;
00042 
00043     return 0;
00044 }
00045 
00046 
00047 /* Function: int sf_sdlist_delete(sfSDList *list) 
00048  * 
00049  * Purpose: delete every item of a list
00050  * Args: list -> pointer to a dlist structure
00051  * 
00052  * Returns: 1 on failure , 0 on success
00053  */ 
00054 int sf_sdlist_delete(sfSDList *list)
00055 {
00056     while(list->head != NULL)
00057     {
00058         sf_sdlist_remove_next(list, NULL);
00059     }
00060     
00061     return 0;
00062 }
00063 
00064 /*
00065  * Function: int sf_sdlist_insert_next(sfSDList *list, SDListItem *item,
00066  *                                    void *data, SDListItem *container) 
00067  *
00068  * Purpose: insert data in container in the list after the item
00069  * Args: list - dlist structure
00070  *       item - current position in list structure
00071  *       data - current data to insert
00072  *       container - place to put the data 
00073  *
00074  * Returns: 0 on sucess,  1 on failure
00075  */ 
00076 int sf_sdlist_insert_next(sfSDList *list, SDListItem *item, void *data,
00077                           SDListItem *container) 
00078 {
00079     SDListItem *new = container;
00080     
00081     if(!new) return -1;
00082 
00083     new->data = data;
00084 
00085     if(item == NULL)
00086     {
00087         /* We are inserting at the head of the list HEAD */
00088 
00089         if(list->size == 0)
00090         {
00091             list->tail = new;
00092         }
00093         
00094         new->next = list->head;
00095         list->head = new;
00096     }
00097     else
00098     {
00099         if(item->next == NULL)
00100         {
00101             /* TAIL */
00102             list->tail = new;
00103         }
00104 
00105         new->next = item->next;
00106         item->next = new;        
00107     }
00108 
00109     new->prev = item;
00110     list->size++;
00111     return 0;
00112 }
00113 
00114 int sf_sdlist_append(sfSDList *list, void *data, SDListItem *container) {
00115     return sf_sdlist_insert_next(list, list->tail, data, container);
00116 }
00117 
00118 int sf_sdlist_remove_next(sfSDList *list, SDListItem *item) {
00119     SDListItem *li = NULL;
00120     void *data;
00121 
00122     if(list->size == 0)
00123     {
00124         return -1;
00125     }
00126 
00127     /* remove the head */
00128     if(item == NULL)
00129     {
00130         li = list->head;
00131         data = li->data;
00132         list->head = li->next;
00133     }
00134     else
00135     {
00136         data = item->data;        
00137         if(item->next == NULL)
00138         {
00139             return -1;
00140         }
00141 
00142         li = item->next;
00143         item->next = li->next;
00144         item->prev = li->prev;
00145     }
00146 
00147     if(li->next != NULL)
00148     {
00149         li->next->prev = item;
00150     }
00151 
00152     
00153     if(list->destroy != NULL)
00154         list->destroy(data);
00155     
00156     list->size--;
00157     
00158     if(list->size == 0) {
00159         list->tail = NULL;
00160     }
00161 
00162     return 0;
00163 }
00164 
00165 
00166 /*
00167  * Function: int sf_sdlist_remove(sfSDList *list, SDListItem *item)
00168  *
00169  * Purpose: remove the item pointed to by item
00170  * Args: list - list pointer
00171  *       item - item to unlink from the list
00172  *
00173  * Returns: 0 on success , 1 on exception
00174  *  
00175  */ 
00176 int sf_sdlist_remove(sfSDList *list, SDListItem *item)
00177 {
00178     SDListItem *next_item;
00179     SDListItem *prev_item;
00180 
00181     if(item == NULL)
00182     {
00183         return -1;
00184     }
00185 
00186     next_item = item->next;
00187     prev_item = item->prev;
00188 
00189     if(next_item != NULL)
00190     {
00191         next_item->prev = prev_item;
00192     } else {
00193         list->tail = prev_item;
00194     }
00195 
00196     if(prev_item != NULL)
00197     {
00198         prev_item->next = next_item;       
00199     } else {
00200         /* HEAD */
00201         list->head = next_item;
00202     }
00203         
00204         
00205         
00206     
00207     if(list->destroy != NULL)
00208         list->destroy(item->data);
00209 
00210 
00211     list->size--;
00212     
00213     if(list->size == 0)
00214     {
00215         list->head = NULL;
00216         list->tail = NULL;
00217     }
00218 
00219     return 0;
00220 }
00221 
00222 
00223 void print_sdlist(sfSDList *a)
00224 {
00225     SDListItem *li;
00226     printf("***");
00227     printf(" size: %d\n", a->size);
00228     for(li = a->head; li != NULL; li = li->next) {
00229         printf(" `- %p\n", li);
00230     }
00231 }
00232 
00233 #ifdef TEST_SDLIST
00234 void bad(void *d) {
00235     free(d);
00236     return;
00237 }
00238 
00239 int main(void) {
00240     sfSDList a;
00241 
00242     SDListItem *li;
00243     SDListItem listpool[1000];
00244     
00245     sf_sdlist_init(&a, &bad);
00246     if(sf_sdlist_append(&a, (char *) strdup("hello"), &listpool[0]))
00247     {
00248         printf("error appending!\n");
00249     }
00250     
00251     sf_sdlist_append(&a, (char *) strdup("goodbye"), &listpool[1]);
00252 
00253     sf_sdlist_insert_next(&a, NULL, (char *) strdup("woo"), &listpool[2]);
00254 
00255     printf("list size %d\n", a.size);
00256     
00257     for(li = a.head; li != NULL; li = li->next)
00258     {
00259         printf("%s\n", (char *) li->data);
00260     }
00261 
00262 
00263     printf("*** removing ***\n");
00264     sf_sdlist_remove(&a, &listpool[1]);
00265     printf("list size %d\n", a.size);
00266     for(li = a.head; li != NULL; li = li->next)
00267     {
00268         printf("%s\n", (char *) li->data);
00269     }
00270 
00271     sf_sdlist_delete(&a);
00272 
00273     printf("list size %d\n", a.size);
00274     return 0;
00275 }
00276 #endif /* TEST_SDLIST */

Generated on Sun May 14 14:51:18 2006 by  doxygen 1.4.2