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)
33 TextFileInputStream file(filename);
36 XMLStreamParser parser(file);
37 XMLPreferenceDictionaryImporter importer(preferences);
38 parser.exportXML(importer);
46 void SavePrefs(PreferenceDictionary& preferences, const char* filename)
48 TextFileOutputStream file(filename);
51 XMLStreamWriter writer(file);
52 XMLPreferenceDictionaryExporter exporter(preferences, "1");
53 exporter.exportXML(writer);
62 class StringPreference
68 virtual void onChanged() = 0;
72 CopiedString m_string;
75 StringPreference(Observer& observer)
76 : m_observer(observer)
79 void importString(const char* value)
82 m_observer.onChanged();
84 typedef MemberCaller1<StringPreference, const char*, &StringPreference::importString> ImportStringCaller;
85 void exportString(StringImportCallback& importer)
87 importer(m_string.c_str());
89 typedef MemberCaller1<StringPreference, StringImportCallback&, &StringPreference::exportString> ExportStringCaller;
92 inline void int_export(int i, StringImportCallback& importer)
95 sprintf(buffer, "%d", i);
99 inline int int_import(const char* value)
110 virtual void onChanged() = 0;
115 Observer& m_observer;
118 IntPreference(Observer& observer)
119 : m_observer(observer)
122 void importString(const char* value)
124 m_int = int_import(value);
125 m_observer.onChanged();
127 typedef MemberCaller1<IntPreference, const char*, &IntPreference::importString> ImportStringCaller;
128 void exportString(StringImportCallback& importer)
130 int_export(m_int, importer);
132 typedef MemberCaller1<IntPreference, StringImportCallback&, &IntPreference::exportString> ExportStringCaller;
135 class IntPreferenceImporter
140 IntPreferenceImporter(int& i)
144 void importString(const char* value)
146 m_i = int_import(value);
156 PreferenceDictionary preferences;
158 class StringObserver : public StringPreference::Observer
166 StringPreference string1(string_observer);
167 string1.importString("twenty-three");
169 class IntObserver : public IntPreference::Observer
178 IntPreference int1(int_observer);
179 int1.importString("23");
181 preferences.registerPreference("string1", StringPreference::ImportStringCaller(string1), StringPreference::ExportStringCaller(string1));
182 preferences.registerPreference("int1", IntPreference::ImportStringCaller(int1), IntPreference::ExportStringCaller(int1));
184 LoadPrefs(preferences, "test.pref");
185 SavePrefs(preferences, "test.pref");
191 TestPrefs g_TestPrefs;
194 void readpref(PreferenceDictionary& preferences, int& int_variable)
196 PreferenceDictionary::iterator i = preferences.find("int_variable");
197 IntPreferenceImporter importer(int_variable);
198 (*i).second.exporter().exportString(importer);
201 void writepref(PreferenceDictionary& preferences, int& int_variable)
203 PreferenceDictionary::iterator i = preferences.find("int_variable");
204 int_export(int_variable, (*i).second.importer());