324
|
1 /*
|
|
2 lw_stringlist.c
|
|
3
|
|
4 Copyright © 2010 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
326
|
24 #include <stdlib.h>
|
|
25
|
324
|
26 #define ___lw_stringlist_c_seen___
|
|
27 #include "lw_stringlist.h"
|
326
|
28 #include "lw_string.h"
|
324
|
29 #include "lw_alloc.h"
|
|
30
|
|
31 lw_stringlist_t lw_stringlist_create(void)
|
|
32 {
|
|
33 return lw_alloc(sizeof(struct lw_stringlist_priv));
|
|
34 }
|
|
35
|
|
36 void lw_stringlist_destroy(lw_stringlist_t S)
|
|
37 {
|
|
38 if (S)
|
|
39 {
|
|
40 int i;
|
|
41 for (i = 0; i < S -> nstrings; i++)
|
|
42 {
|
|
43 lw_free(S -> strings[i]);
|
|
44 }
|
|
45 lw_free(S);
|
|
46 }
|
|
47 }
|
326
|
48
|
|
49 void lw_stringlist_addstring(lw_stringlist_t S, char *str)
|
|
50 {
|
|
51 S -> strings = lw_realloc(S -> strings, sizeof(char *) * (S -> nstrings + 1));
|
|
52 S -> strings[S -> nstrings] = lw_strdup(str);
|
|
53 S -> nstrings++;
|
|
54 }
|
|
55
|
|
56 void lw_stringlist_reset(lw_stringlist_t S)
|
|
57 {
|
|
58 S -> cstring = 0;
|
|
59 }
|
|
60
|
|
61 char *lw_stringlist_current(lw_stringlist_t S)
|
|
62 {
|
|
63 if (S -> cstring >= S -> nstrings)
|
|
64 return NULL;
|
|
65 return S -> strings[S -> cstring];
|
|
66 }
|
|
67
|
|
68 char *lw_stringlist_next(lw_stringlist_t S)
|
|
69 {
|
|
70 S -> cstring++;
|
|
71 return lw_stringlist_current(S);
|
|
72 }
|
|
73
|
|
74 int lw_stringlist_nstrings(lw_stringlist_t S)
|
|
75 {
|
|
76 return S -> nstrings;
|
|
77 }
|
327
|
78
|
|
79 lw_stringlist_t lw_stringlist_copy(lw_stringlist_t S)
|
|
80 {
|
|
81 lw_stringlist_t r;
|
|
82
|
|
83 r = lw_alloc(sizeof(lw_stringlist_t));
|
|
84 r -> nstrings = S -> nstrings;
|
|
85 if (S -> nstrings)
|
|
86 {
|
|
87 int i;
|
|
88
|
|
89 r -> strings = lw_alloc(sizeof(char *) * S -> nstrings);
|
|
90 for (i = 0; i < S -> nstrings; i++)
|
|
91 {
|
|
92 r -> strings[i] = lw_strdup(S -> strings[i]);
|
|
93 }
|
|
94 }
|
|
95 return r;
|
|
96 }
|