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

wvconf.h

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Definition of the WvConfigFile, WvConfigSection, and WvConfigEntry classes, 
00006  * which are used to read and write entries from a Windows-INI-style file.
00007  *
00008  * Created:     Sept 12 1997            D. Coombs
00009  *
00010  */
00011 
00012 #ifndef __WVCONF_H
00013 #define __WVCONF_H
00014 
00015 #include "strutils.h"
00016 #include "wvlinklist.h"
00017 #include "wvlog.h"
00018 #include "wvstringlist.h"
00019 #include "wvcallback.h"
00020 
00021 class WvConf;
00022 
00023 
00024 class WvConfigEntry
00025 {
00026 public:
00027     WvConfigEntry();
00028     WvConfigEntry(const WvString &_name, const WvString &_value);
00029     ~WvConfigEntry();
00030     
00031     void set(const WvString &_value)
00032         { value = _value; value.unique(); }
00033     
00034     WvString name, value;
00035 };
00036 
00037 
00038 DeclareWvList(WvConfigEntry);
00039 
00040 
00041 class WvConfigSection : public WvConfigEntryList
00042 {
00043 public:
00044     WvConfigSection(const WvString &name);
00045     ~WvConfigSection();
00046     
00047     WvConfigEntry *operator[] (const WvString &s);
00048 
00049     const char *get(const WvString &entry, const char *def_val = NULL);
00050     void set(const WvString &entry, const WvString &value);
00051     void set(WvConfigEntry *e, const WvString &value);
00052     
00053     // add an entry to the end of the section, _assuming_ no duplicates exist
00054     void quick_set(const WvString &entry, const WvString &value);
00055 
00056     void dump(WvStream &fp);
00057 
00058     WvString name;
00059 };
00060 
00061 
00062 // parameters are: userdata, section, entry, oldval, newval
00063 DeclareWvCallback(5, void, WvConfCallback,
00064                   void *,
00065                   const WvString &, const WvString &,
00066                   const WvString &, const WvString &);
00067 
00068 class WvConfCallbackInfo
00069 {
00070 public:
00071     WvConfCallback callback;
00072     void *userdata;
00073     const WvString section, entry;
00074     
00075     WvConfCallbackInfo(WvConfCallback _callback, void *_userdata,
00076                        const WvString &_section, const WvString &_entry)
00077         : callback(_callback), section(_section), entry(_entry)
00078         { userdata = _userdata; }
00079 };
00080 
00081 
00082 DeclareWvList(WvConfCallbackInfo);
00083 DeclareWvList(WvConfigSection);
00084 
00089 class WvConf : public WvConfigSectionList
00090 {
00091 public:
00092     WvConf(const WvString &_filename, int _create_mode = 0666);
00093     ~WvConf();
00094     
00095     bool isok() const
00096         { return !error; }
00097     bool isclean() const
00098         { return isok() && !dirty; }
00099     void save(const WvString &filename);
00100     void save();
00101     void flush();
00102 
00103     WvConfigSection *operator[] (const WvString &s);
00104 
00105     int getint(const WvString &section, const WvString &entry, int def_val);
00106     
00107     const char *get(const WvString &section, const WvString &entry,
00108                     const char *def_val = NULL);
00109 
00110     int fuzzy_getint(WvStringList &sect, const WvString &entry,
00111                   int def_val);
00112     const char *fuzzy_get(WvStringList &sect, const WvString &entry,
00113                           const char *def_val = NULL);
00114 
00115     int fuzzy_getint(WvStringList &sect, WvStringList &entry,
00116                   int def_val);
00117     const char *fuzzy_get(WvStringList & sect, WvStringList & ent,
00118                           const char *def_val = NULL);
00119 
00120     void setint(const WvString &section, const WvString &entry, int value);
00121     void set(const WvString &section, const WvString &entry,
00122              const char *value);
00123     
00124     void maybesetint(const WvString &section, const WvString &entry,
00125                      int value);
00126     void maybeset(const WvString &section, const WvString &entry,
00127                   const char *value);
00128 
00129     void delete_section(const WvString &section);
00130 
00131     // section and entry may be blank -- that means _all_ sections/entries!
00132     void add_callback(WvConfCallback callback, void *userdata,
00133                       const WvString &section, const WvString &entry);
00134     void del_callback(WvConfCallback callback, void *userdata,
00135                       const WvString &section, const WvString &entry);
00136     void run_callbacks(const WvString &section, const WvString &entry,
00137                        const WvString &oldvalue, const WvString &newvalue);
00138     void run_all_callbacks();
00139     
00140     // generic callback function for setting a bool to "true" when changed
00141     void setbool(void *userdata,
00142                  const WvString &section, const WvString &entry,
00143                  const WvString &oldval, const WvString &newval);
00144     
00145     void add_setbool(bool *b, const WvString &section, const WvString &entry)
00146         { add_callback(wvcallback(WvConfCallback, *this, WvConf::setbool),
00147                        b, section, entry); }
00148     void del_setbool(bool *b, const WvString &section, const WvString &entry)
00149         { del_callback(wvcallback(WvConfCallback, *this, WvConf::setbool),
00150                        b, section, entry); }
00151                     
00152     void load_file() // append the contents of the real config file
00153         { load_file(filename); }
00154     void load_file(const WvString &filename); // append any config file
00155 
00156 private:
00157     bool dirty;                 // true if changed since last flush()
00158     bool error;                 // true if something has gone wrong
00159     bool loaded_once;           // true if load_file succeeded at least once
00160     int create_mode;            // if we must create config file
00161 
00162     WvString filename;
00163     WvLog log;
00164 
00165     WvConfigSection globalsection;
00166     WvConfCallbackInfoList callbacks;
00167 
00168     char *parse_section(char *s);
00169     char *parse_value(char *s);
00170 };
00171 
00172 
00173 #endif // __WVCONF_H

Generated on Sat Aug 24 21:37:00 2002 for WvStreams by doxygen1.2.15