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 #define wxCONFIG_WIN32_NATIVE TRUE
#endif #endif
// Style flags for constructor style parameter
#define wxCONFIG_USE_LOCAL_FILE 1
#define wxCONFIG_USE_GLOBAL_FILE 2
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// various helper global functions // various helper global functions
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -101,7 +105,17 @@ public:
// ctor & virtual dtor // ctor & virtual dtor
// environment variable expansion is on by default // 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 // empty but ensures that dtor of all derived classes is virtual
virtual ~wxConfigBase() { } virtual ~wxConfigBase() { }
@@ -138,31 +152,44 @@ public:
// key access: returns TRUE if value was really read, FALSE if default used // 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.) // (and if the key is not found the default value is returned.)
// read a string from the key // read a string from the key
virtual bool Read(wxString *pStr, const char *szKey, virtual bool Read(const wxString& key, wxString *pStr) const = 0;
const char *szDefault = (const char *) NULL) const = 0; virtual bool Read(const wxString& key, wxString *pStr, const wxString& defVal) const;
// another version using statis buffer - it means it will be overwritten
// after each call to this function! virtual wxString Read(const wxString& key, const wxString& defVal) const;
virtual const char *Read(const char *szKey,
const char *szDefault = (const char *) NULL) const; virtual bool Read(const wxString& key, long *pl) const = 0;
// the same for longs virtual bool Read(const wxString& key, long *pl, long defVal) const;
virtual long Read(const char *szKey, long lDefault) const
{ long l; Read(&l, szKey, lDefault); return l; } virtual long Read(const wxString& strKey, long defVal) const
// and another version: returns true if default value is returned { long l; Read(strKey, &l, defVal); return l; }
virtual bool Read(long *pl, const char *szKey, long lDefault = 0) const = 0;
// 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) // write the value (return true on success)
virtual bool Write(const char *szKey, const char *szValue) = 0; virtual bool Write(const wxString& key, const wxString& value) = 0;
virtual bool Write(const char *szKey, long lValue) = 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 // permanently writes all changes
virtual bool Flush(bool bCurrentOnly = FALSE) = 0; virtual bool Flush(bool bCurrentOnly = FALSE) = 0;
// delete entries/groups // delete entries/groups
// deletes the specified entry and the group it belongs to if // deletes the specified entry and the group it belongs to if
// it was the last key in it and the second parameter is true // 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; bool bDeleteGroupIfEmpty = TRUE) = 0;
// delete the group (with all subgroups) // 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, ...) // delete the whole underlying object (disk file, registry key, ...)
// primarly for use by desinstallation routine. // primarly for use by desinstallation routine.
virtual bool DeleteAll() = 0; virtual bool DeleteAll() = 0;
@@ -186,20 +213,49 @@ public:
return tmp; 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: protected:
static bool IsImmutable(const char *szKey) static bool IsImmutable(const wxString& key)
{ return *szKey == wxCONFIG_IMMUTABLE_PREFIX; } { 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 // 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, // 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 // you work in the entry directory and the path is automatically restored
// when the function returns // when the function returns
class PathChanger // Taken out of wxConfig since not all compilers can cope with nested classes.
class wxConfigPathChanger
{ {
public: public:
// ctor/dtor do path changing/restorin // ctor/dtor do path changing/restorin
PathChanger(const wxConfigBase *pContainer, const wxString& strEntry); wxConfigPathChanger(const wxConfigBase *pContainer, const wxString& strEntry);
~PathChanger(); ~wxConfigPathChanger();
// get the key name // get the key name
const wxString& Name() const { return m_strName; } const wxString& Name() const { return m_strName; }
@@ -211,16 +267,6 @@ protected:
bool m_bChanged; // was the path changed? 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 // the native wxConfigBase implementation
@@ -240,5 +286,7 @@ private:
#define classwxConfig classwxFileConfig #define classwxConfig classwxFileConfig
#endif #endif
#endif // _WX_CONFIG_H_ #endif // _WX_CONFIG_H_

View File

@@ -112,6 +112,8 @@ public:
static wxString GetLocalFileName(const char *szFile); static wxString GetLocalFileName(const char *szFile);
// ctor & dtor // ctor & dtor
#if 0
// the names of local and global (if not disabled) config files are // the names of local and global (if not disabled) config files are
// constructed using Get{Local|Global}FileName functions described above // constructed using Get{Local|Global}FileName functions described above
// (szAppName is just the (short) name of your application) // (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 // directory). If either of strings is empty, the corresponding file is not
// used. // used.
wxFileConfig(const wxString& strLocal, const wxString& strGlobal); 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 // dtor will save unsaved data
virtual ~wxFileConfig(); virtual ~wxFileConfig();
@@ -141,6 +151,7 @@ public:
virtual bool HasGroup(const wxString& strName) const; virtual bool HasGroup(const wxString& strName) const;
virtual bool HasEntry(const wxString& strName) const; virtual bool HasEntry(const wxString& strName) const;
#if 0
virtual bool Read(wxString *pstr, const char *szKey, virtual bool Read(wxString *pstr, const char *szKey,
const char *szDefault = 0) const; const char *szDefault = 0) const;
virtual const char *Read(const char *szKey, virtual const char *Read(const char *szKey,
@@ -150,10 +161,31 @@ public:
{ return wxConfigBase::Read(szKey, lDefault); } { return wxConfigBase::Read(szKey, lDefault); }
virtual bool Write(const char *szKey, const char *szValue); virtual bool Write(const char *szKey, const char *szValue);
virtual bool Write(const char *szKey, long lValue); 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 Flush(bool bCurrentOnly = FALSE);
virtual bool DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso); virtual bool DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso);
virtual bool DeleteGroup(const char *szKey); virtual bool DeleteGroup(const wxString& szKey);
virtual bool DeleteAll(); virtual bool DeleteAll();
public: public:

View File

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

View File

@@ -28,8 +28,11 @@ class wxRegConfig : public wxConfigBase
{ {
public: public:
// ctor & dtor // ctor & dtor
// will store data in HKLM\strRegHive and HKCU\strRegHive // will store data in HKLM\appName and HKCU\appName
wxRegConfig(const wxString& strRegHive); 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 // dtor will save unsaved data
virtual ~wxRegConfig(); virtual ~wxRegConfig();
@@ -57,16 +60,30 @@ public:
virtual size_t GetNumberOfGroups(bool bRecursive = FALSE) const; virtual size_t GetNumberOfGroups(bool bRecursive = FALSE) const;
// read/write // read/write
virtual bool Read(wxString *pStr, const char *szKey, bool Read(const wxString& key, wxString *pStr) const;
const char *szDefault = 0) const; bool Read(const wxString& key, wxString *pStr, const wxString& szDefault) const;
virtual bool Read(long *result, const char *szKey, long lDefault = 0) const; bool Read(const wxString& key, long *plResult) const;
virtual bool Write(const char *szKey, const char *szValue);
virtual bool Write(const char *szKey, long Value); // 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; } virtual bool Flush(bool /* bCurrentOnly = FALSE */ ) { return TRUE; }
// delete // delete
virtual bool DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso); virtual bool DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso);
virtual bool DeleteGroup(const char *szKey); virtual bool DeleteGroup(const wxString& key);
virtual bool DeleteAll(); virtual bool DeleteAll();
private: private:

View File

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

View File

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

View File

@@ -13,6 +13,8 @@
#ifndef _SCOREFILE_H_ #ifndef _SCOREFILE_H_
#define _SCOREFILE_H_ #define _SCOREFILE_H_
#include <wx/config.h>
class wxConfig; class wxConfig;
class ScoreFile { class ScoreFile {
@@ -28,8 +30,8 @@ public:
private: private:
long CalcCheck(const char* name, int p1, int p2, int p3); long CalcCheck(const char* name, int p1, int p2, int p3);
wxString m_configFilename; wxString m_configFilename;
wxConfig* m_config; wxConfig* m_config;
}; };
#endif #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_VIEW, MyFrame::OnSmallIconView)
EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView) EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll) EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnSelectAll) EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
END_EVENT_TABLE() END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl) BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)

