45#include "EST_walloc.h"
46#include "EST_TNamedEnum.h"
51template<
class ENUM,
class VAL,
class INFO>
58 for(n=1;
defs[n].token !=
defs[0].token; n++)
61 this->ndefinitions = n;
62 this->definitions =
new defn[n];
64 this->definitions[0] =
defs[0];
65 for(n=1;
defs[n].token !=
defs[0].token; n++)
66 this->definitions[n] =
defs[n];
68 this->p_unknown_enum =
defs[n].token;
69 this->p_unknown_value =
defs[n].values[0];
72template<
class ENUM,
class VAL,
class INFO>
90 this->ndefinitions = n;
92 this->definitions =
new defn[n];
94 this->definitions[0].token =
conv(
defs[0].token);
95 for(
int i=0; i<NAMED_ENUM_MAX_SYNONYMS; i++)
96 this->definitions[0].values[i] =
defs[0].values[i];
97 this->definitions[0].info =
defs[0].info;
100 this->definitions[n].token =
conv(
defs[n].token);
101 for(
int i2=0; i2<NAMED_ENUM_MAX_SYNONYMS; i2++)
102 this->definitions[n].values[i2] =
defs[n].values[i2];
103 this->definitions[n].info =
defs[n].info;
106 this->p_unknown_enum =
conv(
defs[n].token);
107 this->p_unknown_value =
defs[n].values[0];
110template<
class ENUM,
class VAL,
class INFO>
113 if (this->definitions)
114 delete[] this->definitions;
117template<
class ENUM,
class VAL,
class INFO>
120return this->ndefinitions;
123template<
class ENUM,
class VAL,
class INFO>
128 for(i=0; i<this->ndefinitions; i++)
129 if (this->definitions[i].token == token)
130 return this->definitions[i].values[n];
132 return this->p_unknown_value;
135template<
class ENUM,
class VAL,
class INFO>
140 for(i=0; i<this->ndefinitions; i++)
141 if (this->definitions[i].token == token)
142 return this->definitions[i].info;
144 cerr <<
"Fetching info for invalid entry\n";
151template<
class ENUM,
class VAL,
class INFO>
155 return this->definitions[n].token;
157 return this->p_unknown_enum;
160template<
class ENUM,
class VAL,
class INFO>
165 for(i=0; i<this->ndefinitions; i++)
166 for(
j=0;
j<NAMED_ENUM_MAX_SYNONYMS && this->definitions[i].values[
j] ;
j++)
167 if (eq_vals(this->definitions[i].values[
j], value))
168 return this->definitions[i].token;
170 return this->p_unknown_enum;
177#define LINE_LENGTH (1024)
179 char *buffer = (
char *)line;
187 return misc_read_error;
189 if (this->definitions)
190 delete[] this->definitions;
192 this->ndefinitions= -1;
193 this->definitions=NULL;
195 buffer[LINE_LENGTH-1] =
'x';
199 if ( buffer[LINE_LENGTH-1] !=
'x')
201 cerr <<
"line too long .. '" << buffer <<
"'\n";
205 if (this->ndefinitions>=0 && quote !=
'\0' && buffer[0] ==
'=')
209 if ( n>= this->ndefinitions)
211 cerr <<
"too many definitions\n";
215 int ntokens = split(line, tokens, NAMED_ENUM_MAX_SYNONYMS+2, RXwhite,
'"');
216 this->definitions[n].token = (
ENUM)
atoi(tokens[0].after(0,1));
219 this->definitions[n].values[i-1] = wstrdup(tokens[i].unquote_if_needed(quote));
220 for(
int j=
ntokens-1 ;
j< NAMED_ENUM_MAX_SYNONYMS;
j++)
221 this->definitions[n].values[
j]=NULL;
225 else if (
have_unknown && this->ndefinitions>=0 && quote !=
'\0' && buffer[0] == quote)
230 cerr <<
"can't use names in this definition\n";
233 if ( n>= this->ndefinitions)
235 cerr <<
"too many definitions\n";
239 int ntokens = split(line, tokens, NAMED_ENUM_MAX_SYNONYMS+2, RXwhite, quote);
241 this->definitions[n].token =
definitive->token(tokens[0].unquote(quote));
244 this->definitions[n].values[i-1] = wstrdup(tokens[i].unquote_if_needed(quote));
245 for(
int j=
ntokens-1 ;
j< NAMED_ENUM_MAX_SYNONYMS;
j++)
246 this->definitions[n].values[
j]=NULL;
255 int eq = line.search(
"=", 1,
mlen);
259 cerr <<
"bad header line '" << line;
270 else if (key ==
"number")
272 this->ndefinitions=
atoi(line.after(eq,1));
274 this->definitions =
new Defn[this->ndefinitions];
275 for(
int i=0; i<this->ndefinitions; i++)
276 this->definitions[i].values[0] =NULL;
279 else if (key ==
"unknown")
281 this->p_unknown_enum=(
ENUM)
atoi(line.after(eq,1));
287 cerr <<
"bad header line '" << line;
308 fprintf(
file,
"unknown=%d\n", this->p_unknown_enum);
312 for(
int i=0; i<this->ndefinitions; i++)
313 if (this->definitions[i].values[0])
318 fprintf(
file,
"=%d ", (
int)this->definitions[i].token);
320 for(
int j=0;
j<NAMED_ENUM_MAX_SYNONYMS && this->definitions[i].values[
j] != NULL;
j++)
EST_String quote(const char quotec) const
Return the string in quotes with internal quotes protected.