ze-filter  (ze-filter-0.8.0-develop-180218)
zeLinkedList.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : janvier 2002
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 #include <ze-sys.h>
26 
27 #include "libze.h"
28 
29 /* ****************************************************************************
30  * *
31  * *
32  ******************************************************************************/
33 LISTR_T *
34 zeLinkedList_Add(head, s, nb, data, size)
35  LISTR_T *head;
36  char *s;
37  int nb;
38  void *data;
39  size_t size;
40 {
41  LISTR_T *p;
42 
43  if ((s == NULL) || (strlen(s) == 0))
44  return NULL;
45 
46  for (p = head; p != NULL; p = p->next) {
47  if (strcasecmp(p->key, s) == 0)
48  break;
49  }
50 
51  if (p != NULL) {
52  p->count += nb;
53  return head;
54  }
55 
56  p = (LISTR_T *) malloc(sizeof (LISTR_T));
57  if (p == NULL) {
58  ZE_LogSysError("malloc(LISTR_T)");
59  return NULL;
60  }
61  memset(p, 0, sizeof (LISTR_T));
62 
63  /*
64  * add as the first element
65  */
66  p->next = head;
67  p->prev = NULL;
68  if (head != NULL)
69  head->prev = p;
70 
71  if ((p->key = strdup(s)) == NULL) {
72  ZE_LogSysError("strdup(LISTR_T)");
73  FREE(p);
74  return NULL;
75  }
76  if (data != NULL && size > 0) {
77  if ((p->data = malloc(size)) != NULL) {
78  memcpy(p->data, data, size);
79  p->size = size;
80  } else
81  ZE_LogSysError("strdup(LISTR_T)");
82  }
83  p->count = nb;
84  head = p;
85 
86  return head;
87 }
88 
89 /* ****************************************************************************
90  * *
91  * *
92  ******************************************************************************/
93 LISTR_T *
94 zeLinkedList_Set(head, s, nb, data, size)
95  LISTR_T *head;
96  char *s;
97  int nb;
98  void *data;
99  size_t size;
100 {
101  LISTR_T *p;
102 
103  if ((s == NULL) || (strlen(s) == 0))
104  return NULL;
105 
106  for (p = head; p != NULL; p = p->next) {
107  if (strcasecmp(p->key, s) == 0)
108  break;
109  }
110 
111  if (p != NULL) {
112  p->count = nb;
113  if (p->data != NULL) {
114  FREE(p->data);
115  p->size = 0;
116  if (data != NULL && size > 0) {
117  if ((p->data = malloc(size)) != NULL) {
118  memcpy(p->data, data, size);
119  p->size = size;
120  } else
121  ZE_LogSysError("malloc(data)");
122  }
123  }
124  return p;
125  }
126 
127  return p;
128 }
129 
130 /* ****************************************************************************
131  * *
132  * *
133  ******************************************************************************/
134 bool
135 zeLinkedList_Remove(head, key, func)
136  LISTR_T *head;
137  char *key;
138  LISTCLEAR_F func;
139 {
140  LISTR_T *p;
141  bool result = FALSE;
142 
143  if ((key == NULL) || (strlen(key) == 0))
144  return FALSE;
145 
146  for (p = head; p != NULL; p = p->next) {
147  if (strcasecmp(p->key, key) == 0) {
148 
149  if (p->prev != NULL)
150  p->prev->next = p->next;
151  if (p->next != NULL)
152  p->next->prev = p->prev;
153  FREE(p->key);
154  if (func != NULL && p->data != NULL)
155  func(p->data);
156  FREE(p->data);
157  FREE(p);
158 
159  result = TRUE;
160  break;
161  }
162  }
163 
164  return result;
165 }
166 
167 /* ****************************************************************************
168  * *
169  * *
170  ******************************************************************************/
171 LISTR_T *
173  LISTR_T *head;
174  char *s;
175 {
176  LISTR_T *p;
177 
178  if ((s == NULL) || (strlen(s) == 0))
179  return NULL;
180 
181  for (p = head; p != NULL; p = p->next) {
182  if (strcasecmp(p->key, s) == 0)
183  return p;
184  }
185 
186  return NULL;
187 }
188 
189 
190 /* ****************************************************************************
191  * *
192  * *
193  ******************************************************************************/
194 bool
196  LISTR_T *head;
197  LISTCLEAR_F func;
198 {
199  LISTR_T *p;
200 
201  while (head != NULL) {
202  p = head;
203  head = p->next;
204  FREE(p->key);
205  if (func != NULL && p->data != NULL)
206  func(p->data);
207  FREE(p->data);
208  FREE(p);
209  }
210  return TRUE;
211 }
212 
213 /* ****************************************************************************
214  * *
215  * *
216  ******************************************************************************/
217 
218 int
220  LISTR_T *head;
221  char *key;
222  int nb;
223 {
224  LISTR_T *p;
225 
226  p = zeLinkedList_Find(head, key);
227  if (p != NULL) {
228  p->count = nb;
229  return p->count;
230  }
231  return 0;
232 }
233 
234 /* ****************************************************************************
235  * *
236  * *
237  ******************************************************************************/
238 int
240  LISTR_T *head;
241  char *key;
242 {
243  LISTR_T *p;
244 
245  p = zeLinkedList_Find(head, key);
246  if (p != NULL)
247  return p->count;
248  return 0;
249 }
250 
251 /* ****************************************************************************
252  * *
253  * *
254  ******************************************************************************/
255 int
257  LISTR_T *head;
258  char *key;
259 {
260  LISTR_T *p;
261 
262  p = zeLinkedList_Find(head, key);
263  if (p != NULL) {
264  p->count++;
265  return p->count;
266  }
267  return 0;
268 }
LISTR_T * prev
Definition: zeLinkedList.h:36
LISTR_T * zeLinkedList_Add(LISTR_T *head, char *s, int nb, void *data, size_t size)
Definition: zeLinkedList.c:34
int zeLinkedList_CountInc(LISTR_T *head, char *key)
Definition: zeLinkedList.c:256
bool zeLinkedList_Clear(LISTR_T *head, LISTCLEAR_F func)
Definition: zeLinkedList.c:195
#define FREE(x)
Definition: macros.h:37
void(* LISTCLEAR_F)(void *)
Definition: zeLinkedList.h:44
int count
Definition: zeLinkedList.h:41
char * key
Definition: zeLinkedList.h:38
#define FALSE
Definition: macros.h:160
int zeLinkedList_CountSet(LISTR_T *head, char *key, int nb)
Definition: zeLinkedList.c:219
bool zeLinkedList_Remove(LISTR_T *head, char *key, LISTCLEAR_F func)
Definition: zeLinkedList.c:135
LISTR_T * zeLinkedList_Set(LISTR_T *head, char *s, int nb, void *data, size_t size)
Definition: zeLinkedList.c:94
int nb
Definition: ze-connopen.c:61
#define TRUE
Definition: macros.h:157
LISTR_T * next
Definition: zeLinkedList.h:37
#define memcpy(d, s, n)
Definition: ze-sys.h:224
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
LISTR_T * zeLinkedList_Find(LISTR_T *head, char *s)
Definition: zeLinkedList.c:172
void * data
Definition: zeLinkedList.h:39
size_t size
Definition: zeLinkedList.h:40
int zeLinkedList_CountGet(LISTR_T *head, char *key)
Definition: zeLinkedList.c:239