-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.cpp
363 lines (304 loc) · 9.06 KB
/
settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* PET
* Platform for Experimentation with efficient HPSG processing Techniques
* (C) 1999 - 2002 Ulrich Callmeier [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* parse the settings file */
#include "lex-tdl.h"
#include "settings.h"
#include "utility.h"
#include "types.h"
#include "errors.h"
#include "list-int.h"
#include "logging.h"
using std::string;
settings::settings(string name, string base_dir, const char *message)
: _valid(false), _lloc(0), _li_cache(), _updates() {
_n = 0;
_set = new setting*[SET_TABLE_SIZE];
_fname = find_set_file(name, SET_EXT, base_dir);
_base = dir_name(base_dir);
_prefix = dir_name(_fname);
if(! _fname.empty()) {
push_file(_fname, message);
const char *sv = lexer_idchars;
lexer_idchars = "_+-*?$";
parse();
lexer_idchars = sv;
_valid = true;
}
}
settings::settings(const std::string &input)
: _valid(false), _lloc(0), _li_cache(), _updates() {
_n = 0;
_set = new setting*[SET_TABLE_SIZE];
push_string(input, NULL);
const char *sv = lexer_idchars;
lexer_idchars = "_+-*?$";
parse();
lexer_idchars = sv;
_valid = true;
}
settings::~settings() {
for(int i = 0; i < _n; ++i) {
for(int j = 0; j < _set[i]->n; ++j)
free(_set[i]->values[j]);
free(_set[i]->values);
delete _set[i];
}
delete[] _set;
for(std::map<std::string, struct list_int *>::iterator it = _li_cache.begin();
it != _li_cache.end(); ++it) {
free_list(it->second);
}
}
/** Do a linear search for \a name in the settings and return the first
* matching setting.
*/
setting *settings::lookup(const char *name) {
//
// first, in case there are settings updates (which should shadow any values
// in the top-level .settings. object), check these in order. thus, multiple
// updates can shadow each other, in the order determined by install() calls.
//
setting *match;
for(std::list<settings *>::iterator update = _updates.begin();
update != _updates.end();
++update) {
match = (*update)->lookup(name);
if(match != NULL) {
_lloc = match->loc;
return match;
} // if
} // for
for(int i = 0; i < _n; ++i) {
if(strcmp(_set[i]->name, name) == 0) {
if(i != 0) {
// put to front, so further lookup is faster
setting *tmp;
tmp = _set[i]; _set[i] = _set[0]; _set[0] = tmp;
} // if
_lloc = _set[0]->loc;
return _set[0];
} // if
} // for
_lloc = 0;
return 0;
}
/** Return the value of the first setting matching \a name or NULL if there is
* no such setting.
*/
const char *settings::value(const char *name) {
setting *s;
s = lookup(name);
if(s == 0) return 0;
return s->values[0];
}
/** Return the value of the first setting matching \a name or throw an error if
* there is no such setting (short for required_value).
*/
const char *settings::req_value(const char *name)
{
const char *v = value(name);
if(v == 0)
{
throw tError("no definition for required parameter `" + string(name) + "'");
}
return v;
}
/** \brief Is \a value in the list for \a name? If so, return \c true */
bool settings::member(const char *name, const char *value)
{
setting *set = lookup(name);
if(set == 0) return false;
for(int i = 0; i < set->n; ++i)
if(strcasecmp(set->values[i], value) == 0)
return true;
return false;
}
/** Get an element of a setting that is an assoc list.
*
* \param name The setting to look for
* \param key The key to search in the assoc list
* \param arity The number of elements in one assoc-cons
* \param nth The number of the element to return
* \return NULL if there is no such setting or no such key in the assoc list,
* the \a nth element of the assoc cons otherwise
*/
const char *settings::assoc(const char *name, const char *key, int arity,
int nth)
{
setting *set = lookup(name);
if(set == 0) return 0;
assert(nth <= arity && arity > 1);
for(int i = 0; i < set->n; i+=arity)
{
if(i+nth >= set->n) return 0;
if(strcasecmp(set->values[i], key) == 0)
return set->values[i+nth];
}
return 0;
}
#ifndef FLOP
// subtype based map
std::set<std::string> settings::smap(const char *name, int key_type)
{
std::set<std::string> res;
setting *set = lookup(name);
if(set == 0) return res;
if(key_type == -1) return res;
for(int i = 0; i < set->n; i+=2)
{
if(i+2 > set->n)
{
LOG(logAppl, WARN, "warning: incomplete last entry in `" << name
<< "' mapping - ignored");
break;
}
char *lhs = set->values[i], *rhs = set->values[i+1];
int id = lookup_type(lhs);
if(id != -1)
{
if(subtype(key_type, id))
res.insert(rhs);
}
else
LOG(logAppl, WARN, "unknown type `" << name << "' in `"
<< lhs << "' mapping - ignored");
}
return res;
}
#endif
/** Return \c true, if the \a key is a type with status \a name.
*/
bool settings::statusmember(const char *name, type_t key)
{
// first try to find the list of status types for name in the cache
list_int *l = _li_cache[string(name)];
if(l == 0)
{
setting *set = lookup(name);
// convert the set of status names into a list of code numbers and store
// it in the cache. All status names that do not occur in the grammar
// are reported to be unknown.
if(set != 0)
{
for(int i = 0; i < set->n; ++i)
{
int v = lookup_status(set->values[i]);
if(v == -1)
{
LOG(logAppl, WARN, "ignoring unknown status `"
<< set->values[i] << "' in setting `" << name << "'");
}
else
l = cons(v, l);
}
_li_cache[string(name)] = l;
}
}
return contains(l, key);
}
void settings::parse_one()
{
char *option;
struct setting *set;
option = LA(0)->text; LA(0)->text = NULL;
consume(1);
set = lookup(option);
if(set)
{
LOG(logAppl, WARN,
"more than one definition for setting `" << option << "'...");
}
else
{
assert(_n < SET_TABLE_SIZE);
set = _set[_n++] = new setting;
set->name = option;
set->loc = LA(0)->loc; LA(0)->loc = NULL;
set->n = 0;
set->allocated = SET_TABLE_SIZE;
set->values = (char **) malloc(set->allocated * sizeof(char *));
}
if(LA(0)->tag != T_DOT)
{
match(T_ISEQ, "option setting", true);
while(LA(0)->tag != T_DOT && LA(0)->tag != T_EOF)
{
if(LA(0)->tag == T_ID || LA(0)->tag == T_KEYWORD ||
LA(0)->tag == T_STRING)
{
if(set->n >= set->allocated)
{
set->allocated += SET_TABLE_SIZE;
set->values = (char **) realloc(set->values, set->allocated * sizeof(char *));
}
set->values[set->n++] = LA(0)->text; LA(0)->text=NULL;
}
else
{
LOG(logSyntax, WARN, LA(0)->loc->fname << ":"
<< LA(0)->loc->linenr << ": warning: ignoring "
<< LA(0)->text);
}
consume(1);
}
}
match(T_DOT, "end of option setting", true);
}
void settings::parse() {
do {
if(LA(0)->tag == T_ID) {
parse_one();
}
else if(LA(0)->tag == T_COLON) {
consume(1);
}
else if(LA(0)->tag == T_KEYWORD && strcmp(LA(0)->text, "include") == 0) {
consume(1);
if(LA(0)->tag != T_STRING) {
LOG(logSyntax, WARN, LA(0)->loc->fname << ":" << LA(0)->loc->linenr
<< ": warning: expecting include file name");
}
else {
string ofname = _prefix + LA(0)->text + SET_EXT;
consume(1);
match(T_DOT, "`.'", true);
if(file_exists_p(ofname.c_str())) {
push_file(ofname, "including");
} else {
LOG(logAppl, WARN, "file `" << ofname << "' not found. skipping...");
}
}
}
else {
syntax_error("expecting identifier", LA(0));
consume(1);
}
} while(LA(0)->tag != T_EOF);
consume(1);
}
void
settings::install(settings *update) {
_updates.push_front(update);
} // settings::uninstall()
bool
settings::uninstall(settings *update) {
unsigned int n = _updates.size();
_updates.remove(update);
return n != _updates.size();
} // settings::uninstall()