gwenhywfar 5.12.0
stringlist2.c
Go to the documentation of this file.
1/***************************************************************************
2 $RCSfile$
3 -------------------
4 cvs : $Id$
5 begin : Thu Apr 03 2003
6 copyright : (C) 2003 by Martin Preuss
7 email : martin@libchipcard.de
8
9 ***************************************************************************
10 * *
11 * This library is free software; you can redistribute it and/or *
12 * modify it under the terms of the GNU Lesser General Public *
13 * License as published by the Free Software Foundation; either *
14 * version 2.1 of the License, or (at your option) any later version. *
15 * *
16 * This library 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. See the GNU *
19 * Lesser General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU Lesser General Public *
22 * License along with this library; if not, write to the Free Software *
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24 * MA 02111-1307 USA *
25 * *
26 ***************************************************************************/
27
28
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include <gwenhywfar/gwenhywfarapi.h>
34#include <gwenhywfar/misc.h>
35#include "stringlist2_p.h"
36#include "debug.h"
37#include <stdlib.h>
38#include <assert.h>
39#include <string.h>
40#ifdef HAVE_STRINGS_H
41# include <strings.h>
42#endif
43
44
46{
49
54 sl2->listPtr=GWEN_List_new();
55 GWEN_List_SetRefPtrInfo(sl2->listPtr, rpi);
57
58 return sl2;
59}
60
61
62
64{
65 if (sl2) {
66 GWEN_List_free(sl2->listPtr);
68 }
69}
70
71
72
74{
75 GWEN_STRINGLIST2 *nsl2;
76
78 nsl2->listPtr=GWEN_List_dup(sl2->listPtr);
79 nsl2->senseCase=sl2->senseCase;
80
81 return nsl2;
82}
83
84
85
86int GWEN_StringList2_toDb(GWEN_STRINGLIST2 *sl2, GWEN_DB_NODE *db, const char *name)
87{
88 GWEN_DB_DeleteVar(db, name);
89
90 if (sl2) {
92
94 if (it) {
95 const char *s;
96
98 while (s) {
99 int rv;
100
101 rv=GWEN_DB_SetCharValue(db, 0, name, s);
102 if (rv<0) {
103 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
104 return rv;
105 }
106
108 }
110 }
111 }
112
113 return 0;
114}
115
116
117
119{
120 GWEN_STRINGLIST2 *sl2;
121 int i;
122
124 for (i=0; ; i++) {
125 const char *s;
126
127 s=GWEN_DB_GetCharValue(db, name, i, NULL);
128 if (!s)
129 break;
130 GWEN_StringList2_AppendString(sl2, s, 0, m);
131 }
132
133 return sl2;
134}
135
136
137
139{
141
143 if (it) {
144 const char *s;
145
147 while (s) {
148 GWEN_XMLNode_SetCharValue(node, "elem", s);
150 }
152 }
153
154 return 0;
155}
156
157
158
160{
161 GWEN_STRINGLIST2 *sl2;
162 GWEN_XMLNODE *n;
163
165
166
168 while (n) {
169 GWEN_XMLNODE *dn;
170
172 if (dn) {
173 const char *s;
174
176 if (s) {
177 GWEN_StringList2_AppendString(sl2, s, 0, m);
178 }
179 }
181 }
182
183 return sl2;
184}
185
186
187
189{
190 assert(sl2);
191 sl2->senseCase=i;
192}
193
194
195
197 const char *s,
198 int take,
200{
201 GWEN_REFPTR *rp;
202
203 assert(sl2);
204 assert(s);
205
208
210 if (it) {
212 if (take)
213 free((void *)s);
215 return 0;
216 }
219 if (take)
220 free((void *)s);
222 return 0;
223 }
225 }
226 }
227
228 if (take)
229 rp=GWEN_RefPtr_new((void *)s, GWEN_List_GetRefPtrInfo(sl2->listPtr));
230 else
231 rp=GWEN_RefPtr_new(strdup(s), GWEN_List_GetRefPtrInfo(sl2->listPtr));
233 GWEN_List_PushBackRefPtr(sl2->listPtr, rp);
234 return 1;
235}
236
237
238
240 const char *s,
241 int take,
243{
244 GWEN_REFPTR *rp;
245
246 assert(sl2);
247 assert(s);
248
251
253 if (it) {
255 if (take)
256 free((void *)s);
258 return 0;
259 }
262 if (take)
263 free((void *)s);
265 return 0;
266 }
268 }
269 }
270
271 if (take)
272 rp=GWEN_RefPtr_new((void *)s, GWEN_List_GetRefPtrInfo(sl2->listPtr));
273 else
274 rp=GWEN_RefPtr_new(strdup(s), GWEN_List_GetRefPtrInfo(sl2->listPtr));
276 GWEN_List_PushFrontRefPtr(sl2->listPtr, rp);
277 return 1;
278}
279
280
281
283 const char *s)
284{
286
288 if (it) {
289 int lc;
290
292 GWEN_List_Erase(sl2->listPtr, it);
294 if (lc<2)
295 return 1;
296 }
297
298 return 0;
299}
300
301
302
304 const char *s)
305{
307 int gotIt;
308
310 gotIt=0;
311 if (it) {
312 const char *t;
313
315 if (sl2->senseCase) {
316 while (t) {
317 if (strcmp(s, t)) {
318 gotIt=1;
319 break;
320 }
322 }
323 }
324 else {
325 while (t) {
326 if (strcasecmp(s, t)) {
327 gotIt=1;
328 break;
329 }
331 }
332 }
334 }
335
336 return gotIt;
337}
338
339
340
342 const char *s)
343{
345 GWEN_REFPTR *rp;
346
348 if (it) {
350
351 if (sl2->senseCase) {
352 while (rp) {
353 const char *t;
354
355 t=(const char *)GWEN_RefPtr_GetData(rp);
356 assert(t);
357 if (strcmp(s, t)==0)
358 return it;
360 }
361 }
362 else {
363 while (rp) {
364 const char *t;
365
366 t=(const char *)GWEN_RefPtr_GetData(rp);
367 assert(t);
368 if (strcasecmp(s, t)==0)
369 return it;
371 }
372 }
374 }
375
376 return 0;
377}
378
379
380
381const char *GWEN_StringList2_GetStringAt(const GWEN_STRINGLIST2 *sl2, int idx)
382{
384 GWEN_REFPTR *rp;
385
387 if (it) {
389
390 while (rp) {
391 const char *t;
392
393 t=(const char *)GWEN_RefPtr_GetData(rp);
394 assert(t);
395 if (idx--==0) {
397 return t;
398 }
400 }
402 }
403
404 return NULL;
405}
406
407
408
409
410
411
412
413
414
415
416
417
418
424
425
426
432
433
434
440
441
443{
444 assert(li);
445 return (const char *) GWEN_ListIterator_Previous((GWEN_LIST_ITERATOR *)li);
446}
447
448
450{
451 assert(li);
452 return (const char *) GWEN_ListIterator_Next((GWEN_LIST_ITERATOR *)li);
453}
454
455
457{
458 assert(li);
459 return (const char *) GWEN_ListIterator_Data((GWEN_LIST_ITERATOR *)li);
460}
461
462
463
469
470
471
473{
474 assert(li);
476}
477
478
479
481{
482 assert(l);
483 return GWEN_List_GetSize(l->listPtr);
484}
485
486
487
489{
491
493 if (it) {
494 const char *t;
495 int i;
496
498 i=0;
499 while (t) {
500 fprintf(stderr, "String %d: \"%s\" [%d]\n", i, t,
503 }
505 }
506 else {
507 fprintf(stderr, "Empty string list.\n");
508 }
509}
510
511
512
513
514
515
516
517
518
#define NULL
Definition binreloc.c:300
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition db.c:971
int GWEN_DB_DeleteVar(GWEN_DB_NODE *n, const char *path)
Definition db.c:899
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition db.c:997
struct GWEN_DB_NODE GWEN_DB_NODE
Definition db.h:228
#define DBG_INFO(dbg_logger, format,...)
Definition debug.h:181
struct GWEN_LIST_ITERATOR GWEN_LIST_ITERATOR
Definition list.h:72
#define GWEN_LOGDOMAIN
Definition logger.h:35
#define GWEN_FREE_OBJECT(varname)
Definition memory.h:61
#define GWEN_NEW_OBJECT(typ, varname)
Definition memory.h:55
GWEN_REFPTR * GWEN_RefPtr_new(void *dp, GWEN_REFPTR_INFO *rpi)
Definition refptr.c:184
GWEN_REFPTR_INFO * GWEN_RefPtrInfo_new(void)
Definition refptr.c:122
void GWEN_RefPtrInfo_SetFreeFn(GWEN_REFPTR_INFO *rpi, GWEN_REFPTR_INFO_FREE_FN f)
Definition refptr.c:156
void GWEN_RefPtr_AddFlags(GWEN_REFPTR *rp, uint32_t fl)
Definition refptr.c:291
void * GWEN_RefPtr_GetData(const GWEN_REFPTR *rp)
Definition refptr.c:244
void GWEN_RefPtrInfo_free(GWEN_REFPTR_INFO *rpi)
Definition refptr.c:134
struct GWEN_REFPTR_INFO GWEN_REFPTR_INFO
Definition refptr.h:44
#define GWEN_REFPTR_FLAGS_AUTODELETE
Definition refptr.h:37
void(* GWEN_REFPTR_INFO_FREE_FN)(void *dp)
Definition refptr.h:151
struct GWEN_REFPTR GWEN_REFPTR
Definition refptr.h:45
GWEN_LIST * GWEN_List_dup(const GWEN_LIST *l)
void GWEN_List_PushBackRefPtr(GWEN_LIST *l, GWEN_REFPTR *rp)
GWEN_LIST_ITERATOR * GWEN_List_Last(const GWEN_LIST *l)
void GWEN_List_SetRefPtrInfo(GWEN_LIST *l, GWEN_REFPTR_INFO *rpi)
GWEN_LIST * GWEN_List_new(void)
GWEN_LIST_ITERATOR * GWEN_List_First(const GWEN_LIST *l)
GWEN_REFPTR * GWEN_ListIterator_DataRefPtr(GWEN_LIST_ITERATOR *li)
void GWEN_ListIterator_free(GWEN_LIST_ITERATOR *li)
void GWEN_ListIterator_IncLinkCount(GWEN_LIST_ITERATOR *li)
void GWEN_List_free(GWEN_LIST *l)
GWEN_REFPTR * GWEN_ListIterator_NextRefPtr(GWEN_LIST_ITERATOR *li)
void * GWEN_ListIterator_Next(GWEN_LIST_ITERATOR *li)
void GWEN_List_PushFrontRefPtr(GWEN_LIST *l, GWEN_REFPTR *rp)
void GWEN_List_Erase(GWEN_LIST *l, GWEN_LIST_ITERATOR *it)
void * GWEN_ListIterator_Previous(GWEN_LIST_ITERATOR *li)
void * GWEN_ListIterator_Data(GWEN_LIST_ITERATOR *li)
unsigned int GWEN_List_GetSize(const GWEN_LIST *l)
unsigned int GWEN_ListIterator_GetLinkCount(const GWEN_LIST_ITERATOR *li)
GWEN_REFPTR_INFO * GWEN_List_GetRefPtrInfo(const GWEN_LIST *l)
GWEN_STRINGLIST2 * GWEN_StringList2_new(void)
Definition stringlist2.c:45
const char * GWEN_StringList2Iterator_Data(GWEN_STRINGLIST2_ITERATOR *li)
void GWEN_StringList2_Dump(const GWEN_STRINGLIST2 *sl2)
GWEN_STRINGLIST2_ITERATOR * GWEN_StringList2_First(const GWEN_STRINGLIST2 *l)
unsigned int GWEN_StringList2Iterator_GetLinkCount(const GWEN_STRINGLIST2_ITERATOR *li)
void GWEN_StringList2_SetSenseCase(GWEN_STRINGLIST2 *sl2, int i)
const char * GWEN_StringList2Iterator_Previous(GWEN_STRINGLIST2_ITERATOR *li)
GWEN_STRINGLIST2_ITERATOR * GWEN_StringList2_Last(const GWEN_STRINGLIST2 *l)
const char * GWEN_StringList2Iterator_Next(GWEN_STRINGLIST2_ITERATOR *li)
int GWEN_StringList2_toDb(GWEN_STRINGLIST2 *sl2, GWEN_DB_NODE *db, const char *name)
Definition stringlist2.c:86
int GWEN_StringList2_toXml(GWEN_STRINGLIST2 *sl2, GWEN_XMLNODE *node)
int GWEN_StringList2_AppendString(GWEN_STRINGLIST2 *sl2, const char *s, int take, GWEN_STRINGLIST2_INSERTMODE m)
int GWEN_StringList2_HasString(const GWEN_STRINGLIST2 *sl2, const char *s)
const char * GWEN_StringList2_GetStringAt(const GWEN_STRINGLIST2 *sl2, int idx)
int GWEN_StringList2_RemoveString(GWEN_STRINGLIST2 *sl2, const char *s)
GWEN_STRINGLIST2 * GWEN_StringList2_fromXml(GWEN_XMLNODE *node, GWEN_STRINGLIST2_INSERTMODE m)
int GWEN_StringList2_InsertString(GWEN_STRINGLIST2 *sl2, const char *s, int take, GWEN_STRINGLIST2_INSERTMODE m)
GWEN_REFPTR * GWEN_StringList2Iterator_DataRefPtr(GWEN_STRINGLIST2_ITERATOR *li)
GWEN_STRINGLIST2 * GWEN_StringList2_dup(GWEN_STRINGLIST2 *sl2)
Definition stringlist2.c:73
GWEN_STRINGLIST2 * GWEN_StringList2_fromDb(GWEN_DB_NODE *db, const char *name, GWEN_STRINGLIST2_INSERTMODE m)
GWEN_STRINGLIST2_ITERATOR * GWEN_StringList2__GetString(const GWEN_STRINGLIST2 *sl2, const char *s)
void GWEN_StringList2Iterator_free(GWEN_STRINGLIST2_ITERATOR *li)
unsigned int GWEN_StringList2_GetCount(const GWEN_STRINGLIST2 *l)
void GWEN_StringList2_free(GWEN_STRINGLIST2 *sl2)
Definition stringlist2.c:63
struct GWEN_STRINGLIST2 GWEN_STRINGLIST2
Definition stringlist2.h:43
GWEN_LIST_ITERATOR GWEN_STRINGLIST2_ITERATOR
Definition stringlist2.h:44
GWEN_STRINGLIST2_INSERTMODE
Definition stringlist2.h:47
@ GWEN_StringList2_IntertMode_NoDouble
Definition stringlist2.h:49
@ GWEN_StringList2_IntertMode_AlwaysAdd
Definition stringlist2.h:48
@ GWEN_StringList2_IntertMode_Reuse
Definition stringlist2.h:50
GWEN_XMLNODE * GWEN_XMLNode_GetFirstTag(const GWEN_XMLNODE *n)
Definition xml.c:705
GWEN_XMLNODE * GWEN_XMLNode_GetNextTag(const GWEN_XMLNODE *n)
Definition xml.c:712
GWEN_XMLNODE * GWEN_XMLNode_GetFirstData(const GWEN_XMLNODE *n)
Definition xml.c:724
const char * GWEN_XMLNode_GetData(const GWEN_XMLNODE *n)
Definition xml.c:370
void GWEN_XMLNode_SetCharValue(GWEN_XMLNODE *n, const char *name, const char *value)
Definition xml.c:897
struct GWEN__XMLNODE GWEN_XMLNODE
Definition xml.h:156