#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
// ----------------------------------------------------------------------------
// 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() { }
// 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;
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; }
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
#define classwxConfig classwxFileConfig
#endif
+
+
#endif // _WX_CONFIG_H_
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)
// 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();
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,
{ 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:
// 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
// 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);
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 '_'!)
};
{
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();
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:
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);
}
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)
}
#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()
{
wxString result;
m_config->SetPath("/General");
- m_config->Read(&result, "LastPlayer");
+ m_config->Read("LastPlayer", &result);
return result;
}
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))
{
#ifndef _SCOREFILE_H_
#define _SCOREFILE_H_
+#include <wx/config.h>
+
class wxConfig;
class ScoreFile {
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
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)
RegImageList();
};
+// array of children of the node
+struct TreeNode;
+WX_DEFINE_ARRAY(TreeNode *, TreeChildren);
+
// ----------------------------------------------------------------------------
// our control
// ----------------------------------------------------------------------------
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
{
#endif
#include <stdlib.h>
+#include <math.h>
#include <ctype.h> // for isalnum()
// ----------------------------------------------------------------------------
// 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;
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
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));
+ 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);
+}
- return s_szBuf;
+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;
}
}
-wxConfigBase::PathChanger::~PathChanger()
+wxConfigPathChanger::~wxConfigPathChanger()
{
// only restore path if it was changed
if ( m_bChanged ) {
}
}
+
#include <wx/intl.h>
#endif //WX_PRECOMP
+#include <wx/app.h>
#include <wx/dynarray.h>
#include <wx/file.h>
#include <wx/log.h>
}
}
+#if 0
wxFileConfig::wxFileConfig(const char *szAppName, bool bLocalOnly)
{
wxASSERT( !IsEmpty(szAppName) ); // invent a name for your application!
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()
{
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;
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;
// 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) {
- if( IsRecordingDefaults() )
- ((wxFileConfig *)this)->Write(szKey,szDefault);
- *pstr = ExpandEnvVars(szDefault);
return FALSE;
}
else {
- *pstr = ExpandEnvVars(pEntry->Value());
+ *pStr = ExpandEnvVars(pEntry->Value());
return TRUE;
}
}
-const char *wxFileConfig::Read(const char *szKey,
- const char *szDefault) const
+bool wxFileConfig::Read(const wxString& key,
+ wxString* pStr, const wxString& defVal) const
{
- static wxString s_str;
- Read(&s_str, szKey, szDefault);
+ wxConfigPathChanger path(this, key);
- return s_str.c_str();
+ ConfigEntry *pEntry = m_pCurrentGroup->FindEntry(path.Name());
+ if (pEntry == NULL) {
+ if( IsRecordingDefaults() )
+ ((wxFileConfig *)this)->Write(key,defVal);
+ *pStr = ExpandEnvVars(defVal);
+ return FALSE;
+ }
+ else {
+ *pStr = ExpandEnvVars(pEntry->Value());
+ return TRUE;
+ }
}
-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() ) {
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 */)
// 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;
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());
}
#ifndef WX_PRECOMP
#include <wx/string.h>
#include <wx/intl.h>
+ #include <wx/app.h>
#endif //WX_PRECOMP
#include <wx/dynarray.h>
// 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;
-
- // 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";
- }
+ if ( GetAppName().IsEmpty() )
+ {
+ wxString app;
+ if (wxTheApp)
+ app = wxTheApp->GetAppName();
+ wxASSERT( !app.IsEmpty() );
+ SetAppName(app);
+ }
- // set root path
- SetPath("");
+ // 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);
+ }
+
+ 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()
return s_str;
}
-wxString wxIniConfig::GetPrivateKeyName(const char *szKey) const
+wxString wxIniConfig::GetPrivateKeyName(const wxString& szKey) const
{
wxString strKey;
return strKey;
}
-wxString wxIniConfig::GetKeyName(const char *szKey) const
+wxString wxIniConfig::GetKeyName(const wxString& szKey) const
{
wxString strKey;
char szBuf[1024];
GetPrivateProfileString(m_strGroup, NULL, "",
- szBuf, WXSIZEOF(szBuf), m_strAppName);
+ szBuf, WXSIZEOF(szBuf), m_strLocalFilename);
if ( !::IsEmpty(szBuf) )
return FALSE;
// 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...
// 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());
}
if ( ::IsEmpty(szBuf) ) {
- *pstr = szDefault;
return FALSE;
}
else {
}
}
-const char *wxIniConfig::Read(const char *szKey,
- const char *szDefault) const
+bool wxIniConfig::Read(const wxString& szKey, wxString *pstr,
+ const wxString& szDefault) const
{
- static wxString s_str;
- Read(&s_str, szKey, szDefault);
+ 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());
+ GetProfileString(m_strGroup, strKey, "", szBuf, WXSIZEOF(szBuf));
+ }
- return s_str.c_str();
+ if ( ::IsEmpty(szBuf) ) {
+ *pstr = szDefault;
+ return FALSE;
+ }
+ else {
+ return TRUE;
+ }
}
-bool wxIniConfig::Read(long *pl, const char *szKey, long lDefault) const
+bool wxIniConfig::Read(const wxString& szKey, long *pl) 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
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;
}
// 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;
}
// 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
-{
- long lVal;
- Read(&lVal, szKey, lDefault);
-
- return lVal;
-}
-
-bool wxIniConfig::Write(const char *szKey, const char *szValue)
+bool wxIniConfig::Write(const wxString& szKey, const wxString& 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");
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)
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;
}
// ----------------------------------------------------------------------------
// delete the current group too
bool bOk = WritePrivateProfileString(m_strGroup, NULL,
- NULL, m_strAppName) != 0;
+ NULL, m_strLocalFilename) != 0;
if ( !bOk )
wxLogLastError("WritePrivateProfileString");
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");
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];
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());
$(MSWDIR)\helpwin.obj \
$(MSWDIR)\icon.obj \
$(MSWDIR)\imaglist.obj \
+ $(MSWDIR)\iniconf.obj \
$(MSWDIR)\joystick.obj \
$(MSWDIR)\listbox.obj \
$(MSWDIR)\listctrl.obj \
$(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$@
#include <wx/string.h>
#endif //WX_PRECOMP
+#include <wx/app.h>
#include <wx/log.h>
#include <wx/config.h>
#include <wx/msw/registry.h>
// ----------------------------------------------------------------------------
// ctor/dtor
// ----------------------------------------------------------------------------
+
+#if 0
wxRegConfig::wxRegConfig(const wxString& strRoot)
: m_keyLocalRoot(wxRegKey::HKCU, SOFTWARE_KEY + strRoot),
m_keyLocal(m_keyLocalRoot, ""),
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()
{
// 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;
wxLogWarning("User value for immutable key '%s' ignored.",
path.Name().c_str());
}
-
+ *pStr = wxConfigBase::ExpandEnvVars(*pStr);
return TRUE;
}
else {
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;
+
+ // 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());
+ }
+
+ 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)) ) {
+ *pStr = wxConfigBase::ExpandEnvVars(*pStr);
+ return TRUE;
}
else {
if ( IsRecordingDefaults() ) {
- ((wxRegConfig*)this)->Write(szKey, szDefault);
+ ((wxRegConfig*)this)->Write(key, szDefault);
}
// default value
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;
(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());
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());
// ----------------------------------------------------------------------------
// 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;
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());
}