+wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
+{
+ wxConfigBase *pOld = ms_pConfig;
+ ms_pConfig = pConfig;
+ return pOld;
+}
+
+wxConfigBase *wxConfigBase::Create()
+{
+ if ( ms_bAutoCreate && ms_pConfig == NULL ) {
+ wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
+ wxCHECK_MSG( traits, NULL, _T("create wxApp before calling this") );
+
+ ms_pConfig = traits->CreateConfig();
+ }
+
+ return ms_pConfig;
+}
+
+// ----------------------------------------------------------------------------
+// wxConfigBase reading entries
+// ----------------------------------------------------------------------------
+
+// implement both Read() overloads for the given type in terms of DoRead()
+#define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
+ bool wxConfigBase::Read(const wxString& key, type *val) const \
+ { \
+ wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
+ \
+ if ( !DoRead##name(key, val) ) \
+ return false; \
+ \
+ *val = extra(*val); \
+ \
+ return true; \
+ } \
+ \
+ bool wxConfigBase::Read(const wxString& key, \
+ type *val, \
+ deftype defVal) const \
+ { \
+ wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
+ \
+ bool read = DoRead##name(key, val); \
+ if ( !read ) \
+ { \
+ if ( IsRecordingDefaults() ) \
+ { \
+ ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
+ } \
+ \
+ *val = defVal; \
+ } \
+ \
+ *val = extra(*val); \
+ \
+ return read; \
+ }
+
+
+IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars)
+IMPLEMENT_READ_FOR_TYPE(Long, long, long, long)
+IMPLEMENT_READ_FOR_TYPE(Double, double, double, double)
+IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool)
+
+#undef IMPLEMENT_READ_FOR_TYPE
+
+// int is stored as long
+bool wxConfigBase::Read(const wxString& key, int *pi) const
+{
+ long l = *pi;
+ bool r = Read(key, &l);
+ wxASSERT_MSG( l < INT_MAX, _T("int overflow in wxConfig::Read") );
+ *pi = (int)l;
+ return r;
+}
+
+bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const
+{
+ long l = *pi;
+ bool r = Read(key, &l, defVal);
+ wxASSERT_MSG( l < INT_MAX, _T("int overflow in wxConfig::Read") );
+ *pi = (int)l;
+ return r;
+}
+
+// the DoReadXXX() for the other types have implementation in the base class
+// but can be overridden in the derived ones
+bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const
+{
+ wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") );
+
+ long l;
+ if ( !DoReadLong(key, &l) )
+ return false;
+
+ wxASSERT_MSG( l == 0 || l == 1, _T("bad bool value in wxConfig::DoReadInt") );
+
+ *val = l != 0;
+
+ return true;
+}
+
+bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const
+{
+ wxString str;
+ if ( Read(key, &str) )
+ {
+ return str.ToDouble(val);
+ }
+
+ return false;
+}
+
+// string reading helper
+wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
+{
+ wxString tmp; // Required for BC++
+ if (IsExpandingEnvVars())
+ tmp = wxExpandEnvVars(str);
+ else
+ tmp = str;
+ return tmp;
+}
+
+// ----------------------------------------------------------------------------
+// wxConfigBase writing
+// ----------------------------------------------------------------------------
+
+bool wxConfigBase::DoWriteDouble(const wxString& key, double val)
+{
+ return DoWriteString(key, wxString::Format(_T("%g"), val));
+}
+
+bool wxConfigBase::DoWriteBool(const wxString& key, bool value)
+{
+ return DoWriteLong(key, value ? 1l : 0l);
+}
+
+// ----------------------------------------------------------------------------
+// wxConfigPathChanger
+// ----------------------------------------------------------------------------
+
+wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
+ const wxString& strEntry)
+{
+ m_bChanged = false;
+ m_pContainer = (wxConfigBase *)pContainer;
+
+ // the path is everything which precedes the last slash
+ wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR);
+
+ // except in the special case of "/keyname" when there is nothing before "/"
+ if ( strPath.empty() &&
+ ((!strEntry.empty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
+ {
+ strPath = wxCONFIG_PATH_SEPARATOR;
+ }
+
+ if ( !strPath.empty() )
+ {
+ if ( m_pContainer->GetPath() != strPath )
+ {
+ // we do change the path so restore it later
+ m_bChanged = true;
+
+ /* JACS: work around a memory bug that causes an assert
+ when using wxRegConfig, related to reference-counting.
+ Can be reproduced by removing .wc_str() below and
+ adding the following code to the config sample OnInit under
+ Windows:
+
+ pConfig->SetPath(wxT("MySettings"));
+ pConfig->SetPath(wxT(".."));
+ int value;
+ pConfig->Read(_T("MainWindowX"), & value);
+ */
+ m_strOldPath = m_pContainer->GetPath().wc_str();
+ if ( *m_strOldPath.c_str() != wxCONFIG_PATH_SEPARATOR )
+ m_strOldPath += wxCONFIG_PATH_SEPARATOR;
+ m_pContainer->SetPath(strPath);
+ }
+
+ // in any case, use the just the name, not full path
+ m_strName = strEntry.AfterLast(wxCONFIG_PATH_SEPARATOR);
+ }
+ else {
+ // it's a name only, without path - nothing to do
+ m_strName = strEntry;
+ }
+}
+
+void wxConfigPathChanger::UpdateIfDeleted()
+{
+ // we don't have to do anything at all if we didn't change the path
+ if ( !m_bChanged )
+ return;
+
+ // find the deepest still existing parent path of the original path
+ while ( !m_pContainer->HasGroup(m_strOldPath) )
+ {
+ m_strOldPath = m_strOldPath.BeforeLast(wxCONFIG_PATH_SEPARATOR);
+ if ( m_strOldPath.empty() )
+ m_strOldPath = wxCONFIG_PATH_SEPARATOR;
+ }
+}
+
+wxConfigPathChanger::~wxConfigPathChanger()
+{
+ // only restore path if it was changed
+ if ( m_bChanged ) {
+ m_pContainer->SetPath(m_strOldPath);
+ }
+}
+
+// this is a wxConfig method but it's mainly used with wxConfigPathChanger
+/* static */
+wxString wxConfigBase::RemoveTrailingSeparator(const wxString& key)
+{
+ wxString path(key);
+
+ // don't remove the only separator from a root group path!
+ while ( path.length() > 1 )
+ {
+ if ( *path.rbegin() != wxCONFIG_PATH_SEPARATOR )
+ break;
+
+ path.erase(path.end() - 1);
+ }
+
+ return path;
+}
+
+#endif // wxUSE_CONFIG
+