gwenhywfar  4.99.8beta
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  GWEN_STRINGLIST2 *sl2;
47  GWEN_REFPTR_INFO *rpi;
48 
50  rpi=GWEN_RefPtrInfo_new();
53  sl2->listPtr=GWEN_List_new();
54  GWEN_List_SetRefPtrInfo(sl2->listPtr, rpi);
56 
57  return sl2;
58 }
59 
60 
61 
63  if (sl2) {
64  GWEN_List_free(sl2->listPtr);
65  GWEN_FREE_OBJECT(sl2);
66  }
67 }
68 
69 
70 
72  GWEN_STRINGLIST2 *nsl2;
73 
75  nsl2->listPtr=GWEN_List_dup(sl2->listPtr);
76  nsl2->senseCase=sl2->senseCase;
77 
78  return nsl2;
79 }
80 
81 
82 
83 int GWEN_StringList2_toDb(GWEN_STRINGLIST2 *sl2, GWEN_DB_NODE *db, const char *name) {
84  GWEN_DB_DeleteVar(db, name);
85 
86  if (sl2) {
88 
89  it=GWEN_StringList2_First(sl2);
90  if (it) {
91  const char *s;
92 
94  while(s) {
95  int rv;
96 
97  rv=GWEN_DB_SetCharValue(db, 0, name, s);
98  if (rv<0) {
99  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
100  return rv;
101  }
102 
104  }
106  }
107  }
108 
109  return 0;
110 }
111 
112 
113 
115  GWEN_STRINGLIST2 *sl2;
116  int i;
117 
118  sl2=GWEN_StringList2_new();
119  for (i=0; ; i++) {
120  const char *s;
121 
122  s=GWEN_DB_GetCharValue(db, name, i, NULL);
123  if (!s)
124  break;
125  GWEN_StringList2_AppendString(sl2, s, 0, m);
126  }
127 
128  return sl2;
129 }
130 
131 
132 
135 
136  it=GWEN_StringList2_First(sl2);
137  if (it) {
138  const char *s;
139 
141  while(s) {
142  GWEN_XMLNode_SetCharValue(node, "elem", s);
144  }
146  }
147 
148  return 0;
149 }
150 
151 
152 
154  GWEN_STRINGLIST2 *sl2;
155  GWEN_XMLNODE *n;
156 
157  sl2=GWEN_StringList2_new();
158 
159 
160  n=GWEN_XMLNode_GetFirstTag(node);
161  while(n) {
162  GWEN_XMLNODE *dn;
163 
165  if (dn) {
166  const char *s;
167 
168  s=GWEN_XMLNode_GetData(dn);
169  if (s) {
170  GWEN_StringList2_AppendString(sl2, s, 0, m);
171  }
172  }
174  }
175 
176  return sl2;
177 }
178 
179 
180 
182  assert(sl2);
183  sl2->senseCase=i;
184 }
185 
186 
187 
189  const char *s,
190  int take,
192  GWEN_REFPTR *rp;
193 
194  assert(sl2);
195  assert(s);
196 
199 
200  it=GWEN_StringList2__GetString(sl2, s);
201  if (it) {
203  if (take)
204  free((void*)s);
206  return 0;
207  }
210  if (take)
211  free((void*)s);
213  return 0;
214  }
216  }
217  }
218 
219  if (take)
220  rp=GWEN_RefPtr_new((void*)s, GWEN_List_GetRefPtrInfo(sl2->listPtr));
221  else
222  rp=GWEN_RefPtr_new(strdup(s), GWEN_List_GetRefPtrInfo(sl2->listPtr));
224  GWEN_List_PushBackRefPtr(sl2->listPtr, rp);
225  return 1;
226 }
227 
228 
229 
231  const char *s,
232  int take,
234  GWEN_REFPTR *rp;
235 
236  assert(sl2);
237  assert(s);
238 
241 
242  it=GWEN_StringList2__GetString(sl2, s);
243  if (it) {
245  if (take)
246  free((void*)s);
248  return 0;
249  }
252  if (take)
253  free((void*)s);
255  return 0;
256  }
258  }
259  }
260 
261  if (take)
262  rp=GWEN_RefPtr_new((void*)s, GWEN_List_GetRefPtrInfo(sl2->listPtr));
263  else
264  rp=GWEN_RefPtr_new(strdup(s), GWEN_List_GetRefPtrInfo(sl2->listPtr));
266  GWEN_List_PushFrontRefPtr(sl2->listPtr, rp);
267  return 1;
268 }
269 
270 
271 
273  const char *s) {
275 
276  it=GWEN_StringList2__GetString(sl2, s);
277  if (it) {
278  int lc;
279 
281  GWEN_List_Erase(sl2->listPtr, it);
283  if (lc<2)
284  return 1;
285  }
286 
287  return 0;
288 }
289 
290 
291 
293  const char *s) {
295  int gotIt;
296 
297  it=GWEN_StringList2_First(sl2);
298  gotIt=0;
299  if (it) {
300  const char *t;
301 
303  if (sl2->senseCase) {
304  while(t) {
305  if (strcmp(s, t)) {
306  gotIt=1;
307  break;
308  }
310  }
311  }
312  else {
313  while(t) {
314  if (strcasecmp(s, t)) {
315  gotIt=1;
316  break;
317  }
319  }
320  }
322  }
323 
324  return gotIt;
325 }
326 
327 
328 
331  const char *s) {
333  GWEN_REFPTR *rp;
334 
335  it=GWEN_StringList2_First(sl2);
336  if (it) {
338 
339  if (sl2->senseCase) {
340  while(rp) {
341  const char *t;
342 
343  t=(const char*)GWEN_RefPtr_GetData(rp);
344  assert(t);
345  if (strcmp(s, t)==0)
346  return it;
348  }
349  }
350  else {
351  while(rp) {
352  const char *t;
353 
354  t=(const char*)GWEN_RefPtr_GetData(rp);
355  assert(t);
356  if (strcasecmp(s, t)==0)
357  return it;
359  }
360  }
362  }
363 
364  return 0;
365 }
366 
367 
368 
369 const char *GWEN_StringList2_GetStringAt(const GWEN_STRINGLIST2 *sl2, int idx) {
371  GWEN_REFPTR *rp;
372 
373  it=GWEN_StringList2_First(sl2);
374  if (it) {
376 
377  while(rp) {
378  const char *t;
379 
380  t=(const char*)GWEN_RefPtr_GetData(rp);
381  assert(t);
382  if (idx--==0) {
384  return t;
385  }
387  }
389  }
390 
391  return NULL;
392 }
393 
394 
395 
396 
397 
398 
399 
400 
401 
402 
403 
404 
405 
407  assert(l);
408  return (GWEN_STRINGLIST2_ITERATOR*) GWEN_List_First(l->listPtr);
409 }
410 
411 
412 
414  assert(l);
415  return (GWEN_STRINGLIST2_ITERATOR*) GWEN_List_Last(l->listPtr);
416 }
417 
418 
419 
421  assert(li);
423 }
424 
425 
427  assert(li);
428  return (const char*) GWEN_ListIterator_Previous((GWEN_LIST_ITERATOR*)li);
429 }
430 
431 
433  assert(li);
434  return (const char*) GWEN_ListIterator_Next((GWEN_LIST_ITERATOR*)li);
435 }
436 
437 
439  assert(li);
440  return (const char*) GWEN_ListIterator_Data((GWEN_LIST_ITERATOR*)li);
441 }
442 
443 
444 
447  assert(li);
449 }
450 
451 
452 
453 unsigned int
455  assert(li);
457 }
458 
459 
460 
462  assert(l);
463  return GWEN_List_GetSize(l->listPtr);
464 }
465 
466 
467 
470 
471  it=GWEN_StringList2_First(sl2);
472  if (it) {
473  const char *t;
474  int i;
475 
477  i=0;
478  while(t) {
479  fprintf(stderr, "String %d: \"%s\" [%d]\n", i, t,
482  }
484  }
485  else {
486  fprintf(stderr, "Empty string list.\n");
487  }
488 }
489 
490 
491 
492 
493 
494 
495 
496 
497 
GWEN_REFPTR_INFO * GWEN_List_GetRefPtrInfo(const GWEN_LIST *l)
int GWEN_StringList2_RemoveString(GWEN_STRINGLIST2 *sl2, const char *s)
Definition: stringlist2.c:272
void GWEN_List_PushBackRefPtr(GWEN_LIST *l, GWEN_REFPTR *rp)
int GWEN_StringList2_toDb(GWEN_STRINGLIST2 *sl2, GWEN_DB_NODE *db, const char *name)
Definition: stringlist2.c:83
GWEN_STRINGLIST2_INSERTMODE
Definition: stringlist2.h:47
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
const char * GWEN_StringList2Iterator_Previous(GWEN_STRINGLIST2_ITERATOR *li)
Definition: stringlist2.c:426
struct GWEN_LIST_ITERATOR GWEN_LIST_ITERATOR
Definition: list.h:72
GWEN_REFPTR * GWEN_ListIterator_DataRefPtr(GWEN_LIST_ITERATOR *li)
void GWEN_List_Erase(GWEN_LIST *l, GWEN_LIST_ITERATOR *it)
GWEN_STRINGLIST2 * GWEN_StringList2_dup(GWEN_STRINGLIST2 *sl2)
Definition: stringlist2.c:71
GWEN_STRINGLIST2_ITERATOR * GWEN_StringList2__GetString(const GWEN_STRINGLIST2 *sl2, const char *s)
Definition: stringlist2.c:330
GWEN_LIST_ITERATOR * GWEN_List_Last(const GWEN_LIST *l)
GWEN_XMLNODE * GWEN_XMLNode_GetFirstData(const GWEN_XMLNODE *n)
Definition: xml.c:646
void GWEN_RefPtrInfo_free(GWEN_REFPTR_INFO *rpi)
Definition: refptr.c:126
GWEN_LIST_ITERATOR * GWEN_List_First(const GWEN_LIST *l)
void GWEN_List_SetRefPtrInfo(GWEN_LIST *l, GWEN_REFPTR_INFO *rpi)
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:92
#define NULL
Definition: binreloc.c:290
GWEN_STRINGLIST2_ITERATOR * GWEN_StringList2_Last(const GWEN_STRINGLIST2 *l)
Definition: stringlist2.c:413
int GWEN_StringList2_toXml(GWEN_STRINGLIST2 *sl2, GWEN_XMLNODE *node)
Definition: stringlist2.c:133
int GWEN_StringList2_InsertString(GWEN_STRINGLIST2 *sl2, const char *s, int take, GWEN_STRINGLIST2_INSERTMODE m)
Definition: stringlist2.c:230
void GWEN_XMLNode_SetCharValue(GWEN_XMLNODE *n, const char *name, const char *value)
Definition: xml.c:812
#define GWEN_LOGDOMAIN
Definition: logger.h:35
GWEN_STRINGLIST2 * GWEN_StringList2_fromXml(GWEN_XMLNODE *node, GWEN_STRINGLIST2_INSERTMODE m)
Definition: stringlist2.c:153
void(* GWEN_REFPTR_INFO_FREE_FN)(void *dp)
Definition: refptr.h:151
void * GWEN_ListIterator_Previous(GWEN_LIST_ITERATOR *li)
const char * GWEN_StringList2Iterator_Data(GWEN_STRINGLIST2_ITERATOR *li)
Definition: stringlist2.c:438
void GWEN_RefPtrInfo_SetFreeFn(GWEN_REFPTR_INFO *rpi, GWEN_REFPTR_INFO_FREE_FN f)
Definition: refptr.c:146
struct GWEN_REFPTR GWEN_REFPTR
Definition: refptr.h:45
GWEN_REFPTR * GWEN_RefPtr_new(void *dp, GWEN_REFPTR_INFO *rpi)
Definition: refptr.c:172
GWEN_LIST * GWEN_List_new(void)
void GWEN_ListIterator_IncLinkCount(GWEN_LIST_ITERATOR *li)
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:86
void GWEN_ListIterator_free(GWEN_LIST_ITERATOR *li)
const char * GWEN_StringList2Iterator_Next(GWEN_STRINGLIST2_ITERATOR *li)
Definition: stringlist2.c:432
unsigned int GWEN_StringList2_GetCount(const GWEN_STRINGLIST2 *l)
Definition: stringlist2.c:461
GWEN_LIST * GWEN_List_dup(const GWEN_LIST *l)
void GWEN_StringList2_free(GWEN_STRINGLIST2 *sl2)
Definition: stringlist2.c:62
GWEN_XMLNODE * GWEN_XMLNode_GetNextTag(const GWEN_XMLNODE *n)
Definition: xml.c:635
struct GWEN_REFPTR_INFO GWEN_REFPTR_INFO
Definition: refptr.h:44
void GWEN_List_free(GWEN_LIST *l)
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition: db.c:897
void GWEN_RefPtr_AddFlags(GWEN_REFPTR *rp, uint32_t fl)
Definition: refptr.c:271
int GWEN_DB_DeleteVar(GWEN_DB_NODE *n, const char *path)
Definition: db.c:828
void GWEN_StringList2_SetSenseCase(GWEN_STRINGLIST2 *sl2, int i)
Definition: stringlist2.c:181
GWEN_REFPTR * GWEN_StringList2Iterator_DataRefPtr(GWEN_STRINGLIST2_ITERATOR *li)
Definition: stringlist2.c:446
GWEN_XMLNODE * GWEN_XMLNode_GetFirstTag(const GWEN_XMLNODE *n)
Definition: xml.c:629
const char * GWEN_XMLNode_GetData(const GWEN_XMLNODE *n)
Definition: xml.c:351
struct GWEN_STRINGLIST2 GWEN_STRINGLIST2
Definition: stringlist2.h:43
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition: db.c:922
void * GWEN_ListIterator_Data(GWEN_LIST_ITERATOR *li)
GWEN_STRINGLIST2 * GWEN_StringList2_new(void)
Definition: stringlist2.c:45
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:164
unsigned int GWEN_ListIterator_GetLinkCount(const GWEN_LIST_ITERATOR *li)
unsigned int GWEN_List_GetSize(const GWEN_LIST *l)
void GWEN_StringList2Iterator_free(GWEN_STRINGLIST2_ITERATOR *li)
Definition: stringlist2.c:420
void * GWEN_RefPtr_GetData(const GWEN_REFPTR *rp)
Definition: refptr.c:228
int GWEN_StringList2_HasString(const GWEN_STRINGLIST2 *sl2, const char *s)
Definition: stringlist2.c:292
void GWEN_List_PushFrontRefPtr(GWEN_LIST *l, GWEN_REFPTR *rp)
const char * GWEN_StringList2_GetStringAt(const GWEN_STRINGLIST2 *sl2, int idx)
Definition: stringlist2.c:369
#define GWEN_REFPTR_FLAGS_AUTODELETE
Definition: refptr.h:37
unsigned int GWEN_StringList2Iterator_GetLinkCount(const GWEN_STRINGLIST2_ITERATOR *li)
Definition: stringlist2.c:454
void GWEN_StringList2_Dump(const GWEN_STRINGLIST2 *sl2)
Definition: stringlist2.c:468
GWEN_STRINGLIST2_ITERATOR * GWEN_StringList2_First(const GWEN_STRINGLIST2 *l)
Definition: stringlist2.c:406
GWEN_REFPTR * GWEN_ListIterator_NextRefPtr(GWEN_LIST_ITERATOR *li)
GWEN_REFPTR_INFO * GWEN_RefPtrInfo_new(void)
Definition: refptr.c:115
int GWEN_StringList2_AppendString(GWEN_STRINGLIST2 *sl2, const char *s, int take, GWEN_STRINGLIST2_INSERTMODE m)
Definition: stringlist2.c:188
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:148
void * GWEN_ListIterator_Next(GWEN_LIST_ITERATOR *li)
GWEN_LIST_ITERATOR GWEN_STRINGLIST2_ITERATOR
Definition: stringlist2.h:44
GWEN_STRINGLIST2 * GWEN_StringList2_fromDb(GWEN_DB_NODE *db, const char *name, GWEN_STRINGLIST2_INSERTMODE m)
Definition: stringlist2.c:114