1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileConfig derivation of wxConfigBase
4 // Author: Vadim Zeitlin
6 // Created: 07.04.98 (adapted from appconf.cpp)
8 // Copyright: (c) 1997 Karsten Ballüder & Vadim Zeitlin
9 // Ballueder@usa.net <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
17 #pragma interface "fileconf.h"
24 #include "wx/textfile.h"
25 #include "wx/string.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
32 wxFileConfig derives from base Config and implements file based config class,
33 i.e. it uses ASCII disk files to store the information. These files are
34 alternatively called INI, .conf or .rc in the documentation. They are
35 organized in groups or sections, which can nest (i.e. a group contains
36 subgroups, which contain their own subgroups &c). Each group has some
37 number of entries, which are "key = value" pairs. More precisely, the format
40 # comments are allowed after either ';' or '#' (Win/UNIX standard)
42 # blank lines (as above) are ignored
44 # global entries are members of special (no name) top group
48 # the start of the group 'Foo'
49 [Foo] # may put comments like this also
50 # following 3 lines are entries
52 another_key = " strings with spaces in the beginning should be quoted, \
53 otherwise the spaces are lost"
54 last_key = but you don't have to put " normally (nor quote them, like here)
56 # subgroup of the group 'Foo'
57 # (order is not important, only the name is: separator is '/', as in paths)
59 # entries prefixed with "!" are immutable, i.e. can't be changed if they are
60 # set in the system-wide config file
64 [Foo/Bar/Fubar] # depth is (theoretically :-) unlimited
65 # may have the same name as key in another section
66 bar_entry = whatever not
68 You have {read/write/delete}Entry functions (guess what they do) and also
69 setCurrentPath to select current group. enum{Subgroups/Entries} allow you
70 to get all entries in the config file (in the current group). Finally,
71 flush() writes immediately all changed entries to disk (otherwise it would
72 be done automatically in dtor)
74 wxFileConfig manages not less than 2 config files for each program: global
75 and local (or system and user if you prefer). Entries are read from both of
76 them and the local entries override the global ones unless the latter is
77 immutable (prefixed with '!') in which case a warning message is generated
78 and local value is ignored. Of course, the changes are always written to local
81 The names of these files can be specified in a number of ways. First of all,
82 you can use the standard convention: using the ctor which takes 'strAppName'
83 parameter will probably be sufficient for 90% of cases. If, for whatever
84 reason you wish to use the files with some other names, you can always use the
87 wxFileConfig also may automatically expand the values of environment variables
88 in the entries it reads: for example, if you have an entry
89 score_file = $HOME/.score
90 a call to Read(&str, "score_file") will return a complete path to .score file
91 unless the expansion was previousle disabled with SetExpandEnvVars(FALSE) call
92 (it's on by default, the current status can be retrieved with
93 IsExpandingEnvVars function).
95 class WXDLLEXPORT wxFileConfigGroup
;
96 class WXDLLEXPORT wxFileConfigEntry
;
97 class WXDLLEXPORT wxFileConfigLineList
;
98 class WXDLLEXPORT wxInputStream
;
100 class WXDLLEXPORT wxFileConfig
: public wxConfigBase
103 // construct the "standard" full name for global (system-wide) and
104 // local (user-specific) config files from the base file name.
106 // the following are the filenames returned by this functions:
108 // Unix /etc/file.ext ~/.file
109 // Win %windir%\file.ext %USERPROFILE%\file.ext
111 // where file is the basename of szFile, ext is it's extension
112 // or .conf (Unix) or .ini (Win) if it has none
113 static wxString
GetGlobalFileName(const wxChar
*szFile
);
114 static wxString
GetLocalFileName(const wxChar
*szFile
);
117 // New constructor: one size fits all. Specify wxCONFIG_USE_LOCAL_FILE or
118 // wxCONFIG_USE_GLOBAL_FILE to say which files should be used.
119 wxFileConfig(const wxString
& appName
,
120 const wxString
& vendorName
= wxT(""),
121 const wxString
& localFilename
= wxT(""),
122 const wxString
& globalFilename
= wxT(""),
123 long style
= wxCONFIG_USE_LOCAL_FILE
);
126 // ctor that takes an input stream.
127 wxFileConfig(wxInputStream
&inStream
);
128 #endif // wxUSE_STREAMS
130 // dtor will save unsaved data
131 virtual ~wxFileConfig();
133 // under Unix, set the umask to be used for the file creation, do nothing
134 // under other systems
136 void SetUmask(int mode
) { m_umask
= mode
; }
138 void SetUmask(int WXUNUSED(mode
)) { }
139 #endif // __UNIX__/!__UNIX__
141 // implement inherited pure virtual functions
142 virtual void SetPath(const wxString
& strPath
);
143 virtual const wxString
& GetPath() const { return m_strPath
; }
145 virtual bool GetFirstGroup(wxString
& str
, long& lIndex
) const;
146 virtual bool GetNextGroup (wxString
& str
, long& lIndex
) const;
147 virtual bool GetFirstEntry(wxString
& str
, long& lIndex
) const;
148 virtual bool GetNextEntry (wxString
& str
, long& lIndex
) const;
150 virtual size_t GetNumberOfEntries(bool bRecursive
= FALSE
) const;
151 virtual size_t GetNumberOfGroups(bool bRecursive
= FALSE
) const;
153 virtual bool HasGroup(const wxString
& strName
) const;
154 virtual bool HasEntry(const wxString
& strName
) const;
156 virtual bool Flush(bool bCurrentOnly
= FALSE
);
158 virtual bool RenameEntry(const wxString
& oldName
, const wxString
& newName
);
159 virtual bool RenameGroup(const wxString
& oldName
, const wxString
& newName
);
161 virtual bool DeleteEntry(const wxString
& key
, bool bGroupIfEmptyAlso
= TRUE
);
162 virtual bool DeleteGroup(const wxString
& szKey
);
163 virtual bool DeleteAll();
166 // functions to work with this list
167 wxFileConfigLineList
*LineListAppend(const wxString
& str
);
168 wxFileConfigLineList
*LineListInsert(const wxString
& str
,
169 wxFileConfigLineList
*pLine
); // NULL => Prepend()
170 void LineListRemove(wxFileConfigLineList
*pLine
);
171 bool LineListIsEmpty();
174 virtual bool DoReadString(const wxString
& key
, wxString
*pStr
) const;
175 virtual bool DoReadLong(const wxString
& key
, long *pl
) const;
177 virtual bool DoWriteString(const wxString
& key
, const wxString
& szValue
);
178 virtual bool DoWriteLong(const wxString
& key
, long lValue
);
181 // GetXXXFileName helpers: return ('/' terminated) directory names
182 static wxString
GetGlobalDir();
183 static wxString
GetLocalDir();
185 // common part of all ctors (assumes that m_str{Local|Global}File are already
189 // common part of from dtor and DeleteAll
192 // parse the whole file
193 void Parse(wxTextBuffer
& buffer
, bool bLocal
);
195 // the same as SetPath("/")
200 wxFileConfigLineList
*m_linesHead
, // head of the linked list
201 *m_linesTail
; // tail
203 wxString m_strLocalFile
, // local file name passed to ctor
204 m_strGlobalFile
; // global
205 wxString m_strPath
; // current path (not '/' terminated)
207 wxFileConfigGroup
*m_pRootGroup
, // the top (unnamed) group
208 *m_pCurrentGroup
; // the current group
211 int m_umask
; // the umask to use for file creation