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>
    00036 #include <map>
    00037
    00038 #include <string.h>
    00039 #include <stdlib.h>
    00040 #include <stdarg.h>
    00041
    00042 #include <util/class/class.h>
    00043 #include <util/keyval/keyvalval.h>
    00044
    00045 namespace sc {
    00046
    00069 class KeyVal: public RefCount {
    00070     // these classes need to directly access the key_value member
    00071     friend class AggregateKeyVal;
    00072     friend class PrefixKeyVal;
    00073   public:
    00074     enum {MaxKeywordLength = 256};
    00075     enum KeyValError { OK, HasNoValue, WrongType,
    00076                        UnknownKeyword, OperationFailed };
    00077   private:
    00078     KeyValError errcod;
    00079     // do not allow a copy constructor or assignment
    00080     KeyVal(const KeyVal&);
    00081     void operator=(const KeyVal&);
    00082   protected:
    00083     int verbose_;
    00084
    00085     KeyVal();
    00086
    00088     void seterror(KeyValError err);
    00090     void seterror(KeyValValue::KeyValValueError err);
    00091
    00093     virtual int    key_exists(const char*) = 0;
    00095     virtual int    key_count(const char* =0);
    00097     virtual Ref<KeyValValue> key_value(const char*,
    00098                                      const KeyValValue& def) = 0;
    00100     virtual int    key_booleanvalue(const char*,const KeyValValue& def);
    00102     virtual double key_doublevalue(const char* key,const KeyValValue& def);
    00104     virtual float  key_floatvalue(const char* key,const KeyValValue& def);
    00106     virtual char   key_charvalue(const char* key,const KeyValValue& def);
    00108     virtual int    key_intvalue(const char* key,const KeyValValue& def);
    00110     virtual size_t key_sizevalue(const char* key,const KeyValValue& def);
    00112     virtual char*  key_pcharvalue(const char* key,const KeyValValue& def);
    00114     virtual std::string key_stringvalue(const char* key,
    00115                                         const KeyValValue& def);
    00117     virtual Ref<DescribedClass> key_describedclassvalue(const char* key,
    00118                                                       const KeyValValue& def);
    00119
    00120   public:
    00121     virtual ~KeyVal();
    00122
    00123     // For nonindexed things.   If a subclass defines one of these,
    00124     // then the overloaded functions will be hidden.  The key_... functions
    00125     // should be overridden instead.
    00126
    00129     int    exists(const char*);
    00134     int    count(const char* =0);
    00136     Ref<KeyValValue> value(const char* = 0,
    00137                          const KeyValValue& def=KeyValValue());
    00139     int    booleanvalue(const char* key = 0,
    00140                         const KeyValValue& def=KeyValValueboolean());
    00142     double doublevalue(const char* key = 0,
    00143                        const KeyValValue& def=KeyValValuedouble());
    00145     float  floatvalue(const char* key = 0,
    00146                       const KeyValValue& def=KeyValValuefloat());
    00148     char   charvalue(const char* key = 0,
    00149                      const KeyValValue& def=KeyValValuechar());
    00151     int    intvalue(const char* key = 0,
    00152                     const KeyValValue& def=KeyValValueint());
    00154     size_t sizevalue(const char* key = 0,
    00155                      const KeyValValue& def=KeyValValuesize());
    00158     char*  pcharvalue(const char* key = 0,
    00159                       const KeyValValue& def=KeyValValuepchar());
    00161     std::string stringvalue(const char* key = 0,
    00162                             const KeyValValue& def=KeyValValuestring());
    00164     Ref<DescribedClass> describedclassvalue(const char* key = 0,
    00165                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00166
    00174     int    exists(const char* key,int i);
    00175     int    count(const char* key,int i);
    00176     int    booleanvalue(const char* key,int i,
    00177                         const KeyValValue& def=KeyValValueboolean());
    00178     double doublevalue(const char* key,int i,
    00179                        const KeyValValue& def=KeyValValuedouble());
    00180     float  floatvalue(const char* key,int i,
    00181                       const KeyValValue& def=KeyValValuefloat());
    00182     char   charvalue(const char* key,int i,
    00183                      const KeyValValue& def=KeyValValuechar());
    00184     int    intvalue(const char* key,int i,
    00185                     const KeyValValue& def=KeyValValueint());
    00186     size_t sizevalue(const char* key,int i,
    00187                      const KeyValValue& def=KeyValValuesize());
    00188     char*  pcharvalue(const char* key,int i,
    00189                       const KeyValValue& def=KeyValValuepchar());
    00190     std::string stringvalue(const char* key,int i,
    00191                             const KeyValValue& def=KeyValValuestring());
    00192     Ref<DescribedClass> describedclassvalue(const char* key,int,
    00193                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00194
    00195     int    exists(int i);
    00196     int    count(int i);
    00197     int    booleanvalue(int i,
    00198                         const KeyValValue& def=KeyValValueboolean());
    00199     double doublevalue(int i,
    00200                        const KeyValValue& def=KeyValValuedouble());
    00201     float  floatvalue(int i,
    00202                       const KeyValValue& def=KeyValValuefloat());
    00203     char   charvalue(int i,
    00204                      const KeyValValue& def=KeyValValuechar());
    00205     int    intvalue(int i,
    00206                     const KeyValValue& def=KeyValValueint());
    00207     size_t sizevalue(int i,
    00208                      const KeyValValue& def=KeyValValuesize());
    00209     char*  pcharvalue(int i,
    00210                       const KeyValValue& def=KeyValValuepchar());
    00211     std::string stringvalue(int i,
    00212                             const KeyValValue& def=KeyValValuestring());
    00213     Ref<DescribedClass> describedclassvalue(int i,
    00214                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00216
    00223     int    exists(const char*,int,int);
    00224     int    count(const char*,int,int);
    00225     int    booleanvalue(const char*,int,int,
    00226                         const KeyValValue& def=KeyValValueboolean());
    00227     double doublevalue(const char* key,int,int,
    00228                        const KeyValValue& def=KeyValValuedouble());
    00229     float  floatvalue(const char* key,int,int,
    00230                       const KeyValValue& def=KeyValValuefloat());
    00231     char   charvalue(const char* key,int,int,
    00232                      const KeyValValue& def=KeyValValuechar());
    00233     int    intvalue(const char* key,int,int,
    00234                     const KeyValValue& def=KeyValValueint());
    00235     size_t sizevalue(const char* key,int,int,
    00236                      const KeyValValue& def=KeyValValuesize());
    00237     char*  pcharvalue(const char* key,int,int,
    00238                       const KeyValValue& def=KeyValValuepchar());
    00239     std::string stringvalue(const char* key,int,int,
    00240                             const KeyValValue& def=KeyValValuestring());
    00241     Ref<DescribedClass> describedclassvalue(const char* key,int,int,
    00242                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00243
    00244     int    exists(int i,int j);
    00245     int    count(int i,int j);
    00246     int    booleanvalue(int i,int j,
    00247                         const KeyValValue& def=KeyValValueboolean());
    00248     double doublevalue(int i,int j,
    00249                        const KeyValValue& def=KeyValValuedouble());
    00250     float  floatvalue(int i,int j,
    00251                       const KeyValValue& def=KeyValValuefloat());
    00252     char   charvalue(int i,int j,
    00253                      const KeyValValue& def=KeyValValuechar());
    00254     int    intvalue(int i,int j,
    00255                     const KeyValValue& def=KeyValValueint());
    00256     size_t sizevalue(int i,int j,
    00257                      const KeyValValue& def=KeyValValuesize());
    00258     char*  pcharvalue(int i,int j,
    00259                       const KeyValValue& def=KeyValValuepchar());
    00260     std::string stringvalue(int i,int j,
    00261                             const KeyValValue& def=KeyValValuestring());
    00262     Ref<DescribedClass> describedclassvalue(int i,int j,
    00263                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00265
    00271     int    Va_exists(const char* key,int nindex,...);
    00272     int    Va_count(const char* key,int nindex,...);
    00273     int    Va_booleanvalue(const char* key,int nindex,...);
    00274     double Va_doublevalue(const char* key,int nindex,...);
    00275     float  Va_floatvalue(const char* key,int nindex,...);
    00276     char   Va_charvalue(const char* key,int nindex,...);
    00277     int    Va_intvalue(const char* key,int nindex,...);
    00278     size_t Va_sizevalue(const char* key,int nindex,...);
    00279     char*  Va_pcharvalue(const char* key,int nindex,...);
    00280     std::string Va_stringvalue(const char* key,int nindex,...);
    00281     Ref<DescribedClass> Va_describedclassvalue(const char* key,int nindex,...);
    00283
    00285     KeyValError error();
    00287     const char*  errormsg(KeyValError err);
    00289     const char*  errormsg();
    00291     virtual void errortrace(std::ostream&fp=ExEnv::err0());
    00293     virtual void dump(std::ostream&fp=ExEnv::err0());
    00294
    00296     virtual void print_unseen(std::ostream&fp=ExEnv::out0());
    00300     virtual int have_unseen();
    00301
    00303     void verbose(int v) { verbose_ = v; }
    00305     int verbose() const { return verbose_; }
    00306 };
    00307
    00308
    00309
    00312 class AssignedKeyVal: public KeyVal {
    00313   private:
    00314     std::map<std::string,Ref<KeyValValue> > _map;
    00315     // do not allow a copy constructor or assignment
    00316     AssignedKeyVal(const AssignedKeyVal&);
    00317     void operator=(const AssignedKeyVal&);
    00318   protected:
    00319     int    key_exists(const char*);
    00320     Ref<KeyValValue> key_value(const char*,
    00321                              const KeyValValue& def);
    00322   public:
    00323     AssignedKeyVal();
    00324     ~AssignedKeyVal();
    00325
    00328     void assign(const char* key, const Ref<KeyValValue>& val);
    00329     void assign(const char* key, double val);
    00330     void assignboolean(const char* key, int val);
    00331     void assign(const char* key, float val);
    00332     void assign(const char* key, char val);
    00333     void assign(const char* key, int val);
    00334     void assign(const char* key, const char* val);
    00335     void assign(const char* key, const Ref<DescribedClass>& val);
    00337
    00339     void clear();
    00340 };
    00341
    00342
    00343
    00348 class StringKeyVal: public KeyVal {
    00349   private:
    00350     // once a described class is found it is kept here so
    00351     // multiple references to it return the same instance
    00352     std::map<std::string,Ref<KeyValValue> > _map;
    00353     // do not allow a copy constructor or assignment
    00354     StringKeyVal(const StringKeyVal&);
    00355     void operator=(const StringKeyVal&);
    00356   protected:
    00357     StringKeyVal();
    00358     int    key_exists(const char*);
    00359     Ref<KeyValValue> key_value(const char*,
    00360                              const KeyValValue& def);
    00361   public:
    00362     virtual ~StringKeyVal();
    00364     virtual const char* stringrep(const char *key) = 0;
    00367     virtual const char* classname(const char*);
    00371     virtual const char* truekeyword(const char*);
    00372
    00374
    00375     virtual void errortrace(std::ostream&fp=ExEnv::err0());
    00376     virtual void dump(std::ostream&fp=ExEnv::err0());
    00378 };
    00379
    00384 class AggregateKeyVal : public KeyVal {
    00385   private:
    00386     enum { MaxKeyVal = 4 };
    00387     Ref<KeyVal> kv[MaxKeyVal];
    00388     Ref<KeyVal> getkeyval(const char*key);
    00389     // do not allow a copy constructor or assignment
    00390     AggregateKeyVal(const AggregateKeyVal&);
    00391     void operator=(const AggregateKeyVal&);
    00392   protected:
    00393     int    key_exists(const char*);
    00394     Ref<KeyValValue> key_value(const char*,
    00395                              const KeyValValue& def);
    00396   public:
    00401     AggregateKeyVal(const Ref<KeyVal>& keyval1);
    00402     AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2);
    00403     AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2,
    00404                     const Ref<KeyVal>& keyval3);
    00405     AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2,
    00406                     const Ref<KeyVal>& keyval3, const Ref<KeyVal>& keyval4);
    00408     ~AggregateKeyVal();
    00409     void errortrace(std::ostream&fp=ExEnv::err0());
    00410     void dump(std::ostream&fp=ExEnv::err0());
    00411 };
    00412
    00455 class PrefixKeyVal : public KeyVal {
    00456   private:
    00457     char* prefix;
    00458     Ref<KeyVal> keyval;
    00459     void setup(const char*,int,int,int,int,int);
    00460     int getnewprefixkey(const char*key,char*newkey);
    00461     // do not allow a copy constructor or assignment
    00462     PrefixKeyVal(const PrefixKeyVal&);
    00463     void operator=(const PrefixKeyVal&);
    00464     int    key_exists(const char*);
    00465     Ref<KeyValValue> key_value(const char*,
    00466                              const KeyValValue& def);
    00467   public:
    00470     PrefixKeyVal(const Ref<KeyVal>&,int i);
    00471     PrefixKeyVal(const Ref<KeyVal>&,int i,int j);
    00472     PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k);
    00473     PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k,int l);
    00474     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix);
    00475     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i);
    00476     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j);
    00477     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k);
    00478     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k,int l);
    00480
    00481
    00482     PrefixKeyVal(const char*,const Ref<KeyVal>&);
    00483     PrefixKeyVal(const char*,const Ref<KeyVal>&,int);
    00484     PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int);
    00485     PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int,int);
    00486     PrefixKeyVal(const char*,const Ref<KeyVal>&,int,int,int,int);
    00488     ~PrefixKeyVal();
    00489     void errortrace(std::ostream&fp=ExEnv::err0());
    00490     void dump(std::ostream&fp=ExEnv::err0());
    00491 };
    00492
    00493 class IPV2;
    00497 class ParsedKeyVal : public StringKeyVal {
    00498   private:
    00499     int nfile;
    00500     char**file;
    00501     int nfp;
    00502     IPV2* ipv2;
    00503     // do not allow a copy constructor or assignment
    00504     ParsedKeyVal(const ParsedKeyVal&);
    00505     void operator=(const ParsedKeyVal&);
    00506   public:
    00508     ParsedKeyVal();
    00510     ParsedKeyVal(const char*file);
    00512     ParsedKeyVal(std::istream&s);
    00515     ParsedKeyVal(IPV2*);
    00521     ParsedKeyVal(const char*,const Ref<KeyVal>&);
    00523     ~ParsedKeyVal();
    00524
    00527     static void cat_files(const char*,const Ref<KeyVal>&,std::ostream &o);
    00528
    00530     void read(const char*);
    00532     void read(std::istream&);
    00534     void parse_string(const char *);
    00535
    00537
    00538     const char* stringrep(const char*);
    00539     const char* classname(const char*);
    00540     const char* truekeyword(const char*);
    00541     void errortrace(std::ostream&fp=ExEnv::err0());
    00542     void dump(std::ostream&fp=ExEnv::err0());
    00543     void print_unseen(std::ostream&fp=ExEnv::out0());
    00544     int have_unseen();
    00546 };
    00547
    00548 }
    00549
    00550 #endif /* _KeyVal_h */
    00551
    00552 // Local Variables:
    00553 // mode: c++
    00554 // c-file-style: "CLJ"
    00555 // End: