MPQC 2.3.1

- Main Page
- Related Pages
- Classes
- Files

- File List

keyval.h
1 //
2 // keyval.h
3 //
4 // Copyright (C) 1996 Limit Point Systems, Inc.
5 //
6 // Author: Curtis Janssen <cljanss@limitpt.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit 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 Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27
28 #ifndef _util_keyval_keyval_h
29 #define _util_keyval_keyval_h
30 #ifdef __GNUG__
31 #pragma interface
32 #endif
33
34 #include <iostream>
35 #include <string>
36 #include <map>
37
38 #include <string.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41
42 #include <util/class/class.h>
43 #include <util/keyval/keyvalval.h>
44
45 namespace sc {
46
69 class KeyVal: public RefCount {
70 // these classes need to directly access the key_value member
71 friend class AggregateKeyVal;
72 friend class PrefixKeyVal;
73 public:
74 enum {MaxKeywordLength = 256};
75 enum KeyValError { OK, HasNoValue, WrongType,
76 UnknownKeyword, OperationFailed };
77 private:
78 KeyValError errcod;
79 // do not allow a copy constructor or assignment
80 KeyVal(const KeyVal&);
81 void operator=(const KeyVal&);
82 protected:
83 int verbose_;
84
85 KeyVal();
86
88 void seterror(KeyValError err);
90 void seterror(KeyValValue::KeyValValueError err);
91
93 virtual int key_exists(const char*) = 0;
95 virtual int key_count(const char* =0);
97 virtual Ref<KeyValValue> key_value(const char*,
98 const KeyValValue& def) = 0;
100 virtual int key_booleanvalue(const char*,const KeyValValue& def);
102 virtual double key_doublevalue(const char* key,const KeyValValue& def);
104 virtual float key_floatvalue(const char* key,const KeyValValue& def);
106 virtual char key_charvalue(const char* key,const KeyValValue& def);
108 virtual int key_intvalue(const char* key,const KeyValValue& def);
110 virtual size_t key_sizevalue(const char* key,const KeyValValue& def);
112 virtual char* key_pcharvalue(const char* key,const KeyValValue& def);
114 virtual std::string key_stringvalue(const char* key,
115 const KeyValValue& def);
117 virtual Ref<DescribedClass> key_describedclassvalue(const char* key,
118 const KeyValValue& def);
119
120 public:
121 virtual ~KeyVal();
122
123 // For nonindexed things. If a subclass defines one of these,
124 // then the overloaded functions will be hidden. The key_... functions
125 // should be overridden instead.
126
129 int exists(const char*);
134 int count(const char* =0);
136 Ref<KeyValValue> value(const char* = 0,
137 const KeyValValue& def=KeyValValue());
139 int booleanvalue(const char* key = 0,
140 const KeyValValue& def=KeyValValueboolean());
142 double doublevalue(const char* key = 0,
143 const KeyValValue& def=KeyValValuedouble());
145 float floatvalue(const char* key = 0,
146 const KeyValValue& def=KeyValValuefloat());
148 char charvalue(const char* key = 0,
149 const KeyValValue& def=KeyValValuechar());
151 int intvalue(const char* key = 0,
152 const KeyValValue& def=KeyValValueint());
154 size_t sizevalue(const char* key = 0,
155 const KeyValValue& def=KeyValValuesize());
158 char* pcharvalue(const char* key = 0,
159 const KeyValValue& def=KeyValValuepchar());
161 std::string stringvalue(const char* key = 0,
162 const KeyValValue& def=KeyValValuestring());
164 Ref<DescribedClass> describedclassvalue(const char* key = 0,
165 const KeyValValue& def=KeyValValueRefDescribedClass());
166
175 int exists(const char* key,int i);
176 int count(const char* key,int i);
177 int booleanvalue(const char* key,int i,
178 const KeyValValue& def=KeyValValueboolean());
179 double doublevalue(const char* key,int i,
180 const KeyValValue& def=KeyValValuedouble());
181 float floatvalue(const char* key,int i,
182 const KeyValValue& def=KeyValValuefloat());
183 char charvalue(const char* key,int i,
184 const KeyValValue& def=KeyValValuechar());
185 int intvalue(const char* key,int i,
186 const KeyValValue& def=KeyValValueint());
187 size_t sizevalue(const char* key,int i,
188 const KeyValValue& def=KeyValValuesize());
189 char* pcharvalue(const char* key,int i,
190 const KeyValValue& def=KeyValValuepchar());
191 std::string stringvalue(const char* key,int i,
192 const KeyValValue& def=KeyValValuestring());
193 Ref<DescribedClass> describedclassvalue(const char* key,int,
194 const KeyValValue& def=KeyValValueRefDescribedClass());
195
196 int exists(int i);
197 int count(int i);
198 int booleanvalue(int i,
199 const KeyValValue& def=KeyValValueboolean());
200 double doublevalue(int i,
201 const KeyValValue& def=KeyValValuedouble());
202 float floatvalue(int i,
203 const KeyValValue& def=KeyValValuefloat());
204 char charvalue(int i,
205 const KeyValValue& def=KeyValValuechar());
206 int intvalue(int i,
207 const KeyValValue& def=KeyValValueint());
208 size_t sizevalue(int i,
209 const KeyValValue& def=KeyValValuesize());
210 char* pcharvalue(int i,
211 const KeyValValue& def=KeyValValuepchar());
212 std::string stringvalue(int i,
213 const KeyValValue& def=KeyValValuestring());
214 Ref<DescribedClass> describedclassvalue(int i,
215 const KeyValValue& def=KeyValValueRefDescribedClass());
217
225 int exists(const char*,int,int);
226 int count(const char*,int,int);
227 int booleanvalue(const char*,int,int,
228 const KeyValValue& def=KeyValValueboolean());
229 double doublevalue(const char* key,int,int,
230 const KeyValValue& def=KeyValValuedouble());
231 float floatvalue(const char* key,int,int,
232 const KeyValValue& def=KeyValValuefloat());
233 char charvalue(const char* key,int,int,
234 const KeyValValue& def=KeyValValuechar());
235 int intvalue(const char* key,int,int,
236 const KeyValValue& def=KeyValValueint());
237 size_t sizevalue(const char* key,int,int,
238 const KeyValValue& def=KeyValValuesize());
239 char* pcharvalue(const char* key,int,int,
240 const KeyValValue& def=KeyValValuepchar());
241 std::string stringvalue(const char* key,int,int,
242 const KeyValValue& def=KeyValValuestring());
243 Ref<DescribedClass> describedclassvalue(const char* key,int,int,
244 const KeyValValue& def=KeyValValueRefDescribedClass());
245
246 int exists(int i,int j);
247 int count(int i,int j);
248 int booleanvalue(int i,int j,
249 const KeyValValue& def=KeyValValueboolean());
250 double doublevalue(int i,int j,
251 const KeyValValue& def=KeyValValuedouble());
252 float floatvalue(int i,int j,
253 const KeyValValue& def=KeyValValuefloat());
254 char charvalue(int i,int j,
255 const KeyValValue& def=KeyValValuechar());
256 int intvalue(int i,int j,
257 const KeyValValue& def=KeyValValueint());
258 size_t sizevalue(int i,int j,
259 const KeyValValue& def=KeyValValuesize());
260 char* pcharvalue(int i,int j,
261 const KeyValValue& def=KeyValValuepchar());
262 std::string stringvalue(int i,int j,
263 const KeyValValue& def=KeyValValuestring());
264 Ref<DescribedClass> describedclassvalue(int i,int j,
265 const KeyValValue& def=KeyValValueRefDescribedClass());
267
274 int Va_exists(const char* key,int nindex,...);
275 int Va_count(const char* key,int nindex,...);
276 int Va_booleanvalue(const char* key,int nindex,...);
277 double Va_doublevalue(const char* key,int nindex,...);
278 float Va_floatvalue(const char* key,int nindex,...);
279 char Va_charvalue(const char* key,int nindex,...);
280 int Va_intvalue(const char* key,int nindex,...);
281 size_t Va_sizevalue(const char* key,int nindex,...);
282 char* Va_pcharvalue(const char* key,int nindex,...);
283 std::string Va_stringvalue(const char* key,int nindex,...);
284 Ref<DescribedClass> Va_describedclassvalue(const char* key,int nindex,...);
286
288 KeyValError error();
290 const char* errormsg(KeyValError err);
292 const char* errormsg();
294 virtual void errortrace(std::ostream&fp=ExEnv::err0());
296 virtual void dump(std::ostream&fp=ExEnv::err0());
297
299 virtual void print_unseen(std::ostream&fp=ExEnv::out0());
303 virtual int have_unseen();
304
306 void verbose(int v) { verbose_ = v; }
308 int verbose() const { return verbose_; }
309 };
310
311
312
315 class AssignedKeyVal: public KeyVal {
316 private:
317 std::map<std::string,Ref<KeyValValue> > _map;
318 // do not allow a copy constructor or assignment
319 AssignedKeyVal(const AssignedKeyVal&);
320 void operator=(const AssignedKeyVal&);
321 protected:
322 int key_exists(const char*);
323 Ref<KeyValValue> key_value(const char*,
324 const KeyValValue& def);
325 public:
326 AssignedKeyVal();
327 ~AssignedKeyVal();
328
332 void assign(const char* key, const Ref<KeyValValue>& val);
333 void assign(const char* key, double val);
334 void assignboolean(const char* key, int val);
335 void assign(const char* key, float val);
336 void assign(const char* key, char val);
337 void assign(const char* key, int val);
338 void assign(const char* key, const char* val);
339 void assign(const char* key, const Ref<DescribedClass>& val);
341
343 void clear();
344 };
345
346
347
352 class StringKeyVal: public KeyVal {
353 private:
354 // once a described class is found it is kept here so
355 // multiple references to it return the same instance
356 std::map<std::string,Ref<KeyValValue> > _map;
357 // do not allow a copy constructor or assignment
358 StringKeyVal(const StringKeyVal&);
359 void operator=(const StringKeyVal&);
360 protected:
361 StringKeyVal();
362 int key_exists(const char*);
363 Ref<KeyValValue> key_value(const char*,
364 const KeyValValue& def);
365 public:
366 virtual ~StringKeyVal();
368 virtual const char* stringrep(const char *key) = 0;
371 virtual const char* classname(const char*);
375 virtual const char* truekeyword(const char*);
376
381 virtual void errortrace(std::ostream&fp=ExEnv::err0());
382 virtual void dump(std::ostream&fp=ExEnv::err0());
384 };
385
390 class AggregateKeyVal : public KeyVal {
391 private:
392 enum { MaxKeyVal = 4 };
393 Ref<KeyVal> kv[MaxKeyVal];
394 Ref<KeyVal> getkeyval(const char*key);
395 // do not allow a copy constructor or assignment
396 AggregateKeyVal(const AggregateKeyVal&);
397 void operator=(const AggregateKeyVal&);
398 protected:
399 int key_exists(const char*);
400 Ref<KeyValValue> key_value(const char*,
401 const KeyValValue& def);
402 public:
410 AggregateKeyVal(const Ref<KeyVal>& keyval1);
411 AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2);
412 AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2,
413 const Ref<KeyVal>& keyval3);
414 AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2,
415 const Ref<KeyVal>& keyval3, const Ref<KeyVal>& keyval4);
417 ~AggregateKeyVal();
418 void errortrace(std::ostream&fp=ExEnv::err0());
419 void dump(std::ostream&fp=ExEnv::err0());
420 };
421
464 class PrefixKeyVal : public KeyVal {
465 private:
466 char* prefix;
467 Ref<KeyVal> keyval;
468 void setup(const char*,int,int,int,int,int);
469 int getnewprefixkey(const char*key,char*newkey);
470 // do not allow a copy constructor or assignment
471 PrefixKeyVal(const PrefixKeyVal&);
472 void operator=(const PrefixKeyVal&);
473 int key_exists(const char*);
474 Ref<KeyValValue> key_value(const char*,
475 const KeyValValue& def);
476 public:
480 PrefixKeyVal(const Ref<KeyVal>&,int i);
481 PrefixKeyVal(const Ref<KeyVal>&,int i,int j);
482 PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k);
483 PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k,int l);
484 PrefixKeyVal(const Ref<KeyVal>&,const char*prefix);
485 PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i);
486 PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j);
487 PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k);
488 PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k,int l);
490 ~PrefixKeyVal();
491 void errortrace(std::ostream&fp=ExEnv::err0());
492 void dump(std::ostream&fp=ExEnv::err0());
493 };
494
495 class IPV2;
499 class ParsedKeyVal : public StringKeyVal {
500 private:
501 int nfile;
502 char**file;
503 int nfp;
504 IPV2* ipv2;
505 // do not allow a copy constructor or assignment
506 ParsedKeyVal(const ParsedKeyVal&);
507 void operator=(const ParsedKeyVal&);
508 public:
510 ParsedKeyVal();
512 ParsedKeyVal(const char*file);
514 ParsedKeyVal(std::istream&s);
517 ParsedKeyVal(IPV2*);
523 ParsedKeyVal(const char*,const Ref<KeyVal>&);
525 ~ParsedKeyVal();
526
529 static void cat_files(const char*,const Ref<KeyVal>&,std::ostream &o);
530
532 void read(const char*);
534 void read(std::istream&);
536 void parse_string(const char *);
537
541 const char* stringrep(const char*);
542 const char* classname(const char*);
543 const char* truekeyword(const char*);
544 void errortrace(std::ostream&fp=ExEnv::err0());
545 void dump(std::ostream&fp=ExEnv::err0());
546 void print_unseen(std::ostream&fp=ExEnv::out0());
547 int have_unseen();
549 };
550
551 }
552
553 #endif /* _KeyVal_h */
554
555 // Local Variables:
556 // mode: c++
557 // c-file-style: "CLJ"
558 // End: