]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/os2/iniconf.cpp
Removed gsockosx.cpp which had been copied to src/mac/corefoundation long ago.
[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#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
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(wxT(""));
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 ( !::IsEmpty(szBuf) )
258 return false;
259
260// GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
261// if ( !::IsEmpty(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 ( ::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 }
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 ( ::IsEmpty((PSZ)szBuf) ) {
313 // now look in win.ini
314 wxString strKey = GetKeyName(path.Name());
315// GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
316 }
317
318 if ( ::IsEmpty((PSZ)szBuf) ) {
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
338 long lVal = 0; // = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
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
359bool wxIniConfig::Write(const wxString& szKey, const wxString& WXUNUSED(szValue))
360{
361 wxConfigPathChanger path(this, szKey);
362 wxString strKey = GetPrivateKeyName(path.Name());
363
364 bool bOk = false; // = WritePrivateProfileString(m_strGroup, strKey,
365// szValue, m_strLocalFilename) != 0;
366
367 if ( !bOk )
368 wxLogLastError(wxT("WritePrivateProfileString"));
369
370 return bOk;
371}
372
373bool wxIniConfig::Write(const wxString& szKey, long lValue)
374{
375 // ltoa() is not ANSI :-(
376 wxChar szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits)
377 wxSprintf(szBuf, wxT("%ld"), lValue);
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
409 bool bOk = FALSE; // = WritePrivateProfileString(m_strGroup, NULL,
410// NULL, m_strLocalFilename) != 0;
411
412 if ( !bOk )
413 wxLogLastError(wxT("WritePrivateProfileString"));
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
424 bool bOk = FALSE; // = WritePrivateProfileString(path.Name(), NULL,
425 // NULL, m_strLocalFilename) != 0;
426
427 if ( !bOk )
428 wxLogLastError(wxT("WritePrivateProfileString"));
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
443 wxChar szBuf[MAX_PATH];
444 size_t nRc = 0; // = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
445 if ( nRc == 0 )
446 {
447 wxLogLastError(wxT("GetWindowsDirectory"));
448 }
449 else if ( nRc > WXSIZEOF(szBuf) )
450 {
451 wxFAIL_MSG(_("buffer is too small for Windows directory."));
452 }
453
454 wxString strFile = szBuf;
455 strFile << wxT('\\') << m_strLocalFilename;
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
465bool wxIniConfig::RenameEntry(const wxString& WXUNUSED(oldName), const wxString& WXUNUSED(newName))
466{
467 // Not implemented
468 return false;
469}
470
471bool wxIniConfig::RenameGroup(const wxString& WXUNUSED(oldName), const wxString& WXUNUSED(newName))
472{
473 // Not implemented
474 return false;
475}
476
477#endif //wxUSE_CONFIG