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
24 #include "preferencesystem.h"
25 #include "preferencedictionary.h"
27 #include "xml/xmlparser.h"
28 #include "xml/xmlwriter.h"
31 void LoadPrefs( PreferenceDictionary& preferences, const char* filename ){
32 TextFileInputStream file( filename );
33 if ( !file.failed() ) {
34 XMLStreamParser parser( file );
35 XMLPreferenceDictionaryImporter importer( preferences );
36 parser.exportXML( importer );
44 void SavePrefs( PreferenceDictionary& preferences, const char* filename ){
45 TextFileOutputStream file( filename );
46 if ( !file.failed() ) {
47 XMLStreamWriter writer( file );
48 XMLPreferenceDictionaryExporter exporter( preferences, "1" );
49 exporter.exportXML( writer );
58 class StringPreference
64 virtual void onChanged() = 0;
68 CopiedString m_string;
71 StringPreference( Observer& observer )
72 : m_observer( observer ){
74 void importString( const char* value ){
76 m_observer.onChanged();
78 typedef MemberCaller<StringPreference, void(const char*), &StringPreference::importString> ImportStringCaller;
79 void exportString( Callback<void(const char *)>& importer ){
80 importer( m_string.c_str() );
82 typedef MemberCaller<StringPreference, void(Callback<void(const char *)>&), &StringPreference::exportString> ExportStringCaller;
85 inline void int_export( int i, Callback<void(const char *)>& importer ){
87 sprintf( buffer, "%d", i );
91 inline int int_import( const char* value ){
101 virtual void onChanged() = 0;
106 Observer& m_observer;
109 IntPreference( Observer& observer )
110 : m_observer( observer ){
112 void importString( const char* value ){
113 m_int = int_import( value );
114 m_observer.onChanged();
116 typedef MemberCaller<IntPreference, void(const char*), &IntPreference::importString> ImportStringCaller;
117 void exportString( Callback<void(const char *)>& importer ){
118 int_export( m_int, importer );
120 typedef MemberCaller<IntPreference, void(Callback<void(const char *)>&), &IntPreference::exportString> ExportStringCaller;
123 class IntPreferenceImporter
128 IntPreferenceImporter( int& i )
131 void importString( const char* value ){
132 m_i = int_import( value );
141 PreferenceDictionary preferences;
143 class StringObserver : public StringPreference::Observer
150 StringPreference string1( string_observer );
151 string1.importString( "twenty-three" );
153 class IntObserver : public IntPreference::Observer
161 IntPreference int1( int_observer );
162 int1.importString( "23" );
164 preferences.registerPreference( "string1", StringPreference::ImportStringCaller( string1 ), StringPreference::ExportStringCaller( string1 ) );
165 preferences.registerPreference( "int1", IntPreference::ImportStringCaller( int1 ), IntPreference::ExportStringCaller( int1 ) );
167 LoadPrefs( preferences, "test.pref" );
168 SavePrefs( preferences, "test.pref" );
174 TestPrefs g_TestPrefs;
177 void readpref( PreferenceDictionary& preferences, int& int_variable ){
178 PreferenceDictionary::iterator i = preferences.find( "int_variable" );
179 IntPreferenceImporter importer( int_variable );
180 ( *i ).second.exporter().exportString( importer );
183 void writepref( PreferenceDictionary& preferences, int& int_variable ){
184 PreferenceDictionary::iterator i = preferences.find( "int_variable" );
185 int_export( int_variable, ( *i ).second.importer() );