]> git.saurik.com Git - wxWidgets.git/blob - include/wx/fileconf.h
replaced wxEmptyString with "" because it's defined in gdicmn.h (sic!) which,
[wxWidgets.git] / include / wx / fileconf.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: fileconf.h
3 // Purpose: wxFileConfig derivation of wxConfigBase
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 07.04.98 (adapted from appconf.cpp)
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Karsten Ballüder & Vadim Zeitlin
9 // Ballueder@usa.net <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _FILECONF_H
14 #define _FILECONF_H
15
16 #ifdef __GNUG__
17 #pragma interface "fileconf.h"
18 #endif
19
20 #include "wx/defs.h"
21 #include "wx/textfile.h"
22 #include "wx/string.h"
23
24 // ----------------------------------------------------------------------------
25 // compile options
26 // ----------------------------------------------------------------------------
27
28 // it won't compile without it anyhow
29 #ifndef USE_CONFIG
30 #error "Please define USE_CONFIG or remove fileconf.cpp from your makefile"
31 #endif // USE_CONFIG
32
33 // ----------------------------------------------------------------------------
34 // wxFileConfig
35 // ----------------------------------------------------------------------------
36
37 /*
38 wxFileConfig derives from base Config and implements file based config class,
39 i.e. it uses ASCII disk files to store the information. These files are
40 alternatively called INI, .conf or .rc in the documentation. They are
41 organized in groups or sections, which can nest (i.e. a group contains
42 subgroups, which contain their own subgroups &c). Each group has some
43 number of entries, which are "key = value" pairs. More precisely, the format
44 is:
45
46 # comments are allowed after either ';' or '#' (Win/UNIX standard)
47
48 # blank lines (as above) are ignored
49
50 # global entries are members of special (no name) top group
51 written_for = Windows
52 platform = Linux
53
54 # the start of the group 'Foo'
55 [Foo] # may put comments like this also
56 # following 3 lines are entries
57 key = value
58 another_key = " strings with spaces in the beginning should be quoted, \
59 otherwise the spaces are lost"
60 last_key = but you don't have to put " normally (nor quote them, like here)
61
62 # subgroup of the group 'Foo'
63 # (order is not important, only the name is: separator is '/', as in paths)
64 [Foo/Bar]
65 # entries prefixed with "!" are immutable, i.e. can't be changed if they are
66 # set in the system-wide config file
67 !special_key = value
68 bar_entry = whatever
69
70 [Foo/Bar/Fubar] # depth is (theoretically :-) unlimited
71 # may have the same name as key in another section
72 bar_entry = whatever not
73
74 You have {read/write/delete}Entry functions (guess what they do) and also
75 setCurrentPath to select current group. enum{Subgroups/Entries} allow you
76 to get all entries in the config file (in the current group). Finally,
77 flush() writes immediately all changed entries to disk (otherwise it would
78 be done automatically in dtor)
79
80 wxFileConfig manages not less than 2 config files for each program: global
81 and local (or system and user if you prefer). Entries are read from both of
82 them and the local entries override the global ones unless the latter is
83 immutable (prefixed with '!') in which case a warning message is generated
84 and local value is ignored. Of course, the changes are always written to local
85 file only.
86
87 The names of these files can be specified in a number of ways. First of all,
88 you can use the standard convention: using the ctor which takes 'strAppName'
89 parameter will probably be sufficient for 90% of cases. If, for whatever
90 reason you wish to use the files with some other names, you can always use the
91 second ctor.
92
93 wxFileConfig also may automatically expand the values of environment variables
94 in the entries it reads: for example, if you have an entry
95 score_file = $HOME/.score
96 a call to Read(&str, "score_file") will return a complete path to .score file
97 unless the expansion was previousle disabled with SetExpandEnvVars(FALSE) call
98 (it's on by default, the current status can be retrieved with
99 IsExpandingEnvVars function).
100 */
101
102 class wxFileConfig : public wxConfigBase
103 {
104 public:
105 // construct the "standard" full name for global (system-wide) and
106 // local (user-specific) config files from the base file name.
107 //
108 // the following are the filenames returned by this functions:
109 // global local
110 // Unix /etc/file.ext ~/.file
111 // Win %windir%\file.ext %USERPROFILE%\file.ext
112 //
113 // where file is the basename of szFile, ext is it's extension
114 // or .conf (Unix) or .ini (Win) if it has none
115 static wxString GetGlobalFileName(const char *szFile);
116 static wxString GetLocalFileName(const char *szFile);
117
118 // ctor & dtor
119
120 #if 0
121 // the names of local and global (if not disabled) config files are
122 // constructed using Get{Local|Global}FileName functions described above
123 // (szAppName is just the (short) name of your application)
124 wxFileConfig(const char *szAppName, bool bLocalOnly = FALSE);
125 // this ctor allows you to specify custom names for both files (if strGlobal
126 // isn't a full path, it's considered to be relative to the standard
127 // directory, i.e. /etc under Unix and %windir% under Windows, if strLocal
128 // is not an absolute path, it's considered to be relative to the user's
129 // directory). If either of strings is empty, the corresponding file is not
130 // used.
131 wxFileConfig(const wxString& strLocal, const wxString& strGlobal);
132 #endif
133
134 // New constructor: one size fits all. Specify wxCONFIG_USE_LOCAL_FILE
135 // or wxCONFIG_USE_GLOBAL_FILE to say which files should be used.
136 wxFileConfig(const wxString& appName, const wxString& vendorName = "",
137 const wxString& localFilename = "", const wxString& globalFilename = "",
138 long style = wxCONFIG_USE_LOCAL_FILE);
139
140 // dtor will save unsaved data
141 virtual ~wxFileConfig();
142
143 // implement inherited pure virtual functions
144 virtual void SetPath(const wxString& strPath);
145 virtual const wxString& GetPath() const { return m_strPath; }
146
147 virtual bool GetFirstGroup(wxString& str, long& lIndex) const;
148 virtual bool GetNextGroup (wxString& str, long& lIndex) const;
149 virtual bool GetFirstEntry(wxString& str, long& lIndex) const;
150 virtual bool GetNextEntry (wxString& str, long& lIndex) const;
151
152 virtual size_t GetNumberOfEntries(bool bRecursive = FALSE) const;
153 virtual size_t GetNumberOfGroups(bool bRecursive = FALSE) const;
154
155 virtual bool HasGroup(const wxString& strName) const;
156 virtual bool HasEntry(const wxString& strName) const;
157
158 virtual bool Read(const wxString& key, wxString *pStr) const;
159 virtual bool Read(const wxString& key, wxString *pStr, const wxString& defValue) const;
160 virtual bool Read(const wxString& key, long *pl) const;
161
162 // The following are necessary to satisfy the compiler
163 wxString Read(const wxString& key, const wxString& defVal) const
164 { return wxConfigBase::Read(key, defVal); }
165 bool Read(const wxString& key, long *pl, long defVal) const
166 { return wxConfigBase::Read(key, pl, defVal); }
167 long Read(const wxString& key, long defVal) const
168 { return wxConfigBase::Read(key, defVal); }
169 bool Read(const wxString& key, double* val) const
170 { return wxConfigBase::Read(key, val); }
171 bool Read(const wxString& key, double* val, double defVal) const
172 { return wxConfigBase::Read(key, val, defVal); }
173
174 virtual bool Write(const wxString& key, const wxString& szValue);
175 virtual bool Write(const wxString& key, long lValue);
176
177 virtual bool Flush(bool bCurrentOnly = FALSE);
178
179 virtual bool DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso);
180 virtual bool DeleteGroup(const wxString& szKey);
181 virtual bool DeleteAll();
182
183 public:
184 // fwd decl
185 class ConfigGroup;
186 class ConfigEntry;
187
188 // we store all lines of the local config file as a linked list in memory
189 class LineList
190 {
191 public:
192 // ctor
193 LineList(const wxString& str, LineList *pNext = (LineList *) NULL) : m_strLine(str)
194 { SetNext(pNext); SetPrev((LineList *) NULL); }
195
196 //
197 LineList *Next() const { return m_pNext; }
198 LineList *Prev() const { return m_pPrev; }
199 void SetNext(LineList *pNext) { m_pNext = pNext; }
200 void SetPrev(LineList *pPrev) { m_pPrev = pPrev; }
201
202 //
203 void SetText(const wxString& str) { m_strLine = str; }
204 const wxString& Text() const { return m_strLine; }
205
206 private:
207 wxString m_strLine; // line contents
208 LineList *m_pNext, // next node
209 *m_pPrev; // previous one
210 };
211
212 // functions to work with this list
213 LineList *LineListAppend(const wxString& str);
214 LineList *LineListInsert(const wxString& str,
215 LineList *pLine); // NULL => Prepend()
216 void LineListRemove(LineList *pLine);
217 bool LineListIsEmpty();
218
219 private:
220 // GetXXXFileame helpers: return ('/' terminated) directory names
221 static wxString GetGlobalDir();
222 static wxString GetLocalDir();
223
224 // common part of all ctors (assumes that m_str{Local|Global}File are already
225 // initialized
226 void Init();
227
228 // common part of from dtor and DeleteAll
229 void CleanUp();
230
231 // parse the whole file
232 void Parse(wxTextFile& file, bool bLocal);
233
234 // the same as SetPath("/")
235 void SetRootPath();
236
237 // member variables
238 // ----------------
239 LineList *m_linesHead, // head of the linked list
240 *m_linesTail; // tail
241
242 wxString m_strLocalFile, // local file name passed to ctor
243 m_strGlobalFile; // global
244 wxString m_strPath; // current path (not '/' terminated)
245
246 ConfigGroup *m_pRootGroup, // the top (unnamed) group
247 *m_pCurrentGroup; // the current group
248
249 //protected: --- if wxFileConfig::ConfigEntry is not public, functions in
250 // ConfigGroup such as Find/AddEntry can't return "ConfigEntry *"
251 public:
252 WX_DEFINE_SORTED_ARRAY(ConfigEntry *, ArrayEntries);
253 WX_DEFINE_SORTED_ARRAY(ConfigGroup *, ArrayGroups);
254
255 class ConfigEntry
256 {
257 private:
258 ConfigGroup *m_pParent; // group that contains us
259 wxString m_strName, // entry name
260 m_strValue; // value
261 bool m_bDirty, // changed since last read?
262 m_bImmutable; // can be overriden locally?
263 int m_nLine; // used if m_pLine == NULL only
264 LineList *m_pLine; // pointer to our line in the linked list
265 // or NULL if it was found in global file
266
267 public:
268 ConfigEntry(ConfigGroup *pParent, const wxString& strName, int nLine);
269
270 // simple accessors
271 const wxString& Name() const { return m_strName; }
272 const wxString& Value() const { return m_strValue; }
273 ConfigGroup *Group() const { return m_pParent; }
274 bool IsDirty() const { return m_bDirty; }
275 bool IsImmutable() const { return m_bImmutable; }
276 bool IsLocal() const { return m_pLine != 0; }
277 int Line() const { return m_nLine; }
278 LineList *GetLine() const { return m_pLine; }
279
280 // modify entry attributes
281 void SetValue(const wxString& strValue, bool bUser = TRUE);
282 void SetDirty();
283 void SetLine(LineList *pLine);
284 };
285
286 class ConfigGroup
287 {
288 private:
289 wxFileConfig *m_pConfig; // config object we belong to
290 ConfigGroup *m_pParent; // parent group (NULL for root group)
291 ArrayEntries m_aEntries; // entries in this group
292 ArrayGroups m_aSubgroups; // subgroups
293 wxString m_strName; // group's name
294 bool m_bDirty; // if FALSE => all subgroups are not dirty
295 LineList *m_pLine; // pointer to our line in the linked list
296 ConfigEntry *m_pLastEntry; // last entry/subgroup of this group in the
297 ConfigGroup *m_pLastGroup; // local file (we insert new ones after it)
298
299 // DeleteSubgroupByName helper
300 bool DeleteSubgroup(ConfigGroup *pGroup);
301
302 public:
303 // ctor
304 ConfigGroup(ConfigGroup *pParent, const wxString& strName, wxFileConfig *);
305
306 // dtor deletes all entries and subgroups also
307 ~ConfigGroup();
308
309 // simple accessors
310 const wxString& Name() const { return m_strName; }
311 ConfigGroup *Parent() const { return m_pParent; }
312 wxFileConfig *Config() const { return m_pConfig; }
313 bool IsDirty() const { return m_bDirty; }
314
315 bool IsEmpty() const { return Entries().IsEmpty() && Groups().IsEmpty(); }
316 const ArrayEntries& Entries() const { return m_aEntries; }
317 const ArrayGroups& Groups() const { return m_aSubgroups; }
318
319 // find entry/subgroup (NULL if not found)
320 ConfigGroup *FindSubgroup(const char *szName) const;
321 ConfigEntry *FindEntry (const char *szName) const;
322
323 // delete entry/subgroup, return FALSE if doesn't exist
324 bool DeleteSubgroupByName(const char *szName);
325 bool DeleteEntry(const char *szName);
326
327 // create new entry/subgroup returning pointer to newly created element
328 ConfigGroup *AddSubgroup(const wxString& strName);
329 ConfigEntry *AddEntry (const wxString& strName, int nLine = NOT_FOUND);
330
331 // will also recursively set parent's dirty flag
332 void SetDirty();
333 void SetLine(LineList *pLine);
334
335 //
336 wxString GetFullName() const;
337
338 // get the last line belonging to an entry/subgroup of this group
339 LineList *GetGroupLine(); // line which contains [group]
340 LineList *GetLastEntryLine(); // after which our subgroups start
341 LineList *GetLastGroupLine(); // after which the next group starts
342
343 // called by entries/subgroups when they're created/deleted
344 void SetLastEntry(ConfigEntry *pEntry) { m_pLastEntry = pEntry; }
345 void SetLastGroup(ConfigGroup *pGroup) { m_pLastGroup = pGroup; }
346 };
347 };
348
349 #endif //_FILECONF_H
350