gwenhywfar  4.8.0beta
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