// Created: 27.04.98
// RCS-ID: $Id$
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
-// Licence: wxWindows license
+// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
-#pragma implementation "regconf.h"
-#endif
-
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#include "wx/config.h"
-#ifndef __WIN16__
-
#include "wx/msw/registry.h"
#include "wx/msw/regconf.h"
// ----------------------------------------------------------------------------
// we put our data in HKLM\SOFTWARE_KEY\appname
-#define SOFTWARE_KEY wxString("Software\\")
+#define SOFTWARE_KEY wxString(wxT("Software\\"))
// ----------------------------------------------------------------------------
// global functions
m_keyLocal.ReserveMemoryForName(MEMORY_PREALLOC);
m_keyLocalRoot.SetName(wxRegKey::HKCU, SOFTWARE_KEY + str);
- m_keyLocal.SetName(m_keyLocalRoot, _T(""));
+ m_keyLocal.SetName(m_keyLocalRoot, wxEmptyString);
if ( bDoUseGlobal )
{
m_keyGlobal.ReserveMemoryForName(MEMORY_PREALLOC);
m_keyGlobalRoot.SetName(wxRegKey::HKLM, SOFTWARE_KEY + str);
- m_keyGlobal.SetName(m_keyGlobalRoot, _T(""));
+ m_keyGlobal.SetName(m_keyGlobalRoot, wxEmptyString);
}
// Create() will Open() if key already exists
if ( bDoUseGlobal )
{
wxLogNull nolog;
- m_keyGlobalRoot.Open();
- m_keyGlobal.Open();
+ m_keyGlobalRoot.Open(wxRegKey::Read);
+ m_keyGlobal.Open(wxRegKey::Read);
}
}
-wxRegConfig::~wxRegConfig()
-{
- // nothing to do - key will be closed in their dtors
-}
-
// ----------------------------------------------------------------------------
// path management
// ----------------------------------------------------------------------------
{
strFullPath.reserve(2*m_strPath.length());
- strFullPath << m_strPath << wxCONFIG_PATH_SEPARATOR << strPath;
+ strFullPath << m_strPath;
+ if ( strFullPath.Len() == 0 ||
+ strFullPath.Last() != wxCONFIG_PATH_SEPARATOR )
+ strFullPath << wxCONFIG_PATH_SEPARATOR;
+ strFullPath << strPath;
}
// simplify it: we need to handle ".." here
size_t len = strFullPath.length();
const wxChar *end = src + len;
- wxChar *dst = m_strPath.GetWriteBuf(len);
+ wxStringBufferLength buf(m_strPath, len);
+ wxChar *dst = buf;
wxChar *start = dst;
for ( ; src < end; src++, dst++ )
// check the boundary condition below
// this is more efficient than strrchr()
+ dst--;
while ( *dst != wxCONFIG_PATH_SEPARATOR )
{
dst--;
posLastSlash = -1;
}
- // this shouldn't happen
+ // we must have found a slash one way or another!
wxASSERT_MSG( *dst == wxCONFIG_PATH_SEPARATOR,
_T("error in wxRegConfig::SetPath") );
+ // stay at the same position
+ dst--;
+
// we killed one
totalSlashes--;
}
totalSlashes++;
}
- //else: nothing to do, we squeeze several subseuquent
- // slashes into one
+ else // previous char was a slash too
+ {
+ // squeeze several subsequent slashes into one: i.e.
+ // just ignore this one
+ dst--;
+ }
}
}
else // normal character
}
*dst = _T('\0');
-
- m_strPath.UngetWriteBuf(dst - start);
+ buf.SetLength(dst - start);
}
#ifdef WX_DEBUG_SET_PATH
// registry APIs want backslashes instead of slashes
wxString strRegPath;
- size_t len = m_strPath.length();
+ if ( !m_strPath.empty() )
+ {
+ size_t len = m_strPath.length();
- const wxChar *src = m_strPath.c_str();
- wxChar *dst = strRegPath.GetWriteBuf(len);
+ const wxChar *src = m_strPath.c_str();
+ wxStringBufferLength buf(strRegPath, len);
+ wxChar *dst = buf;
- const wxChar *end = src + len;
- for ( ; src < end; src++, dst++ )
- {
- if ( *src == wxCONFIG_PATH_SEPARATOR )
- *dst = _T('\\');
- else
- *dst = *src;
- }
+ const wxChar *end = src + len;
+ for ( ; src < end; src++, dst++ )
+ {
+ if ( *src == wxCONFIG_PATH_SEPARATOR )
+ *dst = _T('\\');
+ else
+ *dst = *src;
+ }
- strRegPath.UngetWriteBuf(len);
+ buf.SetLength(len);
+ }
// this is not needed any longer as we don't create keys unnecessarily any
// more (now it is done on demand, i.e. only when they're going to contain
// change current key(s)
m_keyLocal.SetName(m_keyLocalRoot, strRegPath);
- m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath);
- // don't create it right now, wait until it is accessed
- //m_keyLocal.Create();
+ if ( GetStyle() & wxCONFIG_USE_GLOBAL_FILE )
+ {
+ m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath);
- wxLogNull nolog;
- m_keyGlobal.Open();
+ wxLogNull nolog;
+ m_keyGlobal.Open(wxRegKey::Read);
+ }
}
// ----------------------------------------------------------------------------
while ( m_keyGlobal.GetNextKey(str, lIndex) ) {
if ( !m_keyLocal.Exists() || !LocalKey().HasSubKey(str) ) {
// ok, found one - return it
- return TRUE;
+ return true;
}
}
// if we don't have the key at all, don't try to enumerate anything under it
if ( !m_keyLocal.Exists() )
- return FALSE;
+ return false;
// much easier with local entries: get the next one we find
// (don't forget to clear our flag bit and set it again later)
while ( m_keyGlobal.GetNextValue(str, lIndex) ) {
if ( !m_keyLocal.Exists() || !LocalKey().HasValue(str) ) {
// ok, found one - return it
- return TRUE;
+ return true;
}
}
// if we don't have the key at all, don't try to enumerate anything under it
if ( !m_keyLocal.Exists() )
- return FALSE;
+ return false;
// much easier with local entries: get the next one we find
// (don't forget to clear our flag bit and set it again later)
// reading/writing
// ----------------------------------------------------------------------------
-bool wxRegConfig::Read(const wxString& key, wxString *pStr) const
+bool wxRegConfig::DoReadString(const wxString& key, wxString *pStr) const
{
- wxConfigPathChanger path(this, key);
-
- bool bQueryGlobal = TRUE;
+ wxCHECK_MSG( pStr, false, _T("wxRegConfig::Read(): NULL param") );
- // if immutable key exists in global key we must check that it's not
- // overriden by the local key with the same name
- if ( IsImmutable(path.Name()) ) {
- if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) {
- if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) {
- wxLogWarning(wxT("User value for immutable key '%s' ignored."),
- path.Name().c_str());
- }
- *pStr = wxConfigBase::ExpandEnvVars(*pStr);
- return TRUE;
- }
- else {
- // don't waste time - it's not there anyhow
- bQueryGlobal = FALSE;
- }
- }
-
- // first try local key
- if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *pStr)) ||
- (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
- // nothing to do
-
- *pStr = wxConfigBase::ExpandEnvVars(*pStr);
- return TRUE;
- }
-
- return FALSE;
-}
-
-bool wxRegConfig::Read(const wxString& key, wxString *pStr,
- const wxString& szDefault) const
-{
wxConfigPathChanger path(this, key);
- bool bQueryGlobal = TRUE;
+ bool bQueryGlobal = true;
// if immutable key exists in global key we must check that it's not
// overriden by the local key with the same name
path.Name().c_str());
}
- return TRUE;
+ return true;
}
else {
// don't waste time - it's not there anyhow
- bQueryGlobal = FALSE;
+ bQueryGlobal = false;
}
}
// first try local key
if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *pStr)) ||
(bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
- *pStr = wxConfigBase::ExpandEnvVars(*pStr);
- return TRUE;
+ return true;
}
- else {
- if ( IsRecordingDefaults() ) {
- ((wxRegConfig*)this)->Write(key, szDefault);
- }
- // default value
- *pStr = szDefault;
- }
-
- *pStr = wxConfigBase::ExpandEnvVars(*pStr);
-
- return FALSE;
+ return false;
}
-bool wxRegConfig::Read(const wxString& key, long *plResult) const
+// this exactly reproduces the string version above except for ExpandEnvVars(),
+// we really should avoid this code duplication somehow...
+
+bool wxRegConfig::DoReadLong(const wxString& key, long *plResult) const
{
+ wxCHECK_MSG( plResult, false, _T("wxRegConfig::Read(): NULL param") );
+
wxConfigPathChanger path(this, key);
- bool bQueryGlobal = TRUE;
+ bool bQueryGlobal = true;
// if immutable key exists in global key we must check that it's not
// overriden by the local key with the same name
path.Name().c_str());
}
- return TRUE;
+ return true;
}
else {
// don't waste time - it's not there anyhow
- bQueryGlobal = FALSE;
+ bQueryGlobal = false;
}
}
// first try local key
if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), plResult)) ||
(bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) {
- return TRUE;
+ return true;
}
- return FALSE;
+
+ return false;
}
-bool wxRegConfig::Write(const wxString& key, const wxString& szValue)
+bool wxRegConfig::DoWriteString(const wxString& key, const wxString& szValue)
{
wxConfigPathChanger path(this, key);
if ( IsImmutable(path.Name()) ) {
wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
- return FALSE;
+ return false;
}
return LocalKey().SetValue(path.Name(), szValue);
}
-bool wxRegConfig::Write(const wxString& key, long lValue)
+bool wxRegConfig::DoWriteLong(const wxString& key, long lValue)
{
wxConfigPathChanger path(this, key);
if ( IsImmutable(path.Name()) ) {
wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
- return FALSE;
+ return false;
}
return LocalKey().SetValue(path.Name(), lValue);
{
// check that the old entry exists...
if ( !HasEntry(oldName) )
- return FALSE;
+ return false;
// and that the new one doesn't
if ( HasEntry(newName) )
- return FALSE;
-
- // delete the old entry and create the new one - but do in the reverse
- // order to not lose the data if Create() fails
-
- bool ok;
- if ( m_keyLocal.IsNumericValue(oldName) )
- {
- long val;
- ok = m_keyLocal.QueryValue(oldName, &val) &&
- m_keyLocal.SetValue(newName, val);
- }
- else
- {
- wxString val;
- ok = m_keyLocal.QueryValue(oldName, val) &&
- m_keyLocal.SetValue(newName, val);
- }
-
- if ( !ok )
- return FALSE;
+ return false;
- if ( !m_keyLocal.DeleteValue(oldName) )
- {
- m_keyLocal.DeleteValue(newName);
-
- return FALSE;
- }
-
- return TRUE;
+ return m_keyLocal.RenameValue(oldName, newName);
}
bool wxRegConfig::RenameGroup(const wxString& oldName, const wxString& newName)
{
// check that the old group exists...
if ( !HasGroup(oldName) )
- return FALSE;
+ return false;
// and that the new one doesn't
if ( HasGroup(newName) )
- return FALSE;
+ return false;
- // TODO there is no way to rename a registry key - we must do a deep copy
- // ourselves
- wxFAIL_MSG(wxT("Registry key renaming not implemented"));
-
- return FALSE;
+ return wxRegKey(m_keyLocal, oldName).Rename(newName);
}
// ----------------------------------------------------------------------------
// deleting
// ----------------------------------------------------------------------------
-bool wxRegConfig::DeleteEntry(const wxString& value, bool WXUNUSED(bGroupIfEmptyAlso))
+
+bool wxRegConfig::DeleteEntry(const wxString& value, bool bGroupIfEmptyAlso)
{
wxConfigPathChanger path(this, value);
if ( m_keyLocal.Exists() ) {
if ( !m_keyLocal.DeleteValue(path.Name()) )
- return FALSE;
+ return false;
- if ( m_keyLocal.IsEmpty() ) {
+ if ( bGroupIfEmptyAlso && m_keyLocal.IsEmpty() ) {
wxString strKey = GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR);
- SetPath(".."); // changes m_keyLocal
+ SetPath(_T("..")); // changes m_keyLocal
return LocalKey().DeleteKey(strKey);
}
}
- return TRUE;
+ return true;
}
bool wxRegConfig::DeleteGroup(const wxString& key)
{
wxConfigPathChanger path(this, key);
- return m_keyLocal.Exists() ? LocalKey().DeleteKey(path.Name()) : TRUE;
+ return m_keyLocal.Exists() ? LocalKey().DeleteKey(path.Name()) : true;
}
bool wxRegConfig::DeleteAll()
return bOk;
}
-#endif
- // __WIN16__
-
#endif
// wxUSE_CONFIG