]> git.saurik.com Git - bison.git/blob - src/struniq.c
(AT_TEST_CPP_GUARD_H): Compile with -I., as Forte 7 C requires it,
[bison.git] / src / struniq.c
1 /* Keeping a unique copy of strings.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "system.h"
22 #include "quotearg.h"
23 #include "error.h"
24 #include "hash.h"
25 #include "struniq.h"
26
27 /*-----------------------.
28 | A struniq hash table. |
29 `-----------------------*/
30
31 /* Initial capacity of struniq hash table. */
32 #define HT_INITIAL_CAPACITY 257
33
34 static struct hash_table *struniqs_table = NULL;
35
36 /*-------------------------------------.
37 | Create the struniq for S if needed. |
38 `-------------------------------------*/
39
40 struniq_t
41 struniq_new (const char *s)
42 {
43 struniq_t res = hash_lookup (struniqs_table, s);
44 if (!res)
45 {
46 /* First insertion in the hash. */
47 res = xstrdup (s);
48 hash_insert (struniqs_table, res);
49 }
50 return res;
51 }
52
53
54 /*---------------------------------.
55 | Return TRUE iff S is a struniq. |
56 `---------------------------------*/
57
58 bool
59 struniq_assert_p (const char *s)
60 {
61 if (!hash_lookup (struniqs_table, s))
62 {
63 error (0, 0, "not a struniq: %s", quotearg (s));
64 return false;
65 }
66 else
67 {
68 return true;
69 }
70 }
71
72
73 /*--------------------.
74 | Print the struniq. |
75 `--------------------*/
76
77 static bool
78 struniq_print (const struniq_t s)
79 {
80 fprintf (stderr, "%s\n", s);
81 return true;
82 }
83
84 \f
85 /*-----------------------.
86 | A struniq hash table. |
87 `-----------------------*/
88
89 static bool
90 hash_compare_struniq_t (const struniq_t m1, const struniq_t m2)
91 {
92 return strcmp (m1, m2) == 0;
93 }
94
95 static unsigned int
96 hash_struniq_t (const struniq_t m, unsigned int tablesize)
97 {
98 return hash_string (m, tablesize);
99 }
100
101 /* A function to apply to each symbol. */
102 typedef bool (*struniq_processor_t) (const struniq_t);
103
104 /*----------------------------.
105 | Create the struniqs table. |
106 `----------------------------*/
107
108 void
109 struniqs_new (void)
110 {
111 struniqs_table = hash_initialize (HT_INITIAL_CAPACITY,
112 NULL,
113 (Hash_hasher) hash_struniq_t,
114 (Hash_comparator) hash_compare_struniq_t,
115 (Hash_data_freer) free);
116 }
117
118
119 /*-------------------------------------.
120 | Perform a task on all the struniqs. |
121 `-------------------------------------*/
122
123 static void
124 struniqs_do (struniq_processor_t processor, void *processor_data)
125 {
126 hash_do_for_each (struniqs_table,
127 (Hash_processor) processor,
128 processor_data);
129 }
130
131
132 /*-----------------.
133 | Print them all. |
134 `-----------------*/
135
136 void
137 struniqs_print (void)
138 {
139 struniqs_do (struniq_print, NULL);
140 }
141
142
143 /*--------------------.
144 | Free the struniqs. |
145 `--------------------*/
146
147 void
148 struniqs_free (void)
149 {
150 hash_free (struniqs_table);
151 }