File: port_list.c

package info (click to toggle)
libosip 0.9.7-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,852 kB
  • ctags: 1,812
  • sloc: ansic: 20,266; sh: 9,218; makefile: 215
file content (256 lines) | stat: -rw-r--r-- 4,902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
  The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdlib.h>
#include <stdio.h>

#include <osip/port.h>
#include <osip/list.h>

int
list_init (list_t * li)
{
  li->nb_elt = 0;
  return 0;			/* ok */
}

void
list_special_free (list_t * li, void *(*free_func) (void *))
{
  int pos = 0;
  void *element;

  if (li == NULL)
    return;
  while (!list_eol (li, pos))
    {
      element = (void *) list_get (li, pos);
      list_remove (li, pos);
      free_func (element);
      sfree (element);
    }
}

void
listofchar_free (list_t * li)
{
  int pos = 0;
  char *chain;

  if (li == NULL)
    return;
  while (!list_eol (li, pos))
    {
      chain = (char *) list_get (li, pos);
      list_remove (li, pos);
      sfree (chain);
    }
}

int
list_size (list_t * li)
{
  /* 
     Robin Nayathodan <roooot@softhome.net> 
     N.K Electronics INDIA

     NULL Checks  
   */

  if (li != NULL)
    return li->nb_elt;
  else
    return -1;
}

int
list_eol (list_t * li, int i)
{
  if (i < li->nb_elt)
    return 0;			/* not end of list */
  return 1;			/* end of list */
}

/* index starts from 0; */
int
list_add (list_t * li, void *el, int pos)
{
  node_t *ntmp;
  int i = 0;

  if (pos == -1 || pos >= li->nb_elt)
    {				/* insert at the end  */
      pos = li->nb_elt;
    }

  if (li->nb_elt == 0)
    {

      li->node = (node_t *) smalloc (sizeof (node_t));
      li->node->element = el;
      li->nb_elt++;
      return li->nb_elt;
    }

  ntmp = li->node;		/* exist because nb_elt>0  */

  if (pos == 0)
    {
      li->node = (node_t *) smalloc (sizeof (node_t));
      li->node->element = el;
      li->node->next = ntmp;
      li->nb_elt++;
      return li->nb_elt;
    }
  /* pos = 0 insert before first elt  */

  while (pos > i + 1)
    {
      i++;
      /* when pos>i next node exist  */
      ntmp = (node_t *) ntmp->next;
    }

  /* if pos==nb_elt next node does not exist  */
  if (pos == li->nb_elt)
    {
      ntmp->next = (node_t *) smalloc (sizeof (node_t));
      ntmp = (node_t *) ntmp->next;
      ntmp->element = el;
      li->nb_elt++;
      return li->nb_elt;
    }

  /* here pos==i so next node is where we want to insert new node */
  {
    node_t *nextnode = (node_t *) ntmp->next;

    ntmp->next = (node_t *) smalloc (sizeof (node_t));
    ntmp = (node_t *) ntmp->next;
    ntmp->element = el;
    ntmp->next = nextnode;
    li->nb_elt++;
  }
  return li->nb_elt;
}

/* index starts from 0 */
void *
list_get (list_t * li, int pos)
{
  node_t *ntmp;
  int i = 0;

  if (pos < 0 || pos >= li->nb_elt)
    /* element does not exist */
    return 0;


  ntmp = li->node;		/* exist because nb_elt>0 */

  while (pos > i)
    {
      i++;
      ntmp = (node_t *) ntmp->next;
    }
  return ntmp->element;
}

/* return -1 if failed */
int
list_remove (list_t * li, int pos)
{

  node_t *ntmp;
  int i = 0;

  if (pos < 0 || pos >= li->nb_elt)
    /* element does not exist */
    return -1;

  ntmp = li->node;		/* exist because nb_elt>0 */

  if ((pos == 0))
    {				/* special case  */
      li->node = (node_t *) ntmp->next;
      li->nb_elt--;
      sfree (ntmp);
      return li->nb_elt;
    }

  while (pos > i + 1)
    {
      i++;
      ntmp = (node_t *) ntmp->next;
    }

  /* if pos==nb_elt next node is the last one */
/* Unreachable code!
  if (pos == li->nb_elt)
    {
      sfree (ntmp->next);
      li->nb_elt--;
      return li->nb_elt;
    }
*/

  /* insert new node */
  {
    node_t *remnode;

    remnode = (node_t *) ntmp->next;
    ntmp->next = ((node_t *) ntmp->next)->next;
    sfree (remnode);
    li->nb_elt--;
  }
  return li->nb_elt;
}


/*  
   Robin Nayathodan <roooot@softhome.net> 
   N.K Electronics INDIA
    
   To Modify the element in the List
 
 */
/*

int 
list_set(list_t * li, void *el, int pos)
{
  node_t *ntmp;
  int i = 0;

  if (pos < 0 || pos >= li->nb_elt)
    // element does not exist
    return 0;


  ntmp = li->node;              //exist because nb_elt>0

  while (pos > i)
    {
      i++;
      ntmp = (node_t *) ntmp->next;
    }
  ntmp->element = el;
  return li->nb_elt;
}
*/