]> git.saurik.com Git - wxWidgets.git/blame - src/os2/iniconf.cpp
fixed warnings about truncating 64 bit integers
[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
14#ifndef WX_PRECOMP
15 #include <wx/string.h>
16 #include <wx/intl.h>
17 #include <wx/event.h>
18 #include <wx/app.h>
19 #include <wx/utils.h>
20#endif //WX_PRECOMP
21
22#include <wx/dynarray.h>
23#include <wx/log.h>
7e99520b 24
fb9010ed 25#include <wx/config.h>
7e99520b 26#if wxUSE_CONFIG
fb9010ed
DW
27
28#include <wx/os2/iniconf.h>
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
0fba44b4 75 SetPath(wxT(""));
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);
6670f564
WS
257 if ( !::IsEmpty(szBuf) )
258 return false;
fb9010ed
DW
259
260// GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
261// if ( !::IsEmpty(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);
6670f564
WS
283 if ( ::IsEmpty((PSZ)szBuf) )
284 {
285 // now look in win.ini
286 wxString strKey = GetKeyName(path.Name());
287 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
288 }
289
290 if ( ::IsEmpty((PSZ)szBuf) )
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{
302 wxConfigPathChanger path(this, szKey);
303 wxString strKey = GetPrivateKeyName(path.Name());
304
0fba44b4 305 wxChar szBuf[1024]; // @@ should dynamically allocate memory...
fb9010ed
DW
306
307 // first look in the private INI file
308
309 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
310// GetPrivateProfileString(m_strGroup, strKey, "",
311// szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
0fba44b4 312 if ( ::IsEmpty((PSZ)szBuf) ) {
fb9010ed
DW
313 // now look in win.ini
314 wxString strKey = GetKeyName(path.Name());
315// GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
316 }
317
0fba44b4 318 if ( ::IsEmpty((PSZ)szBuf) ) {
fb9010ed
DW
319 *pstr = szDefault;
320 return FALSE;
321 }
322 else {
323 *pstr = szBuf ;
324 return TRUE;
325 }
326}
327
328bool wxIniConfig::Read(const wxString& szKey, long *pl) const
329{
330 wxConfigPathChanger path(this, szKey);
331 wxString strKey = GetPrivateKeyName(path.Name());
332
333 // hack: we have no mean to know if it really found the default value or
334 // didn't find anything, so we call it twice
335
336 static const int nMagic = 17; // 17 is some "rare" number
337 static const int nMagic2 = 28; // arbitrary number != nMagic
9dea36ef 338 long lVal = 0; // = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
fb9010ed
DW
339 if ( lVal != nMagic ) {
340 // the value was read from the file
341 *pl = lVal;
342 return TRUE;
343 }
344
345 // is it really nMagic?
346// lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
347 if ( lVal == nMagic2 ) {
348 // the nMagic it returned was indeed read from the file
349 *pl = lVal;
350 return TRUE;
351 }
352
353 // no, it was just returning the default value, so now look in win.ini
354// *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
355
356 return TRUE;
357}
358
6670f564 359bool wxIniConfig::Write(const wxString& szKey, const wxString& WXUNUSED(szValue))
fb9010ed 360{
6670f564
WS
361 wxConfigPathChanger path(this, szKey);
362 wxString strKey = GetPrivateKeyName(path.Name());
fb9010ed 363
6670f564 364 bool bOk = false; // = WritePrivateProfileString(m_strGroup, strKey,
fb9010ed
DW
365// szValue, m_strLocalFilename) != 0;
366
6670f564
WS
367 if ( !bOk )
368 wxLogLastError(wxT("WritePrivateProfileString"));
fb9010ed 369
6670f564 370 return bOk;
fb9010ed
DW
371}
372
373bool wxIniConfig::Write(const wxString& szKey, long lValue)
374{
375 // ltoa() is not ANSI :-(
0fba44b4
DW
376 wxChar szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits)
377 wxSprintf(szBuf, wxT("%ld"), lValue);
fb9010ed
DW
378
379 return Write(szKey, szBuf);
380}
381
382bool wxIniConfig::Flush(bool /* bCurrentOnly */)
383{
384 // this is just the way it works
385// return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
386 return FALSE;
387}
388
389// ----------------------------------------------------------------------------
390// delete
391// ----------------------------------------------------------------------------
392
393bool wxIniConfig::DeleteEntry(const wxString& szKey, bool bGroupIfEmptyAlso)
394{
395 // passing NULL as value to WritePrivateProfileString deletes the key
396// if ( !Write(szKey, (const char *)NULL) )
397// return FALSE;
398 wxConfigPathChanger path(this, szKey);
399 wxString strKey = GetPrivateKeyName(path.Name());
400
401// if (WritePrivateProfileString(m_strGroup, szKey,
402// (const char*) NULL, m_strLocalFilename) == 0)
403// return FALSE;
404
405 if ( !bGroupIfEmptyAlso || !IsEmpty() )
406 return TRUE;
407
408 // delete the current group too
9dea36ef 409 bool bOk = FALSE; // = WritePrivateProfileString(m_strGroup, NULL,
fb9010ed
DW
410// NULL, m_strLocalFilename) != 0;
411
412 if ( !bOk )
2173b18f 413 wxLogLastError(wxT("WritePrivateProfileString"));
fb9010ed
DW
414
415 return bOk;
416}
417
418bool wxIniConfig::DeleteGroup(const wxString& szKey)
419{
420 wxConfigPathChanger path(this, szKey);
421
422 // passing NULL as section name to WritePrivateProfileString deletes the
423 // whole section according to the docs
9dea36ef 424 bool bOk = FALSE; // = WritePrivateProfileString(path.Name(), NULL,
fb9010ed
DW
425 // NULL, m_strLocalFilename) != 0;
426
427 if ( !bOk )
2173b18f 428 wxLogLastError(wxT("WritePrivateProfileString"));
fb9010ed
DW
429
430 return bOk;
431}
432
433#ifndef MAX_PATH
434#define MAX_PATH 256
435#endif
436
437bool wxIniConfig::DeleteAll()
438{
439 // first delete our group in win.ini
440// WriteProfileString(GetVendorName(), NULL, NULL);
441
442 // then delete our own ini file
0fba44b4 443 wxChar szBuf[MAX_PATH];
9dea36ef 444 size_t nRc = 0; // = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
fb9010ed
DW
445 if ( nRc == 0 )
446 {
2173b18f 447 wxLogLastError(wxT("GetWindowsDirectory"));
fb9010ed
DW
448 }
449 else if ( nRc > WXSIZEOF(szBuf) )
450 {
0fba44b4 451 wxFAIL_MSG(_("buffer is too small for Windows directory."));
fb9010ed
DW
452 }
453
454 wxString strFile = szBuf;
0fba44b4 455 strFile << wxT('\\') << m_strLocalFilename;
fb9010ed
DW
456
457 if ( !wxRemoveFile(strFile) ) {
458 wxLogSysError(_("Can't delete the INI file '%s'"), strFile.c_str());
459 return FALSE;
460 }
461
462 return TRUE;
463}
464
6670f564 465bool wxIniConfig::RenameEntry(const wxString& WXUNUSED(oldName), const wxString& WXUNUSED(newName))
fb9010ed
DW
466{
467 // Not implemented
6670f564 468 return false;
fb9010ed
DW
469}
470
6670f564 471bool wxIniConfig::RenameGroup(const wxString& WXUNUSED(oldName), const wxString& WXUNUSED(newName))
fb9010ed
DW
472{
473 // Not implemented
6670f564 474 return false;
fb9010ed 475}
7e99520b
DW
476
477#endif //wxUSE_CONFIG