fileconf.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/fileconf.h
  3. // Purpose: wxFileConfig derivation of wxConfigBase
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 07.04.98 (adapted from appconf.cpp)
  7. // Copyright: (c) 1997 Karsten Ballueder & Vadim Zeitlin
  8. // Ballueder@usa.net <zeitlin@dptmaths.ens-cachan.fr>
  9. // Licence: wxWindows licence
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef _FILECONF_H
  12. #define _FILECONF_H
  13. #include "wx/defs.h"
  14. #if wxUSE_CONFIG
  15. #include "wx/textfile.h"
  16. #include "wx/string.h"
  17. #include "wx/confbase.h"
  18. #include "wx/filename.h"
  19. // ----------------------------------------------------------------------------
  20. // wxFileConfig
  21. // ----------------------------------------------------------------------------
  22. /*
  23. wxFileConfig derives from base Config and implements file based config class,
  24. i.e. it uses ASCII disk files to store the information. These files are
  25. alternatively called INI, .conf or .rc in the documentation. They are
  26. organized in groups or sections, which can nest (i.e. a group contains
  27. subgroups, which contain their own subgroups &c). Each group has some
  28. number of entries, which are "key = value" pairs. More precisely, the format
  29. is:
  30. # comments are allowed after either ';' or '#' (Win/UNIX standard)
  31. # blank lines (as above) are ignored
  32. # global entries are members of special (no name) top group
  33. written_for = Windows
  34. platform = Linux
  35. # the start of the group 'Foo'
  36. [Foo] # may put comments like this also
  37. # following 3 lines are entries
  38. key = value
  39. another_key = " strings with spaces in the beginning should be quoted, \
  40. otherwise the spaces are lost"
  41. last_key = but you don't have to put " normally (nor quote them, like here)
  42. # subgroup of the group 'Foo'
  43. # (order is not important, only the name is: separator is '/', as in paths)
  44. [Foo/Bar]
  45. # entries prefixed with "!" are immutable, i.e. can't be changed if they are
  46. # set in the system-wide config file
  47. !special_key = value
  48. bar_entry = whatever
  49. [Foo/Bar/Fubar] # depth is (theoretically :-) unlimited
  50. # may have the same name as key in another section
  51. bar_entry = whatever not
  52. You have {read/write/delete}Entry functions (guess what they do) and also
  53. setCurrentPath to select current group. enum{Subgroups/Entries} allow you
  54. to get all entries in the config file (in the current group). Finally,
  55. flush() writes immediately all changed entries to disk (otherwise it would
  56. be done automatically in dtor)
  57. wxFileConfig manages not less than 2 config files for each program: global
  58. and local (or system and user if you prefer). Entries are read from both of
  59. them and the local entries override the global ones unless the latter is
  60. immutable (prefixed with '!') in which case a warning message is generated
  61. and local value is ignored. Of course, the changes are always written to local
  62. file only.
  63. The names of these files can be specified in a number of ways. First of all,
  64. you can use the standard convention: using the ctor which takes 'strAppName'
  65. parameter will probably be sufficient for 90% of cases. If, for whatever
  66. reason you wish to use the files with some other names, you can always use the
  67. second ctor.
  68. wxFileConfig also may automatically expand the values of environment variables
  69. in the entries it reads: for example, if you have an entry
  70. score_file = $HOME/.score
  71. a call to Read(&str, "score_file") will return a complete path to .score file
  72. unless the expansion was previously disabled with SetExpandEnvVars(false) call
  73. (it's on by default, the current status can be retrieved with
  74. IsExpandingEnvVars function).
  75. */
  76. class WXDLLIMPEXP_FWD_BASE wxFileConfigGroup;
  77. class WXDLLIMPEXP_FWD_BASE wxFileConfigEntry;
  78. class WXDLLIMPEXP_FWD_BASE wxFileConfigLineList;
  79. #if wxUSE_STREAMS
  80. class WXDLLIMPEXP_FWD_BASE wxInputStream;
  81. class WXDLLIMPEXP_FWD_BASE wxOutputStream;
  82. #endif // wxUSE_STREAMS
  83. class WXDLLIMPEXP_BASE wxFileConfig : public wxConfigBase
  84. {
  85. public:
  86. // construct the "standard" full name for global (system-wide) and
  87. // local (user-specific) config files from the base file name.
  88. //
  89. // the following are the filenames returned by this functions:
  90. // global local
  91. // Unix /etc/file.ext ~/.file
  92. // Win %windir%\file.ext %USERPROFILE%\file.ext
  93. //
  94. // where file is the basename of szFile, ext is its extension
  95. // or .conf (Unix) or .ini (Win) if it has none
  96. static wxFileName GetGlobalFile(const wxString& szFile);
  97. static wxFileName GetLocalFile(const wxString& szFile, int style = 0);
  98. static wxString GetGlobalFileName(const wxString& szFile)
  99. {
  100. return GetGlobalFile(szFile).GetFullPath();
  101. }
  102. static wxString GetLocalFileName(const wxString& szFile, int style = 0)
  103. {
  104. return GetLocalFile(szFile, style).GetFullPath();
  105. }
  106. // ctor & dtor
  107. // New constructor: one size fits all. Specify wxCONFIG_USE_LOCAL_FILE or
  108. // wxCONFIG_USE_GLOBAL_FILE to say which files should be used.
  109. wxFileConfig(const wxString& appName = wxEmptyString,
  110. const wxString& vendorName = wxEmptyString,
  111. const wxString& localFilename = wxEmptyString,
  112. const wxString& globalFilename = wxEmptyString,
  113. long style = wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE,
  114. const wxMBConv& conv = wxConvAuto());
  115. #if wxUSE_STREAMS
  116. // ctor that takes an input stream.
  117. wxFileConfig(wxInputStream &inStream, const wxMBConv& conv = wxConvAuto());
  118. #endif // wxUSE_STREAMS
  119. // dtor will save unsaved data
  120. virtual ~wxFileConfig();
  121. // under Unix, set the umask to be used for the file creation, do nothing
  122. // under other systems
  123. #ifdef __UNIX__
  124. void SetUmask(int mode) { m_umask = mode; }
  125. #else // !__UNIX__
  126. void SetUmask(int WXUNUSED(mode)) { }
  127. #endif // __UNIX__/!__UNIX__
  128. // implement inherited pure virtual functions
  129. virtual void SetPath(const wxString& strPath);
  130. virtual const wxString& GetPath() const;
  131. virtual bool GetFirstGroup(wxString& str, long& lIndex) const;
  132. virtual bool GetNextGroup (wxString& str, long& lIndex) const;
  133. virtual bool GetFirstEntry(wxString& str, long& lIndex) const;
  134. virtual bool GetNextEntry (wxString& str, long& lIndex) const;
  135. virtual size_t GetNumberOfEntries(bool bRecursive = false) const;
  136. virtual size_t GetNumberOfGroups(bool bRecursive = false) const;
  137. virtual bool HasGroup(const wxString& strName) const;
  138. virtual bool HasEntry(const wxString& strName) const;
  139. virtual bool Flush(bool bCurrentOnly = false);
  140. virtual bool RenameEntry(const wxString& oldName, const wxString& newName);
  141. virtual bool RenameGroup(const wxString& oldName, const wxString& newName);
  142. virtual bool DeleteEntry(const wxString& key, bool bGroupIfEmptyAlso = true);
  143. virtual bool DeleteGroup(const wxString& szKey);
  144. virtual bool DeleteAll();
  145. // additional, wxFileConfig-specific, functionality
  146. #if wxUSE_STREAMS
  147. // save the entire config file text to the given stream, note that the text
  148. // won't be saved again in dtor when Flush() is called if you use this method
  149. // as it won't be "changed" any more
  150. virtual bool Save(wxOutputStream& os, const wxMBConv& conv = wxConvAuto());
  151. #endif // wxUSE_STREAMS
  152. public:
  153. // functions to work with this list
  154. wxFileConfigLineList *LineListAppend(const wxString& str);
  155. wxFileConfigLineList *LineListInsert(const wxString& str,
  156. wxFileConfigLineList *pLine); // NULL => Prepend()
  157. void LineListRemove(wxFileConfigLineList *pLine);
  158. bool LineListIsEmpty();
  159. protected:
  160. virtual bool DoReadString(const wxString& key, wxString *pStr) const;
  161. virtual bool DoReadLong(const wxString& key, long *pl) const;
  162. #if wxUSE_BASE64
  163. virtual bool DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const;
  164. #endif // wxUSE_BASE64
  165. virtual bool DoWriteString(const wxString& key, const wxString& szValue);
  166. virtual bool DoWriteLong(const wxString& key, long lValue);
  167. #if wxUSE_BASE64
  168. virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf);
  169. #endif // wxUSE_BASE64
  170. private:
  171. // GetXXXFileName helpers: return ('/' terminated) directory names
  172. static wxString GetGlobalDir();
  173. static wxString GetLocalDir(int style = 0);
  174. // common part of all ctors (assumes that m_str{Local|Global}File are already
  175. // initialized
  176. void Init();
  177. // common part of from dtor and DeleteAll
  178. void CleanUp();
  179. // parse the whole file
  180. void Parse(const wxTextBuffer& buffer, bool bLocal);
  181. // the same as SetPath("/")
  182. void SetRootPath();
  183. // real SetPath() implementation, returns true if path could be set or false
  184. // if path doesn't exist and createMissingComponents == false
  185. bool DoSetPath(const wxString& strPath, bool createMissingComponents);
  186. // set/test the dirty flag
  187. void SetDirty() { m_isDirty = true; }
  188. void ResetDirty() { m_isDirty = false; }
  189. bool IsDirty() const { return m_isDirty; }
  190. // member variables
  191. // ----------------
  192. wxFileConfigLineList *m_linesHead, // head of the linked list
  193. *m_linesTail; // tail
  194. wxFileName m_fnLocalFile, // local file name passed to ctor
  195. m_fnGlobalFile; // global
  196. wxString m_strPath; // current path (not '/' terminated)
  197. wxFileConfigGroup *m_pRootGroup, // the top (unnamed) group
  198. *m_pCurrentGroup; // the current group
  199. wxMBConv *m_conv;
  200. #ifdef __UNIX__
  201. int m_umask; // the umask to use for file creation
  202. #endif // __UNIX__
  203. bool m_isDirty; // if true, we have unsaved changes
  204. wxDECLARE_NO_COPY_CLASS(wxFileConfig);
  205. DECLARE_ABSTRACT_CLASS(wxFileConfig)
  206. };
  207. #endif
  208. // wxUSE_CONFIG
  209. #endif
  210. //_FILECONF_H