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.
35 // generic setting handler classes
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
),
66 StringValueSetting::~StringValueSetting()
72 StringValueSetting::ValueChanged(const char* newValue
)
79 StringValueSetting::Value() const
81 return fValue
.String();
86 StringValueSetting::SaveSettingValue(Settings
* settings
)
88 settings
->Write("\"%s\"", fValue
.String());
93 StringValueSetting::NeedsSaving() const
95 // needs saving if different than default
96 return fValue
!= fDefaultValue
;
101 StringValueSetting::Handle(const char* const* argv
)
104 return fValueExpectedErrorString
;
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
),
126 EnumeratedStringValueSetting::ValueChanged(const char* newValue
)
129 // must be one of the enumerated values
131 for (int32 index
= 0; ; index
++) {
132 if (fValues
[index
] == NULL
)
135 if (strcmp(fValues
[index
], newValue
) != 0)
143 StringValueSetting::ValueChanged(newValue
);
148 EnumeratedStringValueSetting::Handle(const char* const* argv
)
151 return fValueExpectedErrorString
;
154 for (int32 index
= 0; ; index
++) {
155 if (fValues
[index
] == NULL
)
158 if (strcmp(fValues
[index
], *argv
) != 0)
166 return fWrongValueErrorString
;
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
),
185 fValueExpectedErrorString(valueExpectedErrorString
),
186 fWrongValueErrorString(wrongValueErrorString
)
192 ScalarValueSetting::ValueChanged(int32 newValue
)
194 ASSERT(newValue
> fMin
);
195 ASSERT(newValue
< fMax
);
201 ScalarValueSetting::Value() const
208 ScalarValueSetting::GetValueAsString(char* buffer
) const
210 sprintf(buffer
, "%" B_PRId32
, fValue
);
215 ScalarValueSetting::Handle(const char* const* argv
)
218 return fValueExpectedErrorString
;
221 if ((*argv
)[0] == '0' && (*argv
)[1] == 'x')
222 sscanf(*argv
, "%" B_PRIx32
, &newValue
);
224 newValue
= atoi(*argv
);
226 if (newValue
< fMin
|| newValue
> fMax
)
227 return fWrongValueErrorString
;
235 ScalarValueSetting::SaveSettingValue(Settings
* settings
)
237 settings
->Write("%ld", fValue
);
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
)
262 HexScalarValueSetting::GetValueAsString(char* buffer
) const
264 sprintf(buffer
, "0x%08" B_PRIx32
, fValue
);
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)
285 BooleanValueSetting::Value() const
292 BooleanValueSetting::SetValue(bool value
)
299 BooleanValueSetting::Handle(const char* const* argv
)
302 return "on or off expected";
304 if (strcmp(*argv
, "on") == 0)
306 else if (strcmp(*argv
, "off") == 0)
309 return "on or off expected";
316 BooleanValueSetting::SaveSettingValue(Settings
* settings
)
318 settings
->Write(fValue
? "on" : "off");