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