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