View File

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

View File

@@ -50,6 +50,7 @@
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <math.h>
#include <ctype.h> // for isalnum() #include <ctype.h> // for isalnum()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -67,6 +68,15 @@ bool wxConfigBase::ms_bAutoCreate = TRUE;
// wxConfigBase // 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 *wxConfigBase::Set(wxConfigBase *pConfig)
{ {
wxConfigBase *pOld = ms_pConfig; wxConfigBase *pOld = ms_pConfig;
@@ -80,8 +90,7 @@ wxConfigBase *wxConfigBase::Create()
ms_pConfig = ms_pConfig =
#if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE) #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
#ifdef __WIN32__ #ifdef __WIN32__
new wxRegConfig(wxTheApp->GetVendorName() + '\\' new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
+ wxTheApp->GetAppName());
#else //WIN16 #else //WIN16
new wxIniConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName()); new wxIniConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
#endif #endif
@@ -93,21 +102,101 @@ wxConfigBase *wxConfigBase::Create()
return ms_pConfig; 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; wxString s;
Read(&s, szKey, szDefault); Read(key, &s, defVal);
strncpy(s_szBuf, s, WXSIZEOF(s_szBuf)); return s;
return s_szBuf;
} }
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) const wxString& strEntry)
{ {
m_pContainer = (wxConfigBase *)pContainer; 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 // only restore path if it was changed
if ( m_bChanged ) { if ( m_bChanged ) {
@@ -293,3 +382,4 @@ void wxSplitPath(wxArrayString& aParts, const char *sz)
} }
} }

