gwenhywfar
4.3.3
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
void
GWEN_StringList2_SetSenseCase
(
GWEN_STRINGLIST2
*sl2,
int
i){
84
assert(sl2);
85
sl2->senseCase=i;
86
}
87
88
89
90
int
GWEN_StringList2_AppendString
(
GWEN_STRINGLIST2
*sl2,
91
const
char
*s,
92
int
take,
93
GWEN_STRINGLIST2_INSERTMODE
m) {
94
GWEN_REFPTR
*rp;
95
96
assert(sl2);
97
assert(s);
98
99
if
(m!=
GWEN_StringList2_IntertMode_AlwaysAdd
) {
100
GWEN_STRINGLIST2_ITERATOR
*it;
101
102
it=
GWEN_StringList2__GetString
(sl2, s);
103
if
(it) {
104
if
(m==
GWEN_StringList2_IntertMode_NoDouble
) {
105
if
(take)
106
free((
void
*)s);
107
GWEN_StringList2Iterator_free
(it);
108
return
0;
109
}
110
if
(m==
GWEN_StringList2_IntertMode_Reuse
) {
111
GWEN_ListIterator_IncLinkCount
((
GWEN_LIST_ITERATOR
*)it);
112
if
(take)
113
free((
void
*)s);
114
GWEN_StringList2Iterator_free
(it);
115
return
0;
116
}
117
GWEN_StringList2Iterator_free
(it);
118
}
119
}
120
121
if
(take)
122
rp=
GWEN_RefPtr_new
((
void
*)s,
GWEN_List_GetRefPtrInfo
(sl2->listPtr));
123
else
124
rp=
GWEN_RefPtr_new
(strdup(s),
GWEN_List_GetRefPtrInfo
(sl2->listPtr));
125
GWEN_RefPtr_AddFlags
(rp,
GWEN_REFPTR_FLAGS_AUTODELETE
);
126
GWEN_List_PushBackRefPtr
(sl2->listPtr, rp);
127
return
1;
128
}
129
130
131
132
int
GWEN_StringList2_InsertString
(
GWEN_STRINGLIST2
*sl2,
133
const
char
*s,
134
int
take,
135
GWEN_STRINGLIST2_INSERTMODE
m) {
136
GWEN_REFPTR
*rp;
137
138
assert(sl2);
139
assert(s);
140
141
if
(m!=
GWEN_StringList2_IntertMode_AlwaysAdd
) {
142
GWEN_STRINGLIST2_ITERATOR
*it;
143
144
it=
GWEN_StringList2__GetString
(sl2, s);
145
if
(it) {
146
if
(m==
GWEN_StringList2_IntertMode_NoDouble
) {
147
if
(take)
148
free((
void
*)s);
149
GWEN_StringList2Iterator_free
(it);
150
return
0;
151
}
152
if
(m==
GWEN_StringList2_IntertMode_Reuse
) {
153
GWEN_ListIterator_IncLinkCount
((
GWEN_LIST_ITERATOR
*)it);
154
if
(take)
155
free((
void
*)s);
156
GWEN_StringList2Iterator_free
(it);
157
return
0;
158
}
159
GWEN_StringList2Iterator_free
(it);
160
}
161
}
162
163
if
(take)
164
rp=
GWEN_RefPtr_new
((
void
*)s,
GWEN_List_GetRefPtrInfo
(sl2->listPtr));
165
else
166
rp=
GWEN_RefPtr_new
(strdup(s),
GWEN_List_GetRefPtrInfo
(sl2->listPtr));
167
GWEN_RefPtr_AddFlags
(rp,
GWEN_REFPTR_FLAGS_AUTODELETE
);
168
GWEN_List_PushFrontRefPtr
(sl2->listPtr, rp);
169
return
1;
170
}
171
172
173
174
int
GWEN_StringList2_RemoveString
(
GWEN_STRINGLIST2
*sl2,
175
const
char
*s){
176
GWEN_STRINGLIST2_ITERATOR
*it;
177
178
it=
GWEN_StringList2__GetString
(sl2, s);
179
if
(it) {
180
int
lc;
181
182
lc=
GWEN_ListIterator_GetLinkCount
(it);
183
GWEN_List_Erase
(sl2->listPtr, it);
184
GWEN_StringList2Iterator_free
(it);
185
if
(lc<2)
186
return
1;
187
}
188
189
return
0;
190
}
191
192
193
194
int
GWEN_StringList2_HasString
(
const
GWEN_STRINGLIST2
*sl2,
195
const
char
*s){
196
GWEN_STRINGLIST2_ITERATOR
*it;
197
int
gotIt;
198
199
it=
GWEN_StringList2_First
(sl2);
200
gotIt=0;
201
if
(it) {
202
const
char
*t;
203
204
t=
GWEN_StringList2Iterator_Data
(it);
205
if
(sl2->senseCase) {
206
while
(t) {
207
if
(strcmp(s, t)) {
208
gotIt=1;
209
break
;
210
}
211
t=
GWEN_StringList2Iterator_Next
(it);
212
}
213
}
214
else
{
215
while
(t) {
216
if
(strcasecmp(s, t)) {
217
gotIt=1;
218
break
;
219
}
220
t=
GWEN_StringList2Iterator_Next
(it);
221
}
222
}
223
GWEN_StringList2Iterator_free
(it);
224
}
225
226
return
gotIt;
227
}
228
229
230
231
GWEN_STRINGLIST2_ITERATOR
*
232
GWEN_StringList2__GetString
(
const
GWEN_STRINGLIST2
*sl2,
233
const
char
*s){
234
GWEN_STRINGLIST2_ITERATOR
*it;
235
GWEN_REFPTR
*rp;
236
237
it=
GWEN_StringList2_First
(sl2);
238
if
(it) {
239
rp=
GWEN_ListIterator_DataRefPtr
((
GWEN_LIST_ITERATOR
*)it);
240
241
if
(sl2->senseCase) {
242
while
(rp) {
243
const
char
*t;
244
245
t=(
const
char
*)
GWEN_RefPtr_GetData
(rp);
246
assert(t);
247
if
(strcmp(s, t)==0)
248
return
it;
249
rp=
GWEN_ListIterator_NextRefPtr
((
GWEN_LIST_ITERATOR
*)it);
250
}
251
}
252
else
{
253
while
(rp) {
254
const
char
*t;
255
256
t=(
const
char
*)
GWEN_RefPtr_GetData
(rp);
257
assert(t);
258
if
(strcasecmp(s, t)==0)
259
return
it;
260
rp=
GWEN_ListIterator_NextRefPtr
((
GWEN_LIST_ITERATOR
*)it);
261
}
262
}
263
GWEN_StringList2Iterator_free
(it);
264
}
265
266
return
0;
267
}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
GWEN_STRINGLIST2_ITERATOR
*
GWEN_StringList2_First
(
const
GWEN_STRINGLIST2
*l) {
284
assert(l);
285
return
(
GWEN_STRINGLIST2_ITERATOR
*)
GWEN_List_First
(l->listPtr);
286
}
287
288
289
290
GWEN_STRINGLIST2_ITERATOR
*
GWEN_StringList2_Last
(
const
GWEN_STRINGLIST2
*l) {
291
assert(l);
292
return
(
GWEN_STRINGLIST2_ITERATOR
*)
GWEN_List_Last
(l->listPtr);
293
}
294
295
296
297
void
GWEN_StringList2Iterator_free
(
GWEN_STRINGLIST2_ITERATOR
*li) {
298
assert(li);
299
GWEN_ListIterator_free
((
GWEN_LIST_ITERATOR
*)li);
300
}
301
302
303
const
char
*
GWEN_StringList2Iterator_Previous
(
GWEN_STRINGLIST2_ITERATOR
*li) {
304
assert(li);
305
return
(
const
char
*)
GWEN_ListIterator_Previous
((
GWEN_LIST_ITERATOR
*)li);
306
}
307
308
309
const
char
*
GWEN_StringList2Iterator_Next
(
GWEN_STRINGLIST2_ITERATOR
*li) {
310
assert(li);
311
return
(
const
char
*)
GWEN_ListIterator_Next
((
GWEN_LIST_ITERATOR
*)li);
312
}
313
314
315
const
char
*
GWEN_StringList2Iterator_Data
(
GWEN_STRINGLIST2_ITERATOR
*li) {
316
assert(li);
317
return
(
const
char
*)
GWEN_ListIterator_Data
((
GWEN_LIST_ITERATOR
*)li);
318
}
319
320
321
322
GWEN_REFPTR
*
323
GWEN_StringList2Iterator_DataRefPtr
(
GWEN_STRINGLIST2_ITERATOR
*li) {
324
assert(li);
325
return
(
GWEN_REFPTR
*)
GWEN_ListIterator_DataRefPtr
((
GWEN_LIST_ITERATOR
*)li);
326
}
327
328
329
330
unsigned
int
331
GWEN_StringList2Iterator_GetLinkCount
(
const
GWEN_STRINGLIST2_ITERATOR
*li){
332
assert(li);
333
return
GWEN_ListIterator_GetLinkCount
((
const
GWEN_LIST_ITERATOR
*)li);
334
}
335
336
337
338
void
GWEN_StringList2_Dump
(
const
GWEN_STRINGLIST2
*sl2){
339
GWEN_STRINGLIST2_ITERATOR
*it;
340
341
it=
GWEN_StringList2_First
(sl2);
342
if
(it) {
343
const
char
*t;
344
int
i;
345
346
t=
GWEN_StringList2Iterator_Data
(it);
347
i=0;
348
while
(t) {
349
fprintf(stderr,
"String %d: \"%s\" [%d]\n"
, i, t,
350
GWEN_StringList2Iterator_GetLinkCount
(it));
351
t=
GWEN_StringList2Iterator_Next
(it);
352
}
353
GWEN_StringList2Iterator_free
(it);
354
}
355
else
{
356
fprintf(stderr,
"Empty string list.\n"
);
357
}
358
}
359
360
361
362
363
364
365
366
367
Generated on Thu Mar 20 2014 17:06:00 for gwenhywfar by
1.8.1.2