comparison lwlib/lw_stringlist.c @ 0:2c24602be78f

Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author lost@l-w.ca
date Wed, 19 Jan 2011 22:27:17 -0700
parents
children a1a88a8ddc98
comparison
equal deleted inserted replaced
-1:000000000000 0:2c24602be78f
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 <stdlib.h>
23
24 #define ___lw_stringlist_c_seen___
25 #include "lw_stringlist.h"
26 #include "lw_string.h"
27 #include "lw_alloc.h"
28
29 lw_stringlist_t lw_stringlist_create(void)
30 {
31 lw_stringlist_t s;
32
33
34 s = lw_alloc(sizeof(struct lw_stringlist_priv));
35 s -> strings = NULL;
36 s -> nstrings = 0;
37 s -> cstring = 0;
38
39 return s;
40 }
41
42 void lw_stringlist_destroy(lw_stringlist_t S)
43 {
44 if (S)
45 {
46 int i;
47 for (i = 0; i < S -> nstrings; i++)
48 {
49 lw_free(S -> strings[i]);
50 }
51 lw_free(S);
52 }
53 }
54
55 void lw_stringlist_addstring(lw_stringlist_t S, char *str)
56 {
57 S -> strings = lw_realloc(S -> strings, sizeof(char *) * (S -> nstrings + 1));
58 S -> strings[S -> nstrings] = lw_strdup(str);
59 S -> nstrings++;
60 }
61
62 void lw_stringlist_reset(lw_stringlist_t S)
63 {
64 S -> cstring = 0;
65 }
66
67 char *lw_stringlist_current(lw_stringlist_t S)
68 {
69 if (S -> cstring >= S -> nstrings)
70 return NULL;
71 return S -> strings[S -> cstring];
72 }
73
74 char *lw_stringlist_next(lw_stringlist_t S)
75 {
76 S -> cstring++;
77 return lw_stringlist_current(S);
78 }
79
80 int lw_stringlist_nstrings(lw_stringlist_t S)
81 {
82 return S -> nstrings;
83 }
84
85 lw_stringlist_t lw_stringlist_copy(lw_stringlist_t S)
86 {
87 lw_stringlist_t r;
88
89 r = lw_alloc(sizeof(lw_stringlist_t));
90 r -> nstrings = S -> nstrings;
91 if (S -> nstrings)
92 {
93 int i;
94
95 r -> strings = lw_alloc(sizeof(char *) * S -> nstrings);
96 for (i = 0; i < S -> nstrings; i++)
97 {
98 r -> strings[i] = lw_strdup(S -> strings[i]);
99 }
100 }
101 return r;
102 }