]> icculus.org git repositories - divverent/netradiant.git/blob - radiant/preferencedictionary.h
always use color normalization, vector normalization on colors is worng
[divverent/netradiant.git] / radiant / preferencedictionary.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #if !defined(INCLUDED_PREFERENCEDICTIONARY_H)
23 #define INCLUDED_PREFERENCEDICTIONARY_H
24
25 #include "preferencesystem.h"
26 #include "xml/ixml.h"
27 #include "stream/stringstream.h"
28 #include "generic/callback.h"
29 #include "versionlib.h"
30 #include <map>
31
32 class PreferenceDictionary : public PreferenceSystem
33 {
34   class PreferenceEntry
35   {
36     StringImportCallback m_importer;
37     StringExportCallback m_exporter;
38   public:
39     PreferenceEntry(const StringImportCallback& importer, const StringExportCallback& exporter)
40       : m_importer(importer), m_exporter(exporter)
41     {
42     }
43     void importString(const char* string)
44     {
45       m_importer(string);
46     }
47     void exportString(const StringImportCallback& importer)
48     {
49       m_exporter(importer);
50     }
51   };
52
53   typedef std::map<CopiedString, PreferenceEntry> PreferenceEntries;
54   PreferenceEntries m_preferences;
55
56   typedef std::map<CopiedString, CopiedString> PreferenceCache;
57   PreferenceCache m_cache;
58
59 public:
60   typedef PreferenceEntries::iterator iterator;
61
62   iterator begin()
63   {
64     return m_preferences.begin();
65   }
66   iterator end()
67   {
68     return m_preferences.end();
69   }
70   iterator find(const char* name)
71   {
72     return m_preferences.find(name);
73   }
74
75   void registerPreference(const char* name, const StringImportCallback& importer, const StringExportCallback& exporter)
76   {
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())
80     {
81       importer((*i).second.c_str());
82       m_cache.erase(i);
83     }
84   }
85
86   void importPref(const char* name, const char* value)
87   {
88     PreferenceEntries::iterator i = m_preferences.find(name);
89     if(i != m_preferences.end())
90     {
91       (*i).second.importString(value);
92     }
93     else
94     {
95       m_cache.erase(name);
96       m_cache.insert(PreferenceCache::value_type(name, value));
97     }
98   }
99 };
100
101 inline void XMLPreference_importString(XMLImporter& importer, const char* value)
102 {
103   importer.write(value, string_length(value));
104 }
105 typedef ReferenceCaller1<XMLImporter, const char*, XMLPreference_importString> XMLPreferenceImportStringCaller;
106
107 class XMLPreferenceDictionaryExporter : public XMLExporter
108 {
109   class XMLQPrefElement : public XMLElement
110   {
111     const char* m_version;
112   public:
113     XMLQPrefElement(const char* version) : m_version(version)
114     {
115     }
116     const char* name() const
117     {
118       return "qpref";
119     }
120     const char* attribute(const char* name) const
121     {
122       if(string_equal(name, "version"))
123       {
124         return m_version;
125       }
126       return "";
127     }
128     void forEachAttribute(XMLAttrVisitor& visitor) const
129     {
130       visitor.visit("version", m_version);
131     }
132   };
133     
134   class XMLPreferenceElement : public XMLElement
135   {
136     const char* m_name;
137   public:
138     XMLPreferenceElement(const char* name)
139       : m_name(name)
140     {
141     }
142     const char* name() const
143     {
144       return "epair";
145     }
146     const char* attribute(const char* name) const
147     {
148       if(string_equal(name, "name"))
149         return m_name;
150       return "";
151     }
152     void forEachAttribute(XMLAttrVisitor& visitor) const
153     {
154       visitor.visit("name", m_name);
155     }
156   };
157   
158   typedef PreferenceDictionary PreferenceEntries;
159   PreferenceEntries& m_preferences;
160   const char* m_version;
161 public:
162   XMLPreferenceDictionaryExporter(PreferenceDictionary& preferences, const char* version)
163     : m_preferences(preferences), m_version(version)
164   {
165   }
166
167   void exportXML(XMLImporter& importer)
168   {
169     importer.write("\n", 1);
170
171     XMLQPrefElement qpref_element(m_version);
172     importer.pushElement(qpref_element);
173     importer.write("\n", 1);
174
175     for(PreferenceEntries::iterator i = m_preferences.begin(); i != m_preferences.end(); ++i)
176     {
177       XMLPreferenceElement epair_element((*i).first.c_str());
178
179       importer.pushElement(epair_element);
180
181       (*i).second.exportString(XMLPreferenceImportStringCaller(importer));
182
183       importer.popElement(epair_element.name());
184       importer.write("\n", 1);
185     }
186
187     importer.popElement(qpref_element.name());
188     importer.write("\n", 1);
189   }
190 };
191
192 class XMLPreferenceDictionaryImporter : public XMLImporter
193 {
194   struct xml_state_t
195   {
196     enum ETag
197     {
198       tag_qpref,
199       tag_qpref_ignore,
200       tag_epair,
201       tag_epair_ignore
202     };
203
204     xml_state_t(ETag tag)
205       : m_tag(tag)
206     {
207     }
208
209     ETag m_tag;
210     CopiedString m_name;
211     StringOutputStream m_ostream;
212   };
213
214   typedef std::vector<xml_state_t> xml_stack_t;
215   xml_stack_t m_xml_stack;
216
217   typedef PreferenceDictionary PreferenceEntries;
218   PreferenceEntries& m_preferences;
219   Version m_version;
220 public:
221   XMLPreferenceDictionaryImporter(PreferenceDictionary& preferences, const char* version)
222     : m_preferences(preferences), m_version(version_parse(version))
223   {
224   }
225
226   void pushElement(const XMLElement& element)
227   {
228     if(m_xml_stack.empty())
229     {
230       if(string_equal(element.name(), "qpref"))
231       {
232         Version dataVersion(version_parse(element.attribute("version")));
233         if(!version_compatible(m_version, dataVersion))
234         {
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);
237         }
238         else
239         {
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);
242         }
243       }
244       else
245       {
246         // not valid
247       }
248     }
249     else
250     {
251       switch(m_xml_stack.back().m_tag)
252       {
253       case xml_state_t::tag_qpref:
254         if(string_equal(element.name(), "epair"))
255         {
256           m_xml_stack.push_back(xml_state_t::tag_epair);
257           m_xml_stack.back().m_name = element.attribute("name");
258         }
259         else
260         {
261           // not valid
262         }
263         break;
264       case xml_state_t::tag_qpref_ignore:
265         if(string_equal(element.name(), "epair"))
266         {
267           m_xml_stack.push_back(xml_state_t::tag_epair_ignore);
268         }
269         else
270         {
271           // not valid
272         }
273         break;
274       case xml_state_t::tag_epair:
275       case xml_state_t::tag_epair_ignore:
276         // not valid
277         break;
278       }
279     }
280
281   }
282   void popElement(const char* name)
283   {
284     if(m_xml_stack.back().m_tag == xml_state_t::tag_epair)
285     {
286       m_preferences.importPref(m_xml_stack.back().m_name.c_str(), m_xml_stack.back().m_ostream.c_str());
287     }
288     m_xml_stack.pop_back();
289   }
290   std::size_t write(const char* buffer, std::size_t length)
291   {
292     return m_xml_stack.back().m_ostream.write(buffer, length);
293   }
294 };
295
296 #endif