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