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