File: list.c

package info (click to toggle)
cabber 0.4.0-test5-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 172 kB
  • ctags: 100
  • sloc: ansic: 1,421; makefile: 82
file content (264 lines) | stat: -rw-r--r-- 3,821 bytes parent folder | download | duplicates (2)
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
257
258
259
260
261
262
263
264
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

List
NewList (void)
{
  List l = malloc (sizeof (node));
  l->next = NULL;
  return l;
}

void
DeleteList (List l)
{
  Ptrnode p, tmp;

  if(l==NULL){
  	exit(1);
	}
  p = l->next;
  l->next = NULL;
  while (p != NULL)
    {
      tmp = p->next;
      free (p);
      p = tmp;
    }
}

Ptrnode
NextInList (Ptrnode p)
{
  return p->next;
}

Ptrnode
FirstInList (List l)
{
  return l->next;
}


Ptrnode
HeaderList (List l)
{
  return l;
}

void
InsertInList (elementT t, Ptrnode p, List l)
{
  Ptrnode tmp;

  tmp = malloc (sizeof (node));
  tmp->elemento = t;
  tmp->next = p->next;
  p->next = tmp;
}

void
DeleteOfList (Ptrnode p, List l)
{
  Ptrnode tmp;

  tmp = FirstInList (l);
  while ((tmp->next != p) && (tmp))
    tmp = NextInList (tmp);

  if (tmp)
    {
      tmp->next = p->next;
      free (p);
    }
}

elementT
RetrieveFromList (Ptrnode p)
{
  return p->elemento;
}

char *
FindSelectedJidResourceInList (int selected, List roster)
{
  Ptrnode p;
  elementT elemento;
  int c;

  p = FirstInList (roster);
  for (c = 0; c < selected; c++)
    p = NextInList (p);

  elemento = RetrieveFromList (p);
  return elemento->resource;
}

char *
FindSelectedJidJidInList (int selected, List l)
{
  Ptrnode p;
  elementT elemento;
  int c;

  p = FirstInList (l);
  for (c = 0; c < selected; c++)
    p = NextInList (p);

  elemento = RetrieveFromList (p);
  return elemento->jid;
}

char *
FindSelectedJidInList (int selected, List l)
{
  Ptrnode p;
  elementT elemento;
  int c;

  p = FirstInList (l);
  for (c = 0; c < selected; c++)
    p = NextInList (p);

  elemento = RetrieveFromList (p);
  return elemento->name;
}

char *
FindJidInList (char *jid, List l)
{
  Ptrnode p;
  elementT elemento;
  char *aux;

  aux = strchr (jid, '/');
  if (aux)
    {
      aux = '\0';
    }

  p = FirstInList (l);
  while (p != NULL)
    {
      elemento = RetrieveFromList (p);
      if (!strcmp (jid, elemento->jid))
	return elemento->jid;
      p = NextInList (p);
    }
  return NULL;
}

char *
FindJidNameInList (char *jid, List l)
{
  Ptrnode p;
  elementT elemento;
  char *aux;

  aux = strchr (jid, '/');
  if (aux)
    {
      *aux = '\0';
    }

  p = FirstInList (l);
  while (p != NULL)
    {
      elemento = RetrieveFromList (p);
      p = NextInList (p);
      if (!strncmp (elemento->jid, jid, strlen (jid)))
	return elemento->name;
    }
  return NULL;
}

int
compara (const char *str1, const char *str2)
{
  int i, lim;

  if (strlen (str1) < strlen (str2))
    lim = strlen (str1);
  else
    lim = strlen (str2);

  for (i = 0; i < lim; i++)
    {
      if (str1[i] < str2[i])
	return 1;		// str1 < str2
      if (str1[i] > str2[i])
	return 2;		// str1 > str2
    }
  return 0;			// str1 == str2
}

void
SortListByName (List l)
{
  int i, j;
  int tam = 1;
  elementT temp, temp2;
  Ptrnode p;

  // count elements
  p = FirstInList (l);
  while ((p = NextInList (p)))
    tam++;

  for (i = 1; i < tam; i++)
    {
      for (j = 1; j < tam; j++)
	{
	  if (j == 1)
	    p = FirstInList (l);
	  else if (p)
	    p = NextInList (p);
	  else
	    break;

	  if (p)
	    temp = RetrieveFromList (p);
	  else
	    break;

	  p = NextInList (p);
	  if (p)
	    temp2 = RetrieveFromList (p);
	  else
	    break;

	  //p=Previous(p);
	  {
	    int co;
	    p = FirstInList (l);
	    for (co = 0; co < j - 1; co++)
	      p = NextInList (p);
	  }

	  if ((temp) && (temp2))
	    {
	      switch (compara (temp->name, temp2->name))
		{
		case 2:
		  {
		    elementT tmp;

		    tmp = malloc (sizeof (element));

		    memcpy (tmp, temp2, sizeof (element));
		    memcpy (temp2, temp, sizeof (element));
		    memcpy (temp, tmp, sizeof (element));

		    free (tmp);
		    break;
		  }

		default:
		  break;
		}
	    }
	}
    }
}