00001
00002
00003
00004
00005
00006 #ifndef dsApprc_h
00007 #define dsApprc_h
00008
00009 #ifdef HAVE_CONFIG_H
00010 # include <config.h>
00011 #endif
00012
00013 #include <iostream>
00014 #include <fstream>
00015
00016 #include <dsHashTable.h>
00017 #include <dsSmartException.h>
00018 #include <dsutil.h>
00019 #include <dsStrstream.h>
00020 #include <xlat.h>
00021
00022 #ifdef Win32
00023 # define DS_GLOBAL_RC "c:/etc/libdms4.rc"
00024 #else
00025 # define DS_GLOBAL_RC "/etc/libdms4.rc"
00026 #endif
00027
00028 #define MAX_INCLUDE_LEVEL 10
00029
00030 #define ADD(k,v) _apprc->add(k,v)
00031
00032 #define GET(k) _apprc->getstring(k)
00033 #define IGET(k,i) _apprc->getstring(k,i)
00034 #define GETL(k) _apprc->getlong(k)
00035 #define GETB(k) _apprc->getbool(k)
00036 #define GETD(k) _apprc->getdouble(k)
00037
00038 #define PUSH(k) _apprc->push(k)
00039 #define POP() _apprc->pop()
00040
00041 #define _ADD(s) _apprc->add(#s,s)
00042 #define _PUSH(s) _apprc->push(#s)
00043
00044 DECLARE_EXCEPTION(key);
00045 DECLARE_EXCEPTION2(INI,key);
00046
00084 class dsApprc
00085 {
00086 protected:
00087 dsHashTable *_h;
00088
00089 int _recursion_cnt;
00090
00091 char *_keyst;
00092 int _kslen;
00093 char _key_delim;
00094
00095 int _xlat_table;
00096
00097 int expand_condition(char *cond);
00098 char *expand_var(char *ptr, int *vlen = 0);
00099
00100 void _add(char *line);
00101 void _add2(const char *key, const char *val);
00102
00103 char *trim(char *src);
00104 void undefined_key_exception(const char *key);
00105 public:
00106
00110 dsApprc();
00111
00117 dsApprc(const char *filename);
00118
00124 dsApprc(std::istream& is);
00125
00126 ~dsApprc();
00127
00135 void load(const char *filename);
00136
00145 void load(std::istream& is);
00146
00151 void dump(std::ostream& os);
00152
00159 void set_delimiter( char delim );
00160
00167 void set_xlat( int table );
00168
00169
00177 void add(const char *key, const char *val);
00178
00186 void add(const char *key, int val);
00187
00194 char *seek(const char *key);
00195
00203 char *seek(const char *key, int idx);
00204
00211 void push(const char *key);
00212
00218 void push(int array_key);
00219
00225 void pop();
00226
00235 int getbool(const char *key);
00236
00249 int getbool(const char *key, int defval);
00250
00258 long getlong(const char *key);
00259
00268 long getlong(const char *key, long defval);
00269
00277 double getdouble(const char *key);
00278
00287 double getdouble(const char *key, double defval);
00288
00296 char * getstring(const char *key);
00297
00306 char *getstring(const char *key, int idx);
00307
00316 char * getstring(const char *key, char *defval);
00317
00318
00319 friend class dsGetoptMore;
00320 };
00321
00322
00323
00324
00325 inline void dsApprc::undefined_key_exception(const char *key)
00326 {
00327 if (_keyst && *key == _key_delim)
00328 throw INIException("Key (%s)%s not defined. ", _keyst, key );
00329 else
00330 throw INIException("Key %s not defined. ", key );
00331 }
00332
00333 inline void dsApprc::set_delimiter( char delim )
00334 { _key_delim = delim;
00335 }
00336
00337 inline void dsApprc::set_xlat( int table )
00338 { _xlat_table = table;
00339 }
00340
00341
00342 inline void dsApprc::add(const char *key, int val)
00343 { char tmp[16];
00344 sprintf(tmp,"%d",val);
00345 add(key,tmp);
00346 }
00347
00348 inline int dsApprc::getbool(const char *key)
00349 { char *sp;
00350 if ( !(sp = seek(key)) )
00351 undefined_key_exception(key);
00352 return ( *sp == 'y' || *sp == 'Y' || *sp == 't' || *sp == 'T' || *sp == '1' );
00353 }
00354
00355 inline int dsApprc::getbool(const char *key, int defval)
00356 { char *sp = seek(key);
00357 return (sp) ? ( *sp == 'y' || *sp == 'Y' || *sp == 't' || *sp == 'T' || *sp == '1' ) : defval;
00358 }
00359
00360
00361 inline long dsApprc::getlong(const char *key)
00362 { char *sp;
00363 if ( !(sp = seek(key)) )
00364 undefined_key_exception(key);
00365 return atol( sp );
00366 }
00367
00368 inline long dsApprc::getlong(const char *key, long defval)
00369 { char *sp = seek(key);
00370 return (sp) ? atol( sp ) : defval;
00371 }
00372
00373 inline double dsApprc::getdouble(const char *key)
00374 { char *sp;
00375 if ( !(sp = seek(key)) )
00376 undefined_key_exception(key);
00377 return atof( sp );
00378 }
00379
00380 inline double dsApprc::getdouble(const char *key, double defval)
00381 { char *sp = seek(key);
00382 return (sp) ? atof( sp ) : defval;
00383 }
00384
00385 inline char *dsApprc::getstring(const char *key)
00386 { char *sp;
00387 if ( !(sp = seek(key)) )
00388 undefined_key_exception(key);
00389 return sp;
00390 }
00391
00392 inline char *dsApprc::getstring(const char *key, char *defval)
00393 { char *sp = seek(key);
00394 return (sp) ? sp : defval;
00395 }
00396
00397 inline char *dsApprc::getstring(const char *key, int idx)
00398 { char *sp;
00399 if ( !(sp = seek(key, idx)) )
00400 undefined_key_exception(key);
00401 return sp;
00402 }
00403
00404 #endif
00405