keyval.h
//
// keyval.h
//
// Copyright (C) 1996 Limit Point Systems, Inc.
//
// Author: Curtis Janssen <cljanss@limitpt.com>
// Maintainer: LPS
//
// This file is part of the SC Toolkit.
//
// The SC Toolkit is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// The SC Toolkit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
// The U.S. Government is granted a limited license as per AL 91-7.
//
#ifndef _util_keyval_keyval_h
#define _util_keyval_keyval_h
#ifdef __GNUG__
#pragma interface
#endif
#include <iostream>
#include <string>
#include <map>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <util/class/class.h>
#include <util/keyval/keyvalval.h>
namespace sc {
class KeyVal: public RefCount {
// these classes need to directly access the key_value member
friend class AggregateKeyVal;
friend class PrefixKeyVal;
public:
enum {MaxKeywordLength = 256};
enum KeyValError { OK, HasNoValue, WrongType,
UnknownKeyword, OperationFailed };
private:
KeyValError errcod;
// do not allow a copy constructor or assignment
KeyVal(const KeyVal&);
void operator=(const KeyVal&);
protected:
int verbose_;
KeyVal();
void seterror(KeyValError err);
void seterror(KeyValValue::KeyValValueError err);
virtual int key_exists(const char*) = 0;
virtual int key_count(const char* =0);
virtual Ref<KeyValValue> key_value(const char*,
const KeyValValue& def) = 0;
virtual int key_booleanvalue(const char*,const KeyValValue& def);
virtual double key_doublevalue(const char* key,const KeyValValue& def);
virtual float key_floatvalue(const char* key,const KeyValValue& def);
virtual char key_charvalue(const char* key,const KeyValValue& def);
virtual int key_intvalue(const char* key,const KeyValValue& def);
virtual size_t key_sizevalue(const char* key,const KeyValValue& def);
virtual char* key_pcharvalue(const char* key,const KeyValValue& def);
virtual std::string key_stringvalue(const char* key,
const KeyValValue& def);
virtual Ref<DescribedClass> key_describedclassvalue(const char* key,
const KeyValValue& def);
public:
virtual ~KeyVal();
// For nonindexed things. If a subclass defines one of these,
// then the overloaded functions will be hidden. The key_... functions
// should be overridden instead.
int exists(const char*);
int count(const char* =0);
Ref<KeyValValue> value(const char* = 0,
const KeyValValue& def=KeyValValue());
int booleanvalue(const char* key = 0,
const KeyValValue& def=KeyValValueboolean());
double doublevalue(const char* key = 0,
const KeyValValue& def=KeyValValuedouble());
float floatvalue(const char* key = 0,
const KeyValValue& def=KeyValValuefloat());
char charvalue(const char* key = 0,
const KeyValValue& def=KeyValValuechar());
int intvalue(const char* key = 0,
const KeyValValue& def=KeyValValueint());
size_t sizevalue(const char* key = 0,
const KeyValValue& def=KeyValValuesize());
char* pcharvalue(const char* key = 0,
const KeyValValue& def=KeyValValuepchar());
std::string stringvalue(const char* key = 0,
const KeyValValue& def=KeyValValuestring());
Ref<DescribedClass> describedclassvalue(const char* key = 0,
const KeyValValue& def=KeyValValueRefDescribedClass());
int exists(const char* key,int i);
int count(const char* key,int i);
int booleanvalue(const char* key,int i,
const KeyValValue& def=KeyValValueboolean());
double doublevalue(const char* key,int i,
const KeyValValue& def=KeyValValuedouble());
float floatvalue(const char* key,int i,
const KeyValValue& def=KeyValValuefloat());
char charvalue(const char* key,int i,
const KeyValValue& def=KeyValValuechar());
int intvalue(const char* key,int i,
const KeyValValue& def=KeyValValueint());
size_t sizevalue(const char* key,int i,
const KeyValValue& def=KeyValValuesize());
char* pcharvalue(const char* key,int i,
const KeyValValue& def=KeyValValuepchar());
std::string stringvalue(const char* key,int i,
const KeyValValue& def=KeyValValuestring());
Ref<DescribedClass> describedclassvalue(const char* key,int,
const KeyValValue& def=KeyValValueRefDescribedClass());
int exists(int i);
int count(int i);
int booleanvalue(int i,
const KeyValValue& def=KeyValValueboolean());
double doublevalue(int i,
const KeyValValue& def=KeyValValuedouble());
float floatvalue(int i,
const KeyValValue& def=KeyValValuefloat());
char charvalue(int i,
const KeyValValue& def=KeyValValuechar());
int intvalue(int i,
const KeyValValue& def=KeyValValueint());
size_t sizevalue(int i,
const KeyValValue& def=KeyValValuesize());
char* pcharvalue(int i,
const KeyValValue& def=KeyValValuepchar());
std::string stringvalue(int i,
const KeyValValue& def=KeyValValuestring());
Ref<DescribedClass> describedclassvalue(int i,
const KeyValValue& def=KeyValValueRefDescribedClass());
int exists(const char*,int,int);
int count(const char*,int,int);
int booleanvalue(const char*,int,int,
const KeyValValue& def=KeyValValueboolean());
double doublevalue(const char* key,int,int,
const KeyValValue& def=KeyValValuedouble());
float floatvalue(const char* key,int,int,
const KeyValValue& def=KeyValValuefloat());
char charvalue(const char* key,int,int,
const KeyValValue& def=KeyValValuechar());
int intvalue(const char* key,int,int,
const KeyValValue& def=KeyValValueint());
size_t sizevalue(const char* key,int,int,
const KeyValValue& def=KeyValValuesize());
char* pcharvalue(const char* key,int,int,
const KeyValValue& def=KeyValValuepchar());
std::string stringvalue(const char* key,int,int,
const KeyValValue& def=KeyValValuestring());
Ref<DescribedClass> describedclassvalue(const char* key,int,int,
const KeyValValue& def=KeyValValueRefDescribedClass());
int exists(int i,int j);
int count(int i,int j);
int booleanvalue(int i,int j,
const KeyValValue& def=KeyValValueboolean());
double doublevalue(int i,int j,
const KeyValValue& def=KeyValValuedouble());
float floatvalue(int i,int j,
const KeyValValue& def=KeyValValuefloat());
char charvalue(int i,int j,
const KeyValValue& def=KeyValValuechar());
int intvalue(int i,int j,
const KeyValValue& def=KeyValValueint());
size_t sizevalue(int i,int j,
const KeyValValue& def=KeyValValuesize());
char* pcharvalue(int i,int j,
const KeyValValue& def=KeyValValuepchar());
std::string stringvalue(int i,int j,
const KeyValValue& def=KeyValValuestring());
Ref<DescribedClass> describedclassvalue(int i,int j,
const KeyValValue& def=KeyValValueRefDescribedClass());
int Va_exists(const char* key,int nindex,...);
int Va_count(const char* key,int nindex,...);
int Va_booleanvalue(const char* key,int nindex,...);
double Va_doublevalue(const char* key,int nindex,...);
float Va_floatvalue(const char* key,int nindex,...);
char Va_charvalue(const char* key,int nindex,...);
int Va_intvalue(const char* key,int nindex,...);
size_t Va_sizevalue(const char* key,int nindex,...);
char* Va_pcharvalue(const char* key,int nindex,...);
std::string Va_stringvalue(const char* key,int nindex,...);
Ref<DescribedClass> Va_describedclassvalue(const char* key,int nindex,...);
KeyValError error();
const char* errormsg(KeyValError err);
const char* errormsg();
virtual void errortrace(std::ostream&fp=ExEnv::err0());
virtual void dump(std::ostream&fp=ExEnv::err0());
virtual void print_unseen(std::ostream&fp=ExEnv::out0());
virtual int have_unseen();
void verbose(int v) { verbose_ = v; }
int verbose() const { return verbose_; }
};
class AssignedKeyVal: public KeyVal {
private:
std::map<std::string,Ref<KeyValValue> > _map;
// do not allow a copy constructor or assignment
AssignedKeyVal(const AssignedKeyVal&);
void operator=(const AssignedKeyVal&);
protected:
int key_exists(const char*);
Ref<KeyValValue> key_value(const char*,
const KeyValValue& def);
public:
AssignedKeyVal();
~AssignedKeyVal();
void assign(const char* key, const Ref<KeyValValue>& val);
void assign(const char* key, double val);
void assignboolean(const char* key, int val);
void assign(const char* key, float val);
void assign(const char* key, char val);
void assign(const char* key, int val);
void assign(const char* key, const char* val);
void assign(const char* key, const Ref<DescribedClass>& val);
void clear();
};
class StringKeyVal: public KeyVal {
private:
// once a described class is found it is kept here so
// multiple references to it return the same instance
std::map<std::string,Ref<KeyValValue> > _map;
// do not allow a copy constructor or assignment
StringKeyVal(const StringKeyVal&);
void operator=(const StringKeyVal&);
protected:
StringKeyVal();
int key_exists(const char*);
Ref<KeyValValue> key_value(const char*,
const KeyValValue& def);
public:
virtual ~StringKeyVal();
virtual const char* stringrep(const char *key) = 0;
virtual const char* classname(const char*);
virtual const char* truekeyword(const char*);
virtual void errortrace(std::ostream&fp=ExEnv::err0());
virtual void dump(std::ostream&fp=ExEnv::err0());
};
class AggregateKeyVal : public KeyVal {
private:
enum { MaxKeyVal = 4 };
Ref<KeyVal> kv[MaxKeyVal];
Ref<KeyVal> getkeyval(const char*key);
// do not allow a copy constructor or assignment
AggregateKeyVal(const AggregateKeyVal&);
void operator=(const AggregateKeyVal&);
protected:
int key_exists(const char*);
Ref<KeyValValue> key_value(const char*,
const KeyValValue& def);
public:
AggregateKeyVal(const Ref<KeyVal>& keyval1);
AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2);
AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2,
const Ref<KeyVal>& keyval3);
AggregateKeyVal(const Ref<KeyVal>& keyval1,const Ref<KeyVal>& keyval2,
const Ref<KeyVal>& keyval3, const Ref<KeyVal>& keyval4);
~AggregateKeyVal();
void errortrace(std::ostream&fp=ExEnv::err0());
void dump(std::ostream&fp=ExEnv::err0());
};
class PrefixKeyVal : public KeyVal {
private:
char* prefix;
Ref<KeyVal> keyval;
void setup(const char*,int,int,int,int,int);
int getnewprefixkey(const char*key,char*newkey);
// do not allow a copy constructor or assignment
PrefixKeyVal(const PrefixKeyVal&);
void operator=(const PrefixKeyVal&);
int key_exists(const char*);
Ref<KeyValValue> key_value(const char*,
const KeyValValue& def);
public:
PrefixKeyVal(const Ref<KeyVal>&,int i);
PrefixKeyVal(const Ref<KeyVal>&,int i,int j);
PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k);
PrefixKeyVal(const Ref<KeyVal>&,int i,int j,int k,int l);
PrefixKeyVal(const Ref<KeyVal>&,const char*prefix);
PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i);
PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j);
PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k);
PrefixKeyVal(const Ref<KeyVal>&,const char*prefix,int i,int j,int k,int l);
~PrefixKeyVal();
void errortrace(std::ostream&fp=ExEnv::err0());
void dump(std::ostream&fp=ExEnv::err0());
};
class IPV2;
class ParsedKeyVal : public StringKeyVal {
private:
int nfile;
char**file;
int nfp;
IPV2* ipv2;
// do not allow a copy constructor or assignment
ParsedKeyVal(const ParsedKeyVal&);
void operator=(const ParsedKeyVal&);
public:
ParsedKeyVal();
ParsedKeyVal(const char*file);
ParsedKeyVal(std::istream&s);
ParsedKeyVal(IPV2*);
ParsedKeyVal(const char*,const Ref<KeyVal>&);
~ParsedKeyVal();
static void cat_files(const char*,const Ref<KeyVal>&,std::ostream &o);
void read(const char*);
void read(std::istream&);
void parse_string(const char *);
const char* stringrep(const char*);
const char* classname(const char*);
const char* truekeyword(const char*);
void errortrace(std::ostream&fp=ExEnv::err0());
void dump(std::ostream&fp=ExEnv::err0());
void print_unseen(std::ostream&fp=ExEnv::out0());
int have_unseen();
};
}
#endif /* _KeyVal_h */
// Local Variables:
// mode: c++
// c-file-style: "CLJ"
// End: