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);
95 m_cache.insert(PreferenceCache::value_type(name, value));
100 inline void XMLPreference_importString(XMLImporter& importer, const char* value)
102 importer.write(value, string_length(value));
104 typedef ReferenceCaller1<XMLImporter, const char*, XMLPreference_importString> XMLPreferenceImportStringCaller;
106 class XMLPreferenceDictionaryExporter : public XMLExporter
108 class XMLQPrefElement : public XMLElement
110 const char* m_version;
112 XMLQPrefElement(const char* version) : m_version(version)
115 const char* name() const
119 const char* attribute(const char* name) const
121 if(string_equal(name, "version"))
127 void forEachAttribute(XMLAttrVisitor& visitor) const
129 visitor.visit("version", m_version);
133 class XMLPreferenceElement : public XMLElement
137 XMLPreferenceElement(const char* name)
141 const char* name() const
145 const char* attribute(const char* name) const
147 if(string_equal(name, "name"))
151 void forEachAttribute(XMLAttrVisitor& visitor) const
153 visitor.visit("name", m_name);
157 typedef PreferenceDictionary PreferenceEntries;
158 PreferenceEntries& m_preferences;
159 const char* m_version;
161 XMLPreferenceDictionaryExporter(PreferenceDictionary& preferences, const char* version)
162 : m_preferences(preferences), m_version(version)
166 void exportXML(XMLImporter& importer)
168 importer.write("\n", 1);
170 XMLQPrefElement qpref_element(m_version);
171 importer.pushElement(qpref_element);
172 importer.write("\n", 1);
174 for(PreferenceEntries::iterator i = m_preferences.begin(); i != m_preferences.end(); ++i)
176 XMLPreferenceElement epair_element((*i).first.c_str());
178 importer.pushElement(epair_element);
180 (*i).second.exportString(XMLPreferenceImportStringCaller(importer));
182 importer.popElement(epair_element.name());
183 importer.write("\n", 1);
186 importer.popElement(qpref_element.name());
187 importer.write("\n", 1);
191 class XMLPreferenceDictionaryImporter : public XMLImporter
203 xml_state_t(ETag tag)
210 StringOutputStream m_ostream;
213 typedef std::vector<xml_state_t> xml_stack_t;
214 xml_stack_t m_xml_stack;
216 typedef PreferenceDictionary PreferenceEntries;
217 PreferenceEntries& m_preferences;
220 XMLPreferenceDictionaryImporter(PreferenceDictionary& preferences, const char* version)
221 : m_preferences(preferences), m_version(version_parse(version))
225 void pushElement(const XMLElement& element)
227 if(m_xml_stack.empty())
229 if(string_equal(element.name(), "qpref"))
231 Version dataVersion(version_parse(element.attribute("version")));
232 if(!version_compatible(m_version, dataVersion))
234 globalOutputStream() << "qpref import: data version " << dataVersion << " is not compatible with code version " << m_version << "\n";
235 m_xml_stack.push_back(xml_state_t::tag_qpref_ignore);
239 globalOutputStream() << "qpref import: data version " << dataVersion << " is compatible with code version " << m_version << "\n";
240 m_xml_stack.push_back(xml_state_t::tag_qpref);
250 switch(m_xml_stack.back().m_tag)
252 case xml_state_t::tag_qpref:
253 if(string_equal(element.name(), "epair"))
255 m_xml_stack.push_back(xml_state_t::tag_epair);
256 m_xml_stack.back().m_name = element.attribute("name");
263 case xml_state_t::tag_qpref_ignore:
264 if(string_equal(element.name(), "epair"))
266 m_xml_stack.push_back(xml_state_t::tag_epair_ignore);
273 case xml_state_t::tag_epair:
274 case xml_state_t::tag_epair_ignore:
281 void popElement(const char* name)
283 if(m_xml_stack.back().m_tag == xml_state_t::tag_epair)
285 m_preferences.importPref(m_xml_stack.back().m_name.c_str(), m_xml_stack.back().m_ostream.c_str());
287 m_xml_stack.pop_back();
289 std::size_t write(const char* buffer, std::size_t length)
291 return m_xml_stack.back().m_ostream.write(buffer, length);