]>
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"
17 #include "wx/dynarray.h"
18 #include "wx/string.h"
26 #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");
75 SetPath(wxEmptyString
);
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 ( !::wxIsEmpty(szBuf
) )
260 // GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
261 // if ( !::wxIsEmpty(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 ( ::wxIsEmpty((PSZ
)szBuf
) )
285 // now look in win.ini
286 wxString strKey
= GetKeyName(path
.Name());
287 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
290 if ( ::wxIsEmpty((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 ( ::wxIsEmpty((PSZ
)szBuf
) )
314 // now look in win.ini
315 wxString strKey
= GetKeyName(path
.Name());
316 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
319 if ( ::wxIsEmpty((PSZ
)szBuf
) )
331 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const
333 wxConfigPathChanger
path(this, szKey
);
334 wxString strKey
= GetPrivateKeyName(path
.Name());
336 // hack: we have no mean to know if it really found the default value or
337 // didn't find anything, so we call it twice
339 static const int nMagic
= 17; // 17 is some "rare" number
340 static const int nMagic2
= 28; // arbitrary number != nMagic
341 long lVal
= 0; // = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
343 if ( lVal
!= nMagic
)
345 // the value was read from the file
350 // is it really nMagic?
351 // lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
352 if ( lVal
== nMagic2
)
354 // the nMagic it returned was indeed read from the file
359 // no, it was just returning the default value, so now look in win.ini
360 // *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
365 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& WXUNUSED(szValue
))
367 wxConfigPathChanger
path(this, szKey
);
368 wxString strKey
= GetPrivateKeyName(path
.Name());
370 bool bOk
= false; // = WritePrivateProfileString(m_strGroup, strKey,
371 // szValue, m_strLocalFilename) != 0;
375 wxLogLastError(wxT("WritePrivateProfileString"));
381 bool wxIniConfig::Write(const wxString
& szKey
, long lValue
)
383 // ltoa() is not ANSI :-(
384 wxChar szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
385 wxSprintf(szBuf
, wxT("%ld"), lValue
);
387 return Write(szKey
, szBuf
);
390 bool wxIniConfig::Flush(bool /* bCurrentOnly */)
392 // this is just the way it works
393 // return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
397 // ----------------------------------------------------------------------------
399 // ----------------------------------------------------------------------------
401 bool wxIniConfig::DeleteEntry(const wxString
& szKey
, bool bGroupIfEmptyAlso
)
403 // passing NULL as value to WritePrivateProfileString deletes the key
404 // if ( !Write(szKey, (const char *)NULL) )
406 wxConfigPathChanger
path(this, szKey
);
407 wxString strKey
= GetPrivateKeyName(path
.Name());
409 // if (WritePrivateProfileString(m_strGroup, szKey,
410 // (const char*) NULL, m_strLocalFilename) == 0)
413 if ( !bGroupIfEmptyAlso
|| !IsEmpty() )
416 // delete the current group too
417 bool bOk
= false; // = WritePrivateProfileString(m_strGroup, NULL,
418 // NULL, m_strLocalFilename) != 0;
422 wxLogLastError(wxT("WritePrivateProfileString"));
428 bool wxIniConfig::DeleteGroup(const wxString
& szKey
)
430 wxConfigPathChanger
path(this, szKey
);
432 // passing NULL as section name to WritePrivateProfileString deletes the
433 // whole section according to the docs
434 bool bOk
= false; // = WritePrivateProfileString(path.Name(), NULL,
435 // NULL, m_strLocalFilename) != 0;
439 wxLogLastError(wxT("WritePrivateProfileString"));
449 bool wxIniConfig::DeleteAll()
451 // first delete our group in win.ini
452 // WriteProfileString(GetVendorName(), NULL, NULL);
454 // then delete our own ini file
455 wxChar szBuf
[MAX_PATH
];
456 size_t nRc
= 0; // = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
459 wxLogLastError(wxT("GetWindowsDirectory"));
461 else if ( nRc
> WXSIZEOF(szBuf
) )
463 wxFAIL_MSG(_("buffer is too small for Windows directory."));
466 wxString strFile
= szBuf
;
467 strFile
<< wxT('\\') << m_strLocalFilename
;
469 if ( !wxRemoveFile(strFile
) )
471 wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str());
478 bool wxIniConfig::RenameEntry(const wxString
& WXUNUSED(oldName
), const wxString
& WXUNUSED(newName
))
484 bool wxIniConfig::RenameGroup(const wxString
& WXUNUSED(oldName
), const wxString
& WXUNUSED(newName
))
490 #endif //wxUSE_CONFIG