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 
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,
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 
315 class 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*);
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
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:
sc::ParsedKeyVal::ParsedKeyVal
ParsedKeyVal()
Create an empty ParsedKeyVal.
sc::KeyVal::intvalue
int intvalue(const char *key=0, const KeyValValue &def=KeyValValueint())
Returns the int value of key.
sc::KeyValValuefloat
Definition: keyvalval.h:87
sc::KeyVal
The KeyVal class is designed to simplify the process of allowing a user to specify keyword/value asso...
Definition: keyval.h:69
sc::AggregateKeyVal::key_exists
int key_exists(const char *)
Ultimately called by exists.
sc::KeyVal::errortrace
virtual void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::ParsedKeyVal::have_unseen
int have_unseen()
Return 1 if there were unseen keywords, 0 if there are none, or -1 this keyval doesn't keep track of ...
sc::ParsedKeyVal::classname
const char * classname(const char *)
Returns the name of the exact class of the object at the keyword.
sc::StringKeyVal::key_exists
int key_exists(const char *)
Ultimately called by exists.
sc::KeyVal::key_intvalue
virtual int key_intvalue(const char *key, const KeyValValue &def)
Ultimately called by intvalue.
sc::KeyVal::have_unseen
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 ...
sc::KeyVal::floatvalue
float floatvalue(const char *key=0, const KeyValValue &def=KeyValValuefloat())
Returns the float value of key.
sc::KeyVal::key_charvalue
virtual char key_charvalue(const char *key, const KeyValValue &def)
Ultimately called by charvalue.
sc::StringKeyVal::classname
virtual const char * classname(const char *)
Returns the name of the exact class of the object at the keyword.
sc::KeyValValuestring
Definition: keyvalval.h:148
sc::KeyValValueRefDescribedClass
Definition: keyvalval.h:161
sc::Ref
A template class that maintains references counts.
Definition: ref.h:332
sc::KeyValValuesize
Definition: keyvalval.h:123
sc::StringKeyVal::dump
virtual void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::ParsedKeyVal::print_unseen
void print_unseen(std::ostream &fp=ExEnv::out0())
Print keywords that were never looked at, if possible.
sc::ParsedKeyVal::stringrep
const char * stringrep(const char *)
Returns the string representation of the value assigned to key.
sc::AggregateKeyVal::errortrace
void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::KeyVal::booleanvalue
int booleanvalue(const char *key=0, const KeyValValue &def=KeyValValueboolean())
Returns the boolean value (0 = false, 1 = true) of key.
sc::PrefixKeyVal::dump
void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::KeyVal::key_floatvalue
virtual float key_floatvalue(const char *key, const KeyValValue &def)
Ultimately called by floatvalue.
sc::ParsedKeyVal::parse_string
void parse_string(const char *)
Read input data from the given string.
sc::KeyVal::exists
int exists(const char *)
This takes as its only argument a keyword.
sc::AssignedKeyVal::clear
void clear()
Erase all of the stored assignments.
sc::StringKeyVal::truekeyword
virtual const char * truekeyword(const char *)
Returns a string which is the actual keyword if some sort of variable substitution takes place (neede...
sc::PrefixKeyVal
PrefixKeyVal is a KeyVal that searches a different KeyVal using modified keys.
Definition: keyval.h:464
sc::ParsedKeyVal
Converts textual information into keyword/value assocations.
Definition: keyval.h:499
sc::KeyVal::value
Ref< KeyValValue > value(const char *=0, const KeyValValue &def=KeyValValue())
Return the value associated with the keyword.
sc::KeyVal::key_doublevalue
virtual double key_doublevalue(const char *key, const KeyValValue &def)
Ultimately called by doublevalue.
sc::KeyVal::print_unseen
virtual void print_unseen(std::ostream &fp=ExEnv::out0())
Print keywords that were never looked at, if possible.
sc::KeyVal::key_value
virtual Ref< KeyValValue > key_value(const char *, const KeyValValue &def)=0
Ultimately called by value.
sc::KeyVal::key_stringvalue
virtual std::string key_stringvalue(const char *key, const KeyValValue &def)
Ultimately called by stringvalue.
sc::KeyVal::describedclassvalue
Ref< DescribedClass > describedclassvalue(const char *key=0, const KeyValValue &def=KeyValValueRefDescribedClass())
Returns a reference to an object of type DescribedClass.
sc::KeyVal::dump
virtual void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::KeyVal::error
KeyValError error()
Return the current error condition.
sc::ParsedKeyVal::dump
void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::ExEnv::err0
static std::ostream & err0()
Return an ostream for error messages that writes from node 0.
Definition: exenv.h:84
sc::KeyValValueboolean
Definition: keyvalval.h:75
sc::KeyVal::key_booleanvalue
virtual int key_booleanvalue(const char *, const KeyValValue &def)
Ultimately called by booleanvalue.
sc::KeyVal::key_pcharvalue
virtual char * key_pcharvalue(const char *key, const KeyValValue &def)
Ultimately called by pcharvalue.
sc::KeyValValueint
Definition: keyvalval.h:111
sc::KeyVal::sizevalue
size_t sizevalue(const char *key=0, const KeyValValue &def=KeyValValuesize())
Returns the size_t value of key.
sc::KeyValValuechar
Definition: keyvalval.h:99
sc::KeyVal::count
int count(const char *=0)
If the value of a keyword is an array, then return its length.
sc::StringKeyVal::key_value
Ref< KeyValValue > key_value(const char *, const KeyValValue &def)
Ultimately called by value.
sc::KeyValValue
Definition: keyvalval.h:40
sc::StringKeyVal
StringKeyVal is a base class for KeyVal implementations that store all values in a string format.
Definition: keyval.h:352
sc::KeyValValuepchar
Definition: keyvalval.h:135
sc::ParsedKeyVal::truekeyword
const char * truekeyword(const char *)
Returns a string which is the actual keyword if some sort of variable substitution takes place (neede...
sc::StringKeyVal::errortrace
virtual void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::KeyVal::key_exists
virtual int key_exists(const char *)=0
Ultimately called by exists.
sc::AssignedKeyVal
This class allows keyval associations to be set up by the program, rather than determined by an exter...
Definition: keyval.h:315
sc::AggregateKeyVal::dump
void dump(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::ParsedKeyVal::read
void read(const char *)
Read input data from the given filename.
sc::KeyVal::stringvalue
std::string stringvalue(const char *key=0, const KeyValValue &def=KeyValValuestring())
Returns a string representation of the key's value.
sc::AssignedKeyVal::key_exists
int key_exists(const char *)
Ultimately called by exists.
sc::AggregateKeyVal
This takes several KeyVal objects and makes them look like one KeyVal object.
Definition: keyval.h:390
sc::KeyVal::charvalue
char charvalue(const char *key=0, const KeyValValue &def=KeyValValuechar())
Returns the char value of key.
sc::ParsedKeyVal::cat_files
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...
sc::ExEnv::out0
static std::ostream & out0()
Return an ostream that writes from node 0.
sc::KeyVal::key_count
virtual int key_count(const char *=0)
Ultimately called by count.
sc::KeyVal::pcharvalue
char * pcharvalue(const char *key=0, const KeyValValue &def=KeyValValuepchar())
Returns a copy of the string representation of the key's value.
sc::ParsedKeyVal::errortrace
void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::KeyVal::errormsg
const char * errormsg()
Return a textual representation of the current error.
sc::RefCount
The base class for all reference counted objects.
Definition: ref.h:194
sc::KeyVal::doublevalue
double doublevalue(const char *key=0, const KeyValValue &def=KeyValValuedouble())
Returns the double value of key.
sc::StringKeyVal::stringrep
virtual const char * stringrep(const char *key)=0
Returns the string representation of the value assigned to key.
sc::KeyValValuedouble
Definition: keyvalval.h:63
sc::IPV2
Definition: ipv2.h:80
sc::AggregateKeyVal::key_value
Ref< KeyValValue > key_value(const char *, const KeyValValue &def)
Ultimately called by value.
sc::PrefixKeyVal::errortrace
void errortrace(std::ostream &fp=ExEnv::err0())
Write a message to fp describing the error.
sc::KeyVal::key_sizevalue
virtual size_t key_sizevalue(const char *key, const KeyValValue &def)
Ultimately called by sizevalue.
sc::KeyVal::key_describedclassvalue
virtual Ref< DescribedClass > key_describedclassvalue(const char *key, const KeyValValue &def)
Ultimately called by describedclassvalue.
sc::KeyVal::verbose
void verbose(int v)
Control printing of assignments.
Definition: keyval.h:306
sc::KeyVal::verbose
int verbose() const
Returns nonzero if assignments are printed.
Definition: keyval.h:308
sc::AssignedKeyVal::key_value
Ref< KeyValValue > key_value(const char *, const KeyValValue &def)
Ultimately called by value.
sc::KeyVal::seterror
void seterror(KeyValError err)
Set the current error condition.
sc::ParsedKeyVal::~ParsedKeyVal
~ParsedKeyVal()
Cleanup, deleting the IPV2 object.

Generated at Sun Jan 26 2020 23:33:04 for MPQC 2.3.1 using the documentation package Doxygen 1.8.16.