confbase.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/confbase.h
  3. // Purpose: declaration of the base class of all config implementations
  4. // (see also: fileconf.h and msw/regconf.h and iniconf.h)
  5. // Author: Karsten Ballueder & Vadim Zeitlin
  6. // Modified by:
  7. // Created: 07.04.98 (adapted from appconf.h)
  8. // Copyright: (c) 1997 Karsten Ballueder Ballueder@usa.net
  9. // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  10. // Licence: wxWindows licence
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #ifndef _WX_CONFBASE_H_
  13. #define _WX_CONFBASE_H_
  14. #include "wx/defs.h"
  15. #include "wx/string.h"
  16. #include "wx/object.h"
  17. #include "wx/base64.h"
  18. class WXDLLIMPEXP_FWD_BASE wxArrayString;
  19. // ----------------------------------------------------------------------------
  20. // constants
  21. // ----------------------------------------------------------------------------
  22. /// shall we be case sensitive in parsing variable names?
  23. #ifndef wxCONFIG_CASE_SENSITIVE
  24. #define wxCONFIG_CASE_SENSITIVE 0
  25. #endif
  26. /// separates group and entry names (probably shouldn't be changed)
  27. #ifndef wxCONFIG_PATH_SEPARATOR
  28. #define wxCONFIG_PATH_SEPARATOR wxT('/')
  29. #endif
  30. /// introduces immutable entries
  31. // (i.e. the ones which can't be changed from the local config file)
  32. #ifndef wxCONFIG_IMMUTABLE_PREFIX
  33. #define wxCONFIG_IMMUTABLE_PREFIX wxT('!')
  34. #endif
  35. #if wxUSE_CONFIG
  36. /// should we use registry instead of configuration files under Windows?
  37. // (i.e. whether wxConfigBase::Create() will create a wxFileConfig (if it's
  38. // false) or wxRegConfig (if it's true and we're under Win32))
  39. #ifndef wxUSE_CONFIG_NATIVE
  40. #define wxUSE_CONFIG_NATIVE 1
  41. #endif
  42. // not all compilers can deal with template Read/Write() methods, define this
  43. // symbol if the template functions are available
  44. #if (!defined(__VISUALC__) || __VISUALC__ > 1200) && \
  45. !defined( __VMS ) && \
  46. !(defined(__HP_aCC) && defined(__hppa)) && \
  47. !defined (__DMC__)
  48. #define wxHAS_CONFIG_TEMPLATE_RW
  49. #endif
  50. // Style flags for constructor style parameter
  51. enum
  52. {
  53. wxCONFIG_USE_LOCAL_FILE = 1,
  54. wxCONFIG_USE_GLOBAL_FILE = 2,
  55. wxCONFIG_USE_RELATIVE_PATH = 4,
  56. wxCONFIG_USE_NO_ESCAPE_CHARACTERS = 8,
  57. wxCONFIG_USE_SUBDIR = 16
  58. };
  59. // ----------------------------------------------------------------------------
  60. // abstract base class wxConfigBase which defines the interface for derived
  61. // classes
  62. //
  63. // wxConfig organizes the items in a tree-like structure (modelled after the
  64. // Unix/Dos filesystem). There are groups (directories) and keys (files).
  65. // There is always one current group given by the current path.
  66. //
  67. // Keys are pairs "key_name = value" where value may be of string or integer
  68. // (long) type (TODO doubles and other types such as wxDate coming soon).
  69. // ----------------------------------------------------------------------------
  70. class WXDLLIMPEXP_BASE wxConfigBase : public wxObject
  71. {
  72. public:
  73. // constants
  74. // the type of an entry
  75. enum EntryType
  76. {
  77. Type_Unknown,
  78. Type_String,
  79. Type_Boolean,
  80. Type_Integer, // use Read(long *)
  81. Type_Float // use Read(double *)
  82. };
  83. // static functions
  84. // sets the config object, returns the previous pointer
  85. static wxConfigBase *Set(wxConfigBase *pConfig);
  86. // get the config object, creates it on demand unless DontCreateOnDemand
  87. // was called
  88. static wxConfigBase *Get(bool createOnDemand = true)
  89. { if ( createOnDemand && (!ms_pConfig) ) Create(); return ms_pConfig; }
  90. // create a new config object: this function will create the "best"
  91. // implementation of wxConfig available for the current platform, see
  92. // comments near definition wxUSE_CONFIG_NATIVE for details. It returns
  93. // the created object and also sets it as ms_pConfig.
  94. static wxConfigBase *Create();
  95. // should Get() try to create a new log object if the current one is NULL?
  96. static void DontCreateOnDemand() { ms_bAutoCreate = false; }
  97. // ctor & virtual dtor
  98. // ctor (can be used as default ctor too)
  99. //
  100. // Not all args will always be used by derived classes, but including
  101. // them all in each class ensures compatibility. If appName is empty,
  102. // uses wxApp name
  103. wxConfigBase(const wxString& appName = wxEmptyString,
  104. const wxString& vendorName = wxEmptyString,
  105. const wxString& localFilename = wxEmptyString,
  106. const wxString& globalFilename = wxEmptyString,
  107. long style = 0);
  108. // empty but ensures that dtor of all derived classes is virtual
  109. virtual ~wxConfigBase();
  110. // path management
  111. // set current path: if the first character is '/', it's the absolute path,
  112. // otherwise it's a relative path. '..' is supported. If the strPath
  113. // doesn't exist it is created.
  114. virtual void SetPath(const wxString& strPath) = 0;
  115. // retrieve the current path (always as absolute path)
  116. virtual const wxString& GetPath() const = 0;
  117. // enumeration: all functions here return false when there are no more items.
  118. // you must pass the same lIndex to GetNext and GetFirst (don't modify it)
  119. // enumerate subgroups
  120. virtual bool GetFirstGroup(wxString& str, long& lIndex) const = 0;
  121. virtual bool GetNextGroup (wxString& str, long& lIndex) const = 0;
  122. // enumerate entries
  123. virtual bool GetFirstEntry(wxString& str, long& lIndex) const = 0;
  124. virtual bool GetNextEntry (wxString& str, long& lIndex) const = 0;
  125. // get number of entries/subgroups in the current group, with or without
  126. // it's subgroups
  127. virtual size_t GetNumberOfEntries(bool bRecursive = false) const = 0;
  128. virtual size_t GetNumberOfGroups(bool bRecursive = false) const = 0;
  129. // tests of existence
  130. // returns true if the group by this name exists
  131. virtual bool HasGroup(const wxString& strName) const = 0;
  132. // same as above, but for an entry
  133. virtual bool HasEntry(const wxString& strName) const = 0;
  134. // returns true if either a group or an entry with a given name exist
  135. bool Exists(const wxString& strName) const
  136. { return HasGroup(strName) || HasEntry(strName); }
  137. // get the entry type
  138. virtual EntryType GetEntryType(const wxString& name) const
  139. {
  140. // by default all entries are strings
  141. return HasEntry(name) ? Type_String : Type_Unknown;
  142. }
  143. // key access: returns true if value was really read, false if default used
  144. // (and if the key is not found the default value is returned.)
  145. // read a string from the key
  146. bool Read(const wxString& key, wxString *pStr) const;
  147. bool Read(const wxString& key, wxString *pStr, const wxString& defVal) const;
  148. // read a number (long)
  149. bool Read(const wxString& key, long *pl) const;
  150. bool Read(const wxString& key, long *pl, long defVal) const;
  151. // read an int (wrapper around `long' version)
  152. bool Read(const wxString& key, int *pi) const;
  153. bool Read(const wxString& key, int *pi, int defVal) const;
  154. // read a double
  155. bool Read(const wxString& key, double* val) const;
  156. bool Read(const wxString& key, double* val, double defVal) const;
  157. // read a float
  158. bool Read(const wxString& key, float* val) const;
  159. bool Read(const wxString& key, float* val, float defVal) const;
  160. // read a bool
  161. bool Read(const wxString& key, bool* val) const;
  162. bool Read(const wxString& key, bool* val, bool defVal) const;
  163. #if wxUSE_BASE64
  164. // read a binary data block
  165. bool Read(const wxString& key, wxMemoryBuffer* data) const
  166. { return DoReadBinary(key, data); }
  167. // no default version since it does not make sense for binary data
  168. #endif // wxUSE_BASE64
  169. #ifdef wxHAS_CONFIG_TEMPLATE_RW
  170. // read other types, for which wxFromString is defined
  171. template <typename T>
  172. bool Read(const wxString& key, T* value) const
  173. {
  174. wxString s;
  175. if ( !Read(key, &s) )
  176. return false;
  177. return wxFromString(s, value);
  178. }
  179. template <typename T>
  180. bool Read(const wxString& key, T* value, const T& defVal) const
  181. {
  182. const bool found = Read(key, value);
  183. if ( !found )
  184. {
  185. if (IsRecordingDefaults())
  186. ((wxConfigBase *)this)->Write(key, defVal);
  187. *value = defVal;
  188. }
  189. return found;
  190. }
  191. #endif // wxHAS_CONFIG_TEMPLATE_RW
  192. // convenience functions returning directly the value
  193. wxString Read(const wxString& key,
  194. const wxString& defVal = wxEmptyString) const
  195. { wxString s; (void)Read(key, &s, defVal); return s; }
  196. // we have to provide a separate version for C strings as otherwise the
  197. // template Read() would be used
  198. wxString Read(const wxString& key, const char* defVal) const
  199. { return Read(key, wxString(defVal)); }
  200. wxString Read(const wxString& key, const wchar_t* defVal) const
  201. { return Read(key, wxString(defVal)); }
  202. long ReadLong(const wxString& key, long defVal) const
  203. { long l; (void)Read(key, &l, defVal); return l; }
  204. double ReadDouble(const wxString& key, double defVal) const
  205. { double d; (void)Read(key, &d, defVal); return d; }
  206. bool ReadBool(const wxString& key, bool defVal) const
  207. { bool b; (void)Read(key, &b, defVal); return b; }
  208. template <typename T>
  209. T ReadObject(const wxString& key, T const& defVal) const
  210. { T t; (void)Read(key, &t, defVal); return t; }
  211. // for compatibility with wx 2.8
  212. long Read(const wxString& key, long defVal) const
  213. { return ReadLong(key, defVal); }
  214. // write the value (return true on success)
  215. bool Write(const wxString& key, const wxString& value)
  216. { return DoWriteString(key, value); }
  217. bool Write(const wxString& key, long value)
  218. { return DoWriteLong(key, value); }
  219. bool Write(const wxString& key, double value)
  220. { return DoWriteDouble(key, value); }
  221. bool Write(const wxString& key, bool value)
  222. { return DoWriteBool(key, value); }
  223. #if wxUSE_BASE64
  224. bool Write(const wxString& key, const wxMemoryBuffer& buf)
  225. { return DoWriteBinary(key, buf); }
  226. #endif // wxUSE_BASE64
  227. // we have to provide a separate version for C strings as otherwise they
  228. // would be converted to bool and not to wxString as expected!
  229. bool Write(const wxString& key, const char *value)
  230. { return Write(key, wxString(value)); }
  231. bool Write(const wxString& key, const unsigned char *value)
  232. { return Write(key, wxString(value)); }
  233. bool Write(const wxString& key, const wchar_t *value)
  234. { return Write(key, wxString(value)); }
  235. // we also have to provide specializations for other types which we want to
  236. // handle using the specialized DoWriteXXX() instead of the generic template
  237. // version below
  238. bool Write(const wxString& key, char value)
  239. { return DoWriteLong(key, value); }
  240. bool Write(const wxString& key, unsigned char value)
  241. { return DoWriteLong(key, value); }
  242. bool Write(const wxString& key, short value)
  243. { return DoWriteLong(key, value); }
  244. bool Write(const wxString& key, unsigned short value)
  245. { return DoWriteLong(key, value); }
  246. bool Write(const wxString& key, unsigned int value)
  247. { return DoWriteLong(key, value); }
  248. bool Write(const wxString& key, int value)
  249. { return DoWriteLong(key, value); }
  250. bool Write(const wxString& key, unsigned long value)
  251. { return DoWriteLong(key, value); }
  252. bool Write(const wxString& key, float value)
  253. { return DoWriteDouble(key, value); }
  254. // Causes ambiguities in VC++ 6 and OpenVMS (at least)
  255. #if ( (!defined(__VISUALC__) || __VISUALC__ > 1200) && !defined( __VMS ) && !defined (__DMC__))
  256. // for other types, use wxToString()
  257. template <typename T>
  258. bool Write(const wxString& key, T const& value)
  259. { return Write(key, wxToString(value)); }
  260. #endif
  261. // permanently writes all changes
  262. virtual bool Flush(bool bCurrentOnly = false) = 0;
  263. // renaming, all functions return false on failure (probably because the new
  264. // name is already taken by an existing entry)
  265. // rename an entry
  266. virtual bool RenameEntry(const wxString& oldName,
  267. const wxString& newName) = 0;
  268. // rename a group
  269. virtual bool RenameGroup(const wxString& oldName,
  270. const wxString& newName) = 0;
  271. // delete entries/groups
  272. // deletes the specified entry and the group it belongs to if
  273. // it was the last key in it and the second parameter is true
  274. virtual bool DeleteEntry(const wxString& key,
  275. bool bDeleteGroupIfEmpty = true) = 0;
  276. // delete the group (with all subgroups)
  277. virtual bool DeleteGroup(const wxString& key) = 0;
  278. // delete the whole underlying object (disk file, registry key, ...)
  279. // primarily for use by uninstallation routine.
  280. virtual bool DeleteAll() = 0;
  281. // options
  282. // we can automatically expand environment variables in the config entries
  283. // (this option is on by default, you can turn it on/off at any time)
  284. bool IsExpandingEnvVars() const { return m_bExpandEnvVars; }
  285. void SetExpandEnvVars(bool bDoIt = true) { m_bExpandEnvVars = bDoIt; }
  286. // recording of default values
  287. void SetRecordDefaults(bool bDoIt = true) { m_bRecordDefaults = bDoIt; }
  288. bool IsRecordingDefaults() const { return m_bRecordDefaults; }
  289. // does expansion only if needed
  290. wxString ExpandEnvVars(const wxString& str) const;
  291. // misc accessors
  292. wxString GetAppName() const { return m_appName; }
  293. wxString GetVendorName() const { return m_vendorName; }
  294. // Used wxIniConfig to set members in constructor
  295. void SetAppName(const wxString& appName) { m_appName = appName; }
  296. void SetVendorName(const wxString& vendorName) { m_vendorName = vendorName; }
  297. void SetStyle(long style) { m_style = style; }
  298. long GetStyle() const { return m_style; }
  299. protected:
  300. static bool IsImmutable(const wxString& key)
  301. { return !key.IsEmpty() && key[0] == wxCONFIG_IMMUTABLE_PREFIX; }
  302. // return the path without trailing separator, if any: this should be called
  303. // to sanitize paths referring to the group names before passing them to
  304. // wxConfigPathChanger as "/foo/bar/" should be the same as "/foo/bar" and it
  305. // isn't interpreted in the same way by it (and this can't be changed there
  306. // as it's not the same for the entries names)
  307. static wxString RemoveTrailingSeparator(const wxString& key);
  308. // do read/write the values of different types
  309. virtual bool DoReadString(const wxString& key, wxString *pStr) const = 0;
  310. virtual bool DoReadLong(const wxString& key, long *pl) const = 0;
  311. virtual bool DoReadDouble(const wxString& key, double* val) const;
  312. virtual bool DoReadBool(const wxString& key, bool* val) const;
  313. #if wxUSE_BASE64
  314. virtual bool DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const = 0;
  315. #endif // wxUSE_BASE64
  316. virtual bool DoWriteString(const wxString& key, const wxString& value) = 0;
  317. virtual bool DoWriteLong(const wxString& key, long value) = 0;
  318. virtual bool DoWriteDouble(const wxString& key, double value);
  319. virtual bool DoWriteBool(const wxString& key, bool value);
  320. #if wxUSE_BASE64
  321. virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf) = 0;
  322. #endif // wxUSE_BASE64
  323. private:
  324. // are we doing automatic environment variable expansion?
  325. bool m_bExpandEnvVars;
  326. // do we record default values?
  327. bool m_bRecordDefaults;
  328. // static variables
  329. static wxConfigBase *ms_pConfig;
  330. static bool ms_bAutoCreate;
  331. // Application name and organisation name
  332. wxString m_appName;
  333. wxString m_vendorName;
  334. // Style flag
  335. long m_style;
  336. DECLARE_ABSTRACT_CLASS(wxConfigBase)
  337. };
  338. // a handy little class which changes current path to the path of given entry
  339. // and restores it in dtor: so if you declare a local variable of this type,
  340. // you work in the entry directory and the path is automatically restored
  341. // when the function returns
  342. // Taken out of wxConfig since not all compilers can cope with nested classes.
  343. class WXDLLIMPEXP_BASE wxConfigPathChanger
  344. {
  345. public:
  346. // ctor/dtor do path changing/restoring of the path
  347. wxConfigPathChanger(const wxConfigBase *pContainer, const wxString& strEntry);
  348. ~wxConfigPathChanger();
  349. // get the key name
  350. const wxString& Name() const { return m_strName; }
  351. // this method must be called if the original path (i.e. the current path at
  352. // the moment of creation of this object) could have been deleted to prevent
  353. // us from restoring the not existing (any more) path
  354. //
  355. // if the original path doesn't exist any more, the path will be restored to
  356. // the deepest still existing component of the old path
  357. void UpdateIfDeleted();
  358. private:
  359. wxConfigBase *m_pContainer; // object we live in
  360. wxString m_strName, // name of entry (i.e. name only)
  361. m_strOldPath; // saved path
  362. bool m_bChanged; // was the path changed?
  363. wxDECLARE_NO_COPY_CLASS(wxConfigPathChanger);
  364. };
  365. #endif // wxUSE_CONFIG
  366. /*
  367. Replace environment variables ($SOMETHING) with their values. The format is
  368. $VARNAME or ${VARNAME} where VARNAME contains alphanumeric characters and
  369. '_' only. '$' must be escaped ('\$') in order to be taken literally.
  370. */
  371. WXDLLIMPEXP_BASE wxString wxExpandEnvVars(const wxString &sz);
  372. /*
  373. Split path into parts removing '..' in progress
  374. */
  375. WXDLLIMPEXP_BASE void wxSplitPath(wxArrayString& aParts, const wxString& path);
  376. #endif // _WX_CONFBASE_H_