]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/preferencesystem.cpp
updated libxml2 win32 package to 2.6.24
[xonotic/netradiant.git] / include / preferencesystem.cpp
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 0
23
24 #include "preferencesystem.h"
25 #include "preferencedictionary.h"
26
27 #include "xml/xmlparser.h"
28 #include "xml/xmlwriter.h"
29
30
31 void LoadPrefs(PreferenceDictionary& preferences, const char* filename)
32 {
33   TextFileInputStream file(filename);
34   if(!file.failed())
35   {
36     XMLStreamParser parser(file);
37     XMLPreferenceDictionaryImporter importer(preferences);
38     parser.exportXML(importer);
39   }
40   else
41   {
42     // error
43   }
44 }
45
46 void SavePrefs(PreferenceDictionary& preferences, const char* filename)
47 {
48   TextFileOutputStream file(filename);
49   if(!file.failed())
50   {
51     XMLStreamWriter writer(file);
52     XMLPreferenceDictionaryExporter exporter(preferences, "1");
53     exporter.exportXML(writer);
54   }
55   else
56   {
57     // error
58   }
59 }
60
61
62 class StringPreference
63 {
64 public:
65   class Observer
66   {
67   public:
68     virtual void onChanged() = 0;
69   };
70
71 private:
72   CopiedString m_string;
73   Observer& m_observer;
74 public:
75   StringPreference(Observer& observer)
76     : m_observer(observer)
77   {
78   }
79   void importString(const char* value)
80   {
81     m_string = value;
82     m_observer.onChanged();
83   }
84   typedef MemberCaller1<StringPreference, const char*, &StringPreference::importString> ImportStringCaller;
85   void exportString(StringImportCallback& importer)
86   {
87     importer(m_string.c_str());
88   }
89   typedef MemberCaller1<StringPreference, StringImportCallback&, &StringPreference::exportString> ExportStringCaller;
90 };
91
92 inline void int_export(int i, StringImportCallback& importer)
93 {
94   char buffer[16];
95   sprintf(buffer, "%d", i);
96   importer(buffer);
97 }
98
99 inline int int_import(const char* value)
100 {
101   return atoi(value);
102 }
103
104 class IntPreference
105 {
106 public:
107   class Observer
108   {
109   public:
110     virtual void onChanged() = 0;
111   };
112
113 private:
114   int m_int;
115   Observer& m_observer;
116 public:
117
118   IntPreference(Observer& observer)
119     : m_observer(observer)
120   {
121   }
122   void importString(const char* value)
123   {
124     m_int = int_import(value);
125     m_observer.onChanged();
126   }
127   typedef MemberCaller1<IntPreference, const char*, &IntPreference::importString> ImportStringCaller;
128   void exportString(StringImportCallback& importer)
129   {
130     int_export(m_int, importer);
131   }
132   typedef MemberCaller1<IntPreference, StringImportCallback&, &IntPreference::exportString> ExportStringCaller;
133 };
134
135 class IntPreferenceImporter
136 {
137   int& m_i;
138 public:
139
140   IntPreferenceImporter(int& i)
141     : m_i(i)
142   {
143   }
144   void importString(const char* value)
145   {
146     m_i = int_import(value);
147   }
148 };
149
150
151 class TestPrefs
152 {
153 public:
154   TestPrefs()
155   {
156     PreferenceDictionary preferences;
157
158     class StringObserver : public StringPreference::Observer
159     {
160     public:
161       void onChanged()
162       {
163         int bleh = 0;
164       }
165     } string_observer;
166     StringPreference string1(string_observer);
167     string1.importString("twenty-three");
168
169     class IntObserver : public IntPreference::Observer
170     {
171     public:
172       void onChanged()
173       {
174         int bleh = 0;
175       }
176
177     } int_observer;
178     IntPreference int1(int_observer);
179     int1.importString("23");
180
181     preferences.registerPreference("string1", StringPreference::ImportStringCaller(string1), StringPreference::ExportStringCaller(string1));
182     preferences.registerPreference("int1", IntPreference::ImportStringCaller(int1), IntPreference::ExportStringCaller(int1));
183
184     LoadPrefs(preferences, "test.pref");
185     SavePrefs(preferences, "test.pref");
186
187   }
188 };
189
190 #if 0
191 TestPrefs g_TestPrefs;
192 #endif
193
194 void readpref(PreferenceDictionary& preferences, int& int_variable)
195 {
196   PreferenceDictionary::iterator i = preferences.find("int_variable");
197   IntPreferenceImporter importer(int_variable);
198   (*i).second.exporter().exportString(importer);
199 }
200
201 void writepref(PreferenceDictionary& preferences, int& int_variable)
202 {
203   PreferenceDictionary::iterator i = preferences.find("int_variable");
204   int_export(int_variable, (*i).second.importer());
205 }
206 #endif