wxConfig changes to be more logical.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@636 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
1998-08-27 21:06:02 +00:00
parent 73fb82f3f3
commit 1824493628
14 changed files with 570 additions and 190 deletions

View File

@@ -55,6 +55,10 @@
#define wxCONFIG_WIN32_NATIVE TRUE
#endif
// Style flags for constructor style parameter
#define wxCONFIG_USE_LOCAL_FILE 1
#define wxCONFIG_USE_GLOBAL_FILE 2
// ----------------------------------------------------------------------------
// various helper global functions
// ----------------------------------------------------------------------------
@@ -101,7 +105,17 @@ public:
// ctor & virtual dtor
// environment variable expansion is on by default
wxConfigBase() { m_bExpandEnvVars = TRUE; m_bRecordDefaults = FALSE; }
// wxConfigBase() { m_bExpandEnvVars = TRUE; m_bRecordDefaults = FALSE; }
// ctor
// Not all args will always be used by derived classes, but
// including them all in each class ensures compatibility.
// If appName is empty, uses wxApp name
wxConfigBase(const wxString& appName = wxEmptyString, const wxString& vendorName = wxEmptyString,
const wxString& localFilename = wxEmptyString, const wxString& globalFilename = wxEmptyString,
long style = 0);
// empty but ensures that dtor of all derived classes is virtual
virtual ~wxConfigBase() { }
@@ -138,31 +152,44 @@ public:
// key access: returns TRUE if value was really read, FALSE if default used
// (and if the key is not found the default value is returned.)
// read a string from the key
virtual bool Read(wxString *pStr, const char *szKey,
const char *szDefault = (const char *) NULL) const = 0;
// another version using statis buffer - it means it will be overwritten
// after each call to this function!
virtual const char *Read(const char *szKey,
const char *szDefault = (const char *) NULL) const;
// the same for longs
virtual long Read(const char *szKey, long lDefault) const
{ long l; Read(&l, szKey, lDefault); return l; }
// and another version: returns true if default value is returned
virtual bool Read(long *pl, const char *szKey, long lDefault = 0) const = 0;
virtual bool Read(const wxString& key, wxString *pStr) const = 0;
virtual bool Read(const wxString& key, wxString *pStr, const wxString& defVal) const;
virtual wxString Read(const wxString& key, const wxString& defVal) const;
virtual bool Read(const wxString& key, long *pl) const = 0;
virtual bool Read(const wxString& key, long *pl, long defVal) const;
virtual long Read(const wxString& strKey, long defVal) const
{ long l; Read(strKey, &l, defVal); return l; }
// Convenience functions that are built on other forms
// double
virtual bool Read(const wxString& key, double* val) const;
virtual bool Read(const wxString& key, double* val, double defVal) const;
// bool
virtual bool Read(const wxString& key, bool* val) const;
virtual bool Read(const wxString& key, bool* val, bool defVal) const;
// write the value (return true on success)
virtual bool Write(const char *szKey, const char *szValue) = 0;
virtual bool Write(const char *szKey, long lValue) = 0;
virtual bool Write(const wxString& key, const wxString& value) = 0;
virtual bool Write(const wxString& key, long value) = 0;
// Convenience functions
virtual bool Write(const wxString& key, double value);
virtual bool Write(const wxString& key, bool value);
// permanently writes all changes
virtual bool Flush(bool bCurrentOnly = FALSE) = 0;
// delete entries/groups
// deletes the specified entry and the group it belongs to if
// it was the last key in it and the second parameter is true
virtual bool DeleteEntry(const char *szKey,
virtual bool DeleteEntry(const wxString& key,
bool bDeleteGroupIfEmpty = TRUE) = 0;
// delete the group (with all subgroups)
virtual bool DeleteGroup(const char *szKey) = 0;
virtual bool DeleteGroup(const wxString& key) = 0;
// delete the whole underlying object (disk file, registry key, ...)
// primarly for use by desinstallation routine.
virtual bool DeleteAll() = 0;
@@ -186,20 +213,49 @@ public:
return tmp;
}
// misc accessors
inline wxString GetAppName() const { return m_appName; }
inline wxString GetVendorName() const { return m_vendorName; }
inline void SetAppName(const wxString& appName) { m_appName = appName; }
inline void SetVendorName(const wxString& vendorName) { m_vendorName = vendorName; }
inline void SetStyle(long style) { m_style; }
inline long GetStyle() const { return m_style; }
protected:
static bool IsImmutable(const char *szKey)
{ return *szKey == wxCONFIG_IMMUTABLE_PREFIX; }
static bool IsImmutable(const wxString& key)
{ return key[0] == wxCONFIG_IMMUTABLE_PREFIX; }
private:
// are we doing automatic environment variable expansion?
bool m_bExpandEnvVars;
// do we record default values?
bool m_bRecordDefaults;
// static variables
static wxConfigBase *ms_pConfig;
static bool ms_bAutoCreate;
// Application name and organisation name
wxString m_appName;
wxString m_vendorName;
// Style flag
long m_style;
};
// a handy little class which changes current path to the path of given entry
// and restores it in dtor: so if you declare a local variable of this type,
// you work in the entry directory and the path is automatically restored
// when the function returns
class PathChanger
// Taken out of wxConfig since not all compilers can cope with nested classes.
class wxConfigPathChanger
{
public:
// ctor/dtor do path changing/restorin
PathChanger(const wxConfigBase *pContainer, const wxString& strEntry);
~PathChanger();
wxConfigPathChanger(const wxConfigBase *pContainer, const wxString& strEntry);
~wxConfigPathChanger();
// get the key name
const wxString& Name() const { return m_strName; }
@@ -211,16 +267,6 @@ protected:
bool m_bChanged; // was the path changed?
};
private:
// are we doing automatic environment variable expansion?
bool m_bExpandEnvVars;
// do we record default values?
bool m_bRecordDefaults;
// static variables
static wxConfigBase *ms_pConfig;
static bool ms_bAutoCreate;
};
// ----------------------------------------------------------------------------
// the native wxConfigBase implementation
@@ -240,5 +286,7 @@ private:
#define classwxConfig classwxFileConfig
#endif
#endif // _WX_CONFIG_H_

View File

@@ -112,6 +112,8 @@ public:
static wxString GetLocalFileName(const char *szFile);
// ctor & dtor
#if 0
// the names of local and global (if not disabled) config files are
// constructed using Get{Local|Global}FileName functions described above
// (szAppName is just the (short) name of your application)
@@ -123,6 +125,14 @@ public:
// directory). If either of strings is empty, the corresponding file is not
// used.
wxFileConfig(const wxString& strLocal, const wxString& strGlobal);
#endif
// New constructor: one size fits all. Specify wxCONFIG_USE_LOCAL_FILE
// or wxCONFIG_USE_GLOBAL_FILE to say which files should be used.
wxFileConfig(const wxString& appName, const wxString& vendorName = wxEmptyString,
const wxString& localFilename = wxEmptyString, const wxString& globalFilename = wxEmptyString,
long style = wxCONFIG_USE_LOCAL_FILE);
// dtor will save unsaved data
virtual ~wxFileConfig();
@@ -141,6 +151,7 @@ public:
virtual bool HasGroup(const wxString& strName) const;
virtual bool HasEntry(const wxString& strName) const;
#if 0
virtual bool Read(wxString *pstr, const char *szKey,
const char *szDefault = 0) const;
virtual const char *Read(const char *szKey,
@@ -150,10 +161,31 @@ public:
{ return wxConfigBase::Read(szKey, lDefault); }
virtual bool Write(const char *szKey, const char *szValue);
virtual bool Write(const char *szKey, long lValue);
#endif
virtual bool Read(const wxString& key, wxString *pStr) const;
virtual bool Read(const wxString& key, wxString *pStr, const wxString& defValue) const;
virtual bool Read(const wxString& key, long *pl) const;
// The following are necessary to satisfy the compiler
wxString Read(const wxString& key, const wxString& defVal) const
{ return wxConfigBase::Read(key, defVal); }
bool Read(const wxString& key, long *pl, long defVal) const
{ return wxConfigBase::Read(key, pl, defVal); }
long Read(const wxString& key, long defVal) const
{ return wxConfigBase::Read(key, defVal); }
bool Read(const wxString& key, double* val) const
{ return wxConfigBase::Read(key, val); }
bool Read(const wxString& key, double* val, double defVal) const
{ return wxConfigBase::Read(key, val, defVal); }
virtual bool Write(const wxString& key, const wxString& szValue);
virtual bool Write(const wxString& key, long lValue);
virtual bool Flush(bool bCurrentOnly = FALSE);
virtual bool DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso);
virtual bool DeleteGroup(const char *szKey);
virtual bool DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso);
virtual bool DeleteGroup(const wxString& szKey);
virtual bool DeleteAll();
public:

View File

@@ -43,7 +43,8 @@ public:
// if strAppName doesn't contain the extension and is not an absolute path,
// ".ini" is appended to it. if strVendor is empty, it's taken to be the
// same as strAppName.
wxIniConfig(const wxString& strAppName, const wxString& strVendor = "");
wxIniConfig(const wxString& strAppName = wxEmptyString, const wxString& strVendor = wxEmptyString,
const wxString& localFilename = wxEmptyString, const wxString& globalFilename = wxEmptyString, long style = wxCONFIG_USE_LOCAL_FILE);
virtual ~wxIniConfig();
// implement inherited pure virtual functions
@@ -64,14 +65,26 @@ public:
// return TRUE if the current group is empty
bool IsEmpty() const;
virtual bool Read(wxString *pstr, const char *szKey,
const char *szDefault = 0) const;
virtual const char *Read(const char *szKey,
const char *szDefault = 0) const;
virtual bool Read(long *pl, const char *szKey, long lDefault) const;
virtual long Read(const char *szKey, long lDefault) const;
virtual bool Write(const char *szKey, const char *szValue);
virtual bool Write(const char *szKey, long lValue);
// read/write
bool Read(const wxString& key, wxString *pStr) const;
bool Read(const wxString& key, wxString *pStr, const wxString& szDefault) const;
bool Read(const wxString& key, long *plResult) const;
// The following are necessary to satisfy the compiler
wxString Read(const wxString& key, const wxString& defVal) const
{ return wxConfigBase::Read(key, defVal); }
bool Read(const wxString& key, long *pl, long defVal) const
{ return wxConfigBase::Read(key, pl, defVal); }
long Read(const wxString& key, long defVal) const
{ return wxConfigBase::Read(key, defVal); }
bool Read(const wxString& key, double* val) const
{ return wxConfigBase::Read(key, val); }
bool Read(const wxString& key, double* val, double defVal) const
{ return wxConfigBase::Read(key, val, defVal); }
bool Write(const wxString& key, const wxString& szValue);
bool Write(const wxString& key, long lValue);
virtual bool Flush(bool bCurrentOnly = FALSE);
virtual bool DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso);
@@ -80,11 +93,10 @@ public:
private:
// helpers
wxString GetPrivateKeyName(const char *szKey) const;
wxString GetKeyName(const char *szKey) const;
wxString GetPrivateKeyName(const wxString& szKey) const;
wxString GetKeyName(const wxString& szKey) const;
wxString m_strAppName, // name of the private INI file
m_strVendor; // name of our section in WIN.INI
wxString m_strLocalFilename; // name of the private INI file
wxString m_strGroup, // current group in appname.ini file
m_strPath; // the rest of the path (no trailing '_'!)
};

