annotate lwlib/lw_stringlist.c @ 429:33c5bc04ea67

Fixed unitialized memory in lw_stringlist_create()
author lost@l-w.ca
date Sun, 19 Sep 2010 12:32:52 -0600
parents 80826bf2827b
children
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 {
429
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
33 lw_stringlist_t s;
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
34
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
35
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
36 s = lw_alloc(sizeof(struct lw_stringlist_priv));
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
37 s -> strings = NULL;
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
38 s -> nstrings = 0;
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
39 s -> cstring = 0;
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
40
33c5bc04ea67 Fixed unitialized memory in lw_stringlist_create()
lost@l-w.ca
parents: 327
diff changeset
41 return s;
324
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
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
44 void lw_stringlist_destroy(lw_stringlist_t S)
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
45 {
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
46 if (S)
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
47 {
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
48 int i;
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
49 for (i = 0; i < S -> nstrings; i++)
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
50 {
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
51 lw_free(S -> strings[i]);
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
52 }
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
53 lw_free(S);
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
54 }
be63116281b0 Created lwlib folder and added first bits to it
lost
parents:
diff changeset
55 }
326
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
56
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
57 void lw_stringlist_addstring(lw_stringlist_t S, char *str)
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
58 {
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
59 S -> strings = lw_realloc(S -> strings, sizeof(char *) * (S -> nstrings + 1));
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
60 S -> strings[S -> nstrings] = lw_strdup(str);
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
61 S -> nstrings++;
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
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
64 void lw_stringlist_reset(lw_stringlist_t S)
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
65 {
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
66 S -> cstring = 0;
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
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
69 char *lw_stringlist_current(lw_stringlist_t S)
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
70 {
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
71 if (S -> cstring >= S -> nstrings)
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
72 return NULL;
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
73 return S -> strings[S -> cstring];
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
74 }
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 char *lw_stringlist_next(lw_stringlist_t S)
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
77 {
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
78 S -> cstring++;
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
79 return lw_stringlist_current(S);
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
80 }
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
81
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
82 int lw_stringlist_nstrings(lw_stringlist_t S)
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
83 {
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
84 return S -> nstrings;
2eb058346cad Added string module and expanded stringlist module
lost
parents: 324
diff changeset
85 }
327
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
86
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
87 lw_stringlist_t lw_stringlist_copy(lw_stringlist_t S)
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
88 {
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
89 lw_stringlist_t r;
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
90
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
91 r = lw_alloc(sizeof(lw_stringlist_t));
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
92 r -> nstrings = S -> nstrings;
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
93 if (S -> nstrings)
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
94 {
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
95 int i;
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
96
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
97 r -> strings = lw_alloc(sizeof(char *) * S -> nstrings);
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
98 for (i = 0; i < S -> nstrings; i++)
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
99 {
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
100 r -> strings[i] = lw_strdup(S -> strings[i]);
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
101 }
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
102 }
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
103 return r;
80826bf2827b Added copier for stringlist module
lost
parents: 326
diff changeset
104 }