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 Property<const char *> m_cb;
38 PreferenceEntry( const Property<const char *>& cb )
41 void importString( const char* string ){
44 void exportString( const Callback<void(const char *)> & importer ){
49 typedef std::map<CopiedString, PreferenceEntry> PreferenceEntries;
50 PreferenceEntries m_preferences;
52 typedef std::map<CopiedString, CopiedString> PreferenceCache;
53 PreferenceCache m_cache;
56 typedef PreferenceEntries::iterator iterator;
59 return m_preferences.begin();
62 return m_preferences.end();
64 iterator find( const char* name ){
65 return m_preferences.find( name );
68 void registerPreference( const char* name, const Property<const char *>& cb ){
69 m_preferences.insert( PreferenceEntries::value_type( name, PreferenceEntry( cb ) ) );
70 PreferenceCache::iterator i = m_cache.find( name );
71 if ( i != m_cache.end() ) {
72 cb.set( i->second.c_str() );
77 void importPref( const char* name, const char* value ){
78 PreferenceEntries::iterator i = m_preferences.find( name );
79 if ( i != m_preferences.end() ) {
80 ( *i ).second.importString( value );
84 m_cache.erase( name );
85 m_cache.insert( PreferenceCache::value_type( name, value ) );
90 inline void XMLPreference_importString( XMLImporter& importer, const char* value ){
91 importer.write( value, string_length( value ) );
93 typedef ReferenceCaller<XMLImporter, void(const char*), XMLPreference_importString> XMLPreferenceImportStringCaller;
95 class XMLPreferenceDictionaryExporter : public XMLExporter
97 class XMLQPrefElement : public XMLElement
99 const char* m_version;
101 XMLQPrefElement( const char* version ) : m_version( version ){
103 const char* name() const {
106 const char* attribute( const char* name ) const {
107 if ( string_equal( name, "version" ) ) {
112 void forEachAttribute( XMLAttrVisitor& visitor ) const {
113 visitor.visit( "version", m_version );
117 class XMLPreferenceElement : public XMLElement
121 XMLPreferenceElement( const char* name )
124 const char* name() const {
127 const char* attribute( const char* name ) const {
128 if ( string_equal( name, "name" ) ) {
133 void forEachAttribute( XMLAttrVisitor& visitor ) const {
134 visitor.visit( "name", m_name );
138 typedef PreferenceDictionary PreferenceEntries;
139 PreferenceEntries& m_preferences;
140 const char* m_version;
142 XMLPreferenceDictionaryExporter( PreferenceDictionary& preferences, const char* version )
143 : m_preferences( preferences ), m_version( version ){
146 void exportXML( XMLImporter& importer ){
147 importer.write( "\n", 1 );
149 XMLQPrefElement qpref_element( m_version );
150 importer.pushElement( qpref_element );
151 importer.write( "\n", 1 );
153 for ( PreferenceEntries::iterator i = m_preferences.begin(); i != m_preferences.end(); ++i )
155 XMLPreferenceElement epair_element( ( *i ).first.c_str() );
157 importer.pushElement( epair_element );
159 ( *i ).second.exportString( XMLPreferenceImportStringCaller( importer ) );
161 importer.popElement( epair_element.name() );
162 importer.write( "\n", 1 );
165 importer.popElement( qpref_element.name() );
166 importer.write( "\n", 1 );
170 class XMLPreferenceDictionaryImporter : public XMLImporter
182 xml_state_t( ETag tag )
188 StringOutputStream m_ostream;
191 typedef std::vector<xml_state_t> xml_stack_t;
192 xml_stack_t m_xml_stack;
194 typedef PreferenceDictionary PreferenceEntries;
195 PreferenceEntries& m_preferences;
198 XMLPreferenceDictionaryImporter( PreferenceDictionary& preferences, const char* version )
199 : m_preferences( preferences ), m_version( version_parse( version ) ){
202 void pushElement( const XMLElement& element ){
203 if ( m_xml_stack.empty() ) {
204 if ( string_equal( element.name(), "qpref" ) ) {
205 Version dataVersion( version_parse( element.attribute( "version" ) ) );
206 if ( !version_compatible( m_version, dataVersion ) ) {
207 globalOutputStream() << "qpref import: data version " << dataVersion << " is not compatible with code version " << m_version << "\n";
208 m_xml_stack.push_back( xml_state_t::tag_qpref_ignore );
212 globalOutputStream() << "qpref import: data version " << dataVersion << " is compatible with code version " << m_version << "\n";
213 m_xml_stack.push_back( xml_state_t::tag_qpref );
223 switch ( m_xml_stack.back().m_tag )
225 case xml_state_t::tag_qpref:
226 if ( string_equal( element.name(), "epair" ) ) {
227 m_xml_stack.push_back( xml_state_t::tag_epair );
228 m_xml_stack.back().m_name = element.attribute( "name" );
235 case xml_state_t::tag_qpref_ignore:
236 if ( string_equal( element.name(), "epair" ) ) {
237 m_xml_stack.push_back( xml_state_t::tag_epair_ignore );
244 case xml_state_t::tag_epair:
245 case xml_state_t::tag_epair_ignore:
252 void popElement( const char* name ){
253 if ( m_xml_stack.back().m_tag == xml_state_t::tag_epair ) {
254 m_preferences.importPref( m_xml_stack.back().m_name.c_str(), m_xml_stack.back().m_ostream.c_str() );
256 m_xml_stack.pop_back();
258 std::size_t write( const char* buffer, std::size_t length ){
259 return m_xml_stack.back().m_ostream.write( buffer, length );