osip_list.c

Go to the documentation of this file.
00001 
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 
00026 #include "osip_port.h"
00027 #include "osip_list.h"
00028 
00029 int
00030 osip_list_init (osip_list_t * li)
00031 {
00032   if (li == NULL)
00033     return -1;
00034   memset (li, 0, sizeof (osip_list_t));
00035   return 0;                     /* ok */
00036 }
00037 
00038 void
00039 osip_list_special_free (osip_list_t * li, void *(*free_func) (void *))
00040 {
00041   int pos = 0;
00042   void *element;
00043 
00044   if (li == NULL)
00045     return;
00046   while (!osip_list_eol (li, pos))
00047     {
00048       element = (void *) osip_list_get (li, pos);
00049       osip_list_remove (li, pos);
00050       if (free_func != NULL)
00051         free_func (element);
00052     }
00053   osip_free (li);
00054 }
00055 
00056 void
00057 osip_list_ofchar_free (osip_list_t * li)
00058 {
00059   int pos = 0;
00060   char *chain;
00061 
00062   if (li == NULL)
00063     return;
00064   while (!osip_list_eol (li, pos))
00065     {
00066       chain = (char *) osip_list_get (li, pos);
00067       osip_list_remove (li, pos);
00068       osip_free (chain);
00069     }
00070   osip_free (li);
00071 }
00072 
00073 int
00074 osip_list_size (const osip_list_t * li)
00075 {
00076   /* 
00077      Robin Nayathodan <roooot@softhome.net> 
00078      N.K Electronics INDIA
00079 
00080      NULL Checks  
00081    */
00082 
00083   if (li != NULL)
00084     return li->nb_elt;
00085   else
00086     return -1;
00087 }
00088 
00089 int
00090 osip_list_eol (const osip_list_t * li, int i)
00091 {
00092   if (li == NULL)
00093     return -1;
00094   if (i < li->nb_elt)
00095     return 0;                   /* not end of list */
00096   return 1;                     /* end of list */
00097 }
00098 
00099 /* index starts from 0; */
00100 int
00101 osip_list_add (osip_list_t * li, void *el, int pos)
00102 {
00103   __node_t *ntmp;
00104   int i = 0;
00105 
00106   if (li == NULL)
00107     return -1;
00108 
00109   if (li->nb_elt == 0)
00110     {
00111 
00112       li->node = (__node_t *) osip_malloc (sizeof (__node_t));
00113       if (li->node == NULL)
00114         return -1;
00115       li->node->element = el;
00116       li->node->next = NULL;
00117       li->nb_elt++;
00118       return li->nb_elt;
00119     }
00120 
00121   if (pos == -1 || pos >= li->nb_elt)
00122     {                           /* insert at the end  */
00123       pos = li->nb_elt;
00124     }
00125 
00126   ntmp = li->node;              /* exist because nb_elt>0  */
00127 
00128   if (pos == 0)                 /* pos = 0 insert before first elt  */
00129     {
00130       li->node = (__node_t *) osip_malloc (sizeof (__node_t));
00131       if (li->node == NULL)
00132         {
00133           /* leave the list unchanged */
00134           li->node = ntmp;
00135           return -1;
00136         }
00137       li->node->element = el;
00138       li->node->next = ntmp;
00139       li->nb_elt++;
00140       return li->nb_elt;
00141     }
00142 
00143 
00144   while (pos > i + 1)
00145     {
00146       i++;
00147       /* when pos>i next node exist  */
00148       ntmp = (__node_t *) ntmp->next;
00149     }
00150 
00151   /* if pos==nb_elt next node does not exist  */
00152   if (pos == li->nb_elt)
00153     {
00154       ntmp->next = (__node_t *) osip_malloc (sizeof (__node_t));
00155       if (ntmp->next == NULL)
00156         return -1;              /* leave the list unchanged */
00157       ntmp = (__node_t *) ntmp->next;
00158       ntmp->element = el;
00159       ntmp->next = NULL;
00160       li->nb_elt++;
00161       return li->nb_elt;
00162     }
00163 
00164   /* here pos==i so next node is where we want to insert new node */
00165   {
00166     __node_t *nextnode = (__node_t *) ntmp->next;
00167 
00168     ntmp->next = (__node_t *) osip_malloc (sizeof (__node_t));
00169     if (ntmp->next == NULL)
00170       {
00171         /* leave the list unchanged */
00172         ntmp->next = nextnode;
00173         return -1;
00174       }
00175     ntmp = (__node_t *) ntmp->next;
00176     ntmp->element = el;
00177     ntmp->next = nextnode;
00178     li->nb_elt++;
00179   }
00180   return li->nb_elt;
00181 }
00182 
00183 /* index starts from 0 */
00184 void *
00185 osip_list_get (const osip_list_t * li, int pos)
00186 {
00187   __node_t *ntmp;
00188   int i = 0;
00189 
00190   if (li == NULL)
00191     return NULL;
00192 
00193   if (pos < 0 || pos >= li->nb_elt)
00194     /* element does not exist */
00195     return NULL;
00196 
00197 
00198   ntmp = li->node;              /* exist because nb_elt>0 */
00199 
00200   while (pos > i)
00201     {
00202       i++;
00203       ntmp = (__node_t *) ntmp->next;
00204     }
00205   return ntmp->element;
00206 }
00207 
00208 /* added by bennewit@cs.tu-berlin.de */
00209 void *
00210 osip_list_get_first (osip_list_t * li, osip_list_iterator_t * iterator)
00211 {
00212   if (0 >= li->nb_elt)
00213     {
00214       iterator->actual = 0;
00215       return 0;
00216     }
00217 
00218   iterator->actual = li->node;
00219   iterator->prev = &li->node;
00220   iterator->li = li;
00221   iterator->pos = 0;
00222 
00223   return li->node->element;
00224 }
00225 
00226 /* added by bennewit@cs.tu-berlin.de */
00227 void *
00228 osip_list_get_next (osip_list_iterator_t * iterator)
00229 {
00230   iterator->prev = (__node_t **) & (iterator->actual->next);
00231   iterator->actual = iterator->actual->next;
00232   ++(iterator->pos);
00233 
00234   if (osip_list_iterator_has_elem (*iterator))
00235     {
00236       return iterator->actual->element;
00237     }
00238 
00239   iterator->actual = 0;
00240   return 0;
00241 }
00242 
00243 /* added by bennewit@cs.tu-berlin.de */
00244 void *
00245 osip_list_iterator_remove (osip_list_iterator_t * iterator)
00246 {
00247   if (osip_list_iterator_has_elem (*iterator))
00248     {
00249       --(iterator->li->nb_elt);
00250 
00251       *(iterator->prev) = iterator->actual->next;
00252 
00253       osip_free (iterator->actual);
00254       iterator->actual = *(iterator->prev);
00255     }
00256 
00257   if (osip_list_iterator_has_elem (*iterator))
00258     {
00259       return iterator->actual->element;
00260     }
00261 
00262   return 0;
00263 }
00264 
00265 /* return -1 if failed */
00266 int
00267 osip_list_remove (osip_list_t * li, int pos)
00268 {
00269 
00270   __node_t *ntmp;
00271   int i = 0;
00272 
00273   if (li == NULL)
00274     return -1;
00275 
00276   if (pos < 0 || pos >= li->nb_elt)
00277     /* element does not exist */
00278     return -1;
00279 
00280   ntmp = li->node;              /* exist because nb_elt>0 */
00281 
00282   if ((pos == 0))
00283     {                           /* special case  */
00284       li->node = (__node_t *) ntmp->next;
00285       li->nb_elt--;
00286       osip_free (ntmp);
00287       return li->nb_elt;
00288     }
00289 
00290   while (pos > i + 1)
00291     {
00292       i++;
00293       ntmp = (__node_t *) ntmp->next;
00294     }
00295 
00296   /* insert new node */
00297   {
00298     __node_t *remnode;
00299 
00300     remnode = (__node_t *) ntmp->next;
00301     ntmp->next = ((__node_t *) ntmp->next)->next;
00302     osip_free (remnode);
00303     li->nb_elt--;
00304   }
00305   return li->nb_elt;
00306 }

Generated on Fri Mar 9 20:08:53 2007 for MAD-FCL by  doxygen 1.5.0