View File

@@ -28,8 +28,11 @@ class wxRegConfig : public wxConfigBase
{
public:
// ctor & dtor
// will store data in HKLM\strRegHive and HKCU\strRegHive
wxRegConfig(const wxString& strRegHive);
// will store data in HKLM\appName and HKCU\appName
wxRegConfig(const wxString& appName = wxEmptyString, const wxString& vendorName = wxEmptyString,
const wxString& localFilename = wxEmptyString, const wxString& globalFilename = wxEmptyString,
long style = 0);
// dtor will save unsaved data
virtual ~wxRegConfig();
@@ -57,16 +60,30 @@ public:
virtual size_t GetNumberOfGroups(bool bRecursive = FALSE) const;
// read/write
virtual bool Read(wxString *pStr, const char *szKey,
const char *szDefault = 0) const;
virtual bool Read(long *result, const char *szKey, long lDefault = 0) const;
virtual bool Write(const char *szKey, const char *szValue);
virtual bool Write(const char *szKey, long Value);
bool Read(const wxString& key, wxString *pStr) const;
bool Read(const wxString& key, wxString *pStr, const wxString& szDefault) const;
bool Read(const wxString& key, long *plResult) const;
// The following are necessary to satisfy the compiler
wxString Read(const wxString& key, const wxString& defVal) const
{ return wxConfigBase::Read(key, defVal); }
bool Read(const wxString& key, long *pl, long defVal) const
{ return wxConfigBase::Read(key, pl, defVal); }
long Read(const wxString& key, long defVal) const
{ return wxConfigBase::Read(key, defVal); }
bool Read(const wxString& key, double* val) const
{ return wxConfigBase::Read(key, val); }
bool Read(const wxString& key, double* val, double defVal) const
{ return wxConfigBase::Read(key, val, defVal); }
bool Write(const wxString& key, const wxString& szValue);
bool Write(const wxString& key, long lValue);
virtual bool Flush(bool /* bCurrentOnly = FALSE */ ) { return TRUE; }
// delete
virtual bool DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso);
virtual bool DeleteGroup(const char *szKey);
virtual bool DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso);
virtual bool DeleteGroup(const wxString& key);
virtual bool DeleteAll();
private:

View File

@@ -225,8 +225,8 @@ MyFrame::~MyFrame()
int x, y, w, h;
GetClientSize(&w, &h);
GetPosition(&x, &y);
pConfig->Write("/MainFrame/x", x);
pConfig->Write("/MainFrame/y", y);
pConfig->Write("/MainFrame/w", w);
pConfig->Write("/MainFrame/h", h);
pConfig->Write("/MainFrame/x", (long) x);
pConfig->Write("/MainFrame/y", (long) y);
pConfig->Write("/MainFrame/w", (long) w);
pConfig->Write("/MainFrame/h", (long) h);
}

View File

