]>
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
8 // Copyright: David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
18 #include "wx/dynarray.h"
19 #include "wx/string.h"
28 #include "wx/config.h"
30 #include "wx/os2/iniconf.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // we replace all path separators with this character
40 #define PATH_SEP_REPLACE '_'
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 wxIniConfig::wxIniConfig(const wxString
& strAppName
,
51 const wxString
& strVendor
,
52 const wxString
& localFilename
,
53 const wxString
& globalFilename
,
55 : wxConfigBase(!strAppName
&& wxTheApp
? wxTheApp
->GetAppName()
57 !strVendor
? (wxTheApp
? wxTheApp
->GetVendorName()
60 localFilename
, globalFilename
, style
)
62 m_strLocalFilename
= localFilename
;
63 if (m_strLocalFilename
.empty())
65 m_strLocalFilename
= GetAppName() + wxT(".ini");
68 // append the extension if none given and it's not an absolute file name
69 // (otherwise we assume that they know what they're doing)
70 if ( !wxIsPathSeparator(m_strLocalFilename
[(size_t) 0]) &&
71 m_strLocalFilename
.Find('.') == wxNOT_FOUND
)
73 m_strLocalFilename
<< wxT(".ini");
77 SetPath(wxEmptyString
);
80 wxIniConfig::~wxIniConfig()
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 void wxIniConfig::SetPath(const wxString
& strPath
)
92 if ( strPath
.empty() )
96 else if ( strPath
[(size_t) 0] == wxCONFIG_PATH_SEPARATOR
)
99 wxSplitPath(aParts
, strPath
);
103 // relative path, combine with current one
104 wxString strFullPath
= GetPath();
105 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
106 wxSplitPath(aParts
, strFullPath
);
109 size_t nPartsCount
= aParts
.Count();
111 if ( nPartsCount
== 0 )
114 m_strGroup
= (wxChar
*)PATH_SEP_REPLACE
;
119 m_strGroup
= aParts
[(size_t) 0];
120 for ( size_t nPart
= 1; nPart
< nPartsCount
; nPart
++ )
123 m_strPath
<< PATH_SEP_REPLACE
;
124 m_strPath
<< aParts
[nPart
];
128 // other functions assume that all this is true, i.e. there are no trailing
129 // underscores at the end except if the group is the root one
130 wxASSERT( (m_strPath
.empty() || m_strPath
.Last() != PATH_SEP_REPLACE
) &&
131 (m_strGroup
== wxString((wxChar
)PATH_SEP_REPLACE
) ||
132 m_strGroup
.Last() != PATH_SEP_REPLACE
) );
135 const wxString
& wxIniConfig::GetPath() const
137 static wxString s_str
;
139 // always return abs path
140 s_str
= wxCONFIG_PATH_SEPARATOR
;
142 if ( m_strGroup
== wxString((wxChar
)PATH_SEP_REPLACE
) )
144 // we're at the root level, nothing to do
149 if ( !m_strPath
.empty() )
150 s_str
<< wxCONFIG_PATH_SEPARATOR
;
151 for ( const wxChar
*p
= m_strPath
; *p
!= '\0'; p
++ )
153 s_str
<< (*p
== PATH_SEP_REPLACE
? wxCONFIG_PATH_SEPARATOR
: *p
);
160 wxString
wxIniConfig::GetPrivateKeyName(const wxString
& szKey
) const
164 if ( !m_strPath
.empty() )
165 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
172 wxString
wxIniConfig::GetKeyName(const wxString
& szKey
) const
176 if (m_strGroup
!= wxString((wxChar
)PATH_SEP_REPLACE
))
177 strKey
<< m_strGroup
<< PATH_SEP_REPLACE
;
178 if ( !m_strPath
.empty() )
179 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 bool wxIniConfig::GetFirstGroup(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
193 wxFAIL_MSG(wxT("not implemeted"));
198 bool wxIniConfig::GetNextGroup(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
200 wxFAIL_MSG(wxT("not implemeted"));
205 bool wxIniConfig::GetFirstEntry(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
207 wxFAIL_MSG(wxT("not implemeted"));
212 bool wxIniConfig::GetNextEntry(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
214 wxFAIL_MSG(wxT("not implemeted"));
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
224 size_t wxIniConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const
226 wxFAIL_MSG(wxT("not implemeted"));
231 size_t wxIniConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const
233 wxFAIL_MSG(wxT("not implemeted"));
238 bool wxIniConfig::HasGroup(const wxString
& WXUNUSED(strName
)) const
240 wxFAIL_MSG(wxT("not implemeted"));
245 bool wxIniConfig::HasEntry(const wxString
& WXUNUSED(strName
)) const
247 wxFAIL_MSG(wxT("not implemeted"));
252 // is current group empty?
253 bool wxIniConfig::IsEmpty() const
257 // GetPrivateProfileString(m_strGroup, NULL, "",
258 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
259 if ( !::IsEmpty(szBuf
) )
262 // GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
263 // if ( !::IsEmpty(szBuf) )
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
) const
275 wxConfigPathChanger
path(this, szKey
);
276 wxString strKey
= GetPrivateKeyName(path
.Name());
278 wxChar szBuf
[1024]; // @@ should dynamically allocate memory...
280 // first look in the private INI file
282 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
283 // GetPrivateProfileString(m_strGroup, strKey, "",
284 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
285 if ( ::IsEmpty((PSZ
)szBuf
) )
287 // now look in win.ini
288 wxString strKey
= GetKeyName(path
.Name());
289 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
292 if ( ::IsEmpty((PSZ
)szBuf
) )
301 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
,
302 const wxString
& szDefault
) const
304 wxConfigPathChanger
path(this, szKey
);
305 wxString strKey
= GetPrivateKeyName(path
.Name());
307 wxChar szBuf
[1024]; // @@ should dynamically allocate memory...
309 // first look in the private INI file
311 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
312 // GetPrivateProfileString(m_strGroup, strKey, "",
313 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
314 if ( ::IsEmpty((PSZ
)szBuf
) )
316 // now look in win.ini
317 wxString strKey
= GetKeyName(path
.Name());
318 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
321 if ( ::IsEmpty((PSZ
)szBuf
) )
333 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const
335 wxConfigPathChanger
path(this, szKey
);
336 wxString strKey
= GetPrivateKeyName(path
.Name());
338 // hack: we have no mean to know if it really found the default value or
339 // didn't find anything, so we call it twice
341 static const int nMagic
= 17; // 17 is some "rare" number
342 static const int nMagic2
= 28; // arbitrary number != nMagic
343 long lVal
= 0; // = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
345 if ( lVal
!= nMagic
)
347 // the value was read from the file
352 // is it really nMagic?
353 // lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
354 if ( lVal
== nMagic2
)
356 // the nMagic it returned was indeed read from the file
361 // no, it was just returning the default value, so now look in win.ini
362 // *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
367 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& WXUNUSED(szValue
))
369 wxConfigPathChanger
path(this, szKey
);
370 wxString strKey
= GetPrivateKeyName(path
.Name());
372 bool bOk
= false; // = WritePrivateProfileString(m_strGroup, strKey,
373 // szValue, m_strLocalFilename) != 0;
376 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;
421 wxLogLastError(wxT("WritePrivateProfileString"));
426 bool wxIniConfig::DeleteGroup(const wxString
& szKey
)
428 wxConfigPathChanger
path(this, szKey
);
430 // passing NULL as section name to WritePrivateProfileString deletes the
431 // whole section according to the docs
432 bool bOk
= false; // = WritePrivateProfileString(path.Name(), NULL,
433 // NULL, m_strLocalFilename) != 0;
436 wxLogLastError(wxT("WritePrivateProfileString"));
445 bool wxIniConfig::DeleteAll()
447 // first delete our group in win.ini
448 // WriteProfileString(GetVendorName(), NULL, NULL);
450 // then delete our own ini file
451 wxChar szBuf
[MAX_PATH
];
452 size_t nRc
= 0; // = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
455 wxLogLastError(wxT("GetWindowsDirectory"));
457 else if ( nRc
> WXSIZEOF(szBuf
) )
459 wxFAIL_MSG(_("buffer is too small for Windows directory."));
462 wxString strFile
= szBuf
;
463 strFile
<< wxT('\\') << m_strLocalFilename
;
465 if ( !wxRemoveFile(strFile
) )
467 wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str());
474 bool wxIniConfig::RenameEntry(const wxString
& WXUNUSED(oldName
), const wxString
& WXUNUSED(newName
))
480 bool wxIniConfig::RenameGroup(const wxString
& WXUNUSED(oldName
), const wxString
& WXUNUSED(newName
))
486 #endif //wxUSE_CONFIG