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