]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/iniconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/iniconf.cpp
3 // Purpose: implementation of wxIniConfig class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "iniconf.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/string.h"
31 // Doesn't yet compile in Unicode mode
33 #if wxUSE_CONFIG && !wxUSE_UNICODE
35 #include "wx/dynarray.h"
37 #include "wx/config.h"
39 #include "wx/msw/iniconf.h"
41 // _WINDOWS_ is defined when windows.h is included,
42 // __WXMSW__ is defined for MS Windows compilation
43 #if defined(__WXMSW__) && !defined(_WINDOWS_)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // we replace all path separators with this character
52 #define PATH_SEP_REPLACE '_'
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 wxIniConfig::wxIniConfig(const wxString
& strAppName
,
63 const wxString
& strVendor
,
64 const wxString
& localFilename
,
65 const wxString
& globalFilename
,
67 : wxConfigBase(strAppName
, strVendor
, localFilename
, globalFilename
, style
)
69 #if 0 // This is too complex for some compilers, e.g. BC++ 5.01
70 : wxConfigBase((strAppName
.IsEmpty() && wxTheApp
) ? wxTheApp
->GetAppName()
72 strVendor
.IsEmpty() ? (wxTheApp
? wxTheApp
->GetVendorName()
75 localFilename
, globalFilename
, style
)
78 if (strAppName
.IsEmpty() && wxTheApp
)
79 SetAppName(wxTheApp
->GetAppName());
80 if (strVendor
.IsEmpty() && wxTheApp
)
81 SetVendorName(wxTheApp
->GetVendorName());
83 m_strLocalFilename
= localFilename
;
84 if (m_strLocalFilename
.IsEmpty())
86 m_strLocalFilename
= GetAppName() + wxT(".ini");
89 // append the extension if none given and it's not an absolute file name
90 // (otherwise we assume that they know what they're doing)
91 if ( !wxIsPathSeparator(m_strLocalFilename
[0u]) &&
92 m_strLocalFilename
.Find(wxT('.')) == wxNOT_FOUND
)
94 m_strLocalFilename
<< wxT(".ini");
101 wxIniConfig::~wxIniConfig()
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 void wxIniConfig::SetPath(const wxString
& strPath
)
111 wxArrayString aParts
;
113 if ( strPath
.IsEmpty() ) {
116 else if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
) {
118 wxSplitPath(aParts
, strPath
);
121 // relative path, combine with current one
122 wxString strFullPath
= GetPath();
123 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
124 wxSplitPath(aParts
, strFullPath
);
127 size_t nPartsCount
= aParts
.Count();
129 if ( nPartsCount
== 0 ) {
131 m_strGroup
= PATH_SEP_REPLACE
;
135 m_strGroup
= aParts
[0u];
136 for ( size_t nPart
= 1; nPart
< nPartsCount
; nPart
++ ) {
138 m_strPath
<< PATH_SEP_REPLACE
;
139 m_strPath
<< aParts
[nPart
];
143 // other functions assume that all this is true, i.e. there are no trailing
144 // underscores at the end except if the group is the root one
145 wxASSERT( (m_strPath
.IsEmpty() || m_strPath
.Last() != PATH_SEP_REPLACE
) &&
146 (m_strGroup
== wxString(PATH_SEP_REPLACE
) ||
147 m_strGroup
.Last() != PATH_SEP_REPLACE
) );
150 const wxString
& wxIniConfig::GetPath() const
152 static wxString s_str
;
154 // always return abs path
155 s_str
= wxCONFIG_PATH_SEPARATOR
;
157 if ( m_strGroup
== wxString(PATH_SEP_REPLACE
) ) {
158 // we're at the root level, nothing to do
162 if ( !m_strPath
.IsEmpty() )
163 s_str
<< wxCONFIG_PATH_SEPARATOR
;
164 for ( const char *p
= m_strPath
; *p
!= '\0'; p
++ ) {
165 s_str
<< (*p
== PATH_SEP_REPLACE
? wxCONFIG_PATH_SEPARATOR
: *p
);
172 wxString
wxIniConfig::GetPrivateKeyName(const wxString
& szKey
) const
176 if ( !m_strPath
.IsEmpty() )
177 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
184 wxString
wxIniConfig::GetKeyName(const wxString
& szKey
) const
188 if ( m_strGroup
!= wxString(PATH_SEP_REPLACE
) )
189 strKey
<< m_strGroup
<< PATH_SEP_REPLACE
;
190 if ( !m_strPath
.IsEmpty() )
191 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
203 bool wxIniConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
205 wxFAIL_MSG("not implemented");
210 bool wxIniConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
212 wxFAIL_MSG("not implemented");
217 bool wxIniConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
219 wxFAIL_MSG("not implemented");
224 bool wxIniConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
226 wxFAIL_MSG("not implemented");
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
236 size_t wxIniConfig::GetNumberOfEntries(bool bRecursive
) const
238 wxFAIL_MSG("not implemented");
243 size_t wxIniConfig::GetNumberOfGroups(bool bRecursive
) const
245 wxFAIL_MSG("not implemented");
250 bool wxIniConfig::HasGroup(const wxString
& strName
) const
252 wxFAIL_MSG("not implemented");
257 bool wxIniConfig::HasEntry(const wxString
& strName
) const
259 wxFAIL_MSG("not implemented");
264 // is current group empty?
265 bool wxIniConfig::IsEmpty() const
269 GetPrivateProfileString(m_strGroup
, NULL
, "",
270 szBuf
, WXSIZEOF(szBuf
), m_strLocalFilename
);
271 if ( !::IsEmpty(szBuf
) )
274 GetProfileString(m_strGroup
, NULL
, "", szBuf
, WXSIZEOF(szBuf
));
275 if ( !::IsEmpty(szBuf
) )
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
) const
287 wxConfigPathChanger
path(this, szKey
);
288 wxString strKey
= GetPrivateKeyName(path
.Name());
290 char szBuf
[1024]; // @@ should dynamically allocate memory...
292 // first look in the private INI file
294 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
295 GetPrivateProfileString(m_strGroup
, strKey
, "",
296 szBuf
, WXSIZEOF(szBuf
), m_strLocalFilename
);
297 if ( ::IsEmpty(szBuf
) ) {
298 // now look in win.ini
299 wxString strKey
= GetKeyName(path
.Name());
300 GetProfileString(m_strGroup
, strKey
, "", szBuf
, WXSIZEOF(szBuf
));
303 if ( ::IsEmpty(szBuf
) ) {
312 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
,
313 const wxString
& szDefault
) const
315 wxConfigPathChanger
path(this, szKey
);
316 wxString strKey
= GetPrivateKeyName(path
.Name());
318 char szBuf
[1024]; // @@ should dynamically allocate memory...
320 // first look in the private INI file
322 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
323 GetPrivateProfileString(m_strGroup
, strKey
, "",
324 szBuf
, WXSIZEOF(szBuf
), m_strLocalFilename
);
325 if ( ::IsEmpty(szBuf
) ) {
326 // now look in win.ini
327 wxString strKey
= GetKeyName(path
.Name());
328 GetProfileString(m_strGroup
, strKey
, "", szBuf
, WXSIZEOF(szBuf
));
331 if ( ::IsEmpty(szBuf
) ) {
341 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const
343 wxConfigPathChanger
path(this, szKey
);
344 wxString strKey
= GetPrivateKeyName(path
.Name());
346 // hack: we have no mean to know if it really found the default value or
347 // didn't find anything, so we call it twice
349 static const int nMagic
= 17; // 17 is some "rare" number
350 static const int nMagic2
= 28; // arbitrary number != nMagic
351 long lVal
= GetPrivateProfileInt(m_strGroup
, strKey
, nMagic
, m_strLocalFilename
);
352 if ( lVal
!= nMagic
) {
353 // the value was read from the file
358 // is it really nMagic?
359 lVal
= GetPrivateProfileInt(m_strGroup
, strKey
, nMagic2
, m_strLocalFilename
);
360 if ( lVal
!= nMagic2
) {
361 // the nMagic it returned was indeed read from the file
366 // CS : I have no idea why they should look up in win.ini
367 // and if at all they have to do the same procedure using the two magic numbers
368 // otherwise it always returns true, even if the key was not there at all
370 // no, it was just returning the default value, so now look in win.ini
371 *pl
= GetProfileInt(GetVendorName(), GetKeyName(szKey
), *pl
);
378 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& szValue
)
380 wxConfigPathChanger
path(this, szKey
);
381 wxString strKey
= GetPrivateKeyName(path
.Name());
383 bool bOk
= WritePrivateProfileString(m_strGroup
, strKey
,
384 szValue
, m_strLocalFilename
) != 0;
387 wxLogLastError(wxT("WritePrivateProfileString"));
392 bool wxIniConfig::Write(const wxString
& szKey
, long lValue
)
394 // ltoa() is not ANSI :-(
395 char szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
396 sprintf(szBuf
, "%ld", lValue
);
398 return Write(szKey
, szBuf
);
401 bool wxIniConfig::Flush(bool /* bCurrentOnly */)
403 // this is just the way it works
404 return WritePrivateProfileString(NULL
, NULL
, NULL
, m_strLocalFilename
) != 0;
407 // ----------------------------------------------------------------------------
409 // ----------------------------------------------------------------------------
411 bool wxIniConfig::DeleteEntry(const wxString
& szKey
, bool bGroupIfEmptyAlso
)
413 // passing NULL as value to WritePrivateProfileString deletes the key
414 // if ( !Write(szKey, (const char *)NULL) )
416 wxConfigPathChanger
path(this, szKey
);
417 wxString strKey
= GetPrivateKeyName(path
.Name());
419 if (WritePrivateProfileString(m_strGroup
, szKey
,
420 (const char*) NULL
, m_strLocalFilename
) == 0)
423 if ( !bGroupIfEmptyAlso
|| !IsEmpty() )
426 // delete the current group too
427 bool bOk
= WritePrivateProfileString(m_strGroup
, NULL
,
428 NULL
, m_strLocalFilename
) != 0;
431 wxLogLastError(wxT("WritePrivateProfileString"));
436 bool wxIniConfig::DeleteGroup(const wxString
& szKey
)
438 wxConfigPathChanger
path(this, szKey
);
440 // passing NULL as section name to WritePrivateProfileString deletes the
441 // whole section according to the docs
442 bool bOk
= WritePrivateProfileString(path
.Name(), NULL
,
443 NULL
, m_strLocalFilename
) != 0;
446 wxLogLastError(wxT("WritePrivateProfileString"));
455 bool wxIniConfig::DeleteAll()
457 // first delete our group in win.ini
458 WriteProfileString(GetVendorName(), NULL
, NULL
);
460 // then delete our own ini file
461 char szBuf
[MAX_PATH
];
462 size_t nRc
= GetWindowsDirectory(szBuf
, WXSIZEOF(szBuf
));
465 wxLogLastError(wxT("GetWindowsDirectory"));
467 else if ( nRc
> WXSIZEOF(szBuf
) )
469 wxFAIL_MSG(wxT("buffer is too small for Windows directory."));
472 wxString strFile
= szBuf
;
473 strFile
<< '\\' << m_strLocalFilename
;
475 if ( !wxRemoveFile(strFile
) ) {
476 wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str());
483 bool wxIniConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
489 bool wxIniConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
496 // wxUSE_CONFIG && wxUSE_UNICODE