]>
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"
27 #include "wx/config.h"
29 #include "wx/os2/iniconf.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // we replace all path separators with this character
39 #define PATH_SEP_REPLACE '_'
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 wxIniConfig::wxIniConfig(const wxString
& strAppName
,
50 const wxString
& strVendor
,
51 const wxString
& localFilename
,
52 const wxString
& globalFilename
,
54 : wxConfigBase(!strAppName
&& wxTheApp
? wxTheApp
->GetAppName()
56 !strVendor
? (wxTheApp
? wxTheApp
->GetVendorName()
59 localFilename
, globalFilename
, style
)
61 m_strLocalFilename
= localFilename
;
62 if (m_strLocalFilename
.empty())
64 m_strLocalFilename
= GetAppName() + wxT(".ini");
67 // append the extension if none given and it's not an absolute file name
68 // (otherwise we assume that they know what they're doing)
69 if ( !wxIsPathSeparator(m_strLocalFilename
[(size_t) 0]) &&
70 m_strLocalFilename
.Find('.') == wxNOT_FOUND
)
72 m_strLocalFilename
<< wxT(".ini");
76 SetPath(wxEmptyString
);
79 wxIniConfig::~wxIniConfig()
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 void wxIniConfig::SetPath(const wxString
& strPath
)
91 if ( strPath
.empty() )
95 else if ( strPath
[(size_t) 0] == wxCONFIG_PATH_SEPARATOR
)
98 wxSplitPath(aParts
, strPath
);
102 // relative path, combine with current one
103 wxString strFullPath
= GetPath();
104 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
105 wxSplitPath(aParts
, strFullPath
);
108 size_t nPartsCount
= aParts
.Count();
110 if ( nPartsCount
== 0 )
113 m_strGroup
= (wxChar
*)PATH_SEP_REPLACE
;
118 m_strGroup
= aParts
[(size_t) 0];
119 for ( size_t nPart
= 1; nPart
< nPartsCount
; nPart
++ )
122 m_strPath
<< PATH_SEP_REPLACE
;
123 m_strPath
<< aParts
[nPart
];
127 // other functions assume that all this is true, i.e. there are no trailing
128 // underscores at the end except if the group is the root one
129 wxASSERT( (m_strPath
.empty() || m_strPath
.Last() != PATH_SEP_REPLACE
) &&
130 (m_strGroup
== wxString((wxChar
)PATH_SEP_REPLACE
) ||
131 m_strGroup
.Last() != PATH_SEP_REPLACE
) );
134 const wxString
& wxIniConfig::GetPath() const
136 static wxString s_str
;
138 // always return abs path
139 s_str
= wxCONFIG_PATH_SEPARATOR
;
141 if ( m_strGroup
== wxString((wxChar
)PATH_SEP_REPLACE
) )
143 // we're at the root level, nothing to do
148 if ( !m_strPath
.empty() )
149 s_str
<< wxCONFIG_PATH_SEPARATOR
;
150 for ( const wxChar
*p
= m_strPath
; *p
!= '\0'; p
++ )
152 s_str
<< (*p
== PATH_SEP_REPLACE
? wxCONFIG_PATH_SEPARATOR
: *p
);
159 wxString
wxIniConfig::GetPrivateKeyName(const wxString
& szKey
) const
163 if ( !m_strPath
.empty() )
164 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
171 wxString
wxIniConfig::GetKeyName(const wxString
& szKey
) const
175 if (m_strGroup
!= wxString((wxChar
)PATH_SEP_REPLACE
))
176 strKey
<< m_strGroup
<< PATH_SEP_REPLACE
;
177 if ( !m_strPath
.empty() )
178 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 bool wxIniConfig::GetFirstGroup(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
192 wxFAIL_MSG(wxT("not implemeted"));
197 bool wxIniConfig::GetNextGroup(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
199 wxFAIL_MSG(wxT("not implemeted"));
204 bool wxIniConfig::GetFirstEntry(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
206 wxFAIL_MSG(wxT("not implemeted"));
211 bool wxIniConfig::GetNextEntry(wxString
& WXUNUSED(str
), long& WXUNUSED(lIndex
)) const
213 wxFAIL_MSG(wxT("not implemeted"));
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
223 size_t wxIniConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const
225 wxFAIL_MSG(wxT("not implemeted"));
230 size_t wxIniConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const
232 wxFAIL_MSG(wxT("not implemeted"));
237 bool wxIniConfig::HasGroup(const wxString
& WXUNUSED(strName
)) const
239 wxFAIL_MSG(wxT("not implemeted"));
244 bool wxIniConfig::HasEntry(const wxString
& WXUNUSED(strName
)) const
246 wxFAIL_MSG(wxT("not implemeted"));
251 // is current group empty?
252 bool wxIniConfig::IsEmpty() const
256 // GetPrivateProfileString(m_strGroup, NULL, "",
257 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
258 if ( !::wxIsEmpty(szBuf
) )
261 // GetProfileString(m_strGroup, NULL, "", szBuf, WXSIZEOF(szBuf));
262 // if ( !::wxIsEmpty(szBuf) )
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
) const
274 wxConfigPathChanger
path(this, szKey
);
275 wxString strKey
= GetPrivateKeyName(path
.Name());
277 wxChar szBuf
[1024]; // @@ should dynamically allocate memory...
279 // first look in the private INI file
281 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
282 // GetPrivateProfileString(m_strGroup, strKey, "",
283 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
284 if ( ::wxIsEmpty((PSZ
)szBuf
) )
286 // now look in win.ini
287 wxString strKey
= GetKeyName(path
.Name());
288 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
291 if ( ::wxIsEmpty((PSZ
)szBuf
) )
300 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
,
301 const wxString
& szDefault
) const
303 wxConfigPathChanger
path(this, szKey
);
304 wxString strKey
= GetPrivateKeyName(path
.Name());
306 wxChar szBuf
[1024]; // @@ should dynamically allocate memory...
308 // first look in the private INI file
310 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
311 // GetPrivateProfileString(m_strGroup, strKey, "",
312 // szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
313 if ( ::wxIsEmpty((PSZ
)szBuf
) )
315 // now look in win.ini
316 wxString strKey
= GetKeyName(path
.Name());
317 // GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
320 if ( ::wxIsEmpty((PSZ
)szBuf
) )
332 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const
334 wxConfigPathChanger
path(this, szKey
);
335 wxString strKey
= GetPrivateKeyName(path
.Name());
337 // hack: we have no mean to know if it really found the default value or
338 // didn't find anything, so we call it twice
340 static const int nMagic
= 17; // 17 is some "rare" number
341 static const int nMagic2
= 28; // arbitrary number != nMagic
342 long lVal
= 0; // = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
344 if ( lVal
!= nMagic
)
346 // the value was read from the file
351 // is it really nMagic?
352 // lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
353 if ( lVal
== nMagic2
)
355 // the nMagic it returned was indeed read from the file
360 // no, it was just returning the default value, so now look in win.ini
361 // *pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
366 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& WXUNUSED(szValue
))
368 wxConfigPathChanger
path(this, szKey
);
369 wxString strKey
= GetPrivateKeyName(path
.Name());
371 bool bOk
= false; // = WritePrivateProfileString(m_strGroup, strKey,
372 // szValue, m_strLocalFilename) != 0;
376 wxLogLastError(wxT("WritePrivateProfileString"));
382 bool wxIniConfig::Write(const wxString
& szKey
, long lValue
)
384 // ltoa() is not ANSI :-(
385 wxChar szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
386 wxSprintf(szBuf
, wxT("%ld"), lValue
);
388 return Write(szKey
, szBuf
);
391 bool wxIniConfig::Flush(bool /* bCurrentOnly */)
393 // this is just the way it works
394 // return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 bool wxIniConfig::DeleteEntry(const wxString
& szKey
, bool bGroupIfEmptyAlso
)
404 // passing NULL as value to WritePrivateProfileString deletes the key
405 // if ( !Write(szKey, (const char *)NULL) )
407 wxConfigPathChanger
path(this, szKey
);
408 wxString strKey
= GetPrivateKeyName(path
.Name());
410 // if (WritePrivateProfileString(m_strGroup, szKey,
411 // (const char*) NULL, m_strLocalFilename) == 0)
414 if ( !bGroupIfEmptyAlso
|| !IsEmpty() )
417 // delete the current group too
418 bool bOk
= false; // = WritePrivateProfileString(m_strGroup, NULL,
419 // NULL, m_strLocalFilename) != 0;
423 wxLogLastError(wxT("WritePrivateProfileString"));
429 bool wxIniConfig::DeleteGroup(const wxString
& szKey
)
431 wxConfigPathChanger
path(this, szKey
);
433 // passing NULL as section name to WritePrivateProfileString deletes the
434 // whole section according to the docs
435 bool bOk
= false; // = WritePrivateProfileString(path.Name(), NULL,
436 // NULL, m_strLocalFilename) != 0;
440 wxLogLastError(wxT("WritePrivateProfileString"));
450 bool wxIniConfig::DeleteAll()
452 // first delete our group in win.ini
453 // WriteProfileString(GetVendorName(), NULL, NULL);
455 // then delete our own ini file
456 wxChar szBuf
[MAX_PATH
];
457 size_t nRc
= 0; // = GetWindowsDirectory(szBuf, WXSIZEOF(szBuf));
460 wxLogLastError(wxT("GetWindowsDirectory"));
462 else if ( nRc
> WXSIZEOF(szBuf
) )
464 wxFAIL_MSG(_("buffer is too small for Windows directory."));
467 wxString strFile
= szBuf
;
468 strFile
<< wxT('\\') << m_strLocalFilename
;
470 if ( !wxRemoveFile(strFile
) )
472 wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str());
479 bool wxIniConfig::RenameEntry(const wxString
& WXUNUSED(oldName
), const wxString
& WXUNUSED(newName
))
485 bool wxIniConfig::RenameGroup(const wxString
& WXUNUSED(oldName
), const wxString
& WXUNUSED(newName
))
491 #endif //wxUSE_CONFIG