]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/os2/iniconf.cpp
Fix another crash when conversion fails in Unix PostScript code.
[wxWidgets.git] / src / os2 / iniconf.cpp
... / ...
CommitLineData
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
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#if wxUSE_CONFIG
15
16#ifndef WX_PRECOMP
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"
23 #include "wx/log.h"
24#endif //WX_PRECOMP
25
26#include "wx/config.h"
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;
61 if (m_strLocalFilename.empty())
62 {
63 m_strLocalFilename = GetAppName() + wxT(".ini");
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)
68 if ( !wxIsPathSeparator(m_strLocalFilename[(size_t) 0]) &&
69 m_strLocalFilename.Find('.') == wxNOT_FOUND )
70 {
71 m_strLocalFilename << wxT(".ini");
72 }
73
74 // set root path
75 SetPath(wxEmptyString);
76}
77
78wxIniConfig::~wxIniConfig()
79{
80}
81
82// ----------------------------------------------------------------------------
83// path management
84// ----------------------------------------------------------------------------
85
86void wxIniConfig::SetPath(const wxString& strPath)
87{
88 wxArrayString aParts;
89
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 }
106
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 }
124 }
125
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) );
131}
132
133const wxString& wxIniConfig::GetPath() const
134{
135 static wxString s_str;
136
137 // always return abs path
138 s_str = wxCONFIG_PATH_SEPARATOR;
139
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 }
153 }
154
155 return s_str;
156}
157
158wxString wxIniConfig::GetPrivateKeyName(const wxString& szKey) const
159{
160 wxString strKey;
161
162 if ( !m_strPath.empty() )
163 strKey << m_strPath << PATH_SEP_REPLACE;
164
165 strKey << szKey;
166
167 return strKey;
168}
169
170wxString wxIniConfig::GetKeyName(const wxString& szKey) const
171{
172 wxString strKey;
173
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;
178
179 strKey << szKey;
180
181 return strKey;
182}
183
184// ----------------------------------------------------------------------------
185// enumeration
186// ----------------------------------------------------------------------------
187
188// not implemented
189bool wxIniConfig::GetFirstGroup(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
190{
191 wxFAIL_MSG(wxT("not implemeted"));
192
193 return false;
194}
195
196bool wxIniConfig::GetNextGroup(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
197{
198 wxFAIL_MSG(wxT("not implemeted"));
199
200 return false;
201}
202
203bool wxIniConfig::GetFirstEntry(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
204{
205 wxFAIL_MSG(wxT("not implemeted"));
206
207 return false;
208}
209
210bool wxIniConfig::GetNextEntry(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
211{
212 wxFAIL_MSG(wxT("not implemeted"));
213
214 return false;
215}
216
217// ----------------------------------------------------------------------------
218// misc info
219// ----------------------------------------------------------------------------
220
221// not implemented
222size_t wxIniConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive)) const
223{
224 wxFAIL_MSG(wxT("not implemeted"));
225
226 return (size_t)-1;
227}
228
229size_t wxIniConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive)) const
230{
231 wxFAIL_MSG(wxT("not implemeted"));
232
233 return (size_t)-1;
234}
235
236bool wxIniConfig::HasGroup(const wxString& WXUNUSED(strName)) const
237{
238 wxFAIL_MSG(wxT("not implemeted"));
239
240 return false;
241}
242
243bool wxIniConfig::HasEntry(const wxString& WXUNUSED(strName)) const
244{
245 wxFAIL_MSG(wxT("not implemeted"));
246
247 return false;
248}
249
250// is current group empty?
251bool wxIniConfig::IsEmpty() const
252{
253 char szBuf[1024];
254
255// GetPrivateProfileString(m_strGroup, NULL, "",
256// szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
257 if ( !::wxIsEmpty(szBuf) )
258 return false;
259
260// GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
261// if ( !::wxIsEmpty(szBuf) )
262// return false;
263
264 return true;
265}
266
267// ----------------------------------------------------------------------------
268// read/write
269// ----------------------------------------------------------------------------
270
271bool wxIniConfig::Read(const wxString& szKey, wxString *pstr) const
272{
273 wxConfigPathChanger path(this, szKey);
274 wxString strKey = GetPrivateKeyName(path.Name());
275
276 wxChar szBuf[1024]; // @@ should dynamically allocate memory...
277
278 // first look in the private INI file
279
280 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
281// GetPrivateProfileString(m_strGroup, strKey, "",
282// szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
283 if ( ::wxIsEmpty((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 ( ::wxIsEmpty((PSZ)szBuf) )
291 {
292 return false;
293 }
294
295 *pstr = szBuf ;
296 return true;
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
305 wxChar szBuf[1024]; // @@ should dynamically allocate memory...
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);
312 if ( ::wxIsEmpty((PSZ)szBuf) )
313 {
314 // now look in win.ini
315 wxString strKey = GetKeyName(path.Name());
316 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
317 }
318
319 if ( ::wxIsEmpty((PSZ)szBuf) )
320 {
321 *pstr = szDefault;
322 return false;
323 }
324 else
325 {
326 *pstr = szBuf ;
327 return true;
328 }
329}
330
331bool wxIniConfig::Read(const wxString& szKey, long *pl) const
332{
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;
363}
364
365bool wxIniConfig::Write(const wxString& szKey, const wxString& WXUNUSED(szValue))
366{
367 wxConfigPathChanger path(this, szKey);
368 wxString strKey = GetPrivateKeyName(path.Name());
369
370 bool bOk = false; // = WritePrivateProfileString(m_strGroup, strKey,
371// szValue, m_strLocalFilename) != 0;
372
373 if ( !bOk )
374 {
375 wxLogLastError(wxT("WritePrivateProfileString"));
376 }
377
378 return bOk;
379}
380
381bool wxIniConfig::Write(const wxString& szKey, long lValue)
382{
383 // ltoa() is not ANSI :-(
384 wxChar szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits)
385 wxSprintf(szBuf, wxT("%ld"), lValue);
386
387 return Write(szKey, szBuf);
388}
389
390bool wxIniConfig::Flush(bool /* bCurrentOnly */)
391{
392 // this is just the way it works
393 // return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
394 return false;
395}
396
397// ----------------------------------------------------------------------------
398// delete
399// ----------------------------------------------------------------------------
400
401bool wxIniConfig::DeleteEntry(const wxString& szKey, bool bGroupIfEmptyAlso)
402{
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());
408
409 // if (WritePrivateProfileString(m_strGroup, szKey,
410 // (const char*) NULL, m_strLocalFilename) == 0)
411 // return false;
412
413 if ( !bGroupIfEmptyAlso || !IsEmpty() )
414 return true;
415
416 // delete the current group too
417 bool bOk = false; // = WritePrivateProfileString(m_strGroup, NULL,
418 // NULL, m_strLocalFilename) != 0;
419
420 if ( !bOk )
421 {
422 wxLogLastError(wxT("WritePrivateProfileString"));
423 }
424
425 return bOk;
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
434 bool bOk = false; // = WritePrivateProfileString(path.Name(), NULL,
435 // NULL, m_strLocalFilename) != 0;
436
437 if ( !bOk )
438 {
439 wxLogLastError(wxT("WritePrivateProfileString"));
440 }
441
442 return bOk;
443}
444
445#ifndef MAX_PATH
446#define MAX_PATH 256
447#endif
448
449bool wxIniConfig::DeleteAll()
450{
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;
476}
477
478bool wxIniConfig::RenameEntry(const wxString& WXUNUSED(oldName), const wxString& WXUNUSED(newName))
479{
480 // Not implemented
481 return false;
482}
483
484bool wxIniConfig::RenameGroup(const wxString& WXUNUSED(oldName), const wxString& WXUNUSED(newName))
485{
486 // Not implemented
487 return false;
488}
489
490#endif //wxUSE_CONFIG