gwenhywfar
4.8.0beta
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
src
base
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
45
GWEN_STRINGLIST2
*
GWEN_StringList2_new
(
void
){
46
GWEN_STRINGLIST2
*sl2;
47
GWEN_REFPTR_INFO
*rpi;
48
49
GWEN_NEW_OBJECT
(
GWEN_STRINGLIST2
, sl2);
50
rpi=
GWEN_RefPtrInfo_new
();
51
GWEN_RefPtrInfo_SetFreeFn
(rpi,
52
(
GWEN_REFPTR_INFO_FREE_FN
)free);
53
sl2->listPtr=
GWEN_List_new
();
54
GWEN_List_SetRefPtrInfo
(sl2->listPtr, rpi);
55
GWEN_RefPtrInfo_free
(rpi);
56
57
return
sl2;
58
}
59
60
61
62
void
GWEN_StringList2_free
(
GWEN_STRINGLIST2
*sl2){
63
if
(sl2) {
64
GWEN_List_free
(sl2->listPtr);
65
GWEN_FREE_OBJECT
(sl2);
66
}
67
}
68
69
70
71
GWEN_STRINGLIST2
*
GWEN_StringList2_dup
(
GWEN_STRINGLIST2
*sl2){
72
GWEN_STRINGLIST2
*nsl2;
73
74
GWEN_NEW_OBJECT
(
GWEN_STRINGLIST2
, nsl2);
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) {
87
GWEN_STRINGLIST2_ITERATOR
*it;
88
89
it=
GWEN_StringList2_First
(sl2);
90
if
(it) {
91
const
char
*s;
92
93
s=
GWEN_StringList2Iterator_Data
(it);
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
103
s=
GWEN_StringList2Iterator_Next
(it);
104
}
105
GWEN_StringList2Iterator_free
(it);
106
}
107
}
108
109
return
0;
110
}
111
112
113
114
GWEN_STRINGLIST2
*
GWEN_StringList2_fromDb
(
GWEN_DB_NODE
*db,
const
char
*name,
GWEN_STRINGLIST2_INSERTMODE
m) {
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
133
int
GWEN_StringList2_toXml
(
GWEN_STRINGLIST2
*sl2,
GWEN_XMLNODE
*node) {
134
GWEN_STRINGLIST2_ITERATOR
*it;
135
136
it=
GWEN_StringList2_First
(sl2);
137
if
(it) {
138
const
char
*s;
139
140
s=
GWEN_StringList2Iterator_Data
(it);
141
while
(s) {
142
GWEN_XMLNode_SetCharValue
(node,
"elem"
, s);
143
s=
GWEN_StringList2Iterator_Next
(it);
144
}
145
GWEN_StringList2Iterator_free
(it);
146
}
147
148
return
0;
149
}
150
151
152
153
GWEN_STRINGLIST2
*
GWEN_StringList2_fromXml
(
GWEN_XMLNODE
*node,
GWEN_STRINGLIST2_INSERTMODE
m) {
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
164
dn=
GWEN_XMLNode_GetFirstData
(n);
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
}
173
n=
GWEN_XMLNode_GetNextTag
(n);
174
}
175
176
return
sl2;
177
}
178
179
180
181
void
GWEN_StringList2_SetSenseCase
(
GWEN_STRINGLIST2
*sl2,
int
i){
182
assert(sl2);
183
sl2->senseCase=i;
184
}
185
186
187
188
int
GWEN_StringList2_AppendString
(
GWEN_STRINGLIST2
*sl2,
189
const
char
*s,
190
int
take,
191
GWEN_STRINGLIST2_INSERTMODE
m) {
192
GWEN_REFPTR
*rp;
193
194
assert(sl2);
195
assert(s);
196
197
if
(m!=
GWEN_StringList2_IntertMode_AlwaysAdd
) {
198
GWEN_STRINGLIST2_ITERATOR
*it;
199
200
it=
GWEN_StringList2__GetString
(sl2, s);
201
if
(it) {
202
if
(m==
GWEN_StringList2_IntertMode_NoDouble
) {
203
if
(take)
204
free((
void
*)s);
205
GWEN_StringList2Iterator_free
(it);
206
return
0;
207
}
208
if
(m==
GWEN_StringList2_IntertMode_Reuse
) {
209
GWEN_ListIterator_IncLinkCount
((
GWEN_LIST_ITERATOR
*)it);
210
if
(take)
211
free((
void
*)s);
212
GWEN_StringList2Iterator_free
(it);
213
return
0;
214
}
215
GWEN_StringList2Iterator_free
(it);
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));
223
GWEN_RefPtr_AddFlags
(rp,
GWEN_REFPTR_FLAGS_AUTODELETE
);
224
GWEN_List_PushBackRefPtr
(sl2->listPtr, rp);
225
return
1;
226
}
227
228
229
230
int
GWEN_StringList2_InsertString
(
GWEN_STRINGLIST2
*sl2,
231
const
char
*s,
232
int
take,
233
GWEN_STRINGLIST2_INSERTMODE
m) {
234
GWEN_REFPTR
*rp;
235
236
assert(sl2);
237
assert(s);
238
239
if
(m!=
GWEN_StringList2_IntertMode_AlwaysAdd
) {
240
GWEN_STRINGLIST2_ITERATOR
*it;
241
242
it=
GWEN_StringList2__GetString
(sl2, s);
243
if
(it) {
244
if
(m==
GWEN_StringList2_IntertMode_NoDouble
) {
245
if
(take)
246
free((
void
*)s);
247
GWEN_StringList2Iterator_free
(it);
248
return
0;
249
}
250
if
(m==
GWEN_StringList2_IntertMode_Reuse
) {
251
GWEN_ListIterator_IncLinkCount
((
GWEN_LIST_ITERATOR
*)it);
252
if
(take)
253
free((
void
*)s);
254
GWEN_StringList2Iterator_free
(it);
255
return
0;
256
}
257
GWEN_StringList2Iterator_free
(it);
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));
265
GWEN_RefPtr_AddFlags
(rp,
GWEN_REFPTR_FLAGS_AUTODELETE
);
266
GWEN_List_PushFrontRefPtr
(sl2->listPtr, rp);
267
return
1;
268
}
269
270
271
272
int
GWEN_StringList2_RemoveString
(
GWEN_STRINGLIST2
*sl2,
273
const
char
*s){
274
GWEN_STRINGLIST2_ITERATOR
*it;
275
276
it=
GWEN_StringList2__GetString
(sl2, s);
277
if
(it) {
278
int
lc;
279
280
lc=
GWEN_ListIterator_GetLinkCount
(it);
281
GWEN_List_Erase
(sl2->listPtr, it);
282
GWEN_StringList2Iterator_free
(it);
283
if
(lc<2)
284
return
1;
285
}
286
287
return
0;
288
}
289
290
291
292
int
GWEN_StringList2_HasString
(
const
GWEN_STRINGLIST2
*sl2,
293
const
char
*s){
294
GWEN_STRINGLIST2_ITERATOR
*it;
295
int
gotIt;
296
297
it=
GWEN_StringList2_First
(sl2);
298
gotIt=0;
299
if
(it) {
300
const
char
*t;
301
302
t=
GWEN_StringList2Iterator_Data
(it);
303
if
(sl2->senseCase) {
304
while
(t) {
305
if
(strcmp(s, t)) {
306
gotIt=1;
307
break
;
308
}
309
t=
GWEN_StringList2Iterator_Next
(it);
310
}
311
}
312
else
{
313
while
(t) {
314
if
(strcasecmp(s, t)) {
315
gotIt=1;
316
break
;
317
}
318
t=
GWEN_StringList2Iterator_Next
(it);
319
}
320
}
321
GWEN_StringList2Iterator_free
(it);
322
}
323
324
return
gotIt;
325
}
326
327
328
329
GWEN_STRINGLIST2_ITERATOR
*
330
GWEN_StringList2__GetString
(
const
GWEN_STRINGLIST2
*sl2,
331
const
char
*s){
332
GWEN_STRINGLIST2_ITERATOR
*it;
333
GWEN_REFPTR
*rp;
334
335
it=
GWEN_StringList2_First
(sl2);
336
if
(it) {
337
rp=
GWEN_ListIterator_DataRefPtr
((
GWEN_LIST_ITERATOR
*)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;
347
rp=
GWEN_ListIterator_NextRefPtr
((
GWEN_LIST_ITERATOR
*)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;
358
rp=
GWEN_ListIterator_NextRefPtr
((
GWEN_LIST_ITERATOR
*)it);
359
}
360
}
361
GWEN_StringList2Iterator_free
(it);
362
}
363
364
return
0;
365
}
366
367
368
369
const
char
*
GWEN_StringList2_GetStringAt
(
const
GWEN_STRINGLIST2
*sl2,
int
idx) {
370
GWEN_STRINGLIST2_ITERATOR
*it;
371
GWEN_REFPTR
*rp;
372
373
it=
GWEN_StringList2_First
(sl2);
374
if
(it) {
375
rp=
GWEN_ListIterator_DataRefPtr
((
GWEN_LIST_ITERATOR
*)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) {
383
GWEN_StringList2Iterator_free
(it);
384
return
t;
385
}
386
rp=
GWEN_ListIterator_NextRefPtr
((
GWEN_LIST_ITERATOR
*)it);
387
}
388
GWEN_StringList2Iterator_free
(it);
389
}
390
391
return
NULL
;
392
}
393
394
395
396
397
398
399
400
401
402
403
404
405
406
GWEN_STRINGLIST2_ITERATOR
*
GWEN_StringList2_First
(
const
GWEN_STRINGLIST2
*l) {
407
assert(l);
408
return
(
GWEN_STRINGLIST2_ITERATOR
*)
GWEN_List_First
(l->listPtr);
409
}
410
411
412
413
GWEN_STRINGLIST2_ITERATOR
*
GWEN_StringList2_Last
(
const
GWEN_STRINGLIST2
*l) {
414
assert(l);
415
return
(
GWEN_STRINGLIST2_ITERATOR
*)
GWEN_List_Last
(l->listPtr);
416
}
417
418
419
420
void
GWEN_StringList2Iterator_free
(
GWEN_STRINGLIST2_ITERATOR
*li) {
421
assert(li);
422
GWEN_ListIterator_free
((
GWEN_LIST_ITERATOR
*)li);
423
}
424
425
426
const
char
*
GWEN_StringList2Iterator_Previous
(
GWEN_STRINGLIST2_ITERATOR
*li) {
427
assert(li);
428
return
(
const
char
*)
GWEN_ListIterator_Previous
((
GWEN_LIST_ITERATOR
*)li);
429
}
430
431
432
const
char
*
GWEN_StringList2Iterator_Next
(
GWEN_STRINGLIST2_ITERATOR
*li) {
433
assert(li);
434
return
(
const
char
*)
GWEN_ListIterator_Next
((
GWEN_LIST_ITERATOR
*)li);
435
}
436
437
438
const
char
*
GWEN_StringList2Iterator_Data
(
GWEN_STRINGLIST2_ITERATOR
*li) {
439
assert(li);
440
return
(
const
char
*)
GWEN_ListIterator_Data
((
GWEN_LIST_ITERATOR
*)li);
441
}
442
443
444
445
GWEN_REFPTR
*
446
GWEN_StringList2Iterator_DataRefPtr
(
GWEN_STRINGLIST2_ITERATOR
*li) {
447
assert(li);
448
return
(
GWEN_REFPTR
*)
GWEN_ListIterator_DataRefPtr
((
GWEN_LIST_ITERATOR
*)li);
449
}
450
451
452
453
unsigned
int
454
GWEN_StringList2Iterator_GetLinkCount
(
const
GWEN_STRINGLIST2_ITERATOR
*li){
455
assert(li);
456
return
GWEN_ListIterator_GetLinkCount
((
const
GWEN_LIST_ITERATOR
*)li);
457
}
458
459
460
461
unsigned
int
GWEN_StringList2_GetCount
(
const
GWEN_STRINGLIST2
*l) {
462
assert(l);
463
return
GWEN_List_GetSize
(l->listPtr);
464
}
465
466
467
468
void
GWEN_StringList2_Dump
(
const
GWEN_STRINGLIST2
*sl2){
469
GWEN_STRINGLIST2_ITERATOR
*it;
470
471
it=
GWEN_StringList2_First
(sl2);
472
if
(it) {
473
const
char
*t;
474
int
i;
475
476
t=
GWEN_StringList2Iterator_Data
(it);
477
i=0;
478
while
(t) {
479
fprintf(stderr,
"String %d: \"%s\" [%d]\n"
, i, t,
480
GWEN_StringList2Iterator_GetLinkCount
(it));
481
t=
GWEN_StringList2Iterator_Next
(it);
482
}
483
GWEN_StringList2Iterator_free
(it);
484
}
485
else
{
486
fprintf(stderr,
"Empty string list.\n"
);
487
}
488
}
489
490
491
492
493
494
495
496
497
Generated on Tue Nov 12 2013 10:50:21 for gwenhywfar by
1.8.1.2