xref: /haiku/src/kits/tracker/Settings.cpp (revision 99d1318ec02694fc520a0dc38ae38565db7e8c3c)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 // generic setting handler classes
36 
37 
38 #include <Debug.h>
39 
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 
44 #include "TrackerSettings.h"
45 
46 
47 Settings* settings = NULL;
48 
49 
50 //	#pragma mark - StringValueSetting
51 
52 
53 StringValueSetting::StringValueSetting(const char* name,
54 	const char* defaultValue, const char* valueExpectedErrorString,
55 	const char* wrongValueErrorString)
56 	:
57 	SettingsArgvDispatcher(name),
58 	fDefaultValue(defaultValue),
59 	fValueExpectedErrorString(valueExpectedErrorString),
60 	fWrongValueErrorString(wrongValueErrorString),
61 	fValue(defaultValue)
62 {
63 }
64 
65 
66 StringValueSetting::~StringValueSetting()
67 {
68 }
69 
70 
71 void
72 StringValueSetting::ValueChanged(const char* newValue)
73 {
74 	fValue = newValue;
75 }
76 
77 
78 const char*
79 StringValueSetting::Value() const
80 {
81 	return fValue.String();
82 }
83 
84 
85 void
86 StringValueSetting::SaveSettingValue(Settings* settings)
87 {
88 	settings->Write("\"%s\"", fValue.String());
89 }
90 
91 
92 bool
93 StringValueSetting::NeedsSaving() const
94 {
95 	// needs saving if different than default
96 	return fValue != fDefaultValue;
97 }
98 
99 
100 const char*
101 StringValueSetting::Handle(const char* const* argv)
102 {
103 	if (!*++argv)
104 		return fValueExpectedErrorString;
105 
106 	ValueChanged(*argv);
107 	return 0;
108 }
109 
110 
111 //	#pragma mark - EnumeratedStringValueSetting
112 
113 
114 EnumeratedStringValueSetting::EnumeratedStringValueSetting(const char* name,
115 	const char* defaultValue, const char* const* values,
116 	const char* valueExpectedErrorString, const char* wrongValueErrorString)
117 	:
118 	StringValueSetting(name, defaultValue, valueExpectedErrorString,
119 		wrongValueErrorString),
120 	fValues(values)
121 {
122 }
123 
124 
125 void
126 EnumeratedStringValueSetting::ValueChanged(const char* newValue)
127 {
128 #if DEBUG
129 	// must be one of the enumerated values
130 	bool found = false;
131 	for (int32 index = 0; ; index++) {
132 		if (fValues[index] == NULL)
133 			break;
134 
135 		if (strcmp(fValues[index], newValue) != 0)
136 			continue;
137 
138 		found = true;
139 		break;
140 	}
141 	ASSERT(found);
142 #endif
143 	StringValueSetting::ValueChanged(newValue);
144 }
145 
146 
147 const char*
148 EnumeratedStringValueSetting::Handle(const char* const* argv)
149 {
150 	if (!*++argv)
151 		return fValueExpectedErrorString;
152 
153 	bool found = false;
154 	for (int32 index = 0; ; index++) {
155 		if (fValues[index] == NULL)
156 			break;
157 
158 		if (strcmp(fValues[index], *argv) != 0)
159 			continue;
160 
161 		found = true;
162 		break;
163 	}
164 
165 	if (!found)
166 		return fWrongValueErrorString;
167 
168 	ValueChanged(*argv);
169 	return 0;
170 }
171 
172 
173 //	#pragma mark - ScalarValueSetting
174 
175 
176 ScalarValueSetting::ScalarValueSetting(const char* name, int32 defaultValue,
177 	const char* valueExpectedErrorString, const char* wrongValueErrorString,
178 	int32 min, int32 max)
179 	:
180 	SettingsArgvDispatcher(name),
181 	fDefaultValue(defaultValue),
182 	fValue(defaultValue),
183 	fMax(max),
184 	fMin(min),
185 	fValueExpectedErrorString(valueExpectedErrorString),
186 	fWrongValueErrorString(wrongValueErrorString)
187 {
188 }
189 
190 
191 void
192 ScalarValueSetting::ValueChanged(int32 newValue)
193 {
194 	ASSERT(newValue > fMin);
195 	ASSERT(newValue < fMax);
196 	fValue = newValue;
197 }
198 
199 
200 int32
201 ScalarValueSetting::Value() const
202 {
203 	return fValue;
204 }
205 
206 
207 void
208 ScalarValueSetting::GetValueAsString(char* buffer) const
209 {
210 	sprintf(buffer, "%" B_PRId32, fValue);
211 }
212 
213 
214 const char*
215 ScalarValueSetting::Handle(const char* const* argv)
216 {
217 	if (!*++argv)
218 		return fValueExpectedErrorString;
219 
220 	int32 newValue;
221 	if ((*argv)[0] == '0' && (*argv)[1] == 'x')
222 		sscanf(*argv, "%" B_PRIx32, &newValue);
223 	else
224 		newValue = atoi(*argv);
225 
226 	if (newValue < fMin || newValue > fMax)
227 		return fWrongValueErrorString;
228 
229 	fValue = newValue;
230 	return NULL;
231 }
232 
233 
234 void
235 ScalarValueSetting::SaveSettingValue(Settings* settings)
236 {
237 	settings->Write("%" B_PRId32, fValue);
238 }
239 
240 
241 bool
242 ScalarValueSetting::NeedsSaving() const
243 {
244 	return fValue != fDefaultValue;
245 }
246 
247 
248 //	#pragma mark - HexScalarValueSetting
249 
250 
251 HexScalarValueSetting::HexScalarValueSetting(const char* name,
252 	int32 defaultValue, const char* valueExpectedErrorString,
253 	const char* wrongValueErrorString, int32 min, int32 max)
254 	:
255 	ScalarValueSetting(name, defaultValue, valueExpectedErrorString,
256 		wrongValueErrorString, min, max)
257 {
258 }
259 
260 
261 void
262 HexScalarValueSetting::GetValueAsString(char* buffer) const
263 {
264 	sprintf(buffer, "0x%08" B_PRIx32, fValue);
265 }
266 
267 
268 void
269 HexScalarValueSetting::SaveSettingValue(Settings* settings)
270 {
271 	settings->Write("0x%08" B_PRIx32, fValue);
272 }
273 
274 
275 //	#pragma mark - BooleanValueSetting
276 
277 
278 BooleanValueSetting::BooleanValueSetting(const char* name, bool defaultValue)
279 	:	ScalarValueSetting(name, defaultValue, 0, 0)
280 {
281 }
282 
283 
284 bool
285 BooleanValueSetting::Value() const
286 {
287 	return fValue != 0;
288 }
289 
290 
291 void
292 BooleanValueSetting::SetValue(bool value)
293 {
294 	fValue = value;
295 }
296 
297 
298 const char*
299 BooleanValueSetting::Handle(const char* const* argv)
300 {
301 	if (!*++argv)
302 		return "on or off expected";
303 
304 	if (strcmp(*argv, "on") == 0)
305 		fValue = true;
306 	else if (strcmp(*argv, "off") == 0)
307 		fValue = false;
308 	else
309 		return "on or off expected";
310 
311 	return 0;
312 }
313 
314 
315 void
316 BooleanValueSetting::SaveSettingValue(Settings* settings)
317 {
318 	settings->Write(fValue ? "on" : "off");
319 }
320