WvStreams
wvconf.h
1/* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * Definition of the WvConfigFile, WvConfigSection, and WvConfigEntry classes,
6 * which are used to read and write entries from a Windows-INI-style file.
7 *
8 * Created: Sept 12 1997 D. Coombs
9 *
10 */
11
12#ifndef __WVCONF_H
13#define __WVCONF_H
14
15#include "strutils.h"
16#include "wvlinklist.h"
17#include "wvlog.h"
18#include "wvstringlist.h"
19#include "wvtr1.h"
20
21
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
30
31
32class WvConf;
33
34
36{
37public:
41
42 void set(WvStringParm _value)
43 { value = _value; }
44
45 WvString name;
46 WvString value;
47};
48
49
50DeclareWvList(WvConfigEntry);
51
52
53class WvConfigSection : public WvConfigEntryList
54{
55public:
58
59 WvConfigEntry *operator[] (WvStringParm s);
60
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);
64
65 // add an entry to the end of the section, _assuming_ no duplicates exist
66 void quick_set(WvStringParm entry, WvStringParm value);
67
68 void dump(WvStream &fp);
69
70 WvString name;
71};
72
73
74// parameters are: userdata, section, entry, oldval, newval
75typedef wv::function<void(void*, WvStringParm, WvStringParm, WvStringParm, WvStringParm)> WvConfCallback;
76
77
79{
80public:
81 WvConfCallback callback;
82 void *userdata, *cookie;
83 const WvString section, entry;
84
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; }
90};
91
92
93DeclareWvList(WvConfCallbackInfo);
94DeclareWvList(WvConfigSection);
95
96
97class WvAuthDaemon;
98class WvAuthDaemonSvc;
99
104class WvConf : public WvConfigSectionList
105{
106public:
107 WvConf(WvStringParm _filename, int _create_mode = 0666);
108 ~WvConf();
109
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();
117
118 WvConfigSection *operator[] (WvStringParm s);
119
120 static int check_for_bool_string(const char *s);
121 int parse_wvconf_request(char *request, char *&section, char *&entry,
122 char *&value);
123
124 int getint(WvStringParm section, WvStringParm entry, int def_val);
125
126 const char *get(WvStringParm section, WvStringParm entry,
127 const char *def_val = NULL);
128 WvString getraw(WvString wvconfstr, int &parse_error);
129
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);
134
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);
139
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);
144
145 void maybesetint(WvStringParm section, WvStringParm entry,
146 int value);
147 void maybeset(WvStringParm section, WvStringParm entry,
148 const char *value);
149
150 void delete_section(WvStringParm section);
151
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();
162
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);
167
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);
172
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);
177
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); }
181
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); }
187
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); }
193
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
197
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);
203
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)
210 {
211 return check_passwd("Users", user, passwd);
212 }
213
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)
217 {
218 return user_exists("Users", user);
219 }
220
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);
226
227 // Converts all passwords to unencrypted format. Not defined in wvconf.cc.
228 void convert_to_old_pw();
229
230 // needed by wvfast_user_import
231 void setdirty()
232 { dirty = true; }
233
234private:
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
239
240 WvString filename;
241 WvLog log;
242
243 WvConfigSection globalsection;
244 WvConfCallbackInfoList callbacks;
245
246 char *parse_section(char *s);
247 char *parse_value(char *s);
248
249/* The following is an ugly hack, but since WvConf is being
250 * deprecated, we don't care.
251 *
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.
256 */
257private:
258 WvAuthDaemon *wvauthd; // Authentication Daemon
259public:
260 friend class WvAuthDaemonSvc;
261};
262
263
264#endif // __WVCONF_H
WvConf configuration file management class: used to read/write config files that are formatted in the...
Definition: wvconf.h:105
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition: wvstring.h:94
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition: wvlog.h:57
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition: wvstream.h:25
This is a WvList of WvStrings, and is a really handy way to parse strings.
Definition: wvstringlist.h:28
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:330