2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_PREFERENCEDICTIONARY_H)
23 #define INCLUDED_PREFERENCEDICTIONARY_H
25 #include "preferencesystem.h"
27 #include "stream/stringstream.h"
28 #include "generic/callback.h"
29 #include "versionlib.h"
32 class PreferenceDictionary : public PreferenceSystem
36 StringImportCallback m_importer;
37 StringExportCallback m_exporter;
39 PreferenceEntry(const StringImportCallback& importer, const StringExportCallback& exporter)
40 : m_importer(importer), m_exporter(exporter)
43 void importString(const char* string)
47 void exportString(const StringImportCallback& importer)
53 typedef std::map<CopiedString, PreferenceEntry> PreferenceEntries;
54 PreferenceEntries m_preferences;
56 typedef std::map<CopiedString, CopiedString> PreferenceCache;
57 PreferenceCache m_cache;
60 typedef PreferenceEntries::iterator iterator;
64 return m_preferences.begin();
68 return m_preferences.end();
70 iterator find(const char* name)
72 return m_preferences.find(name);
75 void registerPreference(const char* name, const StringImportCallback& importer, const StringExportCallback& exporter)
77 m_preferences.insert(PreferenceEntries::value_type(name, PreferenceEntry(importer, exporter)));
78 PreferenceCache::iterator i = m_cache.find(name);
79 if(i != m_cache.end())
81 importer((*i).second.c_str());
86 void importPref(const char* name, const char* value)
88 PreferenceEntries::iterator i = m_preferences.find(name);
89 if(i != m_preferences.end())
91 (*i).second.importString(value);
96 m_cache.insert(PreferenceCache::value_type(name, value));
101 inline void XMLPreference_importString(XMLImporter& importer, const char* value)
103 importer.write(value, string_length(value));
105 typedef ReferenceCaller1<XMLImporter, const char*, XMLPreference_importString> XMLPreferenceImportStringCaller;
107 class XMLPreferenceDictionaryExporter : public XMLExporter
109 class XMLQPrefElement : public XMLElement
111 const char* m_version;
113 XMLQPrefElement(const char* version) : m_version(version)
116 const char* name() const
120 const char* attribute(const char* name) const
122 if(string_equal(name, "version"))
128 void forEachAttribute(XMLAttrVisitor& visitor) const
130 visitor.visit("version", m_version);
134 class XMLPreferenceElement : public XMLElement
138 XMLPreferenceElement(const char* name)
142 const char* name() const
146 const char* attribute(const char* name) const
148 if(string_equal(name, "name"))
152 void forEachAttribute(XMLAttrVisitor& visitor) const
154 visitor.visit("name", m_name);
158 typedef PreferenceDictionary PreferenceEntries;
159 PreferenceEntries& m_preferences;
160 const char* m_version;
162 XMLPreferenceDictionaryExporter(PreferenceDictionary& preferences, const char* version)
163 : m_preferences(preferences), m_version(version)
167 void exportXML(XMLImporter& importer)
169 importer.write("\n", 1);
171 XMLQPrefElement qpref_element(m_version);
172 importer.pushElement(qpref_element);
173 importer.write("\n", 1);
175 for(PreferenceEntries::iterator i = m_preferences.begin(); i != m_preferences.end(); ++i)
177 XMLPreferenceElement epair_element((*i).first.c_str());
179 importer.pushElement(epair_element);
181 (*i).second.exportString(XMLPreferenceImportStringCaller(importer));
183 importer.popElement(epair_element.name());
184 importer.write("\n", 1);
187 importer.popElement(qpref_element.name());
188 importer.write("\n", 1);
192 class XMLPreferenceDictionaryImporter : public XMLImporter
204 xml_state_t(ETag tag)
211 StringOutputStream m_ostream;
214 typedef std::vector<xml_state_t> xml_stack_t;
215 xml_stack_t m_xml_stack;
217 typedef PreferenceDictionary PreferenceEntries;
218 PreferenceEntries& m_preferences;
221 XMLPreferenceDictionaryImporter(PreferenceDictionary& preferences, const char* version)
222 : m_preferences(preferences), m_version(version_parse(version))
226 void pushElement(const XMLElement& element)
228 if(m_xml_stack.empty())
230 if(string_equal(element.name(), "qpref"))
232 Version dataVersion(version_parse(element.attribute("version")));
233 if(!version_compatible(m_version, dataVersion))
235 globalOutputStream() << "qpref import: data version " << dataVersion << " is not compatible with code version " << m_version << "\n";
236 m_xml_stack.push_back(xml_state_t::tag_qpref_ignore);
240 globalOutputStream() << "qpref import: data version " << dataVersion << " is compatible with code version " << m_version << "\n";
241 m_xml_stack.push_back(xml_state_t::tag_qpref);
251 switch(m_xml_stack.back().m_tag)
253 case xml_state_t::tag_qpref:
254 if(string_equal(element.name(), "epair"))
256 m_xml_stack.push_back(xml_state_t::tag_epair);
257 m_xml_stack.back().m_name = element.attribute("name");
264 case xml_state_t::tag_qpref_ignore:
265 if(string_equal(element.name(), "epair"))
267 m_xml_stack.push_back(xml_state_t::tag_epair_ignore);
274 case xml_state_t::tag_epair:
275 case xml_state_t::tag_epair_ignore:
282 void popElement(const char* name)
284 if(m_xml_stack.back().m_tag == xml_state_t::tag_epair)
286 m_preferences.importPref(m_xml_stack.back().m_name.c_str(), m_xml_stack.back().m_ostream.c_str());
288 m_xml_stack.pop_back();
290 std::size_t write(const char* buffer, std::size_t length)
292 return m_xml_stack.back().m_ostream.write(buffer, length);