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