]> git.saurik.com Git - wxWidgets.git/blame - src/msw/iniconf.cpp
Fix for ambiguities which happen in STL=1 mode under DigitalMars C++.
[wxWidgets.git] / src / msw / iniconf.cpp
CommitLineData
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
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a3b46648
UU
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"
999836aa 38#include "wx/file.h"
02569ba8 39
3096bd2f 40#include "wx/msw/iniconf.h"
02569ba8
VZ
41
42// _WINDOWS_ is defined when windows.h is included,
43// __WXMSW__ is defined for MS Windows compilation
44#if defined(__WXMSW__) && !defined(_WINDOWS_)
9ed0d735 45 #include "wx/msw/wrapwin.h"
02569ba8
VZ
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
9869734d
VZ
63wxIniConfig::wxIniConfig(const wxString& strAppName,
64 const wxString& strVendor,
65 const wxString& localFilename,
66 const wxString& globalFilename,
67 long style)
8f19c916
JS
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()
d2c7e39b 72 : strAppName,
8f19c916 73 strVendor.IsEmpty() ? (wxTheApp ? wxTheApp->GetVendorName()
d2c7e39b
JS
74 : strAppName)
75 : strVendor,
76 localFilename, globalFilename, style)
8f19c916 77#endif
02569ba8 78{
8f19c916
JS
79 if (strAppName.IsEmpty() && wxTheApp)
80 SetAppName(wxTheApp->GetAppName());
81 if (strVendor.IsEmpty() && wxTheApp)
82 SetVendorName(wxTheApp->GetVendorName());
83
18244936
JS
84 m_strLocalFilename = localFilename;
85 if (m_strLocalFilename.IsEmpty())
86 {
f6bcfd97 87 m_strLocalFilename = GetAppName() + wxT(".ini");
18244936
JS
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]) &&
f6bcfd97 93 m_strLocalFilename.Find(wxT('.')) == wxNOT_FOUND )
18244936 94 {
f6bcfd97 95 m_strLocalFilename << wxT(".ini");
18244936
JS
96 }
97
98 // set root path
fda7962d 99 SetPath(wxEmptyString);
02569ba8
VZ
100}
101
102wxIniConfig::~wxIniConfig()
103{
104}
105
106// ----------------------------------------------------------------------------
107// path management
108// ----------------------------------------------------------------------------
109
110void 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
c86f1403 128 size_t nPartsCount = aParts.Count();
02569ba8
VZ
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];
c86f1403 137 for ( size_t nPart = 1; nPart < nPartsCount; nPart++ ) {
02569ba8
VZ
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) &&
62448488 147 (m_strGroup == wxString(PATH_SEP_REPLACE) ||
02569ba8
VZ
148 m_strGroup.Last() != PATH_SEP_REPLACE) );
149}
150
151const wxString& wxIniConfig::GetPath() const
152{
153 static wxString s_str;
154
155 // always return abs path
156 s_str = wxCONFIG_PATH_SEPARATOR;
157
ce3ed50d 158 if ( m_strGroup == wxString(PATH_SEP_REPLACE) ) {
02569ba8
VZ
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
18244936 173wxString wxIniConfig::GetPrivateKeyName(const wxString& szKey) const
02569ba8
VZ
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
18244936 185wxString wxIniConfig::GetKeyName(const wxString& szKey) const
02569ba8
VZ
186{
187 wxString strKey;
188
62448488 189 if ( m_strGroup != wxString(PATH_SEP_REPLACE) )
02569ba8
VZ
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
2eb10e2a 204bool wxIniConfig::GetFirstGroup(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
02569ba8
VZ
205{
206 wxFAIL_MSG("not implemented");
207
59af881e 208 return false;
02569ba8
VZ
209}
210
2eb10e2a 211bool wxIniConfig::GetNextGroup (wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
02569ba8
VZ
212{
213 wxFAIL_MSG("not implemented");
214
59af881e 215 return false;
02569ba8
VZ
216}
217
2eb10e2a 218bool wxIniConfig::GetFirstEntry(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
02569ba8
VZ
219{
220 wxFAIL_MSG("not implemented");
221
59af881e 222 return false;
02569ba8
VZ
223}
224
2eb10e2a 225bool wxIniConfig::GetNextEntry (wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
02569ba8
VZ
226{
227 wxFAIL_MSG("not implemented");
228
59af881e 229 return false;
02569ba8
VZ
230}
231
232// ----------------------------------------------------------------------------
233// misc info
234// ----------------------------------------------------------------------------
235
236// not implemented
2eb10e2a 237size_t wxIniConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive)) const
02569ba8
VZ
238{
239 wxFAIL_MSG("not implemented");
240
c86f1403 241 return (size_t)-1;
02569ba8
VZ
242}
243
2eb10e2a 244size_t wxIniConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive)) const
02569ba8
VZ
245{
246 wxFAIL_MSG("not implemented");
247
c86f1403 248 return (size_t)-1;
02569ba8
VZ
249}
250
2eb10e2a 251bool wxIniConfig::HasGroup(const wxString& WXUNUSED(strName)) const
02569ba8
VZ
252{
253 wxFAIL_MSG("not implemented");
254
59af881e 255 return false;
02569ba8
VZ
256}
257
2eb10e2a 258bool wxIniConfig::HasEntry(const wxString& WXUNUSED(strName)) const
02569ba8
VZ
259{
260 wxFAIL_MSG("not implemented");
261
59af881e 262 return false;
02569ba8
VZ
263}
264
265// is current group empty?
266bool wxIniConfig::IsEmpty() const
267{
268 char szBuf[1024];
269
270 GetPrivateProfileString(m_strGroup, NULL, "",
18244936 271 szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
02569ba8 272 if ( !::IsEmpty(szBuf) )
59af881e 273 return false;
02569ba8
VZ
274
275 GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
276 if ( !::IsEmpty(szBuf) )
59af881e 277 return false;
02569ba8 278
59af881e 279 return true;
02569ba8
VZ
280}
281
282// ----------------------------------------------------------------------------
283// read/write
284// ----------------------------------------------------------------------------
285
2ba41305 286bool wxIniConfig::DoReadString(const wxString& szKey, wxString *pstr) const
02569ba8 287{
18244936 288 wxConfigPathChanger path(this, szKey);
02569ba8
VZ
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, "",
18244936 297 szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
02569ba8
VZ
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
0cb50c83 304 if ( ::IsEmpty(szBuf) )
59af881e 305 return false;
0cb50c83
VZ
306
307 *pstr = szBuf;
59af881e 308 return true;
02569ba8
VZ
309}
310
2ba41305 311bool wxIniConfig::DoReadLong(const wxString& szKey, long *pl) const
02569ba8 312{
18244936 313 wxConfigPathChanger path(this, szKey);
02569ba8
VZ
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
18244936 321 long lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
02569ba8
VZ
322 if ( lVal != nMagic ) {
323 // the value was read from the file
324 *pl = lVal;
59af881e 325 return true;
02569ba8
VZ
326 }
327
328 // is it really nMagic?
18244936 329 lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
519cb848 330 if ( lVal != nMagic2 ) {
02569ba8
VZ
331 // the nMagic it returned was indeed read from the file
332 *pl = lVal;
59af881e 333 return true;
02569ba8
VZ
334 }
335
519cb848
SC
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
02569ba8 340 // no, it was just returning the default value, so now look in win.ini
519cb848 341 *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
02569ba8 342
59af881e 343 return true;
519cb848 344#endif
59af881e 345 return false ;
02569ba8
VZ
346}
347
2ba41305 348bool wxIniConfig::DoWriteString(const wxString& szKey, const wxString& szValue)
02569ba8 349{
18244936 350 wxConfigPathChanger path(this, szKey);
02569ba8
VZ
351 wxString strKey = GetPrivateKeyName(path.Name());
352
353 bool bOk = WritePrivateProfileString(m_strGroup, strKey,
18244936 354 szValue, m_strLocalFilename) != 0;
02569ba8
VZ
355
356 if ( !bOk )
f6bcfd97 357 wxLogLastError(wxT("WritePrivateProfileString"));
02569ba8
VZ
358
359 return bOk;
360}
361
2ba41305 362bool wxIniConfig::DoWriteLong(const wxString& szKey, long lValue)
02569ba8
VZ
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
371bool wxIniConfig::Flush(bool /* bCurrentOnly */)
372{
373 // this is just the way it works
18244936 374 return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
02569ba8
VZ
375}
376
377// ----------------------------------------------------------------------------
378// delete
379// ----------------------------------------------------------------------------
380
1e6d9499 381bool wxIniConfig::DeleteEntry(const wxString& szKey, bool bGroupIfEmptyAlso)
02569ba8
VZ
382{
383 // passing NULL as value to WritePrivateProfileString deletes the key
2432b92d
JS
384 wxConfigPathChanger path(this, szKey);
385 wxString strKey = GetPrivateKeyName(path.Name());
386
ff1b5f81 387 if (WritePrivateProfileString(m_strGroup, strKey,
2432b92d 388 (const char*) NULL, m_strLocalFilename) == 0)
59af881e 389 return false;
2432b92d 390
02569ba8 391 if ( !bGroupIfEmptyAlso || !IsEmpty() )
59af881e 392 return true;
02569ba8
VZ
393
394 // delete the current group too
395 bool bOk = WritePrivateProfileString(m_strGroup, NULL,
18244936 396 NULL, m_strLocalFilename) != 0;
02569ba8
VZ
397
398 if ( !bOk )
f6bcfd97 399 wxLogLastError(wxT("WritePrivateProfileString"));
02569ba8
VZ
400
401 return bOk;
402}
403
1e6d9499 404bool wxIniConfig::DeleteGroup(const wxString& szKey)
02569ba8 405{
18244936 406 wxConfigPathChanger path(this, szKey);
02569ba8 407
9869734d 408 // passing NULL as section name to WritePrivateProfileString deletes the
02569ba8
VZ
409 // whole section according to the docs
410 bool bOk = WritePrivateProfileString(path.Name(), NULL,
18244936 411 NULL, m_strLocalFilename) != 0;
02569ba8
VZ
412
413 if ( !bOk )
f6bcfd97 414 wxLogLastError(wxT("WritePrivateProfileString"));
02569ba8
VZ
415
416 return bOk;
417}
418
1e6d9499
JS
419#ifndef MAX_PATH
420#define MAX_PATH 256
421#endif
422
02569ba8
VZ
423bool wxIniConfig::DeleteAll()
424{
425 // first delete our group in win.ini
18244936 426 WriteProfileString(GetVendorName(), NULL, NULL);
02569ba8
VZ
427
428 // then delete our own ini file
429 char szBuf[MAX_PATH];
c86f1403 430 size_t nRc = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
02569ba8 431 if ( nRc == 0 )
a1665b22 432 {
f6bcfd97 433 wxLogLastError(wxT("GetWindowsDirectory"));
a1665b22 434 }
02569ba8 435 else if ( nRc > WXSIZEOF(szBuf) )
a1665b22 436 {
f6bcfd97 437 wxFAIL_MSG(wxT("buffer is too small for Windows directory."));
a1665b22 438 }
02569ba8
VZ
439
440 wxString strFile = szBuf;
18244936 441 strFile << '\\' << m_strLocalFilename;
02569ba8 442
cb820f80 443 if ( wxFile::Exists(strFile) && !wxRemoveFile(strFile) ) {
02569ba8 444 wxLogSysError(_("Can't delete the INI file '%s'"), strFile.c_str());
59af881e 445 return false;
02569ba8
VZ
446 }
447
59af881e 448 return true;
02569ba8 449}
574c0bbf 450
2eb10e2a
VZ
451bool wxIniConfig::RenameEntry(const wxString& WXUNUSED(oldName),
452 const wxString& WXUNUSED(newName))
574c0bbf
JS
453{
454 // Not implemented
59af881e 455 return false;
574c0bbf
JS
456}
457
2eb10e2a
VZ
458bool wxIniConfig::RenameGroup(const wxString& WXUNUSED(oldName),
459 const wxString& WXUNUSED(newName))
574c0bbf
JS
460{
461 // Not implemented
59af881e 462 return false;
574c0bbf 463}
f6bcfd97
BP
464
465#endif
466 // wxUSE_CONFIG && wxUSE_UNICODE