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