@@ -41,7 +41,7 @@
ScoreFile::ScoreFile(const char* appName)
{
#ifdef 0
#if 0
wxString filename;
m_configFilename << "/usr/local/share/" << appName << ".scores";
if (access(m_configFilename, F_OK) == 0)
@@ -69,11 +69,7 @@ ScoreFile::ScoreFile(const char* appName)
}
#endif
#ifdef __UNIX__
m_config = new wxFileConfig( appName, "" ); // only local
#else
m_config = new wxFileConfig( "",appName ); // only global
#endif
m_config = new wxConfig(appName, "wxWindows", appName, "", wxCONFIG_USE_LOCAL_FILE); // only local
}
ScoreFile::~ScoreFile()
@@ -132,7 +128,7 @@ wxString ScoreFile::GetPreviousPlayer() const
{
wxString result;
m_config->SetPath("/General");
m_config->Read(&result, "LastPlayer");
m_config->Read("LastPlayer", &result);
return result;
}
@@ -149,10 +145,10 @@ void ScoreFile::ReadPlayersScore(
m_config->SetPath("/Players");
m_config->SetPath(player);
if (m_config->Read(&myScore, (const char *) "Score",0L) &&
m_config->Read(&myGames, (const char *) "Games",0L) &&
m_config->Read(&myWins, (const char *) "Wins",0L) &&
m_config->Read(&check, (const char *) "Check",0L))
if (m_config->Read("Score", &myScore, 0L) &&
m_config->Read("Games", &myGames, 0L) &&
m_config->Read("Wins", &myWins, 0L) &&
m_config->Read("Check", &check, 0L))
{
if (check != CalcCheck(player, myGames, myWins, myScore))
{

View File

@@ -13,6 +13,8 @@
#ifndef _SCOREFILE_H_
#define _SCOREFILE_H_
#include <wx/config.h>
class wxConfig;
class ScoreFile {
@@ -28,8 +30,8 @@ public:
private:
long CalcCheck(const char* name, int p1, int p2, int p3);
wxString m_configFilename;
wxConfig* m_config;
wxString m_configFilename;
wxConfig* m_config;
};
#endif

View File

@@ -38,7 +38,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnSelectAll)
EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)

View File

@@ -57,6 +57,10 @@ public:
RegImageList();
};
// array of children of the node
struct TreeNode;
WX_DEFINE_ARRAY(TreeNode *, TreeChildren);
// ----------------------------------------------------------------------------
// our control
// ----------------------------------------------------------------------------
@@ -90,10 +94,7 @@ public:
DECLARE_EVENT_TABLE();
private:
// array of children of the node
struct TreeNode;
WX_DEFINE_ARRAY(TreeNode *, TreeChildren);
// structure describing a registry key/value
struct TreeNode
{

View File

@@ -50,6 +50,7 @@
#endif
#include <stdlib.h>
#include <math.h>
#include <ctype.h> // for isalnum()
// ----------------------------------------------------------------------------
@@ -67,6 +68,15 @@ bool wxConfigBase::ms_bAutoCreate = TRUE;
// wxConfigBase
// ----------------------------------------------------------------------------
// Not all args will always be used by derived classes, but
// including them all in each class ensures compatibility.
wxConfigBase::wxConfigBase(const wxString& appName, const wxString& vendorName,
const wxString& localFilename, const wxString& globalFilename, long style):
m_appName(appName), m_vendorName(vendorName), m_style(style)
{
m_bExpandEnvVars = TRUE; m_bRecordDefaults = FALSE;
}
wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
{
wxConfigBase *pOld = ms_pConfig;
@@ -80,8 +90,7 @@ wxConfigBase *wxConfigBase::Create()
ms_pConfig =
#if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
#ifdef __WIN32__
new wxRegConfig(wxTheApp->GetVendorName() + '\\'
+ wxTheApp->GetAppName());
new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
#else //WIN16
new wxIniConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
#endif
@@ -93,21 +102,101 @@ wxConfigBase *wxConfigBase::Create()
return ms_pConfig;
}
const char *wxConfigBase::Read(const char *szKey, const char *szDefault) const
wxString wxConfigBase::Read(const wxString& key, const wxString& defVal) const
{
static char s_szBuf[1024];
wxString s;
Read(&s, szKey, szDefault);
strncpy(s_szBuf, s, WXSIZEOF(s_szBuf));
return s_szBuf;
Read(key, &s, defVal);
return s;
}
bool wxConfigBase::Read(const wxString& key, wxString *str, const wxString& defVal) const
{
if (!Read(key, str))
{
*str = ExpandEnvVars(defVal);
return FALSE;
}
else
return TRUE;
}
bool wxConfigBase::Read(const wxString& key, long *pl, long defVal) const
{
if (!Read(key, pl))
{
*pl = defVal;
return FALSE;
}
else
return TRUE;
}
bool wxConfigBase::Read(const wxString& key, double* val) const
{
wxString str;
if (Read(key, str))
{
*val = atof(str);
return TRUE;
}
else
return FALSE;
}
bool wxConfigBase::Read(const wxString& key, double* val, double defVal) const
{
if (!Read(key, val))
{
*val = defVal;
return FALSE;
}
else
return TRUE;
}
bool wxConfigBase::Read(const wxString& key, bool* val) const
{
long l;
if (Read(key, & l))
{
*val = (l != 0);
return TRUE;
}
else
return FALSE;
}
bool wxConfigBase::Read(const wxString& key, bool* val, bool defVal) const
{
if (!Read(key, val))
{
*val = defVal;
return FALSE;
}
else
return TRUE;
}
// Convenience functions
bool wxConfigBase::Write(const wxString& key, double val)
{
wxString str;
str.Printf("%f", val);
return Write(key, str);
}
bool wxConfigBase::Write(const wxString& key, bool value)
{
long l = (value ? 1 : 0);
return Write(key, l);
}
// ----------------------------------------------------------------------------
// Config::PathChanger
// wxConfigPathChanger
// ----------------------------------------------------------------------------
wxConfigBase::PathChanger::PathChanger(const wxConfigBase *pContainer,
wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
const wxString& strEntry)
{
m_pContainer = (wxConfigBase *)pContainer;
@@ -132,7 +221,7 @@ wxConfigBase::PathChanger::PathChanger(const wxConfigBase *pContainer,
}
}
wxConfigBase::PathChanger::~PathChanger()
wxConfigPathChanger::~wxConfigPathChanger()
{
// only restore path if it was changed
if ( m_bChanged ) {
@@ -293,3 +382,4 @@ void wxSplitPath(wxArrayString& aParts, const char *sz)
}
}

View File

@@ -32,6 +32,7 @@
#include <wx/intl.h>
#endif //WX_PRECOMP
#include <wx/app.h>
#include <wx/dynarray.h>
#include <wx/file.h>
#include <wx/log.h>
@@ -207,6 +208,7 @@ void wxFileConfig::Init()
}
}
#if 0
wxFileConfig::wxFileConfig(const char *szAppName, bool bLocalOnly)
{
wxASSERT( !IsEmpty(szAppName) ); // invent a name for your application!
@@ -237,6 +239,55 @@ wxFileConfig::wxFileConfig(const wxString& strLocal, const wxString& strGlobal)
Init();
}
#endif
// New-style constructor
wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName,
const wxString& strLocal, const wxString& strGlobal, long style)
: wxConfigBase(appName, vendorName, strLocal, strGlobal, style),
m_strLocalFile(strLocal), m_strGlobalFile(strGlobal)
{
// Make up an application name if not supplied
if (appName.IsEmpty() && wxTheApp)
{
SetAppName(wxTheApp->GetAppName());
}
// Make up names for files if empty
if (m_strLocalFile.IsEmpty() && (style & wxCONFIG_USE_LOCAL_FILE) && wxTheApp)
{
m_strLocalFile = wxTheApp->GetAppName();
}
if (m_strGlobalFile.IsEmpty() && (style & wxCONFIG_USE_GLOBAL_FILE))
{
// TODO: What should the default global filename be?
m_strGlobalFile = "global";
}
// Check if styles are not supplied, but filenames are, in which case
// add the correct styles.
if (!m_strLocalFile.IsEmpty() && ((style & wxCONFIG_USE_LOCAL_FILE) != wxCONFIG_USE_LOCAL_FILE))
SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE);
if (!m_strGlobalFile.IsEmpty() && ((style & wxCONFIG_USE_GLOBAL_FILE) != wxCONFIG_USE_GLOBAL_FILE))
SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE);
// if the path is not absolute, prepend the standard directory to it
if ( !strLocal.IsEmpty() && !wxIsAbsolutePath(strLocal) )
{
m_strLocalFile = GetLocalDir();
m_strLocalFile << strLocal;
}
if ( !strGlobal.IsEmpty() && !wxIsAbsolutePath(strGlobal) )
{
m_strGlobalFile = GetGlobalDir();
m_strGlobalFile << strGlobal;
}
Init();
}
void wxFileConfig::CleanUp()
{
@@ -509,7 +560,7 @@ size_t wxFileConfig::GetNumberOfGroups(bool bRecursive) const
bool wxFileConfig::HasGroup(const wxString& strName) const
{
PathChanger path(this, strName);
wxConfigPathChanger path(this, strName);
ConfigGroup *pGroup = m_pCurrentGroup->FindSubgroup(path.Name());
return pGroup != NULL;
@@ -517,7 +568,7 @@ bool wxFileConfig::HasGroup(const wxString& strName) const
bool wxFileConfig::HasEntry(const wxString& strName) const
{
PathChanger path(this, strName);
wxConfigPathChanger path(this, strName);
ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
return pEntry != NULL;
@@ -527,50 +578,54 @@ bool wxFileConfig::HasEntry(const wxString& strName) const
// read/write values
// ----------------------------------------------------------------------------
bool wxFileConfig::Read(wxString *pstr,
const char *szKey,
const char *szDefault) const
bool wxFileConfig::Read(const wxString& key,
wxString* pStr) const
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
if (pEntry == NULL) {
return FALSE;
}
else {
*pStr = ExpandEnvVars(pEntry->Value());
return TRUE;
}
}
bool wxFileConfig::Read(const wxString& key,
wxString* pStr, const wxString& defVal) const
{
wxConfigPathChanger path(this, key);
ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
if (pEntry == NULL) {
if( IsRecordingDefaults() )
((wxFileConfig *)this)->Write(szKey,szDefault);
*pstr = ExpandEnvVars(szDefault);
((wxFileConfig *)this)->Write(key,defVal);
*pStr = ExpandEnvVars(defVal);
return FALSE;
}
else {
*pstr = ExpandEnvVars(pEntry->Value());
*pStr = ExpandEnvVars(pEntry->Value());
return TRUE;
}
}
const char *wxFileConfig::Read(const char *szKey,
const char *szDefault) const
{
static wxString s_str;
Read(&s_str, szKey, szDefault);
return s_str.c_str();
}
bool wxFileConfig::Read(long *pl, const char *szKey, long lDefault) const
bool wxFileConfig::Read(const wxString& key, long *pl) const
{
wxString str;
if ( Read(&str, szKey) ) {
if ( Read(key, & str) ) {
*pl = atol(str);
return TRUE;
}
else {
*pl = lDefault;
return FALSE;
}
}
bool wxFileConfig::Write(const char *szKey, const char *szValue)
bool wxFileConfig::Write(const wxString& key, const wxString& szValue)
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
wxString strName = path.Name();
if ( strName.IsEmpty() ) {
@@ -611,12 +666,12 @@ bool wxFileConfig::Write(const char *szKey, const char *szValue)
return TRUE;
}
bool wxFileConfig::Write(const char *szKey, long lValue)
bool wxFileConfig::Write(const wxString& key, long lValue)
{
// ltoa() is not ANSI :-(
char szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits)
sprintf(szBuf, "%ld", lValue);
return Write(szKey, szBuf);
return Write(key, szBuf);
}
bool wxFileConfig::Flush(bool /* bCurrentOnly */)
@@ -646,9 +701,9 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */)
// delete groups/entries
// ----------------------------------------------------------------------------
bool wxFileConfig::DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso)
bool wxFileConfig::DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso)
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
if ( !m_pCurrentGroup->DeleteEntry(path.Name()) )
return FALSE;
@@ -665,9 +720,9 @@ bool wxFileConfig::DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso)
return TRUE;
}
bool wxFileConfig::DeleteGroup(const char *szKey)
bool wxFileConfig::DeleteGroup(const wxString& key)
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
return m_pCurrentGroup->DeleteSubgroupByName(path.Name());
}

