]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/iniconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/iniconf.cpp
3 // Purpose: implementation of wxIniConfig class
4 // Author: David Webster
7 // Copyright: David Webster
8 // Licence: wxWindows license
9 ///////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
15 #include <wx/string.h>
22 #include <wx/dynarray.h>
24 #include <wx/config.h>
26 #include <wx/os2/iniconf.h>
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // we replace all path separators with this character
36 #define PATH_SEP_REPLACE '_'
38 // ============================================================================
40 // ============================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 wxIniConfig::wxIniConfig(const wxString
& strAppName
,
47 const wxString
& strVendor
,
48 const wxString
& localFilename
,
49 const wxString
& globalFilename
,
51 : wxConfigBase(!strAppName
&& wxTheApp
? wxTheApp
->GetAppName()
53 !strVendor
? (wxTheApp
? wxTheApp
->GetVendorName()
56 localFilename
, globalFilename
, style
)
58 m_strLocalFilename
= localFilename
;
59 if (m_strLocalFilename
.IsEmpty())
61 m_strLocalFilename
= GetAppName() + ".ini";
64 // append the extension if none given and it's not an absolute file name
65 // (otherwise we assume that they know what they're doing)
66 if ( !wxIsPathSeparator(m_strLocalFilename
[0u]) &&
67 m_strLocalFilename
.Find('.') == wxNOT_FOUND
)
69 m_strLocalFilename
<< ".ini";
76 wxIniConfig::~wxIniConfig()
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 void wxIniConfig::SetPath(const wxString
& strPath
)
88 if ( strPath
.IsEmpty() ) {
91 else if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
) {
93 wxSplitPath(aParts
, strPath
);
96 // relative path, combine with current one
97 wxString strFullPath
= GetPath();
98 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
99 wxSplitPath(aParts
, strFullPath
);
102 size_t nPartsCount
= aParts
.Count();
104 if ( nPartsCount
== 0 ) {
106 m_strGroup
= PATH_SEP_REPLACE
;
110 m_strGroup
= aParts
[0u];
111 for ( size_t nPart
= 1; nPart
< nPartsCount
; nPart
++ ) {
113 m_strPath
<< PATH_SEP_REPLACE
;
114 m_strPath
<< aParts
[nPart
];
118 // other functions assume that all this is true, i.e. there are no trailing
119 // underscores at the end except if the group is the root one
120 wxASSERT( (m_strPath
.IsEmpty() || m_strPath
.Last() != PATH_SEP_REPLACE
) &&
121 (m_strGroup
== wxString(PATH_SEP_REPLACE
) ||
122 m_strGroup
.Last() != PATH_SEP_REPLACE
) );
125 const wxString
& wxIniConfig::GetPath() const
127 static wxString s_str
;
129 // always return abs path
130 s_str
= wxCONFIG_PATH_SEPARATOR
;
132 if ( m_strGroup
== wxString(PATH_SEP_REPLACE
) ) {
133 // we're at the root level, nothing to do
137 if ( !m_strPath
.IsEmpty() )
138 s_str
<< wxCONFIG_PATH_SEPARATOR
;
139 for ( const char *p
= m_strPath
; *p
!= '\0'; p
++ ) {
140 s_str
<< (*p
== PATH_SEP_REPLACE
? wxCONFIG_PATH_SEPARATOR
: *p
);
147 wxString
wxIniConfig::GetPrivateKeyName(const wxString
& szKey
) const
151 if ( !m_strPath
.IsEmpty() )
152 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
159 wxString
wxIniConfig::GetKeyName(const wxString
& szKey
) const
163 if ( m_strGroup
!= wxString(PATH_SEP_REPLACE
) )
164 strKey
<< m_strGroup
<< PATH_SEP_REPLACE
;
165 if ( !m_strPath
.IsEmpty() )
166 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
178 bool wxIniConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
180 wxFAIL_MSG("not implemented");
185 bool wxIniConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
187 wxFAIL_MSG("not implemented");
192 bool wxIniConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
194 wxFAIL_MSG("not implemented");
199 bool wxIniConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
201 wxFAIL_MSG("not implemented");
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
211 size_t wxIniConfig::GetNumberOfEntries(bool bRecursive
) const
213 wxFAIL_MSG("not implemented");
218 size_t wxIniConfig::GetNumberOfGroups(bool bRecursive
) const
220 wxFAIL_MSG("not implemented");
225 bool wxIniConfig::HasGroup(const wxString
& strName
) const
227 wxFAIL_MSG("not implemented");
232 bool wxIniConfig::HasEntry(const wxString
& strName
) const
234 wxFAIL_MSG("not implemented");
239 // is current group empty?
240 bool wxIniConfig::IsEmpty() const
244 // GetPrivateProfileString(m_strGroup, NULL, "",
245 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
246 if ( !::IsEmpty(szBuf
) )
249 // GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
250 // if ( !::IsEmpty(szBuf) )
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
) const
262 wxConfigPathChanger
path(this, szKey
);
263 wxString strKey
= GetPrivateKeyName(path
.Name());
265 char szBuf
[1024]; // @@ should dynamically allocate memory...
267 // first look in the private INI file
269 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
270 // GetPrivateProfileString(m_strGroup, strKey, "",
271 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
272 if ( ::IsEmpty(szBuf
) ) {
273 // now look in win.ini
274 wxString strKey
= GetKeyName(path
.Name());
275 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
278 if ( ::IsEmpty(szBuf
) ) {
287 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
,
288 const wxString
& szDefault
) const
290 wxConfigPathChanger
path(this, szKey
);
291 wxString strKey
= GetPrivateKeyName(path
.Name());
293 char szBuf
[1024]; // @@ should dynamically allocate memory...
295 // first look in the private INI file
297 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
298 // GetPrivateProfileString(m_strGroup, strKey, "",
299 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
300 if ( ::IsEmpty(szBuf
) ) {
301 // now look in win.ini
302 wxString strKey
= GetKeyName(path
.Name());
303 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
306 if ( ::IsEmpty(szBuf
) ) {
316 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const
318 wxConfigPathChanger
path(this, szKey
);
319 wxString strKey
= GetPrivateKeyName(path
.Name());
321 // hack: we have no mean to know if it really found the default value or
322 // didn't find anything, so we call it twice
324 static const int nMagic
= 17; // 17 is some "rare" number
325 static const int nMagic2
= 28; // arbitrary number != nMagic
326 long lVal
= 0; // = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
327 if ( lVal
!= nMagic
) {
328 // the value was read from the file
333 // is it really nMagic?
334 // lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
335 if ( lVal
== nMagic2
) {
336 // the nMagic it returned was indeed read from the file
341 // no, it was just returning the default value, so now look in win.ini
342 // *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
347 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& szValue
)
349 wxConfigPathChanger
path(this, szKey
);
350 wxString strKey
= GetPrivateKeyName(path
.Name());
352 bool bOk
= FALSE
; // = WritePrivateProfileString(m_strGroup, strKey,
353 // szValue, m_strLocalFilename) != 0;
356 wxLogLastError("WritePrivateProfileString");
361 bool wxIniConfig::Write(const wxString
& szKey
, long lValue
)
363 // ltoa() is not ANSI :-(
364 char szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
365 sprintf(szBuf
, "%ld", lValue
);
367 return Write(szKey
, szBuf
);
370 bool wxIniConfig::Flush(bool /* bCurrentOnly */)
372 // this is just the way it works
373 // return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 bool wxIniConfig::DeleteEntry(const wxString
& szKey
, bool bGroupIfEmptyAlso
)
383 // passing NULL as value to WritePrivateProfileString deletes the key
384 // if ( !Write(szKey, (const char *)NULL) )
386 wxConfigPathChanger
path(this, szKey
);
387 wxString strKey
= GetPrivateKeyName(path
.Name());
389 // if (WritePrivateProfileString(m_strGroup, szKey,
390 // (const char*) NULL, m_strLocalFilename) == 0)
393 if ( !bGroupIfEmptyAlso
|| !IsEmpty() )
396 // delete the current group too
397 bool bOk
= FALSE
; // = WritePrivateProfileString(m_strGroup, NULL,
398 // NULL, m_strLocalFilename) != 0;
401 wxLogLastError("WritePrivateProfileString");
406 bool wxIniConfig::DeleteGroup(const wxString
& szKey
)
408 wxConfigPathChanger
path(this, szKey
);
410 // passing NULL as section name to WritePrivateProfileString deletes the
411 // whole section according to the docs
412 bool bOk
= FALSE
; // = WritePrivateProfileString(path.Name(), NULL,
413 // NULL, m_strLocalFilename) != 0;
416 wxLogLastError("WritePrivateProfileString");
425 bool wxIniConfig::DeleteAll()
427 // first delete our group in win.ini
428 // WriteProfileString(GetVendorName(), NULL, NULL);
430 // then delete our own ini file
431 char szBuf
[MAX_PATH
];
432 size_t nRc
= 0; // = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
435 wxLogLastError("GetWindowsDirectory");
437 else if ( nRc
> WXSIZEOF(szBuf
) )
439 wxFAIL_MSG("buffer is too small for Windows directory.");
442 wxString strFile
= szBuf
;
443 strFile
<< '\\' << m_strLocalFilename
;
445 if ( !wxRemoveFile(strFile
) ) {
446 wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str());
453 bool wxIniConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
459 bool wxIniConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)