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 void 152 ScalarValueSetting::ValueChanged(int32 newValue) 153 { 154 ASSERT(newValue > fMin); 155 ASSERT(newValue < fMax); 156 fValue = newValue; 157 } 158 159 160 int32 161 ScalarValueSetting::Value() const 162 { 163 return fValue; 164 } 165 166 167 void 168 ScalarValueSetting::GetValueAsString(char* buffer) const 169 { 170 sprintf(buffer, "%ld", fValue); 171 } 172 173 174 const char* 175 ScalarValueSetting::Handle(const char *const *argv) 176 { 177 if (!*++argv) 178 return fValueExpectedErrorString; 179 180 int32 newValue = atoi(*argv); 181 if (newValue < fMin || newValue > fMax) 182 return fWrongValueErrorString; 183 184 fValue = newValue; 185 return 0; 186 } 187 188 189 void 190 ScalarValueSetting::SaveSettingValue(Settings* settings) 191 { 192 settings->Write("%d", fValue); 193 } 194 195 196 bool 197 ScalarValueSetting::NeedsSaving() const 198 { 199 return fValue != fDefaultValue; 200 } 201 202 203 // #pragma mark - 204 205 206 BooleanValueSetting::BooleanValueSetting(const char* name, bool defaultValue) 207 : ScalarValueSetting(name, defaultValue, 0, 0) 208 { 209 } 210 211 212 bool 213 BooleanValueSetting::Value() const 214 { 215 return fValue; 216 } 217 218 219 const char* 220 BooleanValueSetting::Handle(const char *const *argv) 221 { 222 if (!*++argv) 223 return "on or off expected"; 224 225 if (strcmp(*argv, "on") == 0) 226 fValue = true; 227 else if (strcmp(*argv, "off") == 0) 228 fValue = false; 229 else 230 return "on or off expected"; 231 232 return 0; 233 } 234 235 236 void 237 BooleanValueSetting::SaveSettingValue(Settings* settings) 238 { 239 settings->Write(fValue ? "on" : "off"); 240 } 241