Fix several warnings that appear in gcc 4.3.2.
[wvstreams.git] / include / wvconf.h
blobfd7148a94b4eea5997a0b72fdac9265bf65db206
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Definition of the WvConfigFile, WvConfigSection, and WvConfigEntry classes,
6 * which are used to read and write entries from a Windows-INI-style file.
8 * Created: Sept 12 1997 D. Coombs
12 #ifndef __WVCONF_H
13 #define __WVCONF_H
15 #include "strutils.h"
16 #include "wvlinklist.h"
17 #include "wvlog.h"
18 #include "wvstringlist.h"
19 #include "wvtr1.h"
22 #ifdef __WVCONFEMU_H
23 #warning "disabling wvconfemu transparent emulation"
24 #undef WvConf
25 #undef WvConfigSection
26 #undef WvConfigSectionList
27 #undef WvConfigEntry
28 #undef WvConfigEntryList
29 #endif
32 class WvConf;
35 class WvConfigEntry
37 public:
38 WvConfigEntry();
39 WvConfigEntry(WvStringParm _name, WvStringParm _value);
40 ~WvConfigEntry();
42 void set(WvStringParm _value)
43 { value = _value; }
45 WvString name;
46 WvString value;
50 DeclareWvList(WvConfigEntry);
53 class WvConfigSection : public WvConfigEntryList
55 public:
56 WvConfigSection(WvStringParm name);
57 ~WvConfigSection();
59 WvConfigEntry *operator[] (WvStringParm s);
61 const char *get(WvStringParm entry, const char *def_val = NULL);
62 void set(WvStringParm entry, WvStringParm value);
63 void set(WvConfigEntry *e, WvStringParm value);
65 // add an entry to the end of the section, _assuming_ no duplicates exist
66 void quick_set(WvStringParm entry, WvStringParm value);
68 void dump(WvStream &fp);
70 WvString name;
74 // parameters are: userdata, section, entry, oldval, newval
75 typedef wv::function<void(void*, WvStringParm, WvStringParm, WvStringParm, WvStringParm)> WvConfCallback;
78 class WvConfCallbackInfo
80 public:
81 WvConfCallback callback;
82 void *userdata, *cookie;
83 const WvString section, entry;
85 WvConfCallbackInfo(WvConfCallback _callback, void *_userdata,
86 WvStringParm _section, WvStringParm _entry,
87 void *_cookie)
88 : callback(_callback), section(_section), entry(_entry)
89 { userdata = _userdata; cookie = _cookie; }
93 DeclareWvList(WvConfCallbackInfo);
94 DeclareWvList(WvConfigSection);
97 class WvAuthDaemon;
98 class WvAuthDaemonSvc;
101 * WvConf configuration file management class: used to read/write config
102 * files that are formatted in the style of Windows .ini files.
104 class WvConf : public WvConfigSectionList
106 public:
107 WvConf(WvStringParm _filename, int _create_mode = 0666);
108 ~WvConf();
110 bool isok() const
111 { return !error; }
112 bool isclean() const
113 { return isok() && !dirty; }
114 void save(WvStringParm filename);
115 void save();
116 void flush();
118 WvConfigSection *operator[] (WvStringParm s);
120 static int check_for_bool_string(const char *s);
121 int parse_wvconf_request(char *request, char *&section, char *&entry,
122 char *&value);
124 int getint(WvStringParm section, WvStringParm entry, int def_val);
126 const char *get(WvStringParm section, WvStringParm entry,
127 const char *def_val = NULL);
128 WvString getraw(WvString wvconfstr, int &parse_error);
130 int fuzzy_getint(WvStringList &sect, WvStringParm entry,
131 int def_val);
132 const char *fuzzy_get(WvStringList &sect, WvStringParm entry,
133 const char *def_val = NULL);
135 int fuzzy_getint(WvStringList &sect, WvStringList &entry,
136 int def_val);
137 const char *fuzzy_get(WvStringList & sect, WvStringList & ent,
138 const char *def_val = NULL);
140 void setint(WvStringParm section, WvStringParm entry, int value);
141 void set(WvStringParm section, WvStringParm entry,
142 const char *value);
143 void setraw(WvString wvconfstr, const char *&value, int &parse_error);
145 void maybesetint(WvStringParm section, WvStringParm entry,
146 int value);
147 void maybeset(WvStringParm section, WvStringParm entry,
148 const char *value);
150 void delete_section(WvStringParm section);
152 // section and entry may be blank -- that means _all_ sections/entries!
153 // the 'cookie' is a random value that must be unique between all
154 // registered callbacks on a particular key. (Hint: maybe you should
155 // use your 'this' pointer.)
156 void add_callback(WvConfCallback callback, void *userdata,
157 WvStringParm section, WvStringParm entry, void *cookie);
158 void del_callback(WvStringParm section, WvStringParm entry, void *cookie);
159 void run_callbacks(WvStringParm section, WvStringParm entry,
160 WvStringParm oldvalue, WvStringParm newvalue);
161 void run_all_callbacks();
163 // generic callback function for setting a bool to "true" when changed
164 void setbool(void *userdata,
165 WvStringParm section, WvStringParm entry,
166 WvStringParm oldval, WvStringParm newval);
168 // generic callback for adding an entry name to name list when changed
169 void addname(void *userdata,
170 WvStringParm section, WvStringParm entry,
171 WvStringParm oldval, WvStringParm newval);
173 // generic callback to create a file with a one-line backup string
174 void addfile(void *userdata,
175 WvStringParm section, WvStringParm entry,
176 WvStringParm oldval, WvStringParm newval);
178 void add_addfile(WvString *filename, WvStringParm sect, WvStringParm ent)
179 { add_callback(wv::bind(&WvConf::addfile, this, _1, _2, _3, _4, _5),
180 filename, sect, ent, new int); }
182 void add_addname(WvStringList *list, WvStringParm sect, WvStringParm ent)
183 { add_callback(wv::bind(&WvConf::addname, this, _1, _2, _3, _4, _5),
184 list, sect, ent, list); }
185 void del_addname(WvStringList *list, WvStringParm sect, WvStringParm ent)
186 { del_callback(sect, ent, list); }
188 void add_setbool(bool *b, WvStringParm section, WvStringParm entry)
189 { add_callback(wv::bind(&WvConf::setbool, this, _1, _2, _3, _4, _5),
190 b, section, entry, b); }
191 void del_setbool(bool *b, WvStringParm section, WvStringParm entry)
192 { del_callback(section, entry, b); }
194 void load_file() // append the contents of the real config file
195 { load_file(filename); }
196 void load_file(WvStringParm filename); // append any config file
198 // Gets a user's password and decrypts it. This isn't defined in wvconf.cc.
199 WvString get_passwd(WvStringParm sect, WvStringParm user);
200 WvString get_passwd(WvStringParm user)
201 { return get_passwd("Users", user); }
202 WvString get_passwd2(WvString pwenc);
204 // Check the password passed in. This isn't defined in wvconf.cc
205 // We use this function to check passwords since we may not know what
206 // the password actually is!
207 bool check_passwd(WvStringParm sect, WvStringParm user,
208 WvStringParm passwd);
209 bool check_passwd(WvStringParm user, WvStringParm passwd)
211 return check_passwd("Users", user, passwd);
214 // Check if the user exists. This isn't defined in wvconf.cc
215 bool user_exists(WvStringParm sect, WvStringParm user);
216 bool user_exists(WvStringParm user)
218 return user_exists("Users", user);
221 // Encrypts and sets a user's password. This isn't defined in wvconf.cc.
222 void set_passwd(WvStringParm sect, WvStringParm user, WvStringParm passwd);
223 void set_passwd(WvStringParm user, WvStringParm passwd)
224 { set_passwd("Users", user, passwd); }
225 WvString set_passwd2(WvStringParm passwd);
227 // Converts all passwords to unencrypted format. Not defined in wvconf.cc.
228 void convert_to_old_pw();
230 // needed by wvfast_user_import
231 void setdirty()
232 { dirty = true; }
234 private:
235 bool dirty; // true if changed since last flush()
236 bool error; // true if something has gone wrong
237 bool loaded_once; // true if load_file succeeded at least once
238 int create_mode; // if we must create config file
240 WvString filename;
241 WvLog log;
243 WvConfigSection globalsection;
244 WvConfCallbackInfoList callbacks;
246 char *parse_section(char *s);
247 char *parse_value(char *s);
249 /* The following is an ugly hack, but since WvConf is being
250 * deprecated, we don't care.
252 * It seems that check_passwd() and user_exists() need to talk to a
253 * WvAuthDaemon. However, making them virtual functions would break since
254 * everyone else has to implement them. So we'll its pointer and accessors
255 * here.
257 private:
258 WvAuthDaemon *wvauthd; // Authentication Daemon
259 public:
260 friend class WvAuthDaemonSvc;
264 #endif // __WVCONF_H