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