and added files
[bcm963xx.git] / userapps / opensource / libosip2 / src / osipparser2 / osip_list.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include <osipparser2/osip_port.h>
24 #include <osipparser2/osip_list.h>
25
26 int
27 osip_list_init (osip_list_t * li)
28 {
29   li->nb_elt = 0;
30   return 0;                     /* ok */
31 }
32
33 void
34 osip_list_special_free (osip_list_t * li, void *(*free_func) (void *))
35 {
36   int pos = 0;
37   void *element;
38
39   if (li == NULL)
40     return;
41   while (!osip_list_eol (li, pos))
42     {
43       element = (void *) osip_list_get (li, pos);
44       osip_list_remove (li, pos);
45       free_func (element);
46     }
47   osip_free (li);
48 }
49
50 void
51 osip_list_ofchar_free (osip_list_t * li)
52 {
53   int pos = 0;
54   char *chain;
55
56   if (li == NULL)
57     return;
58   while (!osip_list_eol (li, pos))
59     {
60       chain = (char *) osip_list_get (li, pos);
61       osip_list_remove (li, pos);
62       osip_free (chain);
63     }
64   osip_free (li);
65 }
66
67 int
68 osip_list_size (const osip_list_t * li)
69 {
70   /* 
71      Robin Nayathodan <roooot@softhome.net> 
72      N.K Electronics INDIA
73
74      NULL Checks  
75    */
76
77   if (li != NULL)
78     return li->nb_elt;
79   else
80     return -1;
81 }
82
83 int
84 osip_list_eol (const osip_list_t * li, int i)
85 {
86   if(li==NULL) return -1;
87   if (i < li->nb_elt)
88     return 0;                   /* not end of list */
89   return 1;                     /* end of list */
90 }
91
92 /* index starts from 0; */
93 int
94 osip_list_add (osip_list_t * li, void *el, int pos)
95 {
96   __node_t *ntmp;
97   int i = 0;
98
99   if (pos == -1 || pos >= li->nb_elt)
100     {                           /* insert at the end  */
101       pos = li->nb_elt;
102     }
103
104   if (li->nb_elt == 0)
105     {
106
107       li->node = (__node_t *) osip_malloc (sizeof (__node_t));
108       li->node->element = el;
109       li->nb_elt++;
110       return li->nb_elt;
111     }
112
113   ntmp = li->node;              /* exist because nb_elt>0  */
114
115   if (pos == 0)
116     {
117       li->node = (__node_t *) osip_malloc (sizeof (__node_t));
118       li->node->element = el;
119       li->node->next = ntmp;
120       li->nb_elt++;
121       return li->nb_elt;
122     }
123   /* pos = 0 insert before first elt  */
124
125   while (pos > i + 1)
126     {
127       i++;
128       /* when pos>i next node exist  */
129       ntmp = (__node_t *) ntmp->next;
130     }
131
132   /* if pos==nb_elt next node does not exist  */
133   if (pos == li->nb_elt)
134     {
135       ntmp->next = (__node_t *) osip_malloc (sizeof (__node_t));
136       ntmp = (__node_t *) ntmp->next;
137       ntmp->element = el;
138       li->nb_elt++;
139       return li->nb_elt;
140     }
141
142   /* here pos==i so next node is where we want to insert new node */
143   {
144     __node_t *nextnode = (__node_t *) ntmp->next;
145
146     ntmp->next = (__node_t *) osip_malloc (sizeof (__node_t));
147     ntmp = (__node_t *) ntmp->next;
148     ntmp->element = el;
149     ntmp->next = nextnode;
150     li->nb_elt++;
151   }
152   return li->nb_elt;
153 }
154
155 /* index starts from 0 */
156 void *
157 osip_list_get (const osip_list_t * li, int pos)
158 {
159   __node_t *ntmp;
160   int i = 0;
161
162   if (pos < 0 || pos >= li->nb_elt)
163     /* element does not exist */
164     return 0;
165
166
167   ntmp = li->node;              /* exist because nb_elt>0 */
168
169   while (pos > i)
170     {
171       i++;
172       ntmp = (__node_t *) ntmp->next;
173     }
174   return ntmp->element;
175 }
176
177 /* return -1 if failed */
178 int
179 osip_list_remove (osip_list_t * li, int pos)
180 {
181
182   __node_t *ntmp;
183   int i = 0;
184
185   if (pos < 0 || pos >= li->nb_elt)
186     /* element does not exist */
187     return -1;
188
189   ntmp = li->node;              /* exist because nb_elt>0 */
190
191   if ((pos == 0))
192     {                           /* special case  */
193       li->node = (__node_t *) ntmp->next;
194       li->nb_elt--;
195       osip_free (ntmp);
196       return li->nb_elt;
197     }
198
199   while (pos > i + 1)
200     {
201       i++;
202       ntmp = (__node_t *) ntmp->next;
203     }
204
205   /* if pos==nb_elt next node is the last one */
206 /* Unreachable code!
207   if (pos == li->nb_elt)
208     {
209       osip_free (ntmp->next);
210       li->nb_elt--;
211       return li->nb_elt;
212     }
213 */
214
215   /* insert new node */
216   {
217     __node_t *remnode;
218
219     remnode = (__node_t *) ntmp->next;
220     ntmp->next = ((__node_t *) ntmp->next)->next;
221     osip_free (remnode);
222     li->nb_elt--;
223   }
224   return li->nb_elt;
225 }
226
227
228 /*  
229    Robin Nayathodan <roooot@softhome.net> 
230    N.K Electronics INDIA
231     
232    To Modify the element in the List
233  
234  */
235 /*
236
237 int 
238 list_set(osip_list_t * li, void *el, int pos)
239 {
240   __node_t *ntmp;
241   int i = 0;
242
243   if (pos < 0 || pos >= li->nb_elt)
244     // element does not exist
245     return 0;
246
247
248   ntmp = li->node;              //exist because nb_elt>0
249
250   while (pos > i)
251     {
252       i++;
253       ntmp = (__node_t *) ntmp->next;
254     }
255   ntmp->element = el;
256   return li->nb_elt;
257 }
258 */