]>
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
&& wxTheApp
? wxTheApp
->GetAppName()
65 !strVendor
? (wxTheApp
? wxTheApp
->GetVendorName()
68 localFilename
, globalFilename
, style
)
70 m_strLocalFilename
= localFilename
;
71 if (m_strLocalFilename
.IsEmpty())
73 m_strLocalFilename
= GetAppName() + ".ini";
76 // append the extension if none given and it's not an absolute file name
77 // (otherwise we assume that they know what they're doing)
78 if ( !wxIsPathSeparator(m_strLocalFilename
[0u]) &&
79 m_strLocalFilename
.Find('.') == wxNOT_FOUND
)
81 m_strLocalFilename
<< ".ini";
88 wxIniConfig::~wxIniConfig()
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 void wxIniConfig::SetPath(const wxString
& strPath
)
100 if ( strPath
.IsEmpty() ) {
103 else if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
) {
105 wxSplitPath(aParts
, strPath
);
108 // relative path, combine with current one
109 wxString strFullPath
= GetPath();
110 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
111 wxSplitPath(aParts
, strFullPath
);
114 size_t nPartsCount
= aParts
.Count();
116 if ( nPartsCount
== 0 ) {
118 m_strGroup
= PATH_SEP_REPLACE
;
122 m_strGroup
= aParts
[0u];
123 for ( size_t nPart
= 1; nPart
< nPartsCount
; nPart
++ ) {
125 m_strPath
<< PATH_SEP_REPLACE
;
126 m_strPath
<< aParts
[nPart
];
130 // other functions assume that all this is true, i.e. there are no trailing
131 // underscores at the end except if the group is the root one
132 wxASSERT( (m_strPath
.IsEmpty() || m_strPath
.Last() != PATH_SEP_REPLACE
) &&
133 (m_strGroup
== wxString(PATH_SEP_REPLACE
) ||
134 m_strGroup
.Last() != PATH_SEP_REPLACE
) );
137 const wxString
& wxIniConfig::GetPath() const
139 static wxString s_str
;
141 // always return abs path
142 s_str
= wxCONFIG_PATH_SEPARATOR
;
144 if ( m_strGroup
== wxString(PATH_SEP_REPLACE
) ) {
145 // we're at the root level, nothing to do
149 if ( !m_strPath
.IsEmpty() )
150 s_str
<< wxCONFIG_PATH_SEPARATOR
;
151 for ( const char *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
.IsEmpty() )
164 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
171 wxString
wxIniConfig::GetKeyName(const wxString
& szKey
) const
175 if ( m_strGroup
!= wxString(PATH_SEP_REPLACE
) )
176 strKey
<< m_strGroup
<< PATH_SEP_REPLACE
;
177 if ( !m_strPath
.IsEmpty() )
178 strKey
<< m_strPath
<< PATH_SEP_REPLACE
;
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 bool wxIniConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
192 wxFAIL_MSG("not implemented");
197 bool wxIniConfig::GetNextGroup (wxString
& str
, long& lIndex
) const
199 wxFAIL_MSG("not implemented");
204 bool wxIniConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
206 wxFAIL_MSG("not implemented");
211 bool wxIniConfig::GetNextEntry (wxString
& str
, long& lIndex
) const
213 wxFAIL_MSG("not implemented");
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
223 size_t wxIniConfig::GetNumberOfEntries(bool bRecursive
) const
225 wxFAIL_MSG("not implemented");
230 size_t wxIniConfig::GetNumberOfGroups(bool bRecursive
) const
232 wxFAIL_MSG("not implemented");
237 bool wxIniConfig::HasGroup(const wxString
& strName
) const
239 wxFAIL_MSG("not implemented");
244 bool wxIniConfig::HasEntry(const wxString
& strName
) const
246 wxFAIL_MSG("not implemented");
251 // is current group empty?
252 bool wxIniConfig::IsEmpty() const
256 GetPrivateProfileString(m_strGroup
, NULL
, "",
257 szBuf
, WXSIZEOF(szBuf
), m_strLocalFilename
);
258 if ( !::IsEmpty(szBuf
) )
261 GetProfileString(m_strGroup
, NULL
, "", szBuf
, WXSIZEOF(szBuf
));
262 if ( !::IsEmpty(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 char 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 ( ::IsEmpty(szBuf
) ) {
285 // now look in win.ini
286 wxString strKey
= GetKeyName(path
.Name());
287 GetProfileString(m_strGroup
, strKey
, "", szBuf
, WXSIZEOF(szBuf
));
290 if ( ::IsEmpty(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 char 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 ( ::IsEmpty(szBuf
) ) {
313 // now look in win.ini
314 wxString strKey
= GetKeyName(path
.Name());
315 GetProfileString(m_strGroup
, strKey
, "", szBuf
, WXSIZEOF(szBuf
));
318 if ( ::IsEmpty(szBuf
) ) {
328 bool wxIniConfig::Read(const wxString
& szKey
, long *pl
) const
330 wxConfigPathChanger
path(this, szKey
);
331 wxString strKey
= GetPrivateKeyName(path
.Name());
333 // hack: we have no mean to know if it really found the default value or
334 // didn't find anything, so we call it twice
336 static const int nMagic
= 17; // 17 is some "rare" number
337 static const int nMagic2
= 28; // arbitrary number != nMagic
338 long lVal
= GetPrivateProfileInt(m_strGroup
, strKey
, nMagic
, m_strLocalFilename
);
339 if ( lVal
!= nMagic
) {
340 // the value was read from the file
345 // is it really nMagic?
346 lVal
= GetPrivateProfileInt(m_strGroup
, strKey
, nMagic2
, m_strLocalFilename
);
347 if ( lVal
== nMagic2
) {
348 // the nMagic it returned was indeed read from the file
353 // no, it was just returning the default value, so now look in win.ini
354 *pl
= GetProfileInt(GetVendorName(), GetKeyName(szKey
), *pl
);
359 bool wxIniConfig::Write(const wxString
& szKey
, const wxString
& szValue
)
361 wxConfigPathChanger
path(this, szKey
);
362 wxString strKey
= GetPrivateKeyName(path
.Name());
364 bool bOk
= WritePrivateProfileString(m_strGroup
, strKey
,
365 szValue
, m_strLocalFilename
) != 0;
368 wxLogLastError("WritePrivateProfileString");
373 bool wxIniConfig::Write(const wxString
& szKey
, long lValue
)
375 // ltoa() is not ANSI :-(
376 char szBuf
[40]; // should be good for sizeof(long) <= 16 (128 bits)
377 sprintf(szBuf
, "%ld", lValue
);
379 return Write(szKey
, szBuf
);
382 bool wxIniConfig::Flush(bool /* bCurrentOnly */)
384 // this is just the way it works
385 return WritePrivateProfileString(NULL
, NULL
, NULL
, m_strLocalFilename
) != 0;
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 bool wxIniConfig::DeleteEntry(const wxString
& szKey
, bool bGroupIfEmptyAlso
)
394 // passing NULL as value to WritePrivateProfileString deletes the key
395 // if ( !Write(szKey, (const char *)NULL) )
397 wxConfigPathChanger
path(this, szKey
);
398 wxString strKey
= GetPrivateKeyName(path
.Name());
400 if (WritePrivateProfileString(m_strGroup
, szKey
,
401 (const char*) NULL
, m_strLocalFilename
) == 0)
404 if ( !bGroupIfEmptyAlso
|| !IsEmpty() )
407 // delete the current group too
408 bool bOk
= WritePrivateProfileString(m_strGroup
, NULL
,
409 NULL
, m_strLocalFilename
) != 0;
412 wxLogLastError("WritePrivateProfileString");
417 bool wxIniConfig::DeleteGroup(const wxString
& szKey
)
419 wxConfigPathChanger
path(this, szKey
);
421 // passing NULL as section name to WritePrivateProfileString deletes the
422 // whole section according to the docs
423 bool bOk
= WritePrivateProfileString(path
.Name(), NULL
,
424 NULL
, m_strLocalFilename
) != 0;
427 wxLogLastError("WritePrivateProfileString");
436 bool wxIniConfig::DeleteAll()
438 // first delete our group in win.ini
439 WriteProfileString(GetVendorName(), NULL
, NULL
);
441 // then delete our own ini file
442 char szBuf
[MAX_PATH
];
443 size_t nRc
= GetWindowsDirectory(szBuf
, WXSIZEOF(szBuf
));
446 wxLogLastError("GetWindowsDirectory");
448 else if ( nRc
> WXSIZEOF(szBuf
) )
450 wxFAIL_MSG("buffer is too small for Windows directory.");
453 wxString strFile
= szBuf
;
454 strFile
<< '\\' << m_strLocalFilename
;
456 if ( !wxRemoveFile(strFile
) ) {
457 wxLogSysError(_("Can't delete the INI file '%s'"), strFile
.c_str());
464 bool wxIniConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
470 bool wxIniConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)