ze-filter  (ze-filter-0.8.0-develop-180218)
zeTable.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : janvier 2002
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 #include <ze-sys.h>
25 #include <zeTable.h>
26 
27 #include "libze.h"
28 
29 
30 /* ****************************************************************************
31  * *
32  * *
33  **************************************************************************** */
34 static int zeTable_Resize(zeTbl_T *);
35 
36 #define RECPTR(t, i) ((char *) t->data + i * t->sz)
37 
38 /* ****************************************************************************
39  * *
40  * *
41  **************************************************************************** */
42 #define ALIGN_SIZE 16
43 
44 int
45 zeTable_Init(tbh, sz, dim, comp)
46  zeTbl_T *tbh;
47  size_t sz;
48  int dim;
49  int (*comp) (const void *, const void *);
50 {
51  size_t rsz;
52  size_t asz;
53 
54  if (tbh == NULL)
55  return -1;
56 
57  rsz = sz;
58 
59  asz = sz % ALIGN_SIZE;
60  if (asz > 0)
61  sz += asz;
62 
63  if ((tbh->data = malloc(sz * dim)) == NULL)
64  return -1;
65  tbh->sz = sz;
66  tbh->rsz = rsz;
67  tbh->dim = dim;
68  tbh->chunk = dim;
69  tbh->nb = 0;
70  tbh->comp = comp;
71  tbh->index = 0;
72 
73  return 0;
74 }
75 
76 /* ****************************************************************************
77  * *
78  * *
79  **************************************************************************** */
80 static int
81 zeTable_Resize(tbh)
82  zeTbl_T *tbh;
83 {
84  int newsz;
85  void *ptr;
86 
87  if (tbh == NULL)
88  return -1;
89 
90  newsz = tbh->sz * (tbh->dim + tbh->chunk);
91  if (newsz == 0)
92  return -2;
93 
94  if (tbh->data == NULL)
95  ptr = malloc(newsz);
96  else
97  ptr = realloc(tbh->data, newsz);
98 
99  if (ptr != NULL)
100  {
101  tbh->dim += tbh->chunk;
102  tbh->data = ptr;
103  }
104 
105  return ptr != NULL ? 0 : -3;
106 }
107 
108 /* ****************************************************************************
109  * *
110  * *
111  **************************************************************************** */
112 int
114  zeTbl_T *tbh;
115 {
116  if (tbh == NULL)
117  return -1;
118 
119  if (tbh->data)
120  free(tbh->data);
121  tbh->data = NULL;
122  tbh->sz = 0;
123  tbh->dim = 0;
124  tbh->nb = 0;
125  tbh->comp = NULL;
126  tbh->index = 0;
127 
128  return 0;
129 }
130 
131 /* ****************************************************************************
132  * *
133  * *
134  **************************************************************************** */
135 int
137  zeTbl_T *tbh;
138 {
139  if (tbh == NULL)
140  return -1;
141 
142  if (tbh->data)
143  memset(tbh->data, 0, tbh->sz * tbh->dim);
144  tbh->nb = 0;
145  tbh->index = 0;
146 
147  return 0;
148 }
149 
150 /* ****************************************************************************
151  * *
152  * *
153  **************************************************************************** */
154 int
155 zeTable_Add(tbh, data)
156  zeTbl_T *tbh;
157  void *data;
158 {
159  if (tbh == NULL)
160  return -1;
161 
162  if (tbh->data == NULL)
163  return -1;
164 
165  if ((tbh->nb >= tbh->dim) && (zeTable_Resize(tbh) < 0))
166  {
167  return -1;
168  }
169 
170  memcpy(RECPTR(tbh, tbh->nb), data, tbh->rsz);
171  tbh->nb++;
172 
173  return 0;
174 }
175 
176 /* ****************************************************************************
177  * *
178  * *
179  **************************************************************************** */
180 int
181 zeTable_Fetch(tbh, data)
182  zeTbl_T *tbh;
183  void *data;
184 {
185  if (tbh == NULL)
186  return -1;
187 
188  if (tbh->data == NULL)
189  return -1;
190 
191  if (tbh->comp)
192  {
193  void *ptr;
194 
195  if ((ptr = bsearch(data, tbh->data, tbh->nb, tbh->sz, tbh->comp)) != NULL)
196  {
197  memcpy(data, ptr, tbh->rsz);
198  return 0;
199  }
200  }
201  return -1;
202 }
203 
204 /* ****************************************************************************
205  * *
206  * *
207  **************************************************************************** */
208 int
210  zeTbl_T *tbh;
211 {
212  if (tbh == NULL)
213  return -1;
214 
215  if (tbh->data == NULL)
216  return -1;
217 
218  return tbh->nb;
219 }
220 
221 /* ****************************************************************************
222  * *
223  * *
224  **************************************************************************** */
225 int
226 zeTable_Get_Ind(tbh, data, ind)
227  zeTbl_T *tbh;
228  void *data;
229  int ind;
230 {
231  if (tbh == NULL)
232  return -1;
233 
234  if (tbh->data == NULL)
235  return -1;
236 
237  if (data == NULL)
238  return -2;
239 
240  memcpy(data, RECPTR(tbh, ind), tbh->rsz);
241 
242  return 0;
243 }
244 
245 /* ****************************************************************************
246  * *
247  * *
248  **************************************************************************** */
249 int
251  zeTbl_T *tbh;
252  void *data;
253 {
254  if (tbh == NULL)
255  return -1;
256 
257  if (tbh->data == NULL)
258  return -1;
259 
260  if (data == NULL)
261  return -2;
262 
263  if (tbh->nb == 0)
264  return 1;
265 
266  tbh->index = 0;
267  memcpy(data, RECPTR(tbh, tbh->index), tbh->rsz);
268 
269  return 0;
270 }
271 
272 /* ****************************************************************************
273  * *
274  * *
275  **************************************************************************** */
276 int
278  zeTbl_T *tbh;
279  void *data;
280 {
281  if (tbh == NULL)
282  return -1;
283 
284  if (tbh->data == NULL)
285  return -1;
286 
287  if (data == NULL)
288  return -2;
289 
290  if (tbh->index < tbh->nb - 1)
291  tbh->index++;
292  else
293  return 1;
294 
295  memcpy(data, RECPTR(tbh, tbh->index), tbh->rsz);
296 
297  return 0;
298 }
299 
300 /* ****************************************************************************
301  * *
302  * *
303  **************************************************************************** */
304 void *
306  zeTbl_T *tbh;
307 {
308  if (tbh == NULL)
309  return NULL;
310 
311  if (tbh->data == NULL)
312  return NULL;
313 
314  if (tbh->nb == 0)
315  return NULL;
316 
317  tbh->index = 0;
318 
319  return (void *) RECPTR(tbh, tbh->index);
320 }
321 
322 /* ****************************************************************************
323  * *
324  * *
325  **************************************************************************** */
326 void *
328  zeTbl_T *tbh;
329 {
330  if (tbh == NULL)
331  return NULL;
332 
333  if (tbh->data == NULL)
334  return NULL;
335 
336  if (tbh->index < tbh->nb - 1)
337  tbh->index++;
338  else
339  return NULL;
340 
341  return (void *) RECPTR(tbh, tbh->index);
342 }
343 
344 /* ****************************************************************************
345  * *
346  * *
347  **************************************************************************** */
348 int
350  zeTbl_T *tbh;
351 {
352  if (tbh == NULL)
353  return -1;
354 
355  if (tbh->data == NULL)
356  return -1;
357 
358  if (tbh->comp != NULL)
359  qsort(tbh->data, tbh->nb, tbh->sz, tbh->comp);
360 
361  return 0;
362 }
int zeTable_Fetch(zeTbl_T *tbh, void *data)
Definition: zeTable.c:181
size_t rsz
Definition: zeTable.h:35
int zeTable_Free(zeTbl_T *tbh)
Definition: zeTable.c:113
int nb
Definition: zeTable.h:38
void * zeTable_Get_Next_Ptr(zeTbl_T *tbh)
Definition: zeTable.c:327
size_t sz
Definition: zeTable.h:34
void * zeTable_Get_First_Ptr(zeTbl_T *tbh)
Definition: zeTable.c:305
int(* comp)(const void *, const void *)
Definition: zeTable.h:41
int zeTable_Init(zeTbl_T *tbh, size_t sz, int dim, int *comp)
Definition: zeTable.c:45
int zeTable_Clear(zeTbl_T *tbh)
Definition: zeTable.c:136
int zeTable_Add(zeTbl_T *tbh, void *data)
Definition: zeTable.c:155
int zeTable_Get_First(zeTbl_T *tbh, void *data)
Definition: zeTable.c:250
void * data
Definition: zeTable.h:40
int zeTable_Sort(zeTbl_T *tbh)
Definition: zeTable.c:349
#define memcpy(d, s, n)
Definition: ze-sys.h:224
int zeTable_Get_Next(zeTbl_T *tbh, void *data)
Definition: zeTable.c:277
#define RECPTR(t, i)
Definition: zeTable.c:36
int zeTable_Count(zeTbl_T *tbh)
Definition: zeTable.c:209
int index
Definition: zeTable.h:39
int dim
Definition: zeTable.h:37
#define ALIGN_SIZE
Definition: zeTable.c:42
int zeTable_Get_Ind(zeTbl_T *tbh, void *data, int ind)
Definition: zeTable.c:226
int chunk
Definition: zeTable.h:36