Include wx/dynarray.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / os2 / iniconf.cpp
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 // RCS-ID: $Id$
8 // Copyright: David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_CONFIG
16
17 #ifndef WX_PRECOMP
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"
24 #endif //WX_PRECOMP
25
26 #include "wx/log.h"
27
28 #include "wx/config.h"
29
30 #include "wx/os2/iniconf.h"
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
50 wxIniConfig::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;
63 if (m_strLocalFilename.empty())
64 {
65 m_strLocalFilename = GetAppName() + wxT(".ini");
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)
70 if ( !wxIsPathSeparator(m_strLocalFilename[(size_t) 0]) &&
71 m_strLocalFilename.Find('.') == wxNOT_FOUND )
72 {
73 m_strLocalFilename << wxT(".ini");
74 }
75
76 // set root path
77 SetPath(wxEmptyString);
78 }
79
80 wxIniConfig::~wxIniConfig()
81 {
82 }
83
84 // ----------------------------------------------------------------------------
85 // path management
86 // ----------------------------------------------------------------------------
87
88 void wxIniConfig::SetPath(const wxString& strPath)
89 {
90 wxArrayString aParts;
91
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 }
108
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 }
126 }
127
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) );
133 }
134
135 const wxString& wxIniConfig::GetPath() const
136 {
137 static wxString s_str;
138
139 // always return abs path
140 s_str = wxCONFIG_PATH_SEPARATOR;
141
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 }
155 }
156
157 return s_str;
158 }
159
160 wxString wxIniConfig::GetPrivateKeyName(const wxString& szKey) const
161 {
162 wxString strKey;
163
164 if ( !m_strPath.empty() )
165 strKey << m_strPath << PATH_SEP_REPLACE;
166
167 strKey << szKey;
168
169 return strKey;
170 }
171
172 wxString wxIniConfig::GetKeyName(const wxString& szKey) const
173 {
174 wxString strKey;
175
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;
180
181 strKey << szKey;
182
183 return strKey;
184 }
185
186 // ----------------------------------------------------------------------------
187 // enumeration
188 // ----------------------------------------------------------------------------
189
190 // not implemented
191 bool wxIniConfig::GetFirstGroup(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
192 {
193 wxFAIL_MSG(wxT("not implemeted"));
194
195 return false;
196 }
197
198 bool wxIniConfig::GetNextGroup(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
199 {
200 wxFAIL_MSG(wxT("not implemeted"));
201
202 return false;
203 }
204
205 bool wxIniConfig::GetFirstEntry(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
206 {
207 wxFAIL_MSG(wxT("not implemeted"));
208
209 return false;
210 }
211
212 bool wxIniConfig::GetNextEntry(wxString& WXUNUSED(str), long& WXUNUSED(lIndex)) const
213 {
214 wxFAIL_MSG(wxT("not implemeted"));
215
216 return false;
217 }
218
219 // ----------------------------------------------------------------------------
220 // misc info
221 // ----------------------------------------------------------------------------
222
223 // not implemented
224 size_t wxIniConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive)) const
225 {
226 wxFAIL_MSG(wxT("not implemeted"));
227
228 return (size_t)-1;
229 }
230
231 size_t wxIniConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive)) const
232 {
233 wxFAIL_MSG(wxT("not implemeted"));
234
235 return (size_t)-1;
236 }
237
238 bool wxIniConfig::HasGroup(const wxString& WXUNUSED(strName)) const
239 {
240 wxFAIL_MSG(wxT("not implemeted"));
241
242 return false;
243 }
244
245 bool wxIniConfig::HasEntry(const wxString& WXUNUSED(strName)) const
246 {
247 wxFAIL_MSG(wxT("not implemeted"));
248
249 return false;
250 }
251
252 // is current group empty?
253 bool wxIniConfig::IsEmpty() const
254 {
255 char szBuf[1024];
256
257 // GetPrivateProfileString(m_strGroup, NULL, "",
258 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
259 if ( !::IsEmpty(szBuf) )
260 return false;
261
262 // GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
263 // if ( !::IsEmpty(szBuf) )
264 // return false;
265
266 return true;
267 }
268
269 // ----------------------------------------------------------------------------
270 // read/write
271 // ----------------------------------------------------------------------------
272
273 bool wxIniConfig::Read(const wxString& szKey, wxString *pstr) const
274 {
275 wxConfigPathChanger path(this, szKey);
276 wxString strKey = GetPrivateKeyName(path.Name());
277
278 wxChar szBuf[1024]; // @@ should dynamically allocate memory...
279
280 // first look in the private INI file
281
282 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
283 // GetPrivateProfileString(m_strGroup, strKey, "",
284 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
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 }
296
297 *pstr = szBuf ;
298 return true;
299 }
300
301 bool wxIniConfig::Read(const wxString& szKey, wxString *pstr,
302 const wxString& szDefault) const
303 {
304 wxConfigPathChanger path(this, szKey);
305 wxString strKey = GetPrivateKeyName(path.Name());
306
307 wxChar szBuf[1024]; // @@ should dynamically allocate memory...
308
309 // first look in the private INI file
310
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 }
331 }
332
333 bool wxIniConfig::Read(const wxString& szKey, long *pl) const
334 {
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;
365 }
366
367 bool wxIniConfig::Write(const wxString& szKey, const wxString& WXUNUSED(szValue))
368 {
369 wxConfigPathChanger path(this, szKey);
370 wxString strKey = GetPrivateKeyName(path.Name());
371
372 bool bOk = false; // = WritePrivateProfileString(m_strGroup, strKey,
373 // szValue, m_strLocalFilename) != 0;
374
375 if ( !bOk )
376 wxLogLastError(wxT("WritePrivateProfileString"));
377
378 return bOk;
379 }
380
381 bool 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
390 bool 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
401 bool 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 wxLogLastError(wxT("WritePrivateProfileString"));
422
423 return bOk;
424 }
425
426 bool 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
432 bool bOk = false; // = WritePrivateProfileString(path.Name(), NULL,
433 // NULL, m_strLocalFilename) != 0;
434
435 if ( !bOk )
436 wxLogLastError(wxT("WritePrivateProfileString"));
437
438 return bOk;
439 }
440
441 #ifndef MAX_PATH
442 #define MAX_PATH 256
443 #endif
444
445 bool wxIniConfig::DeleteAll()
446 {
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;
472 }
473
474 bool wxIniConfig::RenameEntry(const wxString& WXUNUSED(oldName), const wxString& WXUNUSED(newName))
475 {
476 // Not implemented
477 return false;
478 }
479
480 bool wxIniConfig::RenameGroup(const wxString& WXUNUSED(oldName), const wxString& WXUNUSED(newName))
481 {
482 // Not implemented
483 return false;
484 }
485
486 #endif //wxUSE_CONFIG