View File

@@ -32,6 +32,7 @@
#include <wx/intl.h> #include <wx/intl.h>
#endif //WX_PRECOMP #endif //WX_PRECOMP
#include <wx/app.h>
#include <wx/dynarray.h> #include <wx/dynarray.h>
#include <wx/file.h> #include <wx/file.h>
#include <wx/log.h> #include <wx/log.h>
@@ -207,6 +208,7 @@ void wxFileConfig::Init()
} }
} }
#if 0
wxFileConfig::wxFileConfig(const char *szAppName, bool bLocalOnly) wxFileConfig::wxFileConfig(const char *szAppName, bool bLocalOnly)
{ {
wxASSERT( !IsEmpty(szAppName) ); // invent a name for your application! wxASSERT( !IsEmpty(szAppName) ); // invent a name for your application!
@@ -237,6 +239,55 @@ wxFileConfig::wxFileConfig(const wxString& strLocal, const wxString& strGlobal)
Init(); 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() void wxFileConfig::CleanUp()
{ {
@@ -509,7 +560,7 @@ size_t wxFileConfig::GetNumberOfGroups(bool bRecursive) const
bool wxFileConfig::HasGroup(const wxString& strName) const bool wxFileConfig::HasGroup(const wxString& strName) const
{ {
PathChanger path(this, strName); wxConfigPathChanger path(this, strName);
ConfigGroup *pGroup = m_pCurrentGroup->FindSubgroup(path.Name()); ConfigGroup *pGroup = m_pCurrentGroup->FindSubgroup(path.Name());
return pGroup != NULL; return pGroup != NULL;
@@ -517,7 +568,7 @@ bool wxFileConfig::HasGroup(const wxString& strName) const
bool wxFileConfig::HasEntry(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()); ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
return pEntry != NULL; return pEntry != NULL;
@@ -527,50 +578,54 @@ bool wxFileConfig::HasEntry(const wxString& strName) const
// read/write values // read/write values
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool wxFileConfig::Read(wxString *pstr, bool wxFileConfig::Read(const wxString& key,
const char *szKey, wxString* pStr) const
const char *szDefault) 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()); ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
if (pEntry == NULL) { if (pEntry == NULL) {
if( IsRecordingDefaults() ) if( IsRecordingDefaults() )
((wxFileConfig *)this)->Write(szKey,szDefault); ((wxFileConfig *)this)->Write(key,defVal);
*pstr = ExpandEnvVars(szDefault); *pStr = ExpandEnvVars(defVal);
return FALSE; return FALSE;
} }
else { else {
*pstr = ExpandEnvVars(pEntry->Value()); *pStr = ExpandEnvVars(pEntry->Value());
return TRUE; return TRUE;
} }
} }
const char *wxFileConfig::Read(const char *szKey, bool wxFileConfig::Read(const wxString& key, long *pl) const
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
{ {
wxString str; wxString str;
if ( Read(&str, szKey) ) { if ( Read(key, & str) ) {
*pl = atol(str); *pl = atol(str);
return TRUE; return TRUE;
} }
else { else {
*pl = lDefault;
return FALSE; 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(); wxString strName = path.Name();
if ( strName.IsEmpty() ) { if ( strName.IsEmpty() ) {
@@ -611,12 +666,12 @@ bool wxFileConfig::Write(const char *szKey, const char *szValue)
return TRUE; return TRUE;
} }
bool wxFileConfig::Write(const char *szKey, long lValue) bool wxFileConfig::Write(const wxString& key, long lValue)
{ {
// ltoa() is not ANSI :-( // ltoa() is not ANSI :-(
char szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits) char szBuf[40]; // should be good for sizeof(long) <= 16 (128 bits)
sprintf(szBuf, "%ld", lValue); sprintf(szBuf, "%ld", lValue);
return Write(szKey, szBuf); return Write(key, szBuf);
} }
bool wxFileConfig::Flush(bool /* bCurrentOnly */) bool wxFileConfig::Flush(bool /* bCurrentOnly */)
@@ -646,9 +701,9 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */)
// delete groups/entries // 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()) ) if ( !m_pCurrentGroup->DeleteEntry(path.Name()) )
return FALSE; return FALSE;
@@ -665,9 +720,9 @@ bool wxFileConfig::DeleteEntry(const char *szKey, bool bGroupIfEmptyAlso)
return TRUE; 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()); return m_pCurrentGroup->DeleteSubgroupByName(path.Name());
} }

View File

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

View File

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

View File

@@ -35,6 +35,7 @@
#include <wx/string.h> #include <wx/string.h>
#endif //WX_PRECOMP #endif //WX_PRECOMP
#include <wx/app.h>
#include <wx/log.h> #include <wx/log.h>
#include <wx/config.h> #include <wx/config.h>
#include <wx/msw/registry.h> #include <wx/msw/registry.h>
@@ -69,6 +70,8 @@ bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// ctor/dtor // ctor/dtor
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#if 0
wxRegConfig::wxRegConfig(const wxString& strRoot) wxRegConfig::wxRegConfig(const wxString& strRoot)
: m_keyLocalRoot(wxRegKey::HKCU, SOFTWARE_KEY + strRoot), : m_keyLocalRoot(wxRegKey::HKCU, SOFTWARE_KEY + strRoot),
m_keyLocal(m_keyLocalRoot, ""), m_keyLocal(m_keyLocalRoot, ""),
@@ -84,6 +87,57 @@ wxRegConfig::wxRegConfig(const wxString& strRoot)
wxLogNull nolog; wxLogNull nolog;
m_keyGlobalRoot.Open(); 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() wxRegConfig::~wxRegConfig()
{ {
@@ -252,11 +306,47 @@ bool wxRegConfig::HasEntry(const wxString& strName) const
// reading/writing // reading/writing
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool wxRegConfig::Read(wxString *pStr, bool wxRegConfig::Read(const wxString& key, wxString *pStr) const
const char *szKey,
const char *szDefault) 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; bool bQueryGlobal = TRUE;
@@ -280,11 +370,12 @@ bool wxRegConfig::Read(wxString *pStr,
// first try local key // first try local key
if ( TryGetValue(m_keyLocal, path.Name(), *pStr) || if ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
(bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) { (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
// nothing to do *pStr = wxConfigBase::ExpandEnvVars(*pStr);
return TRUE;
} }
else { else {
if ( IsRecordingDefaults() ) { if ( IsRecordingDefaults() ) {
((wxRegConfig*)this)->Write(szKey, szDefault); ((wxRegConfig*)this)->Write(key, szDefault);
} }
// default value // default value
@@ -296,9 +387,9 @@ bool wxRegConfig::Read(wxString *pStr,
return FALSE; 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; 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)) ) { (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) {
return TRUE; return TRUE;
} }
// default
*plResult = lDefault;
return FALSE; 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()) ) { if ( IsImmutable(path.Name()) ) {
wxLogError("Can't change immutable entry '%s'.", path.Name().c_str()); 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); 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()) ) { if ( IsImmutable(path.Name()) ) {
wxLogError("Can't change immutable entry '%s'.", path.Name().c_str()); wxLogError("Can't change immutable entry '%s'.", path.Name().c_str());
@@ -357,9 +445,9 @@ bool wxRegConfig::Write(const char *szKey, long lValue)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// deleting // 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()) ) if ( !m_keyLocal.DeleteValue(path.Name()) )
return FALSE; return FALSE;
@@ -373,9 +461,9 @@ bool wxRegConfig::DeleteEntry(const char *szValue, bool bGroupIfEmptyAlso)
return TRUE; 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()); return m_keyLocal.DeleteKey(path.Name());
} }