1 | /* This may look like C code, but it is really -*- C++ -*- */ |
2 | |
3 | /* Handles parsing the Options provided to the user. |
4 | |
5 | Copyright (C) 1989-1998, 2000, 2002-2004, 2011 Free Software Foundation, Inc. |
6 | Written by Douglas C. Schmidt <schmidt@ics.uci.edu> |
7 | and Bruno Haible <bruno@clisp.org>. |
8 | |
9 | This file is part of GNU GPERF. |
10 | |
11 | This program is free software: you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published by |
13 | the Free Software Foundation; either version 3 of the License, or |
14 | (at your option) any later version. |
15 | |
16 | This program is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | GNU General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU General Public License |
22 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
23 | |
24 | /* This module provides a uniform interface to the various options available |
25 | to a user of the gperf hash function generator. */ |
26 | |
27 | #ifndef options_h |
28 | #define options_h 1 |
29 | |
30 | #include <stdio.h> |
31 | #include "positions.h" |
32 | |
33 | /* Enumeration of the possible boolean options. */ |
34 | |
35 | enum Option_Type |
36 | { |
37 | /* --- Input file interpretation --- */ |
38 | |
39 | /* Handle user-defined type structured keyword input. */ |
40 | TYPE = 1 << 0, |
41 | |
42 | /* Ignore case of ASCII characters. */ |
43 | UPPERLOWER = 1 << 1, |
44 | |
45 | /* --- Language for the output code --- */ |
46 | |
47 | /* Generate K&R C code: no prototypes, no const. */ |
48 | KRC = 1 << 2, |
49 | |
50 | /* Generate C code: no prototypes, but const (user can #define it away). */ |
51 | C = 1 << 3, |
52 | |
53 | /* Generate ISO/ANSI C code: prototypes and const, but no class. */ |
54 | ANSIC = 1 << 4, |
55 | |
56 | /* Generate C++ code: prototypes, const, class, inline, enum. */ |
57 | CPLUSPLUS = 1 << 5, |
58 | |
59 | /* --- Details in the output code --- */ |
60 | |
61 | /* Assume 7-bit, not 8-bit, characters. */ |
62 | SEVENBIT = 1 << 6, |
63 | |
64 | /* Generate a length table for string comparison. */ |
65 | LENTABLE = 1 << 7, |
66 | |
67 | /* Generate strncmp rather than strcmp. */ |
68 | COMP = 1 << 8, |
69 | |
70 | /* Make the generated tables readonly (const). */ |
71 | CONST = 1 << 9, |
72 | |
73 | /* Use enum for constants. */ |
74 | ENUM = 1 << 10, |
75 | |
76 | /* Generate #include statements. */ |
77 | INCLUDE = 1 << 11, |
78 | |
79 | /* Make the keyword table a global variable. */ |
80 | GLOBAL = 1 << 12, |
81 | |
82 | /* Use NULL strings instead of empty strings for empty table entries. */ |
83 | NULLSTRINGS = 1 << 13, |
84 | |
85 | /* Optimize for position-independent code. */ |
86 | SHAREDLIB = 1 << 14, |
87 | |
88 | /* Generate switch output to save space. */ |
89 | SWITCH = 1 << 15, |
90 | |
91 | /* Don't include user-defined type definition in output -- it's already |
92 | defined elsewhere. */ |
93 | NOTYPE = 1 << 16, |
94 | |
95 | /* --- Algorithm employed by gperf --- */ |
96 | |
97 | /* Use the given key positions. */ |
98 | POSITIONS = 1 << 17, |
99 | |
100 | /* Handle duplicate hash values for keywords. */ |
101 | DUP = 1 << 18, |
102 | |
103 | /* Don't include keyword length in hash computations. */ |
104 | NOLENGTH = 1 << 19, |
105 | |
106 | /* Randomly initialize the associated values table. */ |
107 | RANDOM = 1 << 20, |
108 | |
109 | /* --- Informative output --- */ |
110 | |
111 | /* Enable debugging (prints diagnostics to stderr). */ |
112 | DEBUG = 1 << 21 |
113 | }; |
114 | |
115 | /* Class manager for gperf program Options. */ |
116 | |
117 | class Options |
118 | { |
119 | public: |
120 | /* Constructor. */ |
121 | Options (); |
122 | |
123 | /* Destructor. */ |
124 | ~Options (); |
125 | |
126 | /* Parses the options given in the command-line arguments. */ |
127 | void parse_options (int argc, char *argv[]); |
128 | |
129 | /* Prints the given options. */ |
130 | void print_options () const; |
131 | |
132 | /* Accessors. */ |
133 | |
134 | /* Tests a given boolean option. Returns true if set, false otherwise. */ |
135 | bool operator[] (Option_Type option) const; |
136 | /* Sets a given boolean option. */ |
137 | void set (Option_Type option); |
138 | |
139 | /* Returns the input file name. */ |
140 | const char * get_input_file_name () const; |
141 | |
142 | /* Returns the output file name. */ |
143 | const char * get_output_file_name () const; |
144 | |
145 | /* Sets the output language, if not already set. */ |
146 | void set_language (const char *language); |
147 | |
148 | /* Returns the jump value. */ |
149 | int get_jump () const; |
150 | |
151 | /* Returns the initial associated character value. */ |
152 | int get_initial_asso_value () const; |
153 | |
154 | /* Returns the number of iterations for finding good asso_values. */ |
155 | int get_asso_iterations () const; |
156 | |
157 | /* Returns the total number of switch statements to generate. */ |
158 | int get_total_switches () const; |
159 | /* Sets the total number of switch statements, if not already set. */ |
160 | void set_total_switches (int total_switches); |
161 | |
162 | /* Returns the factor by which to multiply the generated table's size. */ |
163 | float get_size_multiple () const; |
164 | |
165 | /* Returns the generated function name. */ |
166 | const char * get_function_name () const; |
167 | /* Sets the generated function name, if not already set. */ |
168 | void set_function_name (const char *name); |
169 | |
170 | /* Returns the keyword key name. */ |
171 | const char * get_slot_name () const; |
172 | /* Sets the keyword key name, if not already set. */ |
173 | void set_slot_name (const char *name); |
174 | |
175 | /* Returns the struct initializer suffix. */ |
176 | const char * get_initializer_suffix () const; |
177 | /* Sets the struct initializer suffix, if not already set. */ |
178 | void set_initializer_suffix (const char *initializers); |
179 | |
180 | /* Returns the generated class name. */ |
181 | const char * get_class_name () const; |
182 | /* Sets the generated class name, if not already set. */ |
183 | void set_class_name (const char *name); |
184 | |
185 | /* Returns the hash function name. */ |
186 | const char * get_hash_name () const; |
187 | /* Sets the hash function name, if not already set. */ |
188 | void set_hash_name (const char *name); |
189 | |
190 | /* Returns the hash table array name. */ |
191 | const char * get_wordlist_name () const; |
192 | /* Sets the hash table array name, if not already set. */ |
193 | void set_wordlist_name (const char *name); |
194 | |
195 | /* Returns the length table array name. */ |
196 | const char * get_lengthtable_name () const; |
197 | /* Sets the length table array name, if not already set. */ |
198 | void set_lengthtable_name (const char *name); |
199 | |
200 | /* Returns the string pool name. */ |
201 | const char * get_stringpool_name () const; |
202 | /* Sets the string pool name, if not already set. */ |
203 | void set_stringpool_name (const char *name); |
204 | |
205 | /* Returns the prefix for the constants. */ |
206 | const char * get_constants_prefix () const; |
207 | /* Sets the prefix for the constants, if not already set. */ |
208 | void set_constants_prefix (const char *prefix); |
209 | |
210 | /* Returns the string used to delimit keywords from other attributes. */ |
211 | const char * get_delimiters () const; |
212 | /* Sets the delimiters string, if not already set. */ |
213 | void set_delimiters (const char *delimiters); |
214 | |
215 | /* Returns key positions. */ |
216 | const Positions& get_key_positions () const; |
217 | |
218 | private: |
219 | /* Prints program usage to given stream. */ |
220 | static void short_usage (FILE * stream); |
221 | |
222 | /* Prints program usage to given stream. */ |
223 | static void long_usage (FILE * stream); |
224 | |
225 | /* Records count of command-line arguments. */ |
226 | int _argument_count; |
227 | |
228 | /* Stores a pointer to command-line argument vector. */ |
229 | char ** _argument_vector; |
230 | |
231 | /* Holds the boolean options. */ |
232 | int _option_word; |
233 | |
234 | /* Name of input file. */ |
235 | char * _input_file_name; |
236 | |
237 | /* Name of output file. */ |
238 | char * _output_file_name; |
239 | |
240 | /* The output language. */ |
241 | const char * _language; |
242 | |
243 | /* Jump length when trying alternative values. */ |
244 | int _jump; |
245 | |
246 | /* Initial value for asso_values table. */ |
247 | int _initial_asso_value; |
248 | |
249 | /* Number of attempts at finding good asso_values. */ |
250 | int _asso_iterations; |
251 | |
252 | /* Number of switch statements to generate. */ |
253 | int _total_switches; |
254 | |
255 | /* Factor by which to multiply the generated table's size. */ |
256 | float _size_multiple; |
257 | |
258 | /* Names used for generated lookup function. */ |
259 | const char * _function_name; |
260 | |
261 | /* Name used for keyword key. */ |
262 | const char * _slot_name; |
263 | |
264 | /* Suffix for empty struct initializers. */ |
265 | const char * _initializer_suffix; |
266 | |
267 | /* Name used for generated C++ class. */ |
268 | const char * _class_name; |
269 | |
270 | /* Name used for generated hash function. */ |
271 | const char * _hash_name; |
272 | |
273 | /* Name used for hash table array. */ |
274 | const char * _wordlist_name; |
275 | |
276 | /* Name used for length table array. */ |
277 | const char * _lengthtable_name; |
278 | |
279 | /* Name used for the string pool. */ |
280 | const char * _stringpool_name; |
281 | |
282 | /* Prefix for the constants. */ |
283 | const char * _constants_prefix; |
284 | |
285 | /* Separates keywords from other attributes. */ |
286 | const char * _delimiters; |
287 | |
288 | /* Contains user-specified key choices. */ |
289 | Positions _key_positions; |
290 | }; |
291 | |
292 | /* Global option coordinator for the entire program. */ |
293 | extern Options option; |
294 | |
295 | #ifdef __OPTIMIZE__ |
296 | |
297 | #define INLINE inline |
298 | #include "options.icc" |
299 | #undef INLINE |
300 | |
301 | #endif |
302 | |
303 | #endif |
304 | |