Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

keyval.h

00001 //
00002 // keyval.h
00003 //
00004 // Copyright (C) 1996 Limit Point Systems, Inc.
00005 //
00006 // Author: Curtis Janssen <cljanss@limitpt.com>
00007 // Maintainer: LPS
00008 //
00009 // This file is part of the SC Toolkit.
00010 //
00011 // The SC Toolkit is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Library General Public License as published by
00013 // the Free Software Foundation; either version 2, or (at your option)
00014 // any later version.
00015 //
00016 // The SC Toolkit is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU Library General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Library General Public License
00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00024 //
00025 // The U.S. Government is granted a limited license as per AL 91-7.
00026 //
00027 
00028 #ifndef _util_keyval_keyval_h
00029 #define _util_keyval_keyval_h
00030 #ifdef __GNUG__
00031 #pragma interface
00032 #endif
00033 
00034 #include <iostream>
00035 #include <string.h>
00036 #include <stdlib.h>
00037 #include <stdarg.h>
00038 
00039 #include <util/container/avlmap.h>
00040 #include <util/class/class.h>
00041 #include <util/keyval/keyvalval.h>
00042 
00043 class KeyValKeyword {
00044   private:
00045     char* keyword_;
00046   public:
00047     KeyValKeyword();
00048     KeyValKeyword(const char* name);
00049     KeyValKeyword(const KeyValKeyword&);
00050     ~KeyValKeyword();
00051     KeyValKeyword& operator=(const KeyValKeyword&);
00052     int operator==(const KeyValKeyword& ck) const;
00053     int operator<(const KeyValKeyword& ck) const;
00054     int hash() const;
00055     inline int cmp(const KeyValKeyword&ck) const
00056     {
00057       if (!keyword_) {
00058           if (!ck.keyword_) return 0;
00059           return -1;
00060         }
00061       if (!ck.keyword_) return 1;
00062       return strcmp(keyword_,ck.keyword_);
00063     }
00064     inline const char* name() const {return keyword_;}
00065   };
00066 
00089 class KeyVal: public RefCount {
00090     // these classes need to directly access the key_value member
00091     friend class AggregateKeyVal;
00092     friend class PrefixKeyVal;
00093   public:
00094     enum {MaxKeywordLength = 256};
00095     enum KeyValError { OK, HasNoValue, WrongType,
00096                        UnknownKeyword, OperationFailed };
00097   private:
00098     KeyValError errcod;
00099     // do not allow a copy constructor or assignment
00100     KeyVal(const KeyVal&);
00101     void operator=(const KeyVal&);
00102   protected:
00103     int verbose_;
00104 
00105     KeyVal();
00106 
00108     void seterror(KeyValError err);
00110     void seterror(KeyValValue::KeyValValueError err);
00111 
00113     virtual int    key_exists(const char*) = 0;
00115     virtual int    key_count(const char* =0);
00117     virtual Ref<KeyValValue> key_value(const char*,
00118                                      const KeyValValue& def) = 0;
00120     virtual int    key_booleanvalue(const char*,const KeyValValue& def);
00122     virtual double key_doublevalue(const char* key,const KeyValValue& def);
00124     virtual float  key_floatvalue(const char* key,const KeyValValue& def);
00126     virtual char   key_charvalue(const char* key,const KeyValValue& def);
00128     virtual int    key_intvalue(const char* key,const KeyValValue& def);
00130     virtual char*  key_pcharvalue(const char* key,const KeyValValue& def);
00132     virtual Ref<DescribedClass> key_describedclassvalue(const char* key,
00133                                                       const KeyValValue& def);
00134 
00135   public:
00136     virtual ~KeyVal();
00137 
00138     // For nonindexed things.   If a subclass defines one of these,
00139     // then the overloaded functions will be hidden.  The key_... functions
00140     // should be overridden instead.
00141 
00144     int    exists(const char*);
00149     int    count(const char* =0);
00151     Ref<KeyValValue> value(const char* = 0,
00152                          const KeyValValue& def=KeyValValue());
00154     int    booleanvalue(const char* key = 0,
00155                         const KeyValValue& def=KeyValValueboolean());
00157     double doublevalue(const char* key = 0,
00158                        const KeyValValue& def=KeyValValuedouble());
00160     float  floatvalue(const char* key = 0,
00161                       const KeyValValue& def=KeyValValuefloat());
00163     char   charvalue(const char* key = 0,
00164                      const KeyValValue& def=KeyValValuechar());
00166     int    intvalue(const char* key = 0,
00167                     const KeyValValue& def=KeyValValueint());
00170     char*  pcharvalue(const char* key = 0,
00171                       const KeyValValue& def=KeyValValuepchar());
00173     Ref<DescribedClass> describedclassvalue(const char* key = 0,
00174                      const KeyValValue& def=KeyValValueRefDescribedClass());
00175 
00176     // For vectors:
00177     int    exists(const char*,int);
00178     int    count(const char*,int);
00179     int    booleanvalue(const char*,int,
00180                         const KeyValValue& def=KeyValValueboolean());
00181     double doublevalue(const char* key,int,
00182                        const KeyValValue& def=KeyValValuedouble());
00183     float  floatvalue(const char* key,int,
00184                       const KeyValValue& def=KeyValValuefloat());
00185     char   charvalue(const char* key,int,
00186                      const KeyValValue& def=KeyValValuechar());
00187     int    intvalue(const char* key,int,
00188                     const KeyValValue& def=KeyValValueint());
00189     char*  pcharvalue(const char* key,int,
00190                       const KeyValValue& def=KeyValValuepchar());
00191     Ref<DescribedClass> describedclassvalue(const char* key,int,
00192                      const KeyValValue& def=KeyValValueRefDescribedClass());
00193 
00194     int    exists(int i);
00195     int    count(int i);
00196     int    booleanvalue(int i,
00197                         const KeyValValue& def=KeyValValueboolean());
00198     double doublevalue(int i,
00199                        const KeyValValue& def=KeyValValuedouble());
00200     float  floatvalue(int i,
00201                       const KeyValValue& def=KeyValValuefloat());
00202     char   charvalue(int i,
00203                      const KeyValValue& def=KeyValValuechar());
00204     int    intvalue(int i,
00205                     const KeyValValue& def=KeyValValueint());
00206     char*  pcharvalue(int i,
00207                       const KeyValValue& def=KeyValValuepchar());
00208     Ref<DescribedClass> describedclassvalue(int i,
00209                      const KeyValValue& def=KeyValValueRefDescribedClass());
00210 
00211     // For arrays:
00212     int    exists(const char*,int,int);
00213     int    count(const char*,int,int);
00214     int    booleanvalue(const char*,int,int,
00215                         const KeyValValue& def=KeyValValueboolean());
00216     double doublevalue(const char* key,int,int,
00217                        const KeyValValue& def=KeyValValuedouble());
00218     float  floatvalue(const char* key,int,int,
00219                       const KeyValValue& def=KeyValValuefloat());
00220     char   charvalue(const char* key,int,int,
00221                      const KeyValValue& def=KeyValValuechar());
00222     int    intvalue(const char* key,int,int,
00223                     const KeyValValue& def=KeyValValueint());
00224     char*  pcharvalue(const char* key,int,int,
00225                       const KeyValValue& def=KeyValValuepchar());
00226     Ref<DescribedClass> describedclassvalue(const char* key,int,int,
00227                      const KeyValValue& def=KeyValValueRefDescribedClass());
00228 
00229     int    exists(int i,int j);
00230     int    count(int i,int j);
00231     int    booleanvalue(int i,int j,
00232                         const KeyValValue& def=KeyValValueboolean());
00233     double doublevalue(int i,int j,
00234                        const KeyValValue& def=KeyValValuedouble());
00235     float  floatvalue(int i,int j,
00236                       const KeyValValue& def=KeyValValuefloat());
00237     char   charvalue(int i,int j,
00238                      const KeyValValue& def=KeyValValuechar());
00239     int    intvalue(int i,int j,
00240                     const KeyValValue& def=KeyValValueint());
00241     char*  pcharvalue(int i,int j,
00242                       const KeyValValue& def=KeyValValuepchar());
00243     Ref<DescribedClass> describedclassvalue(int i,int j,
00244                      const KeyValValue& def=KeyValValueRefDescribedClass());
00245 
00246     // For all else:
00247     int    Va_exists(const char*,int,...);
00248     int    Va_count(const char*,int,...);
00249     int    Va_booleanvalue(const char*,int,...);
00250     double Va_doublevalue(const char* key,int,...);
00251     float  Va_floatvalue(const char* key,int,...);
00252     char   Va_charvalue(const char* key,int,...);
00253     int    Va_intvalue(const char* key,int,...);
00254     char*  Va_pcharvalue(const char* key,int,...);
00255     Ref<DescribedClass> Va_describedclassvalue(const char* key,int,...);
00256 
00258     KeyValError error();
00260     const char*  errormsg(KeyValError err);
00262     const char*  errormsg();
00263 
00264     virtual void errortrace(std::ostream&fp=ExEnv::err());
00265     virtual void dump(std::ostream&fp=ExEnv::err());
00266 
00268     virtual void print_unseen(std::ostream&fp=ExEnv::out());
00272     virtual int have_unseen();
00273 
00275     void verbose(int v) { verbose_ = v; }
00277     int verbose() const { return verbose_; }
00278 };
00279 
00280 
00281 
00282 // this class allows keyval associations to be set up by the program,
00283 // rather than determined by an external file
00284 class AssignedKeyVal: public KeyVal {
00285   private:
00286     AVLMap<KeyValKeyword,Ref<KeyValValue> > _map;
00287     // do not allow a copy constructor or assignment
00288     AssignedKeyVal(const AssignedKeyVal&);
00289     void operator=(const AssignedKeyVal&);
00290   protected:
00291     int    key_exists(const char*);
00292     Ref<KeyValValue> key_value(const char*,
00293                              const KeyValValue& def);
00294   public:
00295     AssignedKeyVal();
00296     ~AssignedKeyVal();
00297 
00298     void assign(const char*, const Ref<KeyValValue>&);
00299     void assign(const char*, double);
00300     void assignboolean(const char*, int);
00301     void assign(const char*, float);
00302     void assign(const char*, char);
00303     void assign(const char*, int);
00304     void assign(const char*, const char*);
00305     void assign(const char*, const Ref<DescribedClass>&);
00306 
00307     void clear();
00308 };
00309 
00310 
00311 
00312 class StringKeyVal: public KeyVal {
00313   private:
00314     // once a described class is found it is kept here so
00315     // multiple references to it return the same instance
00316     AVLMap<KeyValKeyword,Ref<KeyValValue> > _map;
00317     // do not allow a copy constructor or assignment
00318     StringKeyVal(const StringKeyVal&);
00319     void operator=(const StringKeyVal&);
00320   protected:
00321     StringKeyVal();
00322     int    key_exists(const char*);
00323     Ref<KeyValValue> key_value(const char*,
00324                              const KeyValValue& def);
00325   public:
00326     virtual ~StringKeyVal();
00327     virtual const char* stringvalue(const char *) = 0;
00328     // returns the name of the exact class the object at the keyword
00329     virtual const char* classname(const char*);
00330     // returns a string which is the actual keyword if some sort
00331     // of variable substitution takes place (needed to make multiple
00332     // references to the same object work in input files)
00333     virtual const char* truekeyword(const char*);
00334 
00335     virtual void errortrace(std::ostream&fp=ExEnv::err());
00336     virtual void dump(std::ostream&fp=ExEnv::err());
00337 };
00338 
00339 class AggregateKeyVal : public KeyVal {
00340   private:
00341     enum { MaxKeyVal = 4 };
00342     Ref<KeyVal> kv[MaxKeyVal];
00343     Ref<KeyVal> getkeyval(const char*key);
00344     // do not allow a copy constructor or assignment
00345     AggregateKeyVal(const AggregateKeyVal&);
00346     void operator=(const AggregateKeyVal&);
00347   protected:
00348     int    key_exists(const char*);
00349     Ref<KeyValValue> key_value(const char*,
00350                              const KeyValValue& def);
00351   public:
00352     AggregateKeyVal(const Ref<KeyVal>&);
00353     AggregateKeyVal(const Ref<KeyVal>&,const Ref<KeyVal>&);
00354     AggregateKeyVal(const Ref<KeyVal>&,const Ref<KeyVal>&,const Ref<KeyVal>&);
00355     AggregateKeyVal(const Ref<KeyVal>&,const Ref<KeyVal>&,const Ref<KeyVal>&,
00356                     const Ref<KeyVal>&);
00357     ~AggregateKeyVal();
00358     void errortrace(std::ostream&fp=ExEnv::err());
00359     void dump(std::ostream&fp=ExEnv::err());
00360 };
00361 
00362 class PrefixKeyVal : public KeyVal {
00363   private:
00364     char* prefix;
00365     Ref<KeyVal> keyval;
00366     void setup(const char*,int,int,int,int,int);
00367     int getnewprefixkey(const char*key,char*newkey);
00368     // do not allow a copy constructor or assignment
00369     PrefixKeyVal(const PrefixKeyVal&);
00370     void operator=(const PrefixKeyVal&);
00371     int    key_exists(const char*);
00372     Ref<KeyValValue> key_value(const char*,
00373                              const KeyValValue& def);
00374   public:
00375     PrefixKeyVal(const Ref<KeyVal>&,int);
00376     PrefixKeyVal(const Ref<KeyVal>&,int,int);
00377     PrefixKeyVal(const Ref<KeyVal>&,int,int,int);
00378     PrefixKeyVal(const Ref<KeyVal>&,int,int,int,int);
00379     PrefixKeyVal(const Ref<KeyVal>&,const char*);
00380     PrefixKeyVal(const Ref<KeyVal>&,const char*,int);
00381     PrefixKeyVal(const Ref<KeyVal>&,const char*,int,int);
00382     PrefixKeyVal(const Ref<KeyVal>&,const char*,int,int,int);
00383     PrefixKeyVal(const Ref<KeyVal>&,const char*,int,int,int,int);
00384     // old CTOR syntax (use the above instead)
00385     PrefixKeyVal(const char*,const Ref<KeyVal>&);
00386     PrefixKeyVal(const char*,const Ref<KeyVal>&,int);
00387     PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int);
00388     PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int,int);
00389     PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int,int,int);
00390     ~PrefixKeyVal();
00391     void errortrace(std::ostream&fp=ExEnv::err());
00392     void dump(std::ostream&fp=ExEnv::err());
00393 };
00394 
00395 class IPV2;
00399 class ParsedKeyVal : public StringKeyVal {
00400   private:
00401     int nfile;
00402     char**file;
00403     int nfp;
00404     IPV2* ipv2;
00405     // do not allow a copy constructor or assignment
00406     ParsedKeyVal(const ParsedKeyVal&);
00407     void operator=(const ParsedKeyVal&);
00408   public:
00410     ParsedKeyVal();
00412     ParsedKeyVal(const char*file);
00414     ParsedKeyVal(std::istream&s);
00417     ParsedKeyVal(IPV2*);
00423     ParsedKeyVal(const char*,const Ref<KeyVal>&);
00425     ~ParsedKeyVal();
00426 
00429     static void cat_files(const char*,const Ref<KeyVal>&,std::ostream &o);
00430 
00432     void read(const char*);
00434     void read(std::istream&);
00436     void parse_string(const char *);
00437 
00438     // Overrides of parent members.
00439     const char* stringvalue(const char*);
00440     const char* classname(const char*);
00441     const char* truekeyword(const char*);
00442     void errortrace(std::ostream&fp=ExEnv::err());
00443     void dump(std::ostream&fp=ExEnv::err());
00444     void print_unseen(std::ostream&fp=ExEnv::out());
00445     int have_unseen();
00446 };
00447 
00448 
00449 
00450 #endif /* _KeyVal_h */
00451 
00452 // Local Variables:
00453 // mode: c++
00454 // c-file-style: "CLJ"
00455 // End:

Generated at Thu Oct 4 18:08:45 2001 for MPQC 2.0.0 using the documentation package Doxygen 1.2.5.