]> git.saurik.com Git - wxWidgets.git/blame - src/msw/iniconf.cpp
somehow log target wasn't being created automatically anymore. Restored.
[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
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 license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
22 #pragma hdrstop
23#endif //__BORLANDC__
24
25#ifndef WX_PRECOMP
26 #include <wx/string.h>
27 #include <wx/intl.h>
18244936 28 #include <wx/app.h>
02569ba8
VZ
29#endif //WX_PRECOMP
30
31#include <wx/dynarray.h>
32#include <wx/log.h>
33#include <wx/config.h>
34
35#include <wx/msw/iniconf.h>
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
18244936
JS
58wxIniConfig::wxIniConfig(const wxString& strAppName, const wxString& strVendor,
59 const wxString& localFilename, const wxString& globalFilename, long style):
60 wxConfigBase(strAppName, strVendor, localFilename, globalFilename, style)
02569ba8 61{
18244936
JS
62 if ( GetAppName().IsEmpty() )
63 {
64 wxString app;
65 if (wxTheApp)
66 app = wxTheApp->GetAppName();
67 wxASSERT( !app.IsEmpty() );
68 SetAppName(app);
69 }
02569ba8 70
18244936
JS
71 // Vendor name is required in wxIniConfig.
72 // TODO: should it be required? Why isn't appName used instead? -- JACS
73 if ( GetVendorName().IsEmpty() )
74 {
75 wxString vendor;
76 if (wxTheApp)
77 vendor = wxTheApp->GetVendorName();
78 else
79 vendor = strAppName;
80 SetVendorName(vendor);
81 }
82
83 m_strLocalFilename = localFilename;
84 if (m_strLocalFilename.IsEmpty())
85 {
86 m_strLocalFilename = GetAppName() + ".ini";
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]) &&
92 m_strLocalFilename.Find('.') == NOT_FOUND )
93 {
94 m_strLocalFilename << ".ini";
95 }
96
97 // set root path
98 SetPath("");
02569ba8
VZ
99}
100
101wxIniConfig::~wxIniConfig()
102{
103}
104
105// ----------------------------------------------------------------------------
106// path management
107// ----------------------------------------------------------------------------
108
109void 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) &&
146 (m_strGroup == PATH_SEP_REPLACE ||
147 m_strGroup.Last() != PATH_SEP_REPLACE) );
148}
149
150const wxString& wxIniConfig::GetPath() const
151{
152 static wxString s_str;
153
154 // always return abs path
155 s_str = wxCONFIG_PATH_SEPARATOR;
156
157 if ( m_strGroup == PATH_SEP_REPLACE ) {
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 172wxString 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 184wxString wxIniConfig::GetKeyName(const wxString& szKey) const
02569ba8
VZ
185{
186 wxString strKey;
187
188 if ( m_strGroup != PATH_SEP_REPLACE )
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
203bool wxIniConfig::GetFirstGroup(wxString& str, long& lIndex) const
204{
205 wxFAIL_MSG("not implemented");
206
207 return FALSE;
208}
209
210bool wxIniConfig::GetNextGroup (wxString& str, long& lIndex) const
211{
212 wxFAIL_MSG("not implemented");
213
214 return FALSE;
215}
216
217bool wxIniConfig::GetFirstEntry(wxString& str, long& lIndex) const
218{
219 wxFAIL_MSG("not implemented");
220
221 return FALSE;
222}
223
224bool 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 236size_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 243size_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
250bool wxIniConfig::HasGroup(const wxString& strName) const
251{
252 wxFAIL_MSG("not implemented");
253
254 return FALSE;
255}
256
257bool wxIniConfig::HasEntry(const wxString& strName) const
258{
259 wxFAIL_MSG("not implemented");
260
261 return FALSE;
262}
263
264// is current group empty?
265bool 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
18244936 285bool wxIniConfig::Read(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
303 if ( ::IsEmpty(szBuf) ) {
02569ba8
VZ
304 return FALSE;
305 }
306 else {
307 return TRUE;
308 }
309}
310
18244936
JS
311bool wxIniConfig::Read(const wxString& szKey, wxString *pstr,
312 const wxString& szDefault) const
02569ba8 313{
18244936
JS
314 wxConfigPathChanger path(this, szKey);
315 wxString strKey = GetPrivateKeyName(path.Name());
316
317 char szBuf[1024]; // @@ should dynamically allocate memory...
318
319 // first look in the private INI file
320
321 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
322 GetPrivateProfileString(m_strGroup, strKey, "",
323 szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
324 if ( ::IsEmpty(szBuf) ) {
325 // now look in win.ini
326 wxString strKey = GetKeyName(path.Name());
327 GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
328 }
02569ba8 329
18244936
JS
330 if ( ::IsEmpty(szBuf) ) {
331 *pstr = szDefault;
332 return FALSE;
333 }
334 else {
335 return TRUE;
336 }
02569ba8
VZ
337}
338
18244936 339bool wxIniConfig::Read(const wxString& szKey, long *pl) const
02569ba8 340{
18244936 341 wxConfigPathChanger path(this, szKey);
02569ba8
VZ
342 wxString strKey = GetPrivateKeyName(path.Name());
343
344 // hack: we have no mean to know if it really found the default value or
345 // didn't find anything, so we call it twice
346
347 static const int nMagic = 17; // 17 is some "rare" number
348 static const int nMagic2 = 28; // arbitrary number != nMagic
18244936 349 long lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
02569ba8
VZ
350 if ( lVal != nMagic ) {
351 // the value was read from the file
352 *pl = lVal;
353 return TRUE;
354 }
355
356 // is it really nMagic?
18244936 357 lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
02569ba8
VZ
358 if ( lVal == nMagic ) {
359 // the nMagic it returned was indeed read from the file
360 *pl = lVal;
361 return TRUE;
362 }
363
364 // no, it was just returning the default value, so now look in win.ini
18244936 365 *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
02569ba8 366
02569ba8
VZ
367 return TRUE;
368}
369
18244936 370bool wxIniConfig::Write(const wxString& szKey, const wxString& szValue)
02569ba8 371{
18244936 372 wxConfigPathChanger path(this, szKey);
02569ba8
VZ
373 wxString strKey = GetPrivateKeyName(path.Name());
374
375 bool bOk = WritePrivateProfileString(m_strGroup, strKey,
18244936 376 szValue, m_strLocalFilename) != 0;
02569ba8
VZ
377
378 if ( !bOk )
379 wxLogLastError("WritePrivateProfileString");
380
381 return bOk;
382}
383
18244936 384bool wxIniConfig::Write(const wxString& szKey, long lValue)
02569ba8
VZ
385{
386 // ltoa() is not ANSI :-(
387 char szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits)
388 sprintf(szBuf, "%ld", lValue);
389
390 return Write(szKey, szBuf);
391}
392
393bool wxIniConfig::Flush(bool /* bCurrentOnly */)
394{
395 // this is just the way it works
18244936 396 return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
02569ba8
VZ
397}
398
399// ----------------------------------------------------------------------------
400// delete
401// ----------------------------------------------------------------------------
402
403bool wxIniConfig::DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso)
404{
405 // passing NULL as value to WritePrivateProfileString deletes the key
406 if ( !Write(szKey, (const char *)NULL) )
407 return FALSE;
408
409 if ( !bGroupIfEmptyAlso || !IsEmpty() )
410 return TRUE;
411
412 // delete the current group too
413 bool bOk = WritePrivateProfileString(m_strGroup, NULL,
18244936 414 NULL, m_strLocalFilename) != 0;
02569ba8
VZ
415
416 if ( !bOk )
417 wxLogLastError("WritePrivateProfileString");
418
419 return bOk;
420}
421
422bool wxIniConfig::DeleteGroup(const char *szKey)
423{
18244936 424 wxConfigPathChanger path(this, szKey);
02569ba8
VZ
425
426 // passing NULL as section name to WritePrivateProfileString deletes the
427 // whole section according to the docs
428 bool bOk = WritePrivateProfileString(path.Name(), NULL,
18244936 429 NULL, m_strLocalFilename) != 0;
02569ba8
VZ
430
431 if ( !bOk )
432 wxLogLastError("WritePrivateProfileString");
433
434 return bOk;
435}
436
437bool wxIniConfig::DeleteAll()
438{
439 // first delete our group in win.ini
18244936 440 WriteProfileString(GetVendorName(), NULL, NULL);
02569ba8
VZ
441
442 // then delete our own ini file
443 char szBuf[MAX_PATH];
c86f1403 444 size_t nRc = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
02569ba8
VZ
445 if ( nRc == 0 )
446 wxLogLastError("GetWindowsDirectory");
447 else if ( nRc > WXSIZEOF(szBuf) )
448 wxFAIL_MSG("buffer is too small for Windows directory.");
449
450 wxString strFile = szBuf;
18244936 451 strFile << '\\' << m_strLocalFilename;
02569ba8
VZ
452
453 if ( !DeleteFile(strFile) ) {
454 wxLogSysError(_("Can't delete the INI file '%s'"), strFile.c_str());
455 return FALSE;
456 }
457
458 return TRUE;
459}