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