]> git.saurik.com Git - wxWidgets.git/blob - include/wx/fileconf.h
wxUSE_CRASHREPORT requires wxUSE_ON_FATAL_EXCEPTION
[wxWidgets.git] / include / wx / fileconf.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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 licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _FILECONF_H
14 #define _FILECONF_H
15
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma interface "fileconf.h"
18 #endif
19
20 #include "wx/defs.h"
21
22 #if wxUSE_CONFIG
23
24 #include "wx/textfile.h"
25 #include "wx/string.h"
26 #include "wx/confbase.h"
27
28 // ----------------------------------------------------------------------------
29 // wxFileConfig
30 // ----------------------------------------------------------------------------
31
32 /*
33 wxFileConfig derives from base Config and implements file based config class,
34 i.e. it uses ASCII disk files to store the information. These files are
35 alternatively called INI, .conf or .rc in the documentation. They are
36 organized in groups or sections, which can nest (i.e. a group contains
37 subgroups, which contain their own subgroups &c). Each group has some
38 number of entries, which are "key = value" pairs. More precisely, the format
39 is:
40
41 # comments are allowed after either ';' or '#' (Win/UNIX standard)
42
43 # blank lines (as above) are ignored
44
45 # global entries are members of special (no name) top group
46 written_for = Windows
47 platform = Linux
48
49 # the start of the group 'Foo'
50 [Foo] # may put comments like this also
51 # following 3 lines are entries
52 key = value
53 another_key = " strings with spaces in the beginning should be quoted, \
54 otherwise the spaces are lost"
55 last_key = but you don't have to put " normally (nor quote them, like here)
56
57 # subgroup of the group 'Foo'
58 # (order is not important, only the name is: separator is '/', as in paths)
59 [Foo/Bar]
60 # entries prefixed with "!" are immutable, i.e. can't be changed if they are
61 # set in the system-wide config file
62 !special_key = value
63 bar_entry = whatever
64
65 [Foo/Bar/Fubar] # depth is (theoretically :-) unlimited
66 # may have the same name as key in another section
67 bar_entry = whatever not
68
69 You have {read/write/delete}Entry functions (guess what they do) and also
70 setCurrentPath to select current group. enum{Subgroups/Entries} allow you
71 to get all entries in the config file (in the current group). Finally,
72 flush() writes immediately all changed entries to disk (otherwise it would
73 be done automatically in dtor)
74
75 wxFileConfig manages not less than 2 config files for each program: global
76 and local (or system and user if you prefer). Entries are read from both of
77 them and the local entries override the global ones unless the latter is
78 immutable (prefixed with '!') in which case a warning message is generated
79 and local value is ignored. Of course, the changes are always written to local
80 file only.
81
82 The names of these files can be specified in a number of ways. First of all,
83 you can use the standard convention: using the ctor which takes 'strAppName'
84 parameter will probably be sufficient for 90% of cases. If, for whatever
85 reason you wish to use the files with some other names, you can always use the
86 second ctor.
87
88 wxFileConfig also may automatically expand the values of environment variables
89 in the entries it reads: for example, if you have an entry
90 score_file = $HOME/.score
91 a call to Read(&str, "score_file") will return a complete path to .score file
92 unless the expansion was previously disabled with SetExpandEnvVars(false) call
93 (it's on by default, the current status can be retrieved with
94 IsExpandingEnvVars function).
95 */
96 class WXDLLIMPEXP_BASE wxFileConfigGroup;
97 class WXDLLIMPEXP_BASE wxFileConfigEntry;
98 class WXDLLIMPEXP_BASE wxFileConfigLineList;
99
100 #if wxUSE_STREAMS
101 class WXDLLIMPEXP_BASE wxInputStream;
102 class WXDLLIMPEXP_BASE wxOutputStream;
103 #endif // wxUSE_STREAMS
104
105 class WXDLLIMPEXP_BASE wxFileConfig : public wxConfigBase
106 {
107 public:
108 // construct the "standard" full name for global (system-wide) and
109 // local (user-specific) config files from the base file name.
110 //
111 // the following are the filenames returned by this functions:
112 // global local
113 // Unix /etc/file.ext ~/.file
114 // Win %windir%\file.ext %USERPROFILE%\file.ext
115 //
116 // where file is the basename of szFile, ext is its extension
117 // or .conf (Unix) or .ini (Win) if it has none
118 static wxString GetGlobalFileName(const wxChar *szFile);
119 static wxString GetLocalFileName(const wxChar *szFile);
120
121 // ctor & dtor
122 // New constructor: one size fits all. Specify wxCONFIG_USE_LOCAL_FILE or
123 // wxCONFIG_USE_GLOBAL_FILE to say which files should be used.
124 wxFileConfig(const wxString& appName = wxEmptyString,
125 const wxString& vendorName = wxEmptyString,
126 const wxString& localFilename = wxEmptyString,
127 const wxString& globalFilename = wxEmptyString,
128 long style = wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE,
129 wxMBConv& conv = wxConvUTF8);
130
131 #if wxUSE_STREAMS
132 // ctor that takes an input stream.
133 wxFileConfig(wxInputStream &inStream, wxMBConv& conv = wxConvUTF8);
134 #endif // wxUSE_STREAMS
135
136 // dtor will save unsaved data
137 virtual ~wxFileConfig();
138
139 // under Unix, set the umask to be used for the file creation, do nothing
140 // under other systems
141 #ifdef __UNIX__
142 void SetUmask(int mode) { m_umask = mode; }
143 #else // !__UNIX__
144 void SetUmask(int WXUNUSED(mode)) { }
145 #endif // __UNIX__/!__UNIX__
146
147 // implement inherited pure virtual functions
148 virtual void SetPath(const wxString& strPath);
149 virtual const wxString& GetPath() const { return m_strPath; }
150
151 virtual bool GetFirstGroup(wxString& str, long& lIndex) const;
152 virtual bool GetNextGroup (wxString& str, long& lIndex) const;
153 virtual bool GetFirstEntry(wxString& str, long& lIndex) const;
154 virtual bool GetNextEntry (wxString& str, long& lIndex) const;
155
156 virtual size_t GetNumberOfEntries(bool bRecursive = false) const;
157 virtual size_t GetNumberOfGroups(bool bRecursive = false) const;
158
159 virtual bool HasGroup(const wxString& strName) const;
160 virtual bool HasEntry(const wxString& strName) const;
161
162 virtual bool Flush(bool bCurrentOnly = false);
163
164 virtual bool RenameEntry(const wxString& oldName, const wxString& newName);
165 virtual bool RenameGroup(const wxString& oldName, const wxString& newName);
166
167 virtual bool DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso = true);
168 virtual bool DeleteGroup(const wxString& szKey);
169 virtual bool DeleteAll();
170
171 // additional, wxFileConfig-specific, functionality
172 #if wxUSE_STREAMS
173 // save the entire config file text to the given stream, note that the text
174 // won't be saved again in dtor when Flush() is called if you use this method
175 // as it won't be "changed" any more
176 virtual bool Save(wxOutputStream& os, wxMBConv& conv = wxConvUTF8);
177 #endif // wxUSE_STREAMS
178
179 public:
180 // functions to work with this list
181 wxFileConfigLineList *LineListAppend(const wxString& str);
182 wxFileConfigLineList *LineListInsert(const wxString& str,
183 wxFileConfigLineList *pLine); // NULL => Prepend()
184 void LineListRemove(wxFileConfigLineList *pLine);
185 bool LineListIsEmpty();
186
187 protected:
188 virtual bool DoReadString(const wxString& key, wxString *pStr) const;
189 virtual bool DoReadLong(const wxString& key, long *pl) const;
190
191 virtual bool DoWriteString(const wxString& key, const wxString& szValue);
192 virtual bool DoWriteLong(const wxString& key, long lValue);
193
194 private:
195 // GetXXXFileName helpers: return ('/' terminated) directory names
196 static wxString GetGlobalDir();
197 static wxString GetLocalDir();
198
199 // common part of all ctors (assumes that m_str{Local|Global}File are already
200 // initialized
201 void Init();
202
203 // common part of from dtor and DeleteAll
204 void CleanUp();
205
206 // parse the whole file
207 void Parse(wxTextBuffer& buffer, bool bLocal);
208
209 // the same as SetPath("/")
210 void SetRootPath();
211
212 // set/test the dirty flag
213 void SetDirty() { m_isDirty = true; }
214 void ResetDirty() { m_isDirty = false; }
215 bool IsDirty() const { return m_isDirty; }
216
217
218 // member variables
219 // ----------------
220 wxFileConfigLineList *m_linesHead, // head of the linked list
221 *m_linesTail; // tail
222
223 wxString m_strLocalFile, // local file name passed to ctor
224 m_strGlobalFile; // global
225 wxString m_strPath; // current path (not '/' terminated)
226
227 wxFileConfigGroup *m_pRootGroup, // the top (unnamed) group
228 *m_pCurrentGroup; // the current group
229
230 wxMBConv &m_conv;
231
232 #ifdef __UNIX__
233 int m_umask; // the umask to use for file creation
234 #endif // __UNIX__
235
236 bool m_isDirty; // if true, we have unsaved changes
237
238 DECLARE_NO_COPY_CLASS(wxFileConfig)
239 };
240
241 #endif
242 // wxUSE_CONFIG
243
244 #endif
245 //_FILECONF_H
246