annotate lwcc/strpool.c @ 304:d85d173ba120 ccdev

Checkpoint lwcc development - preprocessor is runnable but nonfunctional The preprocessor is currently runnable but doesn't actually do anything useful. This is just a checkpoint.
author William Astle <lost@l-w.ca>
date Tue, 17 Sep 2013 19:33:41 -0600
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
304
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/strpool.c
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
3
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
5
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
7
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
12
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
17
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
21
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <stdlib.h>
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <string.h>
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
24
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <lw_alloc.h>
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
26 #include <lw_string.h>
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
27
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include "strpool.h"
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
29
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
30 struct strpool *strpool_create(void)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
31 {
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
32 struct strpool *sp;
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
33
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
34 sp = lw_alloc(sizeof(struct strpool));
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
35 sp -> nstrs = 0;
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
36 sp -> strs = NULL;
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
37 return sp;
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
38 }
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
39
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
40 extern void strpool_free(struct strpool *sp)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
41 {
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
42 int i;
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
43
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
44 for (i = 0; i < sp -> nstrs; i++)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
45 lw_free(sp -> strs[i]);
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
46 lw_free(sp -> strs);
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
47 lw_free(sp);
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
48 }
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
49
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
50 char *strpool_strdup(struct strpool *sp, const char *s)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
51 {
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
52 int i;
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
53
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
54 if (!s)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
55 return NULL;
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
56
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
57 /* first do a fast scan for a pointer match */
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
58 for (i = 0; i < sp -> nstrs; i++)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
59 if (sp -> strs[i] == s)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
60 return sp -> strs[i];
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
61
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
62 /* no match - do a slow scan for a string match */
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
63 for (i = 0; i < sp -> nstrs; i++)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
64 if (strcmp(sp -> strs[i], s) == 0)
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
65 return sp -> strs[i];
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
66
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
67 /* no match - create a new string entry */
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
68 sp -> strs = lw_realloc(sp -> strs, sizeof(char *) * (sp -> nstrs + 1));
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
69 sp -> strs[sp -> nstrs] = lw_strdup(s);
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
70 sp -> nstrs++;
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
71 return sp -> strs[sp -> nstrs - 1];
d85d173ba120 Checkpoint lwcc development - preprocessor is runnable but nonfunctional
William Astle <lost@l-w.ca>
parents:
diff changeset
72 }