MPQC 2.3.1
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
45namespace sc {
46
69class 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);
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());
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,
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());
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,
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());
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
315class AssignedKeyVal: public KeyVal {
316 private:
317 std::map<std::string,Ref<KeyValValue> > _map;
318 // do not allow a copy constructor or assignment
320 void operator=(const AssignedKeyVal&);
321 protected:
322 int key_exists(const char*);
324 const KeyValValue& def);
325 public:
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
352class 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
359 void operator=(const StringKeyVal&);
360 protected:
361 StringKeyVal();
362 int key_exists(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
390class 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
397 void operator=(const AggregateKeyVal&);
398 protected:
399 int key_exists(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);
418 void errortrace(std::ostream&fp=ExEnv::err0());
419 void dump(std::ostream&fp=ExEnv::err0());
420};
421
464class 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
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);
491 void errortrace(std::ostream&fp=ExEnv::err0());
492 void dump(std::ostream&fp=ExEnv::err0());
493};
494
495class IPV2;
500 private:
501 int nfile;
502 char**file;
503 int nfp;
504 IPV2* ipv2;
505 // do not allow a copy constructor or assignment
507 void operator=(const ParsedKeyVal&);
508 public:
512 ParsedKeyVal(const char*file);
514 ParsedKeyVal(std::istream&s);
523 ParsedKeyVal(const char*,const Ref<KeyVal>&);
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());
549};
550
551}
552
553#endif /* _KeyVal_h */
554
555// Local Variables:
556// mode: c++
557// c-file-style: "CLJ"
558// End:
This takes several KeyVal objects and makes them look like one KeyVal object.
Definition keyval.h:390
int key_exists(const char *)
Ultimately called by exists.
void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
Ref< KeyValValue > key_value(const char *, const KeyValValue &def)
Ultimately called by value.
This class allows keyval associations to be set up by the program, rather than determined by an exter...
Definition keyval.h:315
Ref< KeyValValue > key_value(const char *, const KeyValValue &def)
Ultimately called by value.
void clear()
Erase all of the stored assignments.
int key_exists(const char *)
Ultimately called by exists.
static std::ostream & out0()
Return an ostream that writes from node 0.
static std::ostream & err0()
Return an ostream for error messages that writes from node 0.
Definition exenv.h:84
Definition ipv2.h:81
Definition keyvalval.h:161
Definition keyvalval.h:40
Definition keyvalval.h:75
Definition keyvalval.h:99
Definition keyvalval.h:63
Definition keyvalval.h:87
Definition keyvalval.h:111
Definition keyvalval.h:135
Definition keyvalval.h:123
Definition keyvalval.h:148
The KeyVal class is designed to simplify the process of allowing a user to specify keyword/value asso...
Definition keyval.h:69
virtual Ref< KeyValValue > key_value(const char *, const KeyValValue &def)=0
Ultimately called by value.
Ref< DescribedClass > describedclassvalue(const char *key=0, const KeyValValue &def=KeyValValueRefDescribedClass())
Returns a reference to an object of type DescribedClass.
char charvalue(const char *key=0, const KeyValValue &def=KeyValValuechar())
Returns the char value of key.
char * pcharvalue(const char *key=0, const KeyValValue &def=KeyValValuepchar())
Returns a copy of the string representation of the key's value.
int intvalue(const char *key=0, const KeyValValue &def=KeyValValueint())
Returns the int value of key.
virtual float key_floatvalue(const char *key, const KeyValValue &def)
Ultimately called by floatvalue.
virtual int have_unseen()
Return 1 if there were unseen keywords, 0 if there are none, or -1 this keyval doesn't keep track of ...
virtual int key_booleanvalue(const char *, const KeyValValue &def)
Ultimately called by booleanvalue.
virtual char key_charvalue(const char *key, const KeyValValue &def)
Ultimately called by charvalue.
const char * errormsg()
Return a textual representation of the current error.
Ref< KeyValValue > value(const char *=0, const KeyValValue &def=KeyValValue())
Return the value associated with the keyword.
std::string stringvalue(const char *key=0, const KeyValValue &def=KeyValValuestring())
Returns a string representation of the key's value.
virtual char * key_pcharvalue(const char *key, const KeyValValue &def)
Ultimately called by pcharvalue.
virtual size_t key_sizevalue(const char *key, const KeyValValue &def)
Ultimately called by sizevalue.
virtual int key_intvalue(const char *key, const KeyValValue &def)
Ultimately called by intvalue.
void seterror(KeyValValue::KeyValValueError err)
Set the current error condition.
const char * errormsg(KeyValError err)
Return a textual representation of err.
int verbose() const
Returns nonzero if assignments are printed.
Definition keyval.h:308
float floatvalue(const char *key=0, const KeyValValue &def=KeyValValuefloat())
Returns the float value of key.
virtual void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
KeyValError error()
Return the current error condition.
int count(const char *=0)
If the value of a keyword is an array, then return its length.
virtual void print_unseen(std::ostream &fp=ExEnv::out0())
Print keywords that were never looked at, if possible.
virtual void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
virtual std::string key_stringvalue(const char *key, const KeyValValue &def)
Ultimately called by stringvalue.
void verbose(int v)
Control printing of assignments.
Definition keyval.h:306
int exists(const char *)
This takes as its only argument a keyword.
virtual Ref< DescribedClass > key_describedclassvalue(const char *key, const KeyValValue &def)
Ultimately called by describedclassvalue.
virtual double key_doublevalue(const char *key, const KeyValValue &def)
Ultimately called by doublevalue.
int booleanvalue(const char *key=0, const KeyValValue &def=KeyValValueboolean())
Returns the boolean value (0 = false, 1 = true) of key.
virtual int key_count(const char *=0)
Ultimately called by count.
double doublevalue(const char *key=0, const KeyValValue &def=KeyValValuedouble())
Returns the double value of key.
virtual int key_exists(const char *)=0
Ultimately called by exists.
size_t sizevalue(const char *key=0, const KeyValValue &def=KeyValValuesize())
Returns the size_t value of key.
void seterror(KeyValError err)
Set the current error condition.
Converts textual information into keyword/value assocations.
Definition keyval.h:499
void parse_string(const char *)
Read input data from the given string.
void read(const char *)
Read input data from the given filename.
void read(std::istream &)
Read input data from the given stream.
int have_unseen()
Return 1 if there were unseen keywords, 0 if there are none, or -1 this keyval doesn't keep track of ...
ParsedKeyVal()
Create an empty ParsedKeyVal.
const char * classname(const char *)
Returns the name of the exact class of the object at the keyword.
void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
ParsedKeyVal(IPV2 *)
Use the given IPV2* object.
ParsedKeyVal(const char *file)
Parse the given input file.
void print_unseen(std::ostream &fp=ExEnv::out0())
Print keywords that were never looked at, if possible.
const char * stringrep(const char *)
Returns the string representation of the value assigned to key.
void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
ParsedKeyVal(const char *, const Ref< KeyVal > &)
This ctor is given a string which is used to form keywords that are sought in the keyval argument.
const char * truekeyword(const char *)
Returns a string which is the actual keyword if some sort of variable substitution takes place (neede...
ParsedKeyVal(std::istream &s)
Read input from s.
~ParsedKeyVal()
Cleanup, deleting the IPV2 object.
static void cat_files(const char *, const Ref< KeyVal > &, std::ostream &o)
This is like the ParsedKeyVal(const char*,const Ref<KeyVal>&) ctor, but writes the contents of the fi...
PrefixKeyVal is a KeyVal that searches a different KeyVal using modified keys.
Definition keyval.h:464
void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
The base class for all reference counted objects.
Definition ref.h:194
A template class that maintains references counts.
Definition ref.h:332
StringKeyVal is a base class for KeyVal implementations that store all values in a string format.
Definition keyval.h:352
virtual const char * stringrep(const char *key)=0
Returns the string representation of the value assigned to key.
virtual void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
virtual const char * truekeyword(const char *)
Returns a string which is the actual keyword if some sort of variable substitution takes place (neede...
virtual void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
virtual const char * classname(const char *)
Returns the name of the exact class of the object at the keyword.
Ref< KeyValValue > key_value(const char *, const KeyValValue &def)
Ultimately called by value.
int key_exists(const char *)
Ultimately called by exists.

Generated at Wed Mar 27 2024 02:45:28 for MPQC 2.3.1 using the documentation package Doxygen 1.9.8.