]> git.saurik.com Git - wxWidgets.git/blob - src/msw/iniconf.cpp
Removed the PREEXTRALIBS hack - no longer needed
[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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
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 #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
58 wxIniConfig::wxIniConfig(const wxString& strAppName,
59 const wxString& strVendor,
60 const wxString& localFilename,
61 const wxString& globalFilename,
62 long style)
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()
67 : strAppName,
68 strVendor.IsEmpty() ? (wxTheApp ? wxTheApp->GetVendorName()
69 : strAppName)
70 : strVendor,
71 localFilename, globalFilename, style)
72 #endif
73 {
74 if (strAppName.IsEmpty() && wxTheApp)
75 SetAppName(wxTheApp->GetAppName());
76 if (strVendor.IsEmpty() && wxTheApp)
77 SetVendorName(wxTheApp->GetVendorName());
78
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]) &&
88 m_strLocalFilename.Find('.') == wxNOT_FOUND )
89 {
90 m_strLocalFilename << ".ini";
91 }
92
93 // set root path
94 SetPath("");
95 }
96
97 wxIniConfig::~wxIniConfig()
98 {
99 }
100
101 // ----------------------------------------------------------------------------
102 // path management
103 // ----------------------------------------------------------------------------
104
105 void 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
123 size_t nPartsCount = aParts.Count();
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];
132 for ( size_t nPart = 1; nPart < nPartsCount; nPart++ ) {
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) &&
142 (m_strGroup == wxString(PATH_SEP_REPLACE) ||
143 m_strGroup.Last() != PATH_SEP_REPLACE) );
144 }
145
146 const wxString& wxIniConfig::GetPath() const
147 {
148 static wxString s_str;
149
150 // always return abs path
151 s_str = wxCONFIG_PATH_SEPARATOR;
152
153 if ( m_strGroup == wxString(PATH_SEP_REPLACE) ) {
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
168 wxString wxIniConfig::GetPrivateKeyName(const wxString& szKey) const
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
180 wxString wxIniConfig::GetKeyName(const wxString& szKey) const
181 {
182 wxString strKey;
183
184 if ( m_strGroup != wxString(PATH_SEP_REPLACE) )
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
199 bool wxIniConfig::GetFirstGroup(wxString& str, long& lIndex) const
200 {
201 wxFAIL_MSG("not implemented");
202
203 return FALSE;
204 }
205
206 bool wxIniConfig::GetNextGroup (wxString& str, long& lIndex) const
207 {
208 wxFAIL_MSG("not implemented");
209
210 return FALSE;
211 }
212
213 bool wxIniConfig::GetFirstEntry(wxString& str, long& lIndex) const
214 {
215 wxFAIL_MSG("not implemented");
216
217 return FALSE;
218 }
219
220 bool 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
232 size_t wxIniConfig::GetNumberOfEntries(bool bRecursive) const
233 {
234 wxFAIL_MSG("not implemented");
235
236 return (size_t)-1;
237 }
238
239 size_t wxIniConfig::GetNumberOfGroups(bool bRecursive) const
240 {
241 wxFAIL_MSG("not implemented");
242
243 return (size_t)-1;
244 }
245
246 bool wxIniConfig::HasGroup(const wxString& strName) const
247 {
248 wxFAIL_MSG("not implemented");
249
250 return FALSE;
251 }
252
253 bool wxIniConfig::HasEntry(const wxString& strName) const
254 {
255 wxFAIL_MSG("not implemented");
256
257 return FALSE;
258 }
259
260 // is current group empty?
261 bool wxIniConfig::IsEmpty() const
262 {
263 char szBuf[1024];
264
265 GetPrivateProfileString(m_strGroup, NULL, "",
266 szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
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
281 bool wxIniConfig::Read(const wxString& szKey, wxString *pstr) const
282 {
283 wxConfigPathChanger path(this, szKey);
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, "",
292 szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
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) ) {
300 return FALSE;
301 }
302 else {
303 *pstr = szBuf ;
304 return TRUE;
305 }
306 }
307
308 bool wxIniConfig::Read(const wxString& szKey, wxString *pstr,
309 const wxString& szDefault) const
310 {
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 }
326
327 if ( ::IsEmpty(szBuf) ) {
328 *pstr = szDefault;
329 return FALSE;
330 }
331 else {
332 *pstr = szBuf ;
333 return TRUE;
334 }
335 }
336
337 bool wxIniConfig::Read(const wxString& szKey, long *pl) const
338 {
339 wxConfigPathChanger path(this, szKey);
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
347 long lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
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?
355 lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
356 if ( lVal != nMagic2 ) {
357 // the nMagic it returned was indeed read from the file
358 *pl = lVal;
359 return TRUE;
360 }
361
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
366 // no, it was just returning the default value, so now look in win.ini
367 *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
368
369 return TRUE;
370 #endif
371 return FALSE ;
372 }
373
374 bool wxIniConfig::Write(const wxString& szKey, const wxString& szValue)
375 {
376 wxConfigPathChanger path(this, szKey);
377 wxString strKey = GetPrivateKeyName(path.Name());
378
379 bool bOk = WritePrivateProfileString(m_strGroup, strKey,
380 szValue, m_strLocalFilename) != 0;
381
382 if ( !bOk )
383 wxLogLastError("WritePrivateProfileString");
384
385 return bOk;
386 }
387
388 bool wxIniConfig::Write(const wxString& szKey, long lValue)
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
397 bool wxIniConfig::Flush(bool /* bCurrentOnly */)
398 {
399 // this is just the way it works
400 return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
401 }
402
403 // ----------------------------------------------------------------------------
404 // delete
405 // ----------------------------------------------------------------------------
406
407 bool wxIniConfig::DeleteEntry(const wxString& szKey, bool bGroupIfEmptyAlso)
408 {
409 // passing NULL as value to WritePrivateProfileString deletes the key
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)
417 return FALSE;
418
419 if ( !bGroupIfEmptyAlso || !IsEmpty() )
420 return TRUE;
421
422 // delete the current group too
423 bool bOk = WritePrivateProfileString(m_strGroup, NULL,
424 NULL, m_strLocalFilename) != 0;
425
426 if ( !bOk )
427 wxLogLastError("WritePrivateProfileString");
428
429 return bOk;
430 }
431
432 bool wxIniConfig::DeleteGroup(const wxString& szKey)
433 {
434 wxConfigPathChanger path(this, szKey);
435
436 // passing NULL as section name to WritePrivateProfileString deletes the
437 // whole section according to the docs
438 bool bOk = WritePrivateProfileString(path.Name(), NULL,
439 NULL, m_strLocalFilename) != 0;
440
441 if ( !bOk )
442 wxLogLastError("WritePrivateProfileString");
443
444 return bOk;
445 }
446
447 #ifndef MAX_PATH
448 #define MAX_PATH 256
449 #endif
450
451 bool wxIniConfig::DeleteAll()
452 {
453 // first delete our group in win.ini
454 WriteProfileString(GetVendorName(), NULL, NULL);
455
456 // then delete our own ini file
457 char szBuf[MAX_PATH];
458 size_t nRc = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
459 if ( nRc == 0 )
460 {
461 wxLogLastError("GetWindowsDirectory");
462 }
463 else if ( nRc > WXSIZEOF(szBuf) )
464 {
465 wxFAIL_MSG("buffer is too small for Windows directory.");
466 }
467
468 wxString strFile = szBuf;
469 strFile << '\\' << m_strLocalFilename;
470
471 if ( !wxRemoveFile(strFile) ) {
472 wxLogSysError(_("Can't delete the INI file '%s'"), strFile.c_str());
473 return FALSE;
474 }
475
476 return TRUE;
477 }
478
479 bool wxIniConfig::RenameEntry(const wxString& oldName, const wxString& newName)
480 {
481 // Not implemented
482 return FALSE;
483 }
484
485 bool wxIniConfig::RenameGroup(const wxString& oldName, const wxString& newName)
486 {
487 // Not implemented
488 return FALSE;
489 }