# 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
    00175     int    exists(const char* key,int i);
    00176     int    count(const char* key,int i);
    00177     int    booleanvalue(const char* key,int i,
    00178                         const KeyValValue& def=KeyValValueboolean());
    00179     double doublevalue(const char* key,int i,
    00180                        const KeyValValue& def=KeyValValuedouble());
    00181     float  floatvalue(const char* key,int i,
    00182                       const KeyValValue& def=KeyValValuefloat());
    00183     char   charvalue(const char* key,int i,
    00184                      const KeyValValue& def=KeyValValuechar());
    00185     int    intvalue(const char* key,int i,
    00186                     const KeyValValue& def=KeyValValueint());
    00187     size_t sizevalue(const char* key,int i,
    00188                      const KeyValValue& def=KeyValValuesize());
    00189     char*  pcharvalue(const char* key,int i,
    00190                       const KeyValValue& def=KeyValValuepchar());
    00191     std::string stringvalue(const char* key,int i,
    00192                             const KeyValValue& def=KeyValValuestring());
    00193     Ref<DescribedClass> describedclassvalue(const char* key,int,
    00194                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00195
    00196     int    exists(int i);
    00197     int    count(int i);
    00198     int    booleanvalue(int i,
    00199                         const KeyValValue& def=KeyValValueboolean());
    00200     double doublevalue(int i,
    00201                        const KeyValValue& def=KeyValValuedouble());
    00202     float  floatvalue(int i,
    00203                       const KeyValValue& def=KeyValValuefloat());
    00204     char   charvalue(int i,
    00205                      const KeyValValue& def=KeyValValuechar());
    00206     int    intvalue(int i,
    00207                     const KeyValValue& def=KeyValValueint());
    00208     size_t sizevalue(int i,
    00209                      const KeyValValue& def=KeyValValuesize());
    00210     char*  pcharvalue(int i,
    00211                       const KeyValValue& def=KeyValValuepchar());
    00212     std::string stringvalue(int i,
    00213                             const KeyValValue& def=KeyValValuestring());
    00214     Ref<DescribedClass> describedclassvalue(int i,
    00215                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00217
    00225     int    exists(const char*,int,int);
    00226     int    count(const char*,int,int);
    00227     int    booleanvalue(const char*,int,int,
    00228                         const KeyValValue& def=KeyValValueboolean());
    00229     double doublevalue(const char* key,int,int,
    00230                        const KeyValValue& def=KeyValValuedouble());
    00231     float  floatvalue(const char* key,int,int,
    00232                       const KeyValValue& def=KeyValValuefloat());
    00233     char   charvalue(const char* key,int,int,
    00234                      const KeyValValue& def=KeyValValuechar());
    00235     int    intvalue(const char* key,int,int,
    00236                     const KeyValValue& def=KeyValValueint());
    00237     size_t sizevalue(const char* key,int,int,
    00238                      const KeyValValue& def=KeyValValuesize());
    00239     char*  pcharvalue(const char* key,int,int,
    00240                       const KeyValValue& def=KeyValValuepchar());
    00241     std::string stringvalue(const char* key,int,int,
    00242                             const KeyValValue& def=KeyValValuestring());
    00243     Ref<DescribedClass> describedclassvalue(const char* key,int,int,
    00244                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00245
    00246     int    exists(int i,int j);
    00247     int    count(int i,int j);
    00248     int    booleanvalue(int i,int j,
    00249                         const KeyValValue& def=KeyValValueboolean());
    00250     double doublevalue(int i,int j,
    00251                        const KeyValValue& def=KeyValValuedouble());
    00252     float  floatvalue(int i,int j,
    00253                       const KeyValValue& def=KeyValValuefloat());
    00254     char   charvalue(int i,int j,
    00255                      const KeyValValue& def=KeyValValuechar());
    00256     int    intvalue(int i,int j,
    00257                     const KeyValValue& def=KeyValValueint());
    00258     size_t sizevalue(int i,int j,
    00259                      const KeyValValue& def=KeyValValuesize());
    00260     char*  pcharvalue(int i,int j,
    00261                       const KeyValValue& def=KeyValValuepchar());
    00262     std::string stringvalue(int i,int j,
    00263                             const KeyValValue& def=KeyValValuestring());
    00264     Ref<DescribedClass> describedclassvalue(int i,int j,
    00265                      const KeyValValue& def=KeyValValueRefDescribedClass());
    00267
    00274     int    Va_exists(const char* key,int nindex,...);
    00275     int    Va_count(const char* key,int nindex,...);
    00276     int    Va_booleanvalue(const char* key,int nindex,...);
    00277     double Va_doublevalue(const char* key,int nindex,...);
    00278     float  Va_floatvalue(const char* key,int nindex,...);
    00279     char   Va_charvalue(const char* key,int nindex,...);
    00280     int    Va_intvalue(const char* key,int nindex,...);
    00281     size_t Va_sizevalue(const char* key,int nindex,...);
    00282     char*  Va_pcharvalue(const char* key,int nindex,...);
    00283     std::string Va_stringvalue(const char* key,int nindex,...);
    00284     Ref<DescribedClass> Va_describedclassvalue(const char* key,int nindex,...);
    00286
    00288     KeyValError error();
    00290     const char*  errormsg(KeyValError err);
    00292     const char*  errormsg();
    00294     virtual void errortrace(std::ostream&fp=ExEnv::err0());
    00296     virtual void dump(std::ostream&fp=ExEnv::err0());
    00297
    00299     virtual void print_unseen(std::ostream&fp=ExEnv::out0());
    00303     virtual int have_unseen();
    00304
    00306     void verbose(int v) { verbose_ = v; }
    00308     int verbose() const { return verbose_; }
    00309 };
    00310
    00311
    00312
    00315 class AssignedKeyVal: public KeyVal {
    00316   private:
    00317     std::map<std::string,Ref<KeyValValue> > _map;
    00318     // do not allow a copy constructor or assignment
    00319     AssignedKeyVal(const AssignedKeyVal&);
    00320     void operator=(const AssignedKeyVal&);
    00321   protected:
    00322     int    key_exists(const char*);
    00323     Ref<KeyValValue> key_value(const char*,
    00324                              const KeyValValue& def);
    00325   public:
    00326     AssignedKeyVal();
    00327     ~AssignedKeyVal();
    00328
    00332     void assign(const char* key, const Ref<KeyValValue>& val);
    00333     void assign(const char* key, double val);
    00334     void assignboolean(const char* key, int val);
    00335     void assign(const char* key, float val);
    00336     void assign(const char* key, char val);
    00337     void assign(const char* key, int val);
    00338     void assign(const char* key, const char* val);
    00339     void assign(const char* key, const Ref<DescribedClass>& val);
    00341
    00343     void clear();
    00344 };
    00345
    00346
    00347
    00352 class StringKeyVal: public KeyVal {
    00353   private:
    00354     // once a described class is found it is kept here so
    00355     // multiple references to it return the same instance
    00356     std::map<std::string,Ref<KeyValValue> > _map;
    00357     // do not allow a copy constructor or assignment
    00358     StringKeyVal(const StringKeyVal&);
    00359     void operator=(const StringKeyVal&);
    00360   protected:
    00361     StringKeyVal();
    00362     int    key_exists(const char*);
    00363     Ref<KeyValValue> key_value(const char*,
    00364                              const KeyValValue& def);
    00365   public:
    00366     virtual ~StringKeyVal();
    00368     virtual const char* stringrep(const char *key) = 0;
    00371     virtual const char* classname(const char*);
    00375     virtual const char* truekeyword(const char*);
    00376
    00381     virtual void errortrace(std::ostream&fp=ExEnv::err0());
    00382     virtual void dump(std::ostream&fp=ExEnv::err0());
    00384 };
    00385
    00390 class AggregateKeyVal : public KeyVal {
    00391   private:
    00392     enum { MaxKeyVal = 4 };
    00393     Ref<KeyVal> kv[MaxKeyVal];
    00394     Ref<KeyVal> getkeyval(const char*key);
    00395     // do not allow a copy constructor or assignment
    00396     AggregateKeyVal(const AggregateKeyVal&);
    00397     void operator=(const AggregateKeyVal&);
    00398   protected:
    00399     int    key_exists(const char*);
    00400     Ref<KeyValValue> key_value(const char*,
    00401                              const KeyValValue& def);
    00402   public:
    00410     AggregateKeyVal(const Ref<KeyVal>& keyval1);
    00411     AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2);
    00412     AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2,
    00413                     const Ref<KeyVal>& keyval3);
    00414     AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2,
    00415                     const Ref<KeyVal>& keyval3, const Ref<KeyVal>& keyval4);
    00417     ~AggregateKeyVal();
    00418     void errortrace(std::ostream&fp=ExEnv::err0());
    00419     void dump(std::ostream&fp=ExEnv::err0());
    00420 };
    00421
    00464 class PrefixKeyVal : public KeyVal {
    00465   private:
    00466     char* prefix;
    00467     Ref<KeyVal> keyval;
    00468     void setup(const char*,int,int,int,int,int);
    00469     int getnewprefixkey(const char*key,char*newkey);
    00470     // do not allow a copy constructor or assignment
    00471     PrefixKeyVal(const PrefixKeyVal&);
    00472     void operator=(const PrefixKeyVal&);
    00473     int    key_exists(const char*);
    00474     Ref<KeyValValue> key_value(const char*,
    00475                              const KeyValValue& def);
    00476   public:
    00480     PrefixKeyVal(const Ref<KeyVal>&,int i);
    00481     PrefixKeyVal(const Ref<KeyVal>&,int i,int j);
    00482     PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k);
    00483     PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k,int l);
    00484     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix);
    00485     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i);
    00486     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j);
    00487     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k);
    00488     PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k,int l);
    00490     ~PrefixKeyVal();
    00491     void errortrace(std::ostream&fp=ExEnv::err0());
    00492     void dump(std::ostream&fp=ExEnv::err0());
    00493 };
    00494
    00495 class IPV2;
    00499 class ParsedKeyVal : public StringKeyVal {
    00500   private:
    00501     int nfile;
    00502     char**file;
    00503     int nfp;
    00504     IPV2* ipv2;
    00505     // do not allow a copy constructor or assignment
    00506     ParsedKeyVal(const ParsedKeyVal&);
    00507     void operator=(const ParsedKeyVal&);
    00508   public:
    00510     ParsedKeyVal();
    00512     ParsedKeyVal(const char*file);
    00514     ParsedKeyVal(std::istream&s);
    00517     ParsedKeyVal(IPV2*);
    00523     ParsedKeyVal(const char*,const Ref<KeyVal>&);
    00525     ~ParsedKeyVal();
    00526
    00529     static void cat_files(const char*,const Ref<KeyVal>&,std::ostream &o);
    00530
    00532     void read(const char*);
    00534     void read(std::istream&);
    00536     void parse_string(const char *);
    00537
    00541     const char* stringrep(const char*);
    00542     const char* classname(const char*);
    00543     const char* truekeyword(const char*);
    00544     void errortrace(std::ostream&fp=ExEnv::err0());
    00545     void dump(std::ostream&fp=ExEnv::err0());
    00546     void print_unseen(std::ostream&fp=ExEnv::out0());
    00547     int have_unseen();
    00549 };
    00550
    00551 }
    00552
    00553 #endif /* _KeyVal_h */
    00554
    00555 // Local Variables:
    00556 // mode: c++
    00557 // c-file-style: "CLJ"
    00558 // End: