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 36 #include <Debug.h> 37 38 #include <string.h> 39 #include <stdlib.h> 40 #include <stdio.h> 41 42 #include "TrackerSettings.h" 43 44 45 Settings* settings = NULL; 46 47 // generic setting handler classes 48 49 StringValueSetting::StringValueSetting(const char* name, 50 const char* defaultValue, const char* valueExpectedErrorString, 51 const char* wrongValueErrorString) 52 : SettingsArgvDispatcher(name), 53 fDefaultValue(defaultValue), 54 fValueExpectedErrorString(valueExpectedErrorString), 55 fWrongValueErrorString(wrongValueErrorString), 56 fValue(defaultValue) 57 { 58 } 59 60 61 StringValueSetting::~StringValueSetting() 62 { 63 } 64 65 66 void 67 StringValueSetting::ValueChanged(const char* newValue) 68 { 69 fValue = newValue; 70 } 71 72 73 const char* 74 StringValueSetting::Value() const 75 { 76 return fValue.String(); 77 } 78 79 80 void 81 StringValueSetting::SaveSettingValue(Settings* settings) 82 { 83 settings->Write("\"%s\"", fValue.String()); 84 } 85 86 87 bool 88 StringValueSetting::NeedsSaving() const 89 { 90 // needs saving if different than default 91 return fValue != fDefaultValue; 92 } 93 94 95 const char* 96 StringValueSetting::Handle(const char* const* argv) 97 { 98 if (!*++argv) 99 return fValueExpectedErrorString; 100 101 ValueChanged(*argv); 102 return 0; 103 } 104 105 106 // #pragma mark - 107 108 109 EnumeratedStringValueSetting::EnumeratedStringValueSetting(const char* name, 110 const char* defaultValue, const char* const* values, 111 const char* valueExpectedErrorString, const char* wrongValueErrorString) 112 : StringValueSetting(name, defaultValue, valueExpectedErrorString, 113 wrongValueErrorString), 114 fValues(values) 115 { 116 } 117 118 119 void 120 EnumeratedStringValueSetting::ValueChanged(const char* newValue) 121 { 122 #if DEBUG 123 // must be one of the enumerated values 124 bool found = false; 125 for (int32 index = 0; ; index++) { 126 if (!fValues[index]) 127 break; 128 129 if (strcmp(fValues[index], newValue) != 0) 130 continue; 131 132 found = true; 133 break; 134 } 135 ASSERT(found); 136 #endif 137 StringValueSetting::ValueChanged(newValue); 138 } 139 140 141 const char* 142 EnumeratedStringValueSetting::Handle(const char* const* argv) 143 { 144 if (!*++argv) 145 return fValueExpectedErrorString; 146 147 bool found = false; 148 for (int32 index = 0; ; index++) { 149 if (!fValues[index]) 150 break; 151 152 if (strcmp(fValues[index], *argv) != 0) 153 continue; 154 155 found = true; 156 break; 157 } 158 159 if (!found) 160 return fWrongValueErrorString; 161 162 ValueChanged(*argv); 163 return 0; 164 } 165 166 167 // #pragma mark - 168 169 170 ScalarValueSetting::ScalarValueSetting(const char* name, int32 defaultValue, 171 const char* valueExpectedErrorString, const char* wrongValueErrorString, 172 int32 min, int32 max) 173 : SettingsArgvDispatcher(name), 174 fDefaultValue(defaultValue), 175 fValue(defaultValue), 176 fMax(max), 177 fMin(min), 178 fValueExpectedErrorString(valueExpectedErrorString), 179 fWrongValueErrorString(wrongValueErrorString) 180 { 181 } 182 183 184 void 185 ScalarValueSetting::ValueChanged(int32 newValue) 186 { 187 ASSERT(newValue > fMin); 188 ASSERT(newValue < fMax); 189 fValue = newValue; 190 } 191 192 193 int32 194 ScalarValueSetting::Value() const 195 { 196 return fValue; 197 } 198 199 200 void 201 ScalarValueSetting::GetValueAsString(char* buffer) const 202 { 203 sprintf(buffer, "%" B_PRId32, fValue); 204 } 205 206 207 const char* 208 ScalarValueSetting::Handle(const char* const* argv) 209 { 210 if (!*++argv) 211 return fValueExpectedErrorString; 212 213 int32 newValue; 214 if ((*argv)[0] == '0' && (*argv)[1] == 'x') 215 sscanf(*argv, "%" B_PRIx32, &newValue); 216 else 217 newValue = atoi(*argv); 218 219 if (newValue < fMin || newValue > fMax) 220 return fWrongValueErrorString; 221 222 fValue = newValue; 223 return NULL; 224 } 225 226 227 void 228 ScalarValueSetting::SaveSettingValue(Settings* settings) 229 { 230 settings->Write("%ld", fValue); 231 } 232 233 234 bool 235 ScalarValueSetting::NeedsSaving() const 236 { 237 return fValue != fDefaultValue; 238 } 239 240 241 // #pragma mark - 242 243 244 HexScalarValueSetting::HexScalarValueSetting(const char* name, 245 int32 defaultValue, const char* valueExpectedErrorString, 246 const char* wrongValueErrorString, int32 min, int32 max) 247 : ScalarValueSetting(name, defaultValue, valueExpectedErrorString, 248 wrongValueErrorString, min, max) 249 { 250 } 251 252 253 void 254 HexScalarValueSetting::GetValueAsString(char* buffer) const 255 { 256 sprintf(buffer, "0x%08" B_PRIx32, fValue); 257 } 258 259 260 void 261 HexScalarValueSetting::SaveSettingValue(Settings* settings) 262 { 263 settings->Write("0x%08" B_PRIx32, fValue); 264 } 265 266 267 // #pragma mark - 268 269 270 BooleanValueSetting::BooleanValueSetting(const char* name, bool defaultValue) 271 : ScalarValueSetting(name, defaultValue, 0, 0) 272 { 273 } 274 275 276 bool 277 BooleanValueSetting::Value() const 278 { 279 return fValue != 0; 280 } 281 282 283 void 284 BooleanValueSetting::SetValue(bool value) 285 { 286 fValue = value; 287 } 288 289 290 const char* 291 BooleanValueSetting::Handle(const char* const* argv) 292 { 293 if (!*++argv) 294 return "on or off expected"; 295 296 if (strcmp(*argv, "on") == 0) 297 fValue = true; 298 else if (strcmp(*argv, "off") == 0) 299 fValue = false; 300 else 301 return "on or off expected"; 302 303 return 0; 304 } 305 306 307 void 308 BooleanValueSetting::SaveSettingValue(Settings* settings) 309 { 310 settings->Write(fValue ? "on" : "off"); 311 } 312