]>
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 licence
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>
25 #include <wx/config.h>
28 #include <wx/os2/iniconf.h>
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // we replace all path separators with this character
38 #define PATH_SEP_REPLACE '_'
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxIniConfig::wxIniConfig(const wxString
& strAppName
,
49 const wxString
& strVendor
,
50 const wxString
& localFilename
,
51 const wxString
& globalFilename
,
53 : wxConfigBase(!strAppName
&& wxTheApp
? wxTheApp
->GetAppName()
55 !strVendor
? (wxTheApp
? wxTheApp
->GetVendorName()
58 localFilename
, globalFilename
, style
)
60 m_strLocalFilename
= localFilename
;
61 if (m_strLocalFilename
.empty())
63 m_strLocalFilename
= GetAppName() + wxT(".ini");
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
)
71 m_strLocalFilename
<< wxT(".ini");
78 wxIniConfig::~wxIniConfig()
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 void wxIniConfig::SetPath(const wxString
& strPath
)
90 if ( strPath
.empty() )
94 else if ( strPath
[(size_t) 0] == wxCONFIG_PATH_SEPARATOR
)
97 wxSplitPath(aParts
, strPath
);
101 // relative path, combine with current one
102 wxString strFullPath
= GetPath();
103 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
104 wxSplitPath(aParts
, strFullPath
);
107 size_t nPartsCount
= aParts
.Count();
109 if ( nPartsCount
== 0 )
112 m_strGroup
= (wxChar
*)PATH_SEP_REPLACE
;
117 m_strGroup
= aParts
[(size_t) 0];
118 for ( size_t nPart
= 1; nPart
< nPartsCount
; nPart
++ )
121 m_strPath
<< PATH_SEP_REPLACE
;
122 m_strPath
<< aParts
[nPart
];
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
) );
133 const wxString
& wxIniConfig::GetPath() const
135 static wxString s_str
;
137 // always return abs path
138 s_str
= wxCONFIG_PATH_SEPARATOR
;
140 if ( m_strGroup
== wxString((wxChar
)PATH_SEP_REPLACE
) )
142 // we're at the root level, nothing to do
147 if ( !m_strPath
.empty() )
148 s_str
<< wxCONFIG_PATH_SEPARATOR
;
149 for ( const wxChar
*p
= m_strPath
; *p
!= '\0'; p
++ )
151 s_str
<< (*p
== PATH_SEP_REPLACE
? wxCONFIG_PATH_SEPARATOR
: *p
);
158 wxString
wxIniConfig::GetPrivateKeyName(const wxString
& szKey
) const
162 if ( !m_strPath
.empty() )
163 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
170 wxString
wxIniConfig::GetKeyName(const wxString
& szKey
) const
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
;
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
189 bool wxIniConfig::GetFirstGroup(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
191 wxFAIL_MSG(wxT("not implemeted"));
196 bool wxIniConfig::GetNextGroup(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
198 wxFAIL_MSG(wxT("not implemeted"));
203 bool wxIniConfig::GetFirstEntry(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
205 wxFAIL_MSG(wxT("not implemeted"));
210 bool wxIniConfig::GetNextEntry(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
212 wxFAIL_MSG(wxT("not implemeted"));
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
222 size_t wxIniConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const
224 wxFAIL_MSG(wxT("not implemeted"));
229 size_t wxIniConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const
231 wxFAIL_MSG(wxT("not implemeted"));
236 bool wxIniConfig::HasGroup(const wxString
& WXUNUSED(strName
)) const
238 wxFAIL_MSG(wxT("not implemeted"));
243 bool wxIniConfig::HasEntry(const wxString
& WXUNUSED(strName
)) const
245 wxFAIL_MSG(wxT("not implemeted"));
250 // is current group empty?
251 bool wxIniConfig::IsEmpty() const
255 // GetPrivateProfileString(m_strGroup, NULL, "",
256 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
257 if ( !::IsEmpty(szBuf
) )
260 // GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
261 // if ( !::IsEmpty(szBuf) )
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
) const
273 wxConfigPathChanger
path(this, szKey
);
274 wxString strKey
= GetPrivateKeyName(path
.Name());
276 wxChar szBuf
[1024]; // @@ should dynamically allocate memory...
278 // first look in the private INI file
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
) )
285 // now look in win.ini
286 wxString strKey
= GetKeyName(path
.Name());
287 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
290 if ( ::IsEmpty((PSZ
)szBuf
) )
299 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
,
300 const wxString
& szDefault
) const
302 wxConfigPathChanger
path(this, szKey
);
303 wxString strKey
= GetPrivateKeyName(path
.Name());
305 wxChar szBuf
[1024]; // @@ should dynamically allocate memory...
307 // first look in the private INI file
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));
318 if ( ::IsEmpty((PSZ
)szBuf
) ) {
328 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const
330 wxConfigPathChanger
path(this, szKey
);
331 wxString strKey
= GetPrivateKeyName(path
.Name());
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
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
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
353 // no, it was just returning the default value, so now look in win.ini
354 // *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
359 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& WXUNUSED(szValue
))
361 wxConfigPathChanger
path(this, szKey
);
362 wxString strKey
= GetPrivateKeyName(path
.Name());
364 bool bOk
= false; // = WritePrivateProfileString(m_strGroup, strKey,
365 // szValue, m_strLocalFilename) != 0;
368 wxLogLastError(wxT("WritePrivateProfileString"));
373 bool wxIniConfig::Write(const wxString
& szKey
, long lValue
)
375 // ltoa() is not ANSI :-(
376 wxChar szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
377 wxSprintf(szBuf
, wxT("%ld"), lValue
);
379 return Write(szKey
, szBuf
);
382 bool wxIniConfig::Flush(bool /* bCurrentOnly */)
384 // this is just the way it works
385 // return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 bool wxIniConfig::DeleteEntry(const wxString
& szKey
, bool bGroupIfEmptyAlso
)
395 // passing NULL as value to WritePrivateProfileString deletes the key
396 // if ( !Write(szKey, (const char *)NULL) )
398 wxConfigPathChanger
path(this, szKey
);
399 wxString strKey
= GetPrivateKeyName(path
.Name());
401 // if (WritePrivateProfileString(m_strGroup, szKey,
402 // (const char*) NULL, m_strLocalFilename) == 0)
405 if ( !bGroupIfEmptyAlso
|| !IsEmpty() )
408 // delete the current group too
409 bool bOk
= FALSE
; // = WritePrivateProfileString(m_strGroup, NULL,
410 // NULL, m_strLocalFilename) != 0;
413 wxLogLastError(wxT("WritePrivateProfileString"));
418 bool wxIniConfig::DeleteGroup(const wxString
& szKey
)
420 wxConfigPathChanger
path(this, szKey
);
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;
428 wxLogLastError(wxT("WritePrivateProfileString"));
437 bool wxIniConfig::DeleteAll()
439 // first delete our group in win.ini
440 // WriteProfileString(GetVendorName(), NULL, NULL);
442 // then delete our own ini file
443 wxChar szBuf
[MAX_PATH
];
444 size_t nRc
= 0; // = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
447 wxLogLastError(wxT("GetWindowsDirectory"));
449 else if ( nRc
> WXSIZEOF(szBuf
) )
451 wxFAIL_MSG(_("buffer is too small for Windows directory."));
454 wxString strFile
= szBuf
;
455 strFile
<< wxT('\\') << m_strLocalFilename
;
457 if ( !wxRemoveFile(strFile
) ) {
458 wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str());
465 bool wxIniConfig::RenameEntry(const wxString
& WXUNUSED(oldName
), const wxString
& WXUNUSED(newName
))
471 bool wxIniConfig::RenameGroup(const wxString
& WXUNUSED(oldName
), const wxString
& WXUNUSED(newName
))
477 #endif //wxUSE_CONFIG