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