]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/preferencedictionary.h
[dpkdir] revert *MapFileObservers() addition, fix #105
[xonotic/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 void importString( const char* string ){
43         m_importer( string );
44 }
45 void exportString( const StringImportCallback& importer ){
46         m_exporter( importer );
47 }
48 };
49
50 typedef std::map<CopiedString, PreferenceEntry> PreferenceEntries;
51 PreferenceEntries m_preferences;
52
53 typedef std::map<CopiedString, CopiedString> PreferenceCache;
54 PreferenceCache m_cache;
55
56 public:
57 typedef PreferenceEntries::iterator iterator;
58
59 iterator begin(){
60         return m_preferences.begin();
61 }
62 iterator end(){
63         return m_preferences.end();
64 }
65 iterator find( const char* name ){
66         return m_preferences.find( name );
67 }
68
69 void registerPreference( const char* name, const StringImportCallback& importer, const StringExportCallback& exporter ){
70         m_preferences.insert( PreferenceEntries::value_type( name, PreferenceEntry( importer, exporter ) ) );
71         PreferenceCache::iterator i = m_cache.find( name );
72         if ( i != m_cache.end() ) {
73                 importer( ( *i ).second.c_str() );
74                 m_cache.erase( i );
75         }
76 }
77
78 void importPref( const char* name, const char* value ){
79         PreferenceEntries::iterator i = m_preferences.find( name );
80         if ( i != m_preferences.end() ) {
81                 ( *i ).second.importString( value );
82         }
83         else
84         {
85                 m_cache.erase( name );
86                 m_cache.insert( PreferenceCache::value_type( name, value ) );
87         }
88 }
89 };
90
91 inline void XMLPreference_importString( XMLImporter& importer, const char* value ){
92         importer.write( value, string_length( value ) );
93 }
94 typedef ReferenceCaller1<XMLImporter, const char*, XMLPreference_importString> XMLPreferenceImportStringCaller;
95
96 class XMLPreferenceDictionaryExporter : public XMLExporter
97 {
98 class XMLQPrefElement : public XMLElement
99 {
100 const char* m_version;
101 public:
102 XMLQPrefElement( const char* version ) : m_version( version ){
103 }
104 const char* name() const {
105         return "qpref";
106 }
107 const char* attribute( const char* name ) const {
108         if ( string_equal( name, "version" ) ) {
109                 return m_version;
110         }
111         return "";
112 }
113 void forEachAttribute( XMLAttrVisitor& visitor ) const {
114         visitor.visit( "version", m_version );
115 }
116 };
117
118 class XMLPreferenceElement : public XMLElement
119 {
120 const char* m_name;
121 public:
122 XMLPreferenceElement( const char* name )
123         : m_name( name ){
124 }
125 const char* name() const {
126         return "epair";
127 }
128 const char* attribute( const char* name ) const {
129         if ( string_equal( name, "name" ) ) {
130                 return m_name;
131         }
132         return "";
133 }
134 void forEachAttribute( XMLAttrVisitor& visitor ) const {
135         visitor.visit( "name", m_name );
136 }
137 };
138
139 typedef PreferenceDictionary PreferenceEntries;
140 PreferenceEntries& m_preferences;
141 const char* m_version;
142 public:
143 XMLPreferenceDictionaryExporter( PreferenceDictionary& preferences, const char* version )
144         : m_preferences( preferences ), m_version( version ){
145 }
146
147 void exportXML( XMLImporter& importer ){
148         importer.write( "\n", 1 );
149
150         XMLQPrefElement qpref_element( m_version );
151         importer.pushElement( qpref_element );
152         importer.write( "\n", 1 );
153
154         for ( PreferenceEntries::iterator i = m_preferences.begin(); i != m_preferences.end(); ++i )
155         {
156                 XMLPreferenceElement epair_element( ( *i ).first.c_str() );
157
158                 importer.pushElement( epair_element );
159
160                 ( *i ).second.exportString( XMLPreferenceImportStringCaller( importer ) );
161
162                 importer.popElement( epair_element.name() );
163                 importer.write( "\n", 1 );
164         }
165
166         importer.popElement( qpref_element.name() );
167         importer.write( "\n", 1 );
168 }
169 };
170
171 class XMLPreferenceDictionaryImporter : public XMLImporter
172 {
173 struct xml_state_t
174 {
175         enum ETag
176         {
177                 tag_qpref,
178                 tag_qpref_ignore,
179                 tag_epair,
180                 tag_epair_ignore
181         };
182
183         xml_state_t( ETag tag )
184                 : m_tag( tag ){
185         }
186
187         ETag m_tag;
188         CopiedString m_name;
189         StringOutputStream m_ostream;
190 };
191
192 typedef std::vector<xml_state_t> xml_stack_t;
193 xml_stack_t m_xml_stack;
194
195 typedef PreferenceDictionary PreferenceEntries;
196 PreferenceEntries& m_preferences;
197 Version m_version;
198 public:
199 XMLPreferenceDictionaryImporter( PreferenceDictionary& preferences, const char* version )
200         : m_preferences( preferences ), m_version( version_parse( version ) ){
201 }
202
203 void pushElement( const XMLElement& element ){
204         if ( m_xml_stack.empty() ) {
205                 if ( string_equal( element.name(), "qpref" ) ) {
206                         Version dataVersion( version_parse( element.attribute( "version" ) ) );
207                         if ( !version_compatible( m_version, dataVersion ) ) {
208                                 globalOutputStream() << "qpref import: data version " << dataVersion << " is not compatible with code version " << m_version << "\n";
209                                 m_xml_stack.push_back( xml_state_t::tag_qpref_ignore );
210                         }
211                         else
212                         {
213                                 globalOutputStream() << "qpref import: data version " << dataVersion << " is compatible with code version " << m_version << "\n";
214                                 m_xml_stack.push_back( xml_state_t::tag_qpref );
215                         }
216                 }
217                 else
218                 {
219                         // not valid
220                 }
221         }
222         else
223         {
224                 switch ( m_xml_stack.back().m_tag )
225                 {
226                 case xml_state_t::tag_qpref:
227                         if ( string_equal( element.name(), "epair" ) ) {
228                                 m_xml_stack.push_back( xml_state_t::tag_epair );
229                                 m_xml_stack.back().m_name = element.attribute( "name" );
230                         }
231                         else
232                         {
233                                 // not valid
234                         }
235                         break;
236                 case xml_state_t::tag_qpref_ignore:
237                         if ( string_equal( element.name(), "epair" ) ) {
238                                 m_xml_stack.push_back( xml_state_t::tag_epair_ignore );
239                         }
240                         else
241                         {
242                                 // not valid
243                         }
244                         break;
245                 case xml_state_t::tag_epair:
246                 case xml_state_t::tag_epair_ignore:
247                         // not valid
248                         break;
249                 }
250         }
251
252 }
253 void popElement( const char* name ){
254         if ( m_xml_stack.back().m_tag == xml_state_t::tag_epair ) {
255                 m_preferences.importPref( m_xml_stack.back().m_name.c_str(), m_xml_stack.back().m_ostream.c_str() );
256         }
257         m_xml_stack.pop_back();
258 }
259 std::size_t write( const char* buffer, std::size_t length ){
260         return m_xml_stack.back().m_ostream.write( buffer, length );
261 }
262 };
263
264 #endif