1 #include "Settings.h" 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #include <Catalog.h> 8 #include <Debug.h> 9 #include <Locale.h> 10 11 12 #undef B_TRANSLATION_CONTEXT 13 #define B_TRANSLATION_CONTEXT "Settings" 14 15 16 Settings* settings = NULL; 17 18 19 StringValueSetting::StringValueSetting(const char* name, 20 const char* defaultValue, const char* valueExpectedErrorString, 21 const char* wrongValueErrorString) 22 : 23 SettingsArgvDispatcher(name), 24 fDefaultValue(defaultValue), 25 fValueExpectedErrorString(valueExpectedErrorString), 26 fWrongValueErrorString(wrongValueErrorString), 27 fValue(strdup(defaultValue)) 28 { 29 } 30 31 32 StringValueSetting::~StringValueSetting() 33 { 34 free(fValue); 35 } 36 37 38 void 39 StringValueSetting::ValueChanged(const char* newValue) 40 { 41 if (newValue == fValue) 42 // guard against self assingment 43 return; 44 45 free(fValue); 46 fValue = strdup(newValue); 47 } 48 49 50 const char* 51 StringValueSetting::Value() const 52 { 53 return fValue; 54 } 55 56 57 void 58 StringValueSetting::SaveSettingValue(Settings* settings) 59 { 60 printf("-----StringValueSetting::SaveSettingValue %s %s\n", Name(), fValue); 61 settings->Write("\"%s\"", fValue); 62 } 63 64 65 bool 66 StringValueSetting::NeedsSaving() const 67 { 68 // needs saving if different than default 69 return strcmp(fValue, fDefaultValue) != 0; 70 } 71 72 73 const char* 74 StringValueSetting::Handle(const char *const *argv) 75 { 76 if (!*++argv) 77 return fValueExpectedErrorString; 78 79 ValueChanged(*argv); 80 return 0; 81 } 82 83 84 // #pragma mark - 85 86 87 EnumeratedStringValueSetting::EnumeratedStringValueSetting(const char* name, 88 const char* defaultValue, StringEnumerator enumerator, 89 const char* valueExpectedErrorString, 90 const char* wrongValueErrorString) 91 : 92 StringValueSetting(name, defaultValue, valueExpectedErrorString, 93 wrongValueErrorString), 94 fEnumerator(enumerator) 95 { 96 } 97 98 99 void 100 EnumeratedStringValueSetting::ValueChanged(const char* newValue) 101 { 102 #if DEBUG 103 // must be one of the enumerated values 104 ASSERT(_ValidateString(newValue)); 105 #endif 106 StringValueSetting::ValueChanged(newValue); 107 } 108 109 110 const char* 111 EnumeratedStringValueSetting::Handle(const char *const *argv) 112 { 113 if (!*++argv) 114 return fValueExpectedErrorString; 115 116 printf("---EnumeratedStringValueSetting::Handle %s %s\n", *(argv-1), *argv); 117 if (!_ValidateString(*argv)) 118 return fWrongValueErrorString; 119 120 ValueChanged(*argv); 121 return 0; 122 } 123 124 125 bool 126 EnumeratedStringValueSetting::_ValidateString(const char* string) 127 { 128 for (int32 i = 0;; i++) { 129 const char* enumString = fEnumerator(i); 130 if (!enumString) 131 return false; 132 if (strcmp(enumString, string) == 0) 133 return true; 134 } 135 return false; 136 } 137 138 139 // #pragma mark - 140 141 142 ScalarValueSetting::ScalarValueSetting(const char* name, int32 defaultValue, 143 const char* valueExpectedErrorString, const char* wrongValueErrorString, 144 int32 min, int32 max) 145 : SettingsArgvDispatcher(name), 146 fDefaultValue(defaultValue), 147 fValue(defaultValue), 148 fMax(max), 149 fMin(min), 150 fValueExpectedErrorString(valueExpectedErrorString), 151 fWrongValueErrorString(wrongValueErrorString) 152 { 153 } 154 155 156 ScalarValueSetting::~ScalarValueSetting() 157 { 158 } 159 160 161 void 162 ScalarValueSetting::ValueChanged(int32 newValue) 163 { 164 ASSERT(newValue > fMin); 165 ASSERT(newValue < fMax); 166 fValue = newValue; 167 } 168 169 170 int32 171 ScalarValueSetting::Value() const 172 { 173 return fValue; 174 } 175 176 177 void 178 ScalarValueSetting::GetValueAsString(char* buffer) const 179 { 180 sprintf(buffer, "%ld", fValue); 181 } 182 183 184 const char* 185 ScalarValueSetting::Handle(const char *const *argv) 186 { 187 if (!*++argv) 188 return fValueExpectedErrorString; 189 190 int32 newValue = atoi(*argv); 191 if (newValue < fMin || newValue > fMax) 192 return fWrongValueErrorString; 193 194 fValue = newValue; 195 return 0; 196 } 197 198 199 void 200 ScalarValueSetting::SaveSettingValue(Settings* settings) 201 { 202 settings->Write("%d", fValue); 203 } 204 205 206 bool 207 ScalarValueSetting::NeedsSaving() const 208 { 209 return fValue != fDefaultValue; 210 } 211 212 213 // #pragma mark - 214 215 216 BooleanValueSetting::BooleanValueSetting(const char* name, bool defaultValue) 217 : ScalarValueSetting(name, defaultValue, 0, 0) 218 { 219 } 220 221 222 BooleanValueSetting::~BooleanValueSetting() 223 { 224 } 225 226 227 bool 228 BooleanValueSetting::Value() const 229 { 230 return fValue; 231 } 232 233 234 const char* 235 BooleanValueSetting::Handle(const char *const *argv) 236 { 237 if (!*++argv) { 238 return B_TRANSLATE_COMMENT("on or off expected","Do not translate " 239 "'on' and 'off'"); 240 } 241 242 if (strcmp(*argv, "on") == 0) 243 fValue = true; 244 else if (strcmp(*argv, "off") == 0) 245 fValue = false; 246 else { 247 return B_TRANSLATE_COMMENT("on or off expected", "Do not translate " 248 "'on' and 'off'"); 249 } 250 251 return 0; 252 } 253 254 255 void 256 BooleanValueSetting::SaveSettingValue(Settings* settings) 257 { 258 settings->Write(fValue ? "on" : "off"); 259 } 260