]>
Commit | Line | Data |
---|---|---|
02569ba8 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/msw/iniconf.cpp | |
3 | // Purpose: implementation of wxIniConfig class | |
4 | // Author: Vadim Zeitlin | |
9869734d | 5 | // Modified by: |
02569ba8 VZ |
6 | // Created: 27.07.98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
a3b46648 UU |
12 | #ifdef __GNUG__ |
13 | #pragma implementation "iniconf.h" | |
14 | #endif | |
02569ba8 | 15 | |
a3b46648 UU |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
02569ba8 | 18 | |
a3b46648 UU |
19 | #ifdef __BORLANDC__ |
20 | #pragma hdrstop | |
21 | #endif | |
02569ba8 VZ |
22 | |
23 | #ifndef WX_PRECOMP | |
3096bd2f VZ |
24 | #include "wx/string.h" |
25 | #include "wx/intl.h" | |
26 | #include "wx/event.h" | |
27 | #include "wx/app.h" | |
28 | #include "wx/utils.h" | |
02569ba8 VZ |
29 | #endif //WX_PRECOMP |
30 | ||
3096bd2f VZ |
31 | #include "wx/dynarray.h" |
32 | #include "wx/log.h" | |
33 | #include "wx/config.h" | |
02569ba8 | 34 | |
3096bd2f | 35 | #include "wx/msw/iniconf.h" |
02569ba8 VZ |
36 | |
37 | // _WINDOWS_ is defined when windows.h is included, | |
38 | // __WXMSW__ is defined for MS Windows compilation | |
39 | #if defined(__WXMSW__) && !defined(_WINDOWS_) | |
40 | #include <windows.h> | |
41 | #endif //windows.h | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // constants | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | // we replace all path separators with this character | |
48 | #define PATH_SEP_REPLACE '_' | |
49 | ||
50 | // ============================================================================ | |
51 | // implementation | |
52 | // ============================================================================ | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // ctor & dtor | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
9869734d VZ |
58 | wxIniConfig::wxIniConfig(const wxString& strAppName, |
59 | const wxString& strVendor, | |
60 | const wxString& localFilename, | |
61 | const wxString& globalFilename, | |
62 | long style) | |
d2c7e39b JS |
63 | : wxConfigBase(!strAppName && wxTheApp ? wxTheApp->GetAppName() |
64 | : strAppName, | |
65 | !strVendor ? (wxTheApp ? wxTheApp->GetVendorName() | |
66 | : strAppName) | |
67 | : strVendor, | |
68 | localFilename, globalFilename, style) | |
02569ba8 | 69 | { |
18244936 JS |
70 | m_strLocalFilename = localFilename; |
71 | if (m_strLocalFilename.IsEmpty()) | |
72 | { | |
73 | m_strLocalFilename = GetAppName() + ".ini"; | |
74 | } | |
75 | ||
76 | // append the extension if none given and it's not an absolute file name | |
77 | // (otherwise we assume that they know what they're doing) | |
78 | if ( !wxIsPathSeparator(m_strLocalFilename[0u]) && | |
3c67202d | 79 | m_strLocalFilename.Find('.') == wxNOT_FOUND ) |
18244936 JS |
80 | { |
81 | m_strLocalFilename << ".ini"; | |
82 | } | |
83 | ||
84 | // set root path | |
85 | SetPath(""); | |
02569ba8 VZ |
86 | } |
87 | ||
88 | wxIniConfig::~wxIniConfig() | |
89 | { | |
90 | } | |
91 | ||
92 | // ---------------------------------------------------------------------------- | |
93 | // path management | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | void wxIniConfig::SetPath(const wxString& strPath) | |
97 | { | |
98 | wxArrayString aParts; | |
99 | ||
100 | if ( strPath.IsEmpty() ) { | |
101 | // nothing | |
102 | } | |
103 | else if ( strPath[0u] == wxCONFIG_PATH_SEPARATOR ) { | |
104 | // absolute path | |
105 | wxSplitPath(aParts, strPath); | |
106 | } | |
107 | else { | |
108 | // relative path, combine with current one | |
109 | wxString strFullPath = GetPath(); | |
110 | strFullPath << wxCONFIG_PATH_SEPARATOR << strPath; | |
111 | wxSplitPath(aParts, strFullPath); | |
112 | } | |
113 | ||
c86f1403 | 114 | size_t nPartsCount = aParts.Count(); |
02569ba8 VZ |
115 | m_strPath.Empty(); |
116 | if ( nPartsCount == 0 ) { | |
117 | // go to the root | |
118 | m_strGroup = PATH_SEP_REPLACE; | |
119 | } | |
120 | else { | |
121 | // translate | |
122 | m_strGroup = aParts[0u]; | |
c86f1403 | 123 | for ( size_t nPart = 1; nPart < nPartsCount; nPart++ ) { |
02569ba8 VZ |
124 | if ( nPart > 1 ) |
125 | m_strPath << PATH_SEP_REPLACE; | |
126 | m_strPath << aParts[nPart]; | |
127 | } | |
128 | } | |
129 | ||
130 | // other functions assume that all this is true, i.e. there are no trailing | |
131 | // underscores at the end except if the group is the root one | |
132 | wxASSERT( (m_strPath.IsEmpty() || m_strPath.Last() != PATH_SEP_REPLACE) && | |
62448488 | 133 | (m_strGroup == wxString(PATH_SEP_REPLACE) || |
02569ba8 VZ |
134 | m_strGroup.Last() != PATH_SEP_REPLACE) ); |
135 | } | |
136 | ||
137 | const wxString& wxIniConfig::GetPath() const | |
138 | { | |
139 | static wxString s_str; | |
140 | ||
141 | // always return abs path | |
142 | s_str = wxCONFIG_PATH_SEPARATOR; | |
143 | ||
ce3ed50d | 144 | if ( m_strGroup == wxString(PATH_SEP_REPLACE) ) { |
02569ba8 VZ |
145 | // we're at the root level, nothing to do |
146 | } | |
147 | else { | |
148 | s_str << m_strGroup; | |
149 | if ( !m_strPath.IsEmpty() ) | |
150 | s_str << wxCONFIG_PATH_SEPARATOR; | |
151 | for ( const char *p = m_strPath; *p != '\0'; p++ ) { | |
152 | s_str << (*p == PATH_SEP_REPLACE ? wxCONFIG_PATH_SEPARATOR : *p); | |
153 | } | |
154 | } | |
155 | ||
156 | return s_str; | |
157 | } | |
158 | ||
18244936 | 159 | wxString wxIniConfig::GetPrivateKeyName(const wxString& szKey) const |
02569ba8 VZ |
160 | { |
161 | wxString strKey; | |
162 | ||
163 | if ( !m_strPath.IsEmpty() ) | |
164 | strKey << m_strPath << PATH_SEP_REPLACE; | |
165 | ||
166 | strKey << szKey; | |
167 | ||
168 | return strKey; | |
169 | } | |
170 | ||
18244936 | 171 | wxString wxIniConfig::GetKeyName(const wxString& szKey) const |
02569ba8 VZ |
172 | { |
173 | wxString strKey; | |
174 | ||
62448488 | 175 | if ( m_strGroup != wxString(PATH_SEP_REPLACE) ) |
02569ba8 VZ |
176 | strKey << m_strGroup << PATH_SEP_REPLACE; |
177 | if ( !m_strPath.IsEmpty() ) | |
178 | strKey << m_strPath << PATH_SEP_REPLACE; | |
179 | ||
180 | strKey << szKey; | |
181 | ||
182 | return strKey; | |
183 | } | |
184 | ||
185 | // ---------------------------------------------------------------------------- | |
186 | // enumeration | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
189 | // not implemented | |
190 | bool wxIniConfig::GetFirstGroup(wxString& str, long& lIndex) const | |
191 | { | |
192 | wxFAIL_MSG("not implemented"); | |
193 | ||
194 | return FALSE; | |
195 | } | |
196 | ||
197 | bool wxIniConfig::GetNextGroup (wxString& str, long& lIndex) const | |
198 | { | |
199 | wxFAIL_MSG("not implemented"); | |
200 | ||
201 | return FALSE; | |
202 | } | |
203 | ||
204 | bool wxIniConfig::GetFirstEntry(wxString& str, long& lIndex) const | |
205 | { | |
206 | wxFAIL_MSG("not implemented"); | |
207 | ||
208 | return FALSE; | |
209 | } | |
210 | ||
211 | bool wxIniConfig::GetNextEntry (wxString& str, long& lIndex) const | |
212 | { | |
213 | wxFAIL_MSG("not implemented"); | |
214 | ||
215 | return FALSE; | |
216 | } | |
217 | ||
218 | // ---------------------------------------------------------------------------- | |
219 | // misc info | |
220 | // ---------------------------------------------------------------------------- | |
221 | ||
222 | // not implemented | |
c86f1403 | 223 | size_t wxIniConfig::GetNumberOfEntries(bool bRecursive) const |
02569ba8 VZ |
224 | { |
225 | wxFAIL_MSG("not implemented"); | |
226 | ||
c86f1403 | 227 | return (size_t)-1; |
02569ba8 VZ |
228 | } |
229 | ||
c86f1403 | 230 | size_t wxIniConfig::GetNumberOfGroups(bool bRecursive) const |
02569ba8 VZ |
231 | { |
232 | wxFAIL_MSG("not implemented"); | |
233 | ||
c86f1403 | 234 | return (size_t)-1; |
02569ba8 VZ |
235 | } |
236 | ||
237 | bool wxIniConfig::HasGroup(const wxString& strName) const | |
238 | { | |
239 | wxFAIL_MSG("not implemented"); | |
240 | ||
241 | return FALSE; | |
242 | } | |
243 | ||
244 | bool wxIniConfig::HasEntry(const wxString& strName) const | |
245 | { | |
246 | wxFAIL_MSG("not implemented"); | |
247 | ||
248 | return FALSE; | |
249 | } | |
250 | ||
251 | // is current group empty? | |
252 | bool wxIniConfig::IsEmpty() const | |
253 | { | |
254 | char szBuf[1024]; | |
255 | ||
256 | GetPrivateProfileString(m_strGroup, NULL, "", | |
18244936 | 257 | szBuf, WXSIZEOF(szBuf), m_strLocalFilename); |
02569ba8 VZ |
258 | if ( !::IsEmpty(szBuf) ) |
259 | return FALSE; | |
260 | ||
261 | GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf)); | |
262 | if ( !::IsEmpty(szBuf) ) | |
263 | return FALSE; | |
264 | ||
265 | return TRUE; | |
266 | } | |
267 | ||
268 | // ---------------------------------------------------------------------------- | |
269 | // read/write | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
18244936 | 272 | bool wxIniConfig::Read(const wxString& szKey, wxString *pstr) const |
02569ba8 | 273 | { |
18244936 | 274 | wxConfigPathChanger path(this, szKey); |
02569ba8 VZ |
275 | wxString strKey = GetPrivateKeyName(path.Name()); |
276 | ||
277 | char szBuf[1024]; // @@ should dynamically allocate memory... | |
278 | ||
279 | // first look in the private INI file | |
280 | ||
281 | // NB: the lpDefault param to GetPrivateProfileString can't be NULL | |
282 | GetPrivateProfileString(m_strGroup, strKey, "", | |
18244936 | 283 | szBuf, WXSIZEOF(szBuf), m_strLocalFilename); |
02569ba8 VZ |
284 | if ( ::IsEmpty(szBuf) ) { |
285 | // now look in win.ini | |
286 | wxString strKey = GetKeyName(path.Name()); | |
287 | GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf)); | |
288 | } | |
289 | ||
290 | if ( ::IsEmpty(szBuf) ) { | |
02569ba8 VZ |
291 | return FALSE; |
292 | } | |
293 | else { | |
9869734d | 294 | *pstr = szBuf ; |
02569ba8 VZ |
295 | return TRUE; |
296 | } | |
297 | } | |
298 | ||
18244936 JS |
299 | bool wxIniConfig::Read(const wxString& szKey, wxString *pstr, |
300 | const wxString& szDefault) const | |
02569ba8 | 301 | { |
18244936 JS |
302 | wxConfigPathChanger path(this, szKey); |
303 | wxString strKey = GetPrivateKeyName(path.Name()); | |
304 | ||
305 | char szBuf[1024]; // @@ should dynamically allocate memory... | |
306 | ||
307 | // first look in the private INI file | |
308 | ||
309 | // NB: the lpDefault param to GetPrivateProfileString can't be NULL | |
310 | GetPrivateProfileString(m_strGroup, strKey, "", | |
311 | szBuf, WXSIZEOF(szBuf), m_strLocalFilename); | |
312 | if ( ::IsEmpty(szBuf) ) { | |
313 | // now look in win.ini | |
314 | wxString strKey = GetKeyName(path.Name()); | |
315 | GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf)); | |
316 | } | |
02569ba8 | 317 | |
18244936 JS |
318 | if ( ::IsEmpty(szBuf) ) { |
319 | *pstr = szDefault; | |
320 | return FALSE; | |
321 | } | |
322 | else { | |
9869734d | 323 | *pstr = szBuf ; |
18244936 JS |
324 | return TRUE; |
325 | } | |
02569ba8 VZ |
326 | } |
327 | ||
18244936 | 328 | bool wxIniConfig::Read(const wxString& szKey, long *pl) const |
02569ba8 | 329 | { |
18244936 | 330 | wxConfigPathChanger path(this, szKey); |
02569ba8 VZ |
331 | wxString strKey = GetPrivateKeyName(path.Name()); |
332 | ||
333 | // hack: we have no mean to know if it really found the default value or | |
334 | // didn't find anything, so we call it twice | |
335 | ||
336 | static const int nMagic = 17; // 17 is some "rare" number | |
337 | static const int nMagic2 = 28; // arbitrary number != nMagic | |
18244936 | 338 | long lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename); |
02569ba8 VZ |
339 | if ( lVal != nMagic ) { |
340 | // the value was read from the file | |
341 | *pl = lVal; | |
342 | return TRUE; | |
343 | } | |
344 | ||
345 | // is it really nMagic? | |
18244936 | 346 | lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename); |
519cb848 | 347 | if ( lVal != nMagic2 ) { |
02569ba8 VZ |
348 | // the nMagic it returned was indeed read from the file |
349 | *pl = lVal; | |
350 | return TRUE; | |
351 | } | |
352 | ||
519cb848 SC |
353 | // CS : I have no idea why they should look up in win.ini |
354 | // and if at all they have to do the same procedure using the two magic numbers | |
355 | // otherwise it always returns true, even if the key was not there at all | |
356 | #if 0 | |
02569ba8 | 357 | // no, it was just returning the default value, so now look in win.ini |
519cb848 | 358 | *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl); |
02569ba8 | 359 | |
02569ba8 | 360 | return TRUE; |
519cb848 SC |
361 | #endif |
362 | return FALSE ; | |
02569ba8 VZ |
363 | } |
364 | ||
18244936 | 365 | bool wxIniConfig::Write(const wxString& szKey, const wxString& szValue) |
02569ba8 | 366 | { |
18244936 | 367 | wxConfigPathChanger path(this, szKey); |
02569ba8 VZ |
368 | wxString strKey = GetPrivateKeyName(path.Name()); |
369 | ||
370 | bool bOk = WritePrivateProfileString(m_strGroup, strKey, | |
18244936 | 371 | szValue, m_strLocalFilename) != 0; |
02569ba8 VZ |
372 | |
373 | if ( !bOk ) | |
374 | wxLogLastError("WritePrivateProfileString"); | |
375 | ||
376 | return bOk; | |
377 | } | |
378 | ||
18244936 | 379 | bool wxIniConfig::Write(const wxString& szKey, long lValue) |
02569ba8 VZ |
380 | { |
381 | // ltoa() is not ANSI :-( | |
382 | char szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits) | |
383 | sprintf(szBuf, "%ld", lValue); | |
384 | ||
385 | return Write(szKey, szBuf); | |
386 | } | |
387 | ||
388 | bool wxIniConfig::Flush(bool /* bCurrentOnly */) | |
389 | { | |
390 | // this is just the way it works | |
18244936 | 391 | return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0; |
02569ba8 VZ |
392 | } |
393 | ||
394 | // ---------------------------------------------------------------------------- | |
395 | // delete | |
396 | // ---------------------------------------------------------------------------- | |
397 | ||
1e6d9499 | 398 | bool wxIniConfig::DeleteEntry(const wxString& szKey, bool bGroupIfEmptyAlso) |
02569ba8 VZ |
399 | { |
400 | // passing NULL as value to WritePrivateProfileString deletes the key | |
2432b92d JS |
401 | // if ( !Write(szKey, (const char *)NULL) ) |
402 | // return FALSE; | |
403 | wxConfigPathChanger path(this, szKey); | |
404 | wxString strKey = GetPrivateKeyName(path.Name()); | |
405 | ||
406 | if (WritePrivateProfileString(m_strGroup, szKey, | |
407 | (const char*) NULL, m_strLocalFilename) == 0) | |
02569ba8 | 408 | return FALSE; |
2432b92d | 409 | |
02569ba8 VZ |
410 | if ( !bGroupIfEmptyAlso || !IsEmpty() ) |
411 | return TRUE; | |
412 | ||
413 | // delete the current group too | |
414 | bool bOk = WritePrivateProfileString(m_strGroup, NULL, | |
18244936 | 415 | NULL, m_strLocalFilename) != 0; |
02569ba8 VZ |
416 | |
417 | if ( !bOk ) | |
418 | wxLogLastError("WritePrivateProfileString"); | |
419 | ||
420 | return bOk; | |
421 | } | |
422 | ||
1e6d9499 | 423 | bool wxIniConfig::DeleteGroup(const wxString& szKey) |
02569ba8 | 424 | { |
18244936 | 425 | wxConfigPathChanger path(this, szKey); |
02569ba8 | 426 | |
9869734d | 427 | // passing NULL as section name to WritePrivateProfileString deletes the |
02569ba8 VZ |
428 | // whole section according to the docs |
429 | bool bOk = WritePrivateProfileString(path.Name(), NULL, | |
18244936 | 430 | NULL, m_strLocalFilename) != 0; |
02569ba8 VZ |
431 | |
432 | if ( !bOk ) | |
433 | wxLogLastError("WritePrivateProfileString"); | |
434 | ||
435 | return bOk; | |
436 | } | |
437 | ||
1e6d9499 JS |
438 | #ifndef MAX_PATH |
439 | #define MAX_PATH 256 | |
440 | #endif | |
441 | ||
02569ba8 VZ |
442 | bool wxIniConfig::DeleteAll() |
443 | { | |
444 | // first delete our group in win.ini | |
18244936 | 445 | WriteProfileString(GetVendorName(), NULL, NULL); |
02569ba8 VZ |
446 | |
447 | // then delete our own ini file | |
448 | char szBuf[MAX_PATH]; | |
c86f1403 | 449 | size_t nRc = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf)); |
02569ba8 | 450 | if ( nRc == 0 ) |
a1665b22 | 451 | { |
02569ba8 | 452 | wxLogLastError("GetWindowsDirectory"); |
a1665b22 | 453 | } |
02569ba8 | 454 | else if ( nRc > WXSIZEOF(szBuf) ) |
a1665b22 | 455 | { |
02569ba8 | 456 | wxFAIL_MSG("buffer is too small for Windows directory."); |
a1665b22 | 457 | } |
02569ba8 VZ |
458 | |
459 | wxString strFile = szBuf; | |
18244936 | 460 | strFile << '\\' << m_strLocalFilename; |
02569ba8 | 461 | |
1e6d9499 | 462 | if ( !wxRemoveFile(strFile) ) { |
02569ba8 VZ |
463 | wxLogSysError(_("Can't delete the INI file '%s'"), strFile.c_str()); |
464 | return FALSE; | |
465 | } | |
466 | ||
467 | return TRUE; | |
468 | } | |
574c0bbf JS |
469 | |
470 | bool wxIniConfig::RenameEntry(const wxString& oldName, const wxString& newName) | |
471 | { | |
472 | // Not implemented | |
473 | return FALSE; | |
474 | } | |
475 | ||
476 | bool wxIniConfig::RenameGroup(const wxString& oldName, const wxString& newName) | |
477 | { | |
478 | // Not implemented | |
479 | return FALSE; | |
480 | } |