annotate lwlib/lw_stringlist.c @ 426:652eee8f0c82

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