View File

@@ -25,6 +25,7 @@
#ifndef WX_PRECOMP
#include <wx/string.h>
#include <wx/intl.h>
#include <wx/app.h>
#endif //WX_PRECOMP
#include <wx/dynarray.h>
@@ -54,21 +55,47 @@
// ctor & dtor
// ----------------------------------------------------------------------------
wxIniConfig::wxIniConfig(const wxString& strAppName, const wxString& strVendor)
: m_strAppName(strAppName), m_strVendor(strVendor)
wxIniConfig::wxIniConfig(const wxString& strAppName, const wxString& strVendor,
const wxString& localFilename, const wxString& globalFilename, long style):
wxConfigBase(strAppName, strVendor, localFilename, globalFilename, style)
{
if ( strVendor.IsEmpty() )
m_strVendor = strAppName;
if ( GetAppName().IsEmpty() )
{
wxString app;
if (wxTheApp)
app = wxTheApp->GetAppName();
wxASSERT( !app.IsEmpty() );
SetAppName(app);
}
// append the extension if none given and it's not an absolute file name
// (otherwise we assume that they know what they're doing)
if ( !wxIsPathSeparator(m_strAppName[0u]) &&
m_strAppName.Find('.') == NOT_FOUND ) {
m_strAppName << ".ini";
}
// Vendor name is required in wxIniConfig.
// TODO: should it be required? Why isn't appName used instead? -- JACS
if ( GetVendorName().IsEmpty() )
{
wxString vendor;
if (wxTheApp)
vendor = wxTheApp->GetVendorName();
else
vendor = strAppName;
SetVendorName(vendor);
}
// set root path
SetPath("");
m_strLocalFilename = localFilename;
if (m_strLocalFilename.IsEmpty())
{
m_strLocalFilename = GetAppName() + ".ini";
}
// append the extension if none given and it's not an absolute file name
// (otherwise we assume that they know what they're doing)
if ( !wxIsPathSeparator(m_strLocalFilename[0u]) &&
m_strLocalFilename.Find('.') == NOT_FOUND )
{
m_strLocalFilename << ".ini";
}
// set root path
SetPath("");
}
wxIniConfig::~wxIniConfig()
@@ -142,7 +169,7 @@ const wxString& wxIniConfig::GetPath() const
return s_str;
}
wxString wxIniConfig::GetPrivateKeyName(const char *szKey) const
wxString wxIniConfig::GetPrivateKeyName(const wxString& szKey) const
{
wxString strKey;
@@ -154,7 +181,7 @@ wxString wxIniConfig::GetPrivateKeyName(const char *szKey) const
return strKey;
}
wxString wxIniConfig::GetKeyName(const char *szKey) const
wxString wxIniConfig::GetKeyName(const wxString& szKey) const
{
wxString strKey;
@@ -240,7 +267,7 @@ bool wxIniConfig::IsEmpty() const
char szBuf[1024];
GetPrivateProfileString(m_strGroup, NULL, "",
szBuf, WXSIZEOF(szBuf), m_strAppName);
szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
if ( !::IsEmpty(szBuf) )
return FALSE;
@@ -255,11 +282,9 @@ bool wxIniConfig::IsEmpty() const
// read/write
// ----------------------------------------------------------------------------
bool wxIniConfig::Read(wxString *pstr,
const char *szKey,
const char *szDefault) const
bool wxIniConfig::Read(const wxString& szKey, wxString *pstr) const
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, szKey);
wxString strKey = GetPrivateKeyName(path.Name());
char szBuf[1024]; // @@ should dynamically allocate memory...
@@ -268,7 +293,34 @@ bool wxIniConfig::Read(wxString *pstr,
// NB: the lpDefault param to GetPrivateProfileString can't be NULL
GetPrivateProfileString(m_strGroup, strKey, "",
szBuf, WXSIZEOF(szBuf), m_strAppName);
szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
if ( ::IsEmpty(szBuf) ) {
// now look in win.ini
wxString strKey = GetKeyName(path.Name());
GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
}
if ( ::IsEmpty(szBuf) ) {
return FALSE;
}
else {
return TRUE;
}
}
bool wxIniConfig::Read(const wxString& szKey, wxString *pstr,
const wxString& szDefault) const
{
wxConfigPathChanger path(this, szKey);
wxString strKey = GetPrivateKeyName(path.Name());
char szBuf[1024]; // @@ should dynamically allocate memory...
// first look in the private INI file
// NB: the lpDefault param to GetPrivateProfileString can't be NULL
GetPrivateProfileString(m_strGroup, strKey, "",
szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
if ( ::IsEmpty(szBuf) ) {
// now look in win.ini
wxString strKey = GetKeyName(path.Name());
@@ -284,18 +336,9 @@ bool wxIniConfig::Read(wxString *pstr,
}
}
const char *wxIniConfig::Read(const char *szKey,
const char *szDefault) const
bool wxIniConfig::Read(const wxString& szKey, long *pl) const
{
static wxString s_str;
Read(&s_str, szKey, szDefault);
return s_str.c_str();
}
bool wxIniConfig::Read(long *pl, const char *szKey, long lDefault) const
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, szKey);
wxString strKey = GetPrivateKeyName(path.Name());
// hack: we have no mean to know if it really found the default value or
@@ -303,7 +346,7 @@ bool wxIniConfig::Read(long *pl, const char *szKey, long lDefault) const
static const int nMagic = 17; // 17 is some "rare" number
static const int nMagic2 = 28; // arbitrary number != nMagic
long lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strAppName);
long lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic, m_strLocalFilename);
if ( lVal != nMagic ) {
// the value was read from the file
*pl = lVal;
@@ -311,7 +354,7 @@ bool wxIniConfig::Read(long *pl, const char *szKey, long lDefault) const
}
// is it really nMagic?
lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strAppName);
lVal = GetPrivateProfileInt(m_strGroup, strKey, nMagic2, m_strLocalFilename);
if ( lVal == nMagic ) {
// the nMagic it returned was indeed read from the file
*pl = lVal;
@@ -319,28 +362,18 @@ bool wxIniConfig::Read(long *pl, const char *szKey, long lDefault) const
}
// no, it was just returning the default value, so now look in win.ini
*pl = GetProfileInt(m_strVendor, GetKeyName(szKey), lDefault);
*pl = GetProfileInt(GetVendorName(), GetKeyName(szKey), *pl);
// we're not going to check here whether it read the default or not: it's
// not that important
return TRUE;
}
long wxIniConfig::Read(const char *szKey, long lDefault) const
bool wxIniConfig::Write(const wxString& szKey, const wxString& szValue)
{
long lVal;
Read(&lVal, szKey, lDefault);
return lVal;
}
bool wxIniConfig::Write(const char *szKey, const char *szValue)
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, szKey);
wxString strKey = GetPrivateKeyName(path.Name());
bool bOk = WritePrivateProfileString(m_strGroup, strKey,
szValue, m_strAppName) != 0;
szValue, m_strLocalFilename) != 0;
if ( !bOk )
wxLogLastError("WritePrivateProfileString");
@@ -348,7 +381,7 @@ bool wxIniConfig::Write(const char *szKey, const char *szValue)
return bOk;
}
bool wxIniConfig::Write(const char *szKey, long lValue)
bool wxIniConfig::Write(const wxString& szKey, long lValue)
{
// ltoa() is not ANSI :-(
char szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits)
@@ -360,7 +393,7 @@ bool wxIniConfig::Write(const char *szKey, long lValue)
bool wxIniConfig::Flush(bool /* bCurrentOnly */)
{
// this is just the way it works
return WritePrivateProfileString(NULL, NULL, NULL, m_strAppName) != 0;
return WritePrivateProfileString(NULL, NULL, NULL, m_strLocalFilename) != 0;
}
// ----------------------------------------------------------------------------
@@ -378,7 +411,7 @@ bool wxIniConfig::DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso)
// delete the current group too
bool bOk = WritePrivateProfileString(m_strGroup, NULL,
NULL, m_strAppName) != 0;
NULL, m_strLocalFilename) != 0;
if ( !bOk )
wxLogLastError("WritePrivateProfileString");
@@ -388,12 +421,12 @@ bool wxIniConfig::DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso)
bool wxIniConfig::DeleteGroup(const char *szKey)
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, szKey);
// passing NULL as section name to WritePrivateProfileString deletes the
// whole section according to the docs
bool bOk = WritePrivateProfileString(path.Name(), NULL,
NULL, m_strAppName) != 0;
NULL, m_strLocalFilename) != 0;
if ( !bOk )
wxLogLastError("WritePrivateProfileString");
@@ -404,7 +437,7 @@ bool wxIniConfig::DeleteGroup(const char *szKey)
bool wxIniConfig::DeleteAll()
{
// first delete our group in win.ini
WriteProfileString(m_strVendor, NULL, NULL);
WriteProfileString(GetVendorName(), NULL, NULL);
// then delete our own ini file
char szBuf[MAX_PATH];
@@ -415,7 +448,7 @@ bool wxIniConfig::DeleteAll()
wxFAIL_MSG("buffer is too small for Windows directory.");
wxString strFile = szBuf;
strFile << '\\' << m_strAppName;
strFile << '\\' << m_strLocalFilename;
if ( !DeleteFile(strFile) ) {
wxLogSysError(_("Can't delete the INI file '%s'"), strFile.c_str());

View File

@@ -158,6 +158,7 @@ MSWOBJS = \
$(MSWDIR)\helpwin.obj \
$(MSWDIR)\icon.obj \
$(MSWDIR)\imaglist.obj \
$(MSWDIR)\iniconf.obj \
$(MSWDIR)\joystick.obj \
$(MSWDIR)\listbox.obj \
$(MSWDIR)\listctrl.obj \
@@ -787,6 +788,11 @@ $(COMMDIR)/gdicmn.obj: $*.$(SRCSUFF)
$(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@
<<
$(COMMDIR)/iniconf.obj: $*.$(SRCSUFF)
cl @<<
$(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@
<<
$(COMMDIR)/intl.obj: $*.$(SRCSUFF)
cl @<<
$(CPPFLAGS) /c /Tp $*.$(SRCSUFF) /Fo$@

View File

@@ -35,6 +35,7 @@
#include <wx/string.h>
#endif //WX_PRECOMP
#include <wx/app.h>
#include <wx/log.h>
#include <wx/config.h>
#include <wx/msw/registry.h>
@@ -69,6 +70,8 @@ bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal)
// ----------------------------------------------------------------------------
// ctor/dtor
// ----------------------------------------------------------------------------
#if 0
wxRegConfig::wxRegConfig(const wxString& strRoot)
: m_keyLocalRoot(wxRegKey::HKCU, SOFTWARE_KEY + strRoot),
m_keyLocal(m_keyLocalRoot, ""),
@@ -84,6 +87,57 @@ wxRegConfig::wxRegConfig(const wxString& strRoot)
wxLogNull nolog;
m_keyGlobalRoot.Open();
}
#endif
// TODO: vendor name is ignored, because we can't yet do the test for optional vendor
// name in the constructor body. We need a wxRegKey::Set that takes the same
// args as the constructor. Then we'll set m_keyLocalRoot etc. in the constructor body.
wxRegConfig::wxRegConfig(const wxString& appName, const wxString& vendorName,
const wxString& strLocal, const wxString& strGlobal, long style)
: wxConfigBase(appName, vendorName, strLocal, strGlobal, style),
m_keyLocalRoot(wxRegKey::HKCU, SOFTWARE_KEY + appName),
m_keyLocal(m_keyLocalRoot, ""),
m_keyGlobalRoot(wxRegKey::HKLM, SOFTWARE_KEY + appName),
m_keyGlobal(m_keyGlobalRoot, "")
{
// TODO: really, we should check and supply an app name if one isn't supplied.
// Unfortunately I don't know how to initialise the member wxRegKey
// variables from within the constructor body. -- JACS
// Vadim - we just need an implementation of wxRegKey::Set,
// and then we can uncomment this and remove the constructor lines above.
/*
wxString strRoot(appName);
if (appName.IsEmpty() && wxTheApp)
{
strRoot = wxTheApp->GetAppName();
}
wxASSERT( !strRoot.IsEmpty() );
if (!vendorName.IsEmpty())
{
strRoot += "\\";
strRoot += vendorName;
}
m_keyLocalRoot.Set(wxRegKey::HKCU, SOFTWARE_KEY + strRoot),
m_keyLocal.Set(m_keyLocalRoot, ""),
m_keyGlobalRoot.Set(wxRegKey::HKLM, SOFTWARE_KEY + strRoot),
m_keyGlobal.Set(m_keyGlobalRoot, "")
*/
// Create() will Open() if key already exists
m_keyLocalRoot.Create();
// as it's the same key, Open() shouldn't fail (i.e. no need for Create())
m_keyLocal.Open();
wxLogNull nolog;
m_keyGlobalRoot.Open();
}
wxRegConfig::~wxRegConfig()
{
@@ -252,11 +306,47 @@ bool wxRegConfig::HasEntry(const wxString& strName) const
// reading/writing
// ----------------------------------------------------------------------------
bool wxRegConfig::Read(wxString *pStr,
const char *szKey,
const char *szDefault) const
bool wxRegConfig::Read(const wxString& key, wxString *pStr) const
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
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
if ( IsImmutable(path.Name()) ) {
if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) {
if ( m_keyLocal.HasValue(path.Name()) ) {
wxLogWarning("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 ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
(bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
// nothing to do
// TODO: do we return TRUE? Not in original implementation,
// but I don't see why not. -- JACS
*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;
@@ -280,11 +370,12 @@ bool wxRegConfig::Read(wxString *pStr,
// first try local key
if ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
(bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
// nothing to do
*pStr = wxConfigBase::ExpandEnvVars(*pStr);
return TRUE;
}
else {
if ( IsRecordingDefaults() ) {
((wxRegConfig*)this)->Write(szKey, szDefault);
((wxRegConfig*)this)->Write(key, szDefault);
}
// default value
@@ -296,9 +387,9 @@ bool wxRegConfig::Read(wxString *pStr,
return FALSE;
}
bool wxRegConfig::Read(long *plResult, const char *szKey, long lDefault) const
bool wxRegConfig::Read(const wxString& key, long *plResult) const
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
bool bQueryGlobal = TRUE;
@@ -324,15 +415,12 @@ bool wxRegConfig::Read(long *plResult, const char *szKey, long lDefault) const
(bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) {
return TRUE;
}
// default
*plResult = lDefault;
return FALSE;
}
bool wxRegConfig::Write(const char *szKey, const char *szValue)
bool wxRegConfig::Write(const wxString& key, const wxString& szValue)
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
if ( IsImmutable(path.Name()) ) {
wxLogError("Can't change immutable entry '%s'.", path.Name().c_str());
@@ -342,9 +430,9 @@ bool wxRegConfig::Write(const char *szKey, const char *szValue)
return m_keyLocal.SetValue(path.Name(), szValue);
}
bool wxRegConfig::Write(const char *szKey, long lValue)
bool wxRegConfig::Write(const wxString& key, long lValue)
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
if ( IsImmutable(path.Name()) ) {
wxLogError("Can't change immutable entry '%s'.", path.Name().c_str());
@@ -357,9 +445,9 @@ bool wxRegConfig::Write(const char *szKey, long lValue)
// ----------------------------------------------------------------------------
// deleting
// ----------------------------------------------------------------------------
bool wxRegConfig::DeleteEntry(const char *szValue, bool bGroupIfEmptyAlso)
bool wxRegConfig::DeleteEntry(const wxString& value, bool bGroupIfEmptyAlso)
{
PathChanger path(this, szValue);
wxConfigPathChanger path(this, value);
if ( !m_keyLocal.DeleteValue(path.Name()) )
return FALSE;
@@ -373,9 +461,9 @@ bool wxRegConfig::DeleteEntry(const char *szValue, bool bGroupIfEmptyAlso)
return TRUE;
}
bool wxRegConfig::DeleteGroup(const char *szKey)
bool wxRegConfig::DeleteGroup(const wxString& key)
{
PathChanger path(this, szKey);
wxConfigPathChanger path(this, key);
return m_keyLocal.DeleteKey(path.Name());
}