btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / tracker / Settings.cpp
blob8482cd16a07303e040aa7c4ceaf4d3b60c1e254c
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
35 // generic setting handler classes
38 #include <Debug.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdio.h>
44 #include "TrackerSettings.h"
47 Settings* settings = NULL;
50 // #pragma mark - StringValueSetting
53 StringValueSetting::StringValueSetting(const char* name,
54 const char* defaultValue, const char* valueExpectedErrorString,
55 const char* wrongValueErrorString)
57 SettingsArgvDispatcher(name),
58 fDefaultValue(defaultValue),
59 fValueExpectedErrorString(valueExpectedErrorString),
60 fWrongValueErrorString(wrongValueErrorString),
61 fValue(defaultValue)
66 StringValueSetting::~StringValueSetting()
71 void
72 StringValueSetting::ValueChanged(const char* newValue)
74 fValue = newValue;
78 const char*
79 StringValueSetting::Value() const
81 return fValue.String();
85 void
86 StringValueSetting::SaveSettingValue(Settings* settings)
88 settings->Write("\"%s\"", fValue.String());
92 bool
93 StringValueSetting::NeedsSaving() const
95 // needs saving if different than default
96 return fValue != fDefaultValue;
100 const char*
101 StringValueSetting::Handle(const char* const* argv)
103 if (!*++argv)
104 return fValueExpectedErrorString;
106 ValueChanged(*argv);
107 return 0;
111 // #pragma mark - EnumeratedStringValueSetting
114 EnumeratedStringValueSetting::EnumeratedStringValueSetting(const char* name,
115 const char* defaultValue, const char* const* values,
116 const char* valueExpectedErrorString, const char* wrongValueErrorString)
118 StringValueSetting(name, defaultValue, valueExpectedErrorString,
119 wrongValueErrorString),
120 fValues(values)
125 void
126 EnumeratedStringValueSetting::ValueChanged(const char* newValue)
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;
135 if (strcmp(fValues[index], newValue) != 0)
136 continue;
138 found = true;
139 break;
141 ASSERT(found);
142 #endif
143 StringValueSetting::ValueChanged(newValue);
147 const char*
148 EnumeratedStringValueSetting::Handle(const char* const* argv)
150 if (!*++argv)
151 return fValueExpectedErrorString;
153 bool found = false;
154 for (int32 index = 0; ; index++) {
155 if (fValues[index] == NULL)
156 break;
158 if (strcmp(fValues[index], *argv) != 0)
159 continue;
161 found = true;
162 break;
165 if (!found)
166 return fWrongValueErrorString;
168 ValueChanged(*argv);
169 return 0;
173 // #pragma mark - ScalarValueSetting
176 ScalarValueSetting::ScalarValueSetting(const char* name, int32 defaultValue,
177 const char* valueExpectedErrorString, const char* wrongValueErrorString,
178 int32 min, int32 max)
180 SettingsArgvDispatcher(name),
181 fDefaultValue(defaultValue),
182 fValue(defaultValue),
183 fMax(max),
184 fMin(min),
185 fValueExpectedErrorString(valueExpectedErrorString),
186 fWrongValueErrorString(wrongValueErrorString)
191 void
192 ScalarValueSetting::ValueChanged(int32 newValue)
194 ASSERT(newValue > fMin);
195 ASSERT(newValue < fMax);
196 fValue = newValue;
200 int32
201 ScalarValueSetting::Value() const
203 return fValue;
207 void
208 ScalarValueSetting::GetValueAsString(char* buffer) const
210 sprintf(buffer, "%" B_PRId32, fValue);
214 const char*
215 ScalarValueSetting::Handle(const char* const* argv)
217 if (!*++argv)
218 return fValueExpectedErrorString;
220 int32 newValue;
221 if ((*argv)[0] == '0' && (*argv)[1] == 'x')
222 sscanf(*argv, "%" B_PRIx32, &newValue);
223 else
224 newValue = atoi(*argv);
226 if (newValue < fMin || newValue > fMax)
227 return fWrongValueErrorString;
229 fValue = newValue;
230 return NULL;
234 void
235 ScalarValueSetting::SaveSettingValue(Settings* settings)
237 settings->Write("%ld", fValue);
241 bool
242 ScalarValueSetting::NeedsSaving() const
244 return fValue != fDefaultValue;
248 // #pragma mark - HexScalarValueSetting
251 HexScalarValueSetting::HexScalarValueSetting(const char* name,
252 int32 defaultValue, const char* valueExpectedErrorString,
253 const char* wrongValueErrorString, int32 min, int32 max)
255 ScalarValueSetting(name, defaultValue, valueExpectedErrorString,
256 wrongValueErrorString, min, max)
261 void
262 HexScalarValueSetting::GetValueAsString(char* buffer) const
264 sprintf(buffer, "0x%08" B_PRIx32, fValue);
268 void
269 HexScalarValueSetting::SaveSettingValue(Settings* settings)
271 settings->Write("0x%08" B_PRIx32, fValue);
275 // #pragma mark - BooleanValueSetting
278 BooleanValueSetting::BooleanValueSetting(const char* name, bool defaultValue)
279 : ScalarValueSetting(name, defaultValue, 0, 0)
284 bool
285 BooleanValueSetting::Value() const
287 return fValue != 0;
291 void
292 BooleanValueSetting::SetValue(bool value)
294 fValue = value;
298 const char*
299 BooleanValueSetting::Handle(const char* const* argv)
301 if (!*++argv)
302 return "on or off expected";
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";
311 return 0;
315 void
316 BooleanValueSetting::SaveSettingValue(Settings* settings)
318 settings->Write(fValue ? "on" : "off");