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