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