]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/fileconf.h
1 /*****************************************************************************\
2 * Project: CppLib: C++ library for Windows/UNIX platfroms *
3 * File: fileconf.h - file based implementation of Config *
4 *---------------------------------------------------------------------------*
7 *---------------------------------------------------------------------------*
9 *---------------------------------------------------------------------------*
10 * Author: Vadim Zeitlin zeitlin@dptmaths.ens-cachan.fr> *
11 * adapted from earlier class by VZ & Karsten Ballüder *
14 \*****************************************************************************/
20 #pragma interface "fileconf.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 // it won't compile without it anyhow
29 #error "Please define USE_WXCONFIG or remove fileconf.cpp from your makefile"
30 #endif // USE_WXCONFIG
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
37 wxFileConfig derives from base Config and implements file based config class,
38 i.e. it uses ASCII disk files to store the information. These files are
39 alternatively called INI, .conf or .rc in the documentation. They are
40 organized in groups or sections, which can nest (i.e. a group contains
41 subgroups, which contain their own subgroups &c). Each group has some
42 number of entries, which are "key = value" pairs. More precisely, the format
45 # comments are allowed after either ';' or '#' (Win/UNIX standard)
47 # blank lines (as above) are ignored
49 # global entries are members of special (no name) top group
53 # the start of the group 'Foo'
54 [Foo] # may put comments like this also
55 # following 3 lines are entries
57 another_key = " strings with spaces in the beginning should be quoted, \
58 otherwise the spaces are lost"
59 last_key = but you don't have to put " normally (nor quote them, like here)
61 # subgroup of the group 'Foo'
62 # (order is not important, only the name is: separator is '/', as in paths)
64 # entries prefixed with "!" are immutable, i.e. can't be changed if they are
65 # set in the system-wide config file
69 [Foo/Bar/Fubar] # depth is (theoretically :-) unlimited
70 # may have the same name as key in another section
71 bar_entry = whatever not
73 You have {read/write/delete}Entry functions (guess what they do) and also
74 setCurrentPath to select current group. enum{Subgroups/Entries} allow you
75 to get all entries in the config file (in the current group). Finally,
76 flush() writes immediately all changed entries to disk (otherwise it would
77 be done automatically in dtor)
79 wxFileConfig manages not less than 2 config files for each program: global
80 and local (or system and user if you prefer). Entries are read from both of
81 them and the local entries override the global ones unless the latter is
82 immutable (prefixed with '!') in which case a warning message is generated
83 and local value is ignored. Of course, the changes are always written to local
86 @@@@ describe environment variable expansion
89 class wxFileConfig
: public wxConfig
92 // construct the "standard" full name for global (system-wide) and
93 // local (user-specific) config files from the base file name.
95 // the following are the filenames returned by this functions:
97 // Unix /etc/file.ext ~/.file
98 // Win %windir%\file.ext %USERPROFILE%\file.ext
100 // where file is the basename of szFile, ext is it's extension
101 // or .conf (Unix) or .ini (Win) if it has none
102 static wxString
GetGlobalFileName(const char *szFile
);
103 static wxString
GetLocalFileName(const char *szFile
);
106 // if strGlobal is empty, only local config file is used
107 wxFileConfig(const wxString
& strLocal
,
108 const wxString
& strGlobal
= "");
109 // dtor will save unsaved data
110 virtual ~wxFileConfig();
112 // implement inherited pure virtual functions
113 virtual void SetPath(const wxString
& strPath
);
114 virtual const wxString
& GetPath() const { return m_strPath
; }
116 virtual bool GetFirstGroup(wxString
& str
, long& lIndex
);
117 virtual bool GetNextGroup (wxString
& str
, long& lIndex
);
118 virtual bool GetFirstEntry(wxString
& str
, long& lIndex
);
119 virtual bool GetNextEntry (wxString
& str
, long& lIndex
);
121 virtual bool Read(wxString
*pstr
, const char *szKey
,
122 const char *szDefault
= 0) const;
123 virtual const char *Read(const char *szKey
,
124 const char *szDefault
= 0) const;
125 virtual bool Read(long *pl
, const char *szKey
, long lDefault
) const;
126 virtual bool Write(const char *szKey
, const char *szValue
);
127 virtual bool Write(const char *szKey
, long lValue
);
128 virtual bool Flush(bool bCurrentOnly
= FALSE
);
130 virtual bool DeleteEntry(const char *szKey
, bool bGroupIfEmptyAlso
);
131 virtual bool DeleteGroup(const char *szKey
);
132 virtual bool DeleteAll();
139 // we store all lines of the local config file as a linked list in memory
144 LineList(const wxString
& str
, LineList
*pNext
= NULL
) : m_strLine(str
)
148 LineList
*Next() const { return m_pNext
; }
149 void SetNext(LineList
*pNext
) { m_pNext
= pNext
; }
152 void SetText(const wxString
& str
) { m_strLine
= str
; }
153 const wxString
& Text() const { return m_strLine
; }
156 wxString m_strLine
; // line contents
157 LineList
*m_pNext
; // next node
160 // functions to work with this list
161 LineList
*LineListAppend(const wxString
& str
);
162 LineList
*LineListInsert(const wxString
& str
,
163 LineList
*pLine
); // NULL => Append()
164 bool LineListIsEmpty();
167 // put the object in the initial state
170 // parse the whole file
171 void Parse(wxTextFile
& file
, bool bLocal
);
173 // the same as SetPath("/")
178 LineList
*m_linesHead
, // head of the linked list
179 *m_linesTail
; // tail
181 wxString m_strLocalFile
, // local file name passed to ctor
182 m_strGlobalFile
; // global
183 wxString m_strPath
; // current path (not '/' terminated)
185 ConfigGroup
*m_pRootGroup
, // the top (unnamed) group
186 *m_pCurrentGroup
; // the current group
188 //protected: --- if wxFileConfig::ConfigEntry is not public, functions in
189 // ConfigGroup such as Find/AddEntry can't return "ConfigEntry *"
191 WX_DEFINE_ARRAY(ConfigEntry
*, ArrayEntries
);
192 WX_DEFINE_ARRAY(ConfigGroup
*, ArrayGroups
);
197 ConfigGroup
*m_pParent
; // group that contains us
198 wxString m_strName
, // entry name
200 bool m_bDirty
, // changed since last read?
201 m_bImmutable
; // can be overriden locally?
202 int m_nLine
; // used if m_pLine == NULL only
203 LineList
*m_pLine
; // pointer to our line in the linked list
204 // or NULL if it was found in global file
207 ConfigEntry(ConfigGroup
*pParent
, const wxString
& strName
, int nLine
);
210 const wxString
& Name() const { return m_strName
; }
211 const wxString
& Value() const { return m_strValue
; }
212 ConfigGroup
*Group() const { return m_pParent
; }
213 bool IsDirty() const { return m_bDirty
; }
214 bool IsImmutable() const { return m_bImmutable
; }
215 bool IsLocal() const { return m_pLine
!= 0; }
216 int Line() const { return m_nLine
; }
217 LineList
*GetLine() const { return m_pLine
; }
219 // modify entry attributes
220 void SetValue(const wxString
& strValue
, bool bUser
= TRUE
);
222 void SetLine(LineList
*pLine
);
229 wxFileConfig
*m_pConfig
; // config object we belong to
230 ConfigGroup
*m_pParent
; // parent group (NULL for root group)
231 ArrayEntries m_aEntries
; // entries in this group
232 ArrayGroups m_aSubgroups
; // subgroups
233 wxString m_strName
; // group's name
234 bool m_bDirty
; // if FALSE => all subgroups are not dirty
235 LineList
*m_pLine
; // pointer to our line in the linked list
236 ConfigEntry
*m_pLastEntry
; // last entry of this group in the local file
237 ConfigGroup
*m_pLastGroup
; // last subgroup
241 ConfigGroup(ConfigGroup
*pParent
, const wxString
& strName
, wxFileConfig
*);
243 // dtor deletes all entries and subgroups also
247 const wxString
& Name() const { return m_strName
; }
248 ConfigGroup
*Parent() const { return m_pParent
; }
249 wxFileConfig
*Config() const { return m_pConfig
; }
250 bool IsDirty() const { return m_bDirty
; }
252 bool IsEmpty() const { return Entries().IsEmpty() && Groups().IsEmpty(); }
253 const ArrayEntries
& Entries() const { return m_aEntries
; }
254 const ArrayGroups
& Groups() const { return m_aSubgroups
; }
256 // find entry/subgroup (NULL if not found)
257 ConfigGroup
*FindSubgroup(const char *szName
) const;
258 ConfigEntry
*FindEntry (const char *szName
) const;
260 // delete entry/subgroup, return FALSE if doesn't exist
261 bool DeleteSubgroup(const char *szName
);
262 bool DeleteEntry(const char *szName
);
264 // create new entry/subgroup returning pointer to newly created element
265 ConfigGroup
*AddSubgroup(const wxString
& strName
);
266 ConfigEntry
*AddEntry (const wxString
& strName
, int nLine
= NOT_FOUND
);
268 // will also recursively set parent's dirty flag
270 void SetLine(LineList
*pLine
);
272 // the new entries in this subgroup will be inserted after the last subgroup
273 // or, if there is none, after the last entry
274 void SetLastEntry(ConfigEntry
*pLastEntry
) { m_pLastEntry
= pLastEntry
; }
275 void SetLastGroup(ConfigGroup
*pLastGroup
) { m_pLastGroup
= pLastGroup
; }
277 wxString
GetFullName() const;
279 // get the last line belonging to an entry/subgroup of this group
280 LineList
*GetGroupLine();
281 LineList
*GetLastEntryLine();
282 LineList
*GetLastGroupLine();