vfs: check userland buffers before reading them.
[haiku.git] / src / apps / codycam / Settings.cpp
blob537d0bf1f92fa1ad5521053633a6a53d6ec87f41
1 #include "Settings.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include <Catalog.h>
8 #include <Debug.h>
9 #include <Locale.h>
12 #undef B_TRANSLATION_CONTEXT
13 #define B_TRANSLATION_CONTEXT "Settings"
16 Settings* settings = NULL;
19 StringValueSetting::StringValueSetting(const char* name,
20 const char* defaultValue, const char* valueExpectedErrorString,
21 const char* wrongValueErrorString)
23 SettingsArgvDispatcher(name),
24 fDefaultValue(defaultValue),
25 fValueExpectedErrorString(valueExpectedErrorString),
26 fWrongValueErrorString(wrongValueErrorString),
27 fValue(strdup(defaultValue))
32 StringValueSetting::~StringValueSetting()
34 free(fValue);
38 void
39 StringValueSetting::ValueChanged(const char* newValue)
41 if (newValue == fValue)
42 // guard against self assingment
43 return;
45 free(fValue);
46 fValue = strdup(newValue);
50 const char*
51 StringValueSetting::Value() const
53 return fValue;
57 void
58 StringValueSetting::SaveSettingValue(Settings* settings)
60 printf("-----StringValueSetting::SaveSettingValue %s %s\n", Name(), fValue);
61 settings->Write("\"%s\"", fValue);
65 bool
66 StringValueSetting::NeedsSaving() const
68 // needs saving if different than default
69 return strcmp(fValue, fDefaultValue) != 0;
73 const char*
74 StringValueSetting::Handle(const char *const *argv)
76 if (!*++argv)
77 return fValueExpectedErrorString;
79 ValueChanged(*argv);
80 return 0;
84 // #pragma mark -
87 EnumeratedStringValueSetting::EnumeratedStringValueSetting(const char* name,
88 const char* defaultValue, StringEnumerator enumerator,
89 const char* valueExpectedErrorString,
90 const char* wrongValueErrorString)
92 StringValueSetting(name, defaultValue, valueExpectedErrorString,
93 wrongValueErrorString),
94 fEnumerator(enumerator)
99 void
100 EnumeratedStringValueSetting::ValueChanged(const char* newValue)
102 #if DEBUG
103 // must be one of the enumerated values
104 ASSERT(_ValidateString(newValue));
105 #endif
106 StringValueSetting::ValueChanged(newValue);
110 const char*
111 EnumeratedStringValueSetting::Handle(const char *const *argv)
113 if (!*++argv)
114 return fValueExpectedErrorString;
116 printf("---EnumeratedStringValueSetting::Handle %s %s\n", *(argv-1), *argv);
117 if (!_ValidateString(*argv))
118 return fWrongValueErrorString;
120 ValueChanged(*argv);
121 return 0;
125 bool
126 EnumeratedStringValueSetting::_ValidateString(const char* string)
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;
135 return false;
139 // #pragma mark -
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)
156 ScalarValueSetting::~ScalarValueSetting()
161 void
162 ScalarValueSetting::ValueChanged(int32 newValue)
164 ASSERT(newValue > fMin);
165 ASSERT(newValue < fMax);
166 fValue = newValue;
170 int32
171 ScalarValueSetting::Value() const
173 return fValue;
177 void
178 ScalarValueSetting::GetValueAsString(char* buffer) const
180 sprintf(buffer, "%" B_PRId32, fValue);
184 const char*
185 ScalarValueSetting::Handle(const char *const *argv)
187 if (!*++argv)
188 return fValueExpectedErrorString;
190 int32 newValue = atoi(*argv);
191 if (newValue < fMin || newValue > fMax)
192 return fWrongValueErrorString;
194 fValue = newValue;
195 return 0;
199 void
200 ScalarValueSetting::SaveSettingValue(Settings* settings)
202 settings->Write("%d", fValue);
206 bool
207 ScalarValueSetting::NeedsSaving() const
209 return fValue != fDefaultValue;
213 // #pragma mark -
216 BooleanValueSetting::BooleanValueSetting(const char* name, bool defaultValue)
217 : ScalarValueSetting(name, defaultValue, 0, 0)
222 BooleanValueSetting::~BooleanValueSetting()
227 bool
228 BooleanValueSetting::Value() const
230 return fValue;
234 const char*
235 BooleanValueSetting::Handle(const char *const *argv)
237 if (!*++argv) {
238 return B_TRANSLATE_COMMENT("on or off expected","Do not translate "
239 "'on' and 'off'");
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'");
251 return 0;
255 void
256 BooleanValueSetting::SaveSettingValue(Settings* settings)
258 settings->Write(fValue ? "on" : "off");