]>
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 #include "wx/dynarray.h"
33 #include "wx/config.h"
35 #include "wx/msw/iniconf.h"
37 // _WINDOWS_ is defined when windows.h is included,
38 // __WXMSW__ is defined for MS Windows compilation
39 #if defined(__WXMSW__) && !defined(_WINDOWS_)
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // we replace all path separators with this character
48 #define PATH_SEP_REPLACE '_'
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxIniConfig::wxIniConfig(const wxString
& strAppName
,
59 const wxString
& strVendor
,
60 const wxString
& localFilename
,
61 const wxString
& globalFilename
,
63 : wxConfigBase(strAppName
, strVendor
, localFilename
, globalFilename
, style
)
65 #if 0 // This is too complex for some compilers, e.g. BC++ 5.01
66 : wxConfigBase((strAppName
.IsEmpty() && wxTheApp
) ? wxTheApp
->GetAppName()
68 strVendor
.IsEmpty() ? (wxTheApp
? wxTheApp
->GetVendorName()
71 localFilename
, globalFilename
, style
)
74 if (strAppName
.IsEmpty() && wxTheApp
)
75 SetAppName(wxTheApp
->GetAppName());
76 if (strVendor
.IsEmpty() && wxTheApp
)
77 SetVendorName(wxTheApp
->GetVendorName());
79 m_strLocalFilename
= localFilename
;
80 if (m_strLocalFilename
.IsEmpty())
82 m_strLocalFilename
= GetAppName() + ".ini";
85 // append the extension if none given and it's not an absolute file name
86 // (otherwise we assume that they know what they're doing)
87 if ( !wxIsPathSeparator(m_strLocalFilename
[0u]) &&
88 m_strLocalFilename
.Find('.') == wxNOT_FOUND
)
90 m_strLocalFilename
<< ".ini";
97 wxIniConfig::~wxIniConfig()
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 void wxIniConfig::SetPath(const wxString
& strPath
)
107 wxArrayString aParts
;
109 if ( strPath
.IsEmpty() ) {
112 else if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
) {
114 wxSplitPath(aParts
, strPath
);
117 // relative path, combine with current one
118 wxString strFullPath
= GetPath();
119 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
120 wxSplitPath(aParts
, strFullPath
);
123 size_t nPartsCount
= aParts
.Count();
125 if ( nPartsCount
== 0 ) {
127 m_strGroup
= PATH_SEP_REPLACE
;
131 m_strGroup
= aParts
[0u];
132 for ( size_t nPart
= 1; nPart
< nPartsCount
; nPart
++ ) {
134 m_strPath
<< PATH_SEP_REPLACE
;
135 m_strPath
<< aParts
[nPart
];
139 // other functions assume that all this is true, i.e. there are no trailing
140 // underscores at the end except if the group is the root one
141 wxASSERT( (m_strPath
.IsEmpty() || m_strPath
.Last() != PATH_SEP_REPLACE
) &&
142 (m_strGroup
== wxString(PATH_SEP_REPLACE
) ||
143 m_strGroup
.Last() != PATH_SEP_REPLACE
) );
146 const wxString
& wxIniConfig::GetPath() const
148 static wxString s_str
;
150 // always return abs path
151 s_str
= wxCONFIG_PATH_SEPARATOR
;
153 if ( m_strGroup
== wxString(PATH_SEP_REPLACE
) ) {
154 // we're at the root level, nothing to do
158 if ( !m_strPath
.IsEmpty() )
159 s_str
<< wxCONFIG_PATH_SEPARATOR
;
160 for ( const char *p
= m_strPath
; *p
!= '\0'; p
++ ) {
161 s_str
<< (*p
== PATH_SEP_REPLACE
? wxCONFIG_PATH_SEPARATOR
: *p
);
168 wxString
wxIniConfig::GetPrivateKeyName(const wxString
& szKey
) const
172 if ( !m_strPath
.IsEmpty() )
173 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
180 wxString
wxIniConfig::GetKeyName(const wxString
& szKey
) const
184 if ( m_strGroup
!= wxString(PATH_SEP_REPLACE
) )
185 strKey
<< m_strGroup
<< PATH_SEP_REPLACE
;
186 if ( !m_strPath
.IsEmpty() )
187 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
199 bool wxIniConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
201 wxFAIL_MSG("not implemented");
206 bool wxIniConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
208 wxFAIL_MSG("not implemented");
213 bool wxIniConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
215 wxFAIL_MSG("not implemented");
220 bool wxIniConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
222 wxFAIL_MSG("not implemented");
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
232 size_t wxIniConfig::GetNumberOfEntries(bool bRecursive
) const
234 wxFAIL_MSG("not implemented");
239 size_t wxIniConfig::GetNumberOfGroups(bool bRecursive
) const
241 wxFAIL_MSG("not implemented");
246 bool wxIniConfig::HasGroup(const wxString
& strName
) const
248 wxFAIL_MSG("not implemented");
253 bool wxIniConfig::HasEntry(const wxString
& strName
) const
255 wxFAIL_MSG("not implemented");
260 // is current group empty?
261 bool wxIniConfig::IsEmpty() const
265 GetPrivateProfileString(m_strGroup
, NULL
, "",
266 szBuf
, WXSIZEOF(szBuf
), m_strLocalFilename
);
267 if ( !::IsEmpty(szBuf
) )
270 GetProfileString(m_strGroup
, NULL
, "", szBuf
, WXSIZEOF(szBuf
));
271 if ( !::IsEmpty(szBuf
) )
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
) const
283 wxConfigPathChanger
path(this, szKey
);
284 wxString strKey
= GetPrivateKeyName(path
.Name());
286 char szBuf
[1024]; // @@ should dynamically allocate memory...
288 // first look in the private INI file
290 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
291 GetPrivateProfileString(m_strGroup
, strKey
, "",
292 szBuf
, WXSIZEOF(szBuf
), m_strLocalFilename
);
293 if ( ::IsEmpty(szBuf
) ) {
294 // now look in win.ini
295 wxString strKey
= GetKeyName(path
.Name());
296 GetProfileString(m_strGroup
, strKey
, "", szBuf
, WXSIZEOF(szBuf
));
299 if ( ::IsEmpty(szBuf
) ) {
308 bool wxIniConfig::Read(const wxString
& szKey
, wxString
*pstr
,
309 const wxString
& szDefault
) const
311 wxConfigPathChanger
path(this, szKey
);
312 wxString strKey
= GetPrivateKeyName(path
.Name());
314 char szBuf
[1024]; // @@ should dynamically allocate memory...
316 // first look in the private INI file
318 // NB: the lpDefault param to GetPrivateProfileString can't be NULL
319 GetPrivateProfileString(m_strGroup
, strKey
, "",
320 szBuf
, WXSIZEOF(szBuf
), m_strLocalFilename
);
321 if ( ::IsEmpty(szBuf
) ) {
322 // now look in win.ini
323 wxString strKey
= GetKeyName(path
.Name());
324 GetProfileString(m_strGroup
, strKey
, "", szBuf
, WXSIZEOF(szBuf
));
327 if ( ::IsEmpty(szBuf
) ) {
337 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const
339 wxConfigPathChanger
path(this, szKey
);
340 wxString strKey
= GetPrivateKeyName(path
.Name());
342 // hack: we have no mean to know if it really found the default value or
343 // didn't find anything, so we call it twice
345 static const int nMagic
= 17; // 17 is some "rare" number
346 static const int nMagic2
= 28; // arbitrary number != nMagic
347 long lVal
= GetPrivateProfileInt(m_strGroup
, strKey
, nMagic
, m_strLocalFilename
);
348 if ( lVal
!= nMagic
) {
349 // the value was read from the file
354 // is it really nMagic?
355 lVal
= GetPrivateProfileInt(m_strGroup
, strKey
, nMagic2
, m_strLocalFilename
);
356 if ( lVal
!= nMagic2
) {
357 // the nMagic it returned was indeed read from the file
362 // CS : I have no idea why they should look up in win.ini
363 // and if at all they have to do the same procedure using the two magic numbers
364 // otherwise it always returns true, even if the key was not there at all
366 // no, it was just returning the default value, so now look in win.ini
367 *pl
= GetProfileInt(GetVendorName(), GetKeyName(szKey
), *pl
);
374 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& szValue
)
376 wxConfigPathChanger
path(this, szKey
);
377 wxString strKey
= GetPrivateKeyName(path
.Name());
379 bool bOk
= WritePrivateProfileString(m_strGroup
, strKey
,
380 szValue
, m_strLocalFilename
) != 0;
383 wxLogLastError("WritePrivateProfileString");
388 bool wxIniConfig::Write(const wxString
& szKey
, long lValue
)
390 // ltoa() is not ANSI :-(
391 char szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
392 sprintf(szBuf
, "%ld", lValue
);
394 return Write(szKey
, szBuf
);
397 bool wxIniConfig::Flush(bool /* bCurrentOnly */)
399 // this is just the way it works
400 return WritePrivateProfileString(NULL
, NULL
, NULL
, m_strLocalFilename
) != 0;
403 // ----------------------------------------------------------------------------
405 // ----------------------------------------------------------------------------
407 bool wxIniConfig::DeleteEntry(const wxString
& szKey
, bool bGroupIfEmptyAlso
)
409 // passing NULL as value to WritePrivateProfileString deletes the key
410 // if ( !Write(szKey, (const char *)NULL) )
412 wxConfigPathChanger
path(this, szKey
);
413 wxString strKey
= GetPrivateKeyName(path
.Name());
415 if (WritePrivateProfileString(m_strGroup
, szKey
,
416 (const char*) NULL
, m_strLocalFilename
) == 0)
419 if ( !bGroupIfEmptyAlso
|| !IsEmpty() )
422 // delete the current group too
423 bool bOk
= WritePrivateProfileString(m_strGroup
, NULL
,
424 NULL
, m_strLocalFilename
) != 0;
427 wxLogLastError("WritePrivateProfileString");
432 bool wxIniConfig::DeleteGroup(const wxString
& szKey
)
434 wxConfigPathChanger
path(this, szKey
);
436 // passing NULL as section name to WritePrivateProfileString deletes the
437 // whole section according to the docs
438 bool bOk
= WritePrivateProfileString(path
.Name(), NULL
,
439 NULL
, m_strLocalFilename
) != 0;
442 wxLogLastError("WritePrivateProfileString");
451 bool wxIniConfig::DeleteAll()
453 // first delete our group in win.ini
454 WriteProfileString(GetVendorName(), NULL
, NULL
);
456 // then delete our own ini file
457 char szBuf
[MAX_PATH
];
458 size_t nRc
= GetWindowsDirectory(szBuf
, WXSIZEOF(szBuf
));
461 wxLogLastError("GetWindowsDirectory");
463 else if ( nRc
> WXSIZEOF(szBuf
) )
465 wxFAIL_MSG("buffer is too small for Windows directory.");
468 wxString strFile
= szBuf
;
469 strFile
<< '\\' << m_strLocalFilename
;
471 if ( !wxRemoveFile(strFile
) ) {
472 wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str());
479 bool wxIniConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
485 bool wxIniConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)