1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All 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
47Settings* settings = NULL;
48
49
50//	#pragma mark - StringValueSetting
51
52
53StringValueSetting::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
66StringValueSetting::~StringValueSetting()
67{
68}
69
70
71void
72StringValueSetting::ValueChanged(const char* newValue)
73{
74	fValue = newValue;
75}
76
77
78const char*
79StringValueSetting::Value() const
80{
81	return fValue.String();
82}
83
84
85void
86StringValueSetting::SaveSettingValue(Settings* settings)
87{
88	settings->Write("\"%s\"", fValue.String());
89}
90
91
92bool
93StringValueSetting::NeedsSaving() const
94{
95	// needs saving if different than default
96	return fValue != fDefaultValue;
97}
98
99
100const char*
101StringValueSetting::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
114EnumeratedStringValueSetting::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
125void
126EnumeratedStringValueSetting::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
147const char*
148EnumeratedStringValueSetting::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
176ScalarValueSetting::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
191void
192ScalarValueSetting::ValueChanged(int32 newValue)
193{
194	ASSERT(newValue > fMin);
195	ASSERT(newValue < fMax);
196	fValue = newValue;
197}
198
199
200int32
201ScalarValueSetting::Value() const
202{
203	return fValue;
204}
205
206
207void
208ScalarValueSetting::GetValueAsString(char* buffer) const
209{
210	sprintf(buffer, "%" B_PRId32, fValue);
211}
212
213
214const char*
215ScalarValueSetting::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
234void
235ScalarValueSetting::SaveSettingValue(Settings* settings)
236{
237	settings->Write("%ld", fValue);
238}
239
240
241bool
242ScalarValueSetting::NeedsSaving() const
243{
244	return fValue != fDefaultValue;
245}
246
247
248//	#pragma mark - HexScalarValueSetting
249
250
251HexScalarValueSetting::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
261void
262HexScalarValueSetting::GetValueAsString(char* buffer) const
263{
264	sprintf(buffer, "0x%08" B_PRIx32, fValue);
265}
266
267
268void
269HexScalarValueSetting::SaveSettingValue(Settings* settings)
270{
271	settings->Write("0x%08" B_PRIx32, fValue);
272}
273
274
275//	#pragma mark - BooleanValueSetting
276
277
278BooleanValueSetting::BooleanValueSetting(const char* name, bool defaultValue)
279	:	ScalarValueSetting(name, defaultValue, 0, 0)
280{
281}
282
283
284bool
285BooleanValueSetting::Value() const
286{
287	return fValue != 0;
288}
289
290
291void
292BooleanValueSetting::SetValue(bool value)
293{
294	fValue = value;
295}
296
297
298const char*
299BooleanValueSetting::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
315void
316BooleanValueSetting::SaveSettingValue(Settings* settings)
317{
318	settings->Write(fValue ? "on" : "off");
319}
320