/[pcsx2_0.9.7]/trunk/pcsx2/gui/IniInterface.cpp
ViewVC logotype

Contents of /trunk/pcsx2/gui/IniInterface.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 10 months ago) by william
File size: 8100 byte(s)
committing r3113 initial commit again...
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #include "PrecompiledHeader.h"
17 #include "System.h"
18 #include "IniInterface.h"
19
20 static int _calcEnumLength( const wxChar* const* enumArray )
21 {
22 int cnt = 0;
23 while( *enumArray != NULL )
24 {
25 enumArray++;
26 cnt++;
27 }
28
29 return cnt;
30 }
31
32 //////////////////////////////////////////////////////////////////////////////////////////
33 //
34 IniInterface::IniInterface( wxConfigBase& config ) :
35 m_Config( config )
36 {
37 }
38
39 IniInterface::IniInterface() :
40 m_Config( *wxConfigBase::Get() )
41 {
42 }
43
44 IniInterface::~IniInterface()
45 {
46 Flush();
47 }
48
49 void IniInterface::SetPath( const wxString& path )
50 {
51 m_Config.SetPath( path );
52 }
53
54 void IniInterface::Flush()
55 {
56 m_Config.Flush();
57 }
58
59 //////////////////////////////////////////////////////////////////////////////////////////
60 //
61 IniScopedGroup::IniScopedGroup( IniInterface& mommy, const wxString& group ) :
62 m_mom( mommy )
63 {
64 pxAssertDev( wxStringTokenize( group, L"/" ).Count() <= 1, L"Cannot nest more than one group deep per instance of IniScopedGroup." );
65 m_mom.SetPath( group );
66 }
67
68 IniScopedGroup::~IniScopedGroup()
69 {
70 m_mom.SetPath( L".." );
71 }
72
73 //////////////////////////////////////////////////////////////////////////////////////////
74 //
75
76 IniLoader::IniLoader( wxConfigBase& config ) : IniInterface( config )
77 {
78 }
79
80 IniLoader::IniLoader() : IniInterface() {}
81 IniLoader::~IniLoader() throw() {}
82
83
84 void IniLoader::Entry( const wxString& var, wxString& value, const wxString& defvalue )
85 {
86 m_Config.Read( var, &value, defvalue );
87 }
88
89 void IniLoader::Entry( const wxString& var, wxDirName& value, const wxDirName& defvalue )
90 {
91 wxString dest;
92 m_Config.Read( var, &dest, wxEmptyString );
93
94 if( dest.IsEmpty() )
95 value = defvalue;
96 else
97 value = dest;
98 }
99
100 void IniLoader::Entry( const wxString& var, wxFileName& value, const wxFileName& defvalue )
101 {
102 wxString dest;
103 m_Config.Read( var, &dest, defvalue.GetFullPath() );
104 value = dest;
105 }
106
107 void IniLoader::Entry( const wxString& var, int& value, const int defvalue )
108 {
109 m_Config.Read( var, &value, defvalue );
110 }
111
112 void IniLoader::Entry( const wxString& var, uint& value, const uint defvalue )
113 {
114 m_Config.Read( var, (int*)&value, (int)defvalue );
115 }
116
117 void IniLoader::Entry( const wxString& var, bool& value, const bool defvalue )
118 {
119 // TODO : Stricter value checking on enabled/disabled?
120 wxString dest;
121 m_Config.Read( var, &dest, defvalue ? L"enabled" : L"disabled" );
122 value = (dest == L"enabled") || (dest == L"1");
123 }
124
125 bool IniLoader::EntryBitBool( const wxString& var, bool value, const bool defvalue )
126 {
127 // Note: 'value' param is used by inisaver only.
128 bool result;
129 Entry( var, result, defvalue );
130 return result;
131 }
132
133 int IniLoader::EntryBitfield( const wxString& var, int value, const int defvalue )
134 {
135 int result;
136 Entry( var, result, defvalue );
137 return result;
138 }
139
140 void IniLoader::Entry( const wxString& var, Fixed100& value, const Fixed100& defvalue )
141 {
142 // Note: the "easy" way would be to convert to double and load/save that, but floating point
143 // has way too much rounding error so we really need to do things out manually.. >_<
144
145 wxString readval( value.ToString() );
146 m_Config.Read( var, &readval );
147 value = Fixed100::FromString( readval, value );
148 }
149
150 void IniLoader::Entry( const wxString& var, wxPoint& value, const wxPoint& defvalue )
151 {
152 TryParse( value, m_Config.Read( var, ToString( defvalue ) ), defvalue );
153 }
154
155 void IniLoader::Entry( const wxString& var, wxSize& value, const wxSize& defvalue )
156 {
157 TryParse( value, m_Config.Read( var, ToString( defvalue ) ), defvalue );
158 }
159
160 void IniLoader::Entry( const wxString& var, wxRect& value, const wxRect& defvalue )
161 {
162 TryParse( value, m_Config.Read( var, ToString( defvalue ) ), defvalue );
163 }
164
165 void IniLoader::_EnumEntry( const wxString& var, int& value, const wxChar* const* enumArray, int defvalue )
166 {
167 // Confirm default value sanity...
168
169 const int cnt = _calcEnumLength( enumArray );
170 if( !IndexBoundsCheck( L"IniLoader EnumDefaultValue", defvalue, cnt ) )
171 {
172 Console.Error( "(LoadSettings) Default enumeration index is out of bounds. Truncating." );
173 defvalue = cnt-1;
174 }
175
176 // Sanity confirmed, proceed with craziness!
177
178 wxString retval;
179 m_Config.Read( var, &retval, enumArray[defvalue] );
180
181 int i=0;
182 while( enumArray[i] != NULL && ( retval != enumArray[i] ) ) i++;
183
184 if( enumArray[i] == NULL )
185 {
186 Console.Warning( L"(LoadSettings) Warning: Unrecognized value '%s' on key '%s'\n\tUsing the default setting of '%s'.",
187 retval.c_str(), var.c_str(), enumArray[defvalue]
188 );
189 value = defvalue;
190 }
191 else
192 value = i;
193 }
194
195 //////////////////////////////////////////////////////////////////////////////////////////
196 //
197
198 IniSaver::IniSaver( wxConfigBase& config ) : IniInterface( config )
199 {
200 }
201
202 IniSaver::IniSaver() : IniInterface() {}
203 IniSaver::~IniSaver() {}
204
205 void IniSaver::Entry( const wxString& var, wxString& value, const wxString& defvalue )
206 {
207 m_Config.Write( var, value );
208 }
209
210 void IniSaver::Entry( const wxString& var, wxDirName& value, const wxDirName& defvalue )
211 {
212 /*if( value == defvalue )
213 m_Config.Write( var, wxString() );
214 else*/
215 m_Config.Write( var, value.ToString() );
216 }
217
218 void IniSaver::Entry( const wxString& var, wxFileName& value, const wxFileName& defvalue )
219 {
220 m_Config.Write( var, value.GetFullPath() );
221 }
222
223 void IniSaver::Entry( const wxString& var, int& value, const int defvalue )
224 {
225 m_Config.Write( var, value );
226 }
227
228 void IniSaver::Entry( const wxString& var, uint& value, const uint defvalue )
229 {
230 m_Config.Write( var, (int)value );
231 }
232
233 void IniSaver::Entry( const wxString& var, bool& value, const bool defvalue )
234 {
235 m_Config.Write( var, value ? L"enabled" : L"disabled" );
236 }
237
238 bool IniSaver::EntryBitBool( const wxString& var, bool value, const bool defvalue )
239 {
240 m_Config.Write( var, value ? L"enabled" : L"disabled" );
241 return value;
242 }
243
244 int IniSaver::EntryBitfield( const wxString& var, int value, const int defvalue )
245 {
246 m_Config.Write( var, value );
247 return value;
248 }
249
250 void IniSaver::Entry( const wxString& var, Fixed100& value, const Fixed100& defvalue )
251 {
252 // Note: the "easy" way would be to convert to double and load/save that, but floating point
253 // has way too much rounding error so we really need to do things out manually, using strings.
254
255 m_Config.Write( var, value.ToString() );
256 }
257
258 void IniSaver::Entry( const wxString& var, wxPoint& value, const wxPoint& defvalue )
259 {
260 m_Config.Write( var, ToString( value ) );
261 }
262
263 void IniSaver::Entry( const wxString& var, wxSize& value, const wxSize& defvalue )
264 {
265 m_Config.Write( var, ToString( value ) );
266 }
267
268 void IniSaver::Entry( const wxString& var, wxRect& value, const wxRect& defvalue )
269 {
270 m_Config.Write( var, ToString( value ) );
271 }
272
273 void IniSaver::_EnumEntry( const wxString& var, int& value, const wxChar* const* enumArray, int defvalue )
274 {
275 const int cnt = _calcEnumLength( enumArray );
276
277 // Confirm default value sanity...
278
279 if( !IndexBoundsCheck( L"IniSaver EnumDefaultValue", defvalue, cnt ) )
280 {
281 Console.Error( "(SaveSettings) Default enumeration index is out of bounds. Truncating." );
282 defvalue = cnt-1;
283 }
284
285 if( value >= cnt )
286 {
287 Console.Warning( L"(SaveSettings) An illegal enumerated index was detected when saving '%s'", var.c_str() );
288 Console.Indent().Warning(
289 L"Illegal Value: %d\n"
290 L"Using Default: %d (%s)\n",
291 value, defvalue, enumArray[defvalue]
292 );
293
294 // Cause a debug assertion, since this is a fully recoverable error.
295 pxAssert( value < cnt );
296
297 value = defvalue;
298 }
299
300 m_Config.Write( var, enumArray[value] );
301 }
302

  ViewVC Help
Powered by ViewVC 1.1.22