OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkRTConf.h" | 8 #include "SkRTConf.h" |
9 #include "SkOSFile.h" | 9 #include "SkOSFile.h" |
10 | 10 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 | 206 |
207 static inline void str_replace(char *s, char search, char replace) { | 207 static inline void str_replace(char *s, char search, char replace) { |
208 for (char *ptr = s ; *ptr ; ptr++) { | 208 for (char *ptr = s ; *ptr ; ptr++) { |
209 if (*ptr == search) { | 209 if (*ptr == search) { |
210 *ptr = replace; | 210 *ptr = replace; |
211 } | 211 } |
212 } | 212 } |
213 } | 213 } |
214 | 214 |
215 template<typename T> bool SkRTConfRegistry::parse(const char *name, T* value) { | 215 template<typename T> bool SkRTConfRegistry::parse(const char *name, T* value) { |
216 SkString *str = NULL; | 216 const char *str = NULL; |
217 SkString tmp; | |
218 | 217 |
219 for (int i = fConfigFileKeys.count() - 1 ; i >= 0; i--) { | 218 for (int i = fConfigFileKeys.count() - 1 ; i >= 0; i--) { |
220 if (fConfigFileKeys[i]->equals(name)) { | 219 if (fConfigFileKeys[i]->equals(name)) { |
221 str = fConfigFileValues[i]; | 220 str = fConfigFileValues[i]->c_str(); |
222 break; | 221 break; |
223 } | 222 } |
224 } | 223 } |
225 | 224 |
226 SkString environment_variable("skia."); | 225 SkString environment_variable("skia."); |
227 environment_variable.append(name); | 226 environment_variable.append(name); |
228 | 227 |
229 const char *environment_value = getenv(environment_variable.c_str()); | 228 const char *environment_value = getenv(environment_variable.c_str()); |
230 if (environment_value) { | 229 if (environment_value) { |
231 if (NULL == str) { | 230 str = environment_value; |
232 str = &tmp; | |
233 } | |
234 str->set(environment_value); | |
235 } else { | 231 } else { |
236 // apparently my shell doesn't let me have environment variables that | 232 // apparently my shell doesn't let me have environment variables that |
237 // have periods in them, so also let the user substitute underscores. | 233 // have periods in them, so also let the user substitute underscores. |
238 SkString underscore_environment_variable("skia_"); | 234 SkAutoTMalloc<char> underscore_name(SkStrDup(environment_variable.c_str(
))); |
239 char *underscore_name = SkStrDup(name); | 235 str_replace(underscore_name.get(),'.','_'); |
240 str_replace(underscore_name,'.','_'); | 236 environment_value = getenv(underscore_name.get()); |
241 underscore_environment_variable.append(underscore_name); | |
242 sk_free(underscore_name); | |
243 environment_value = getenv(underscore_environment_variable.c_str()); | |
244 if (environment_value) { | 237 if (environment_value) { |
245 if (NULL == str) { | 238 str = environment_value; |
246 str = &tmp; | |
247 } | |
248 str->set(environment_value); | |
249 } | 239 } |
250 } | 240 } |
251 | 241 |
252 if (!str) { | 242 if (!str) { |
253 return false; | 243 return false; |
254 } | 244 } |
255 | 245 |
256 bool success; | 246 bool success; |
257 T new_value = doParse<T>(str->c_str(),&success); | 247 T new_value = doParse<T>(str, &success); |
258 if (success) { | 248 if (success) { |
259 *value = new_value; | 249 *value = new_value; |
260 } else { | 250 } else { |
261 SkDebugf("WARNING: Couldn't parse value \'%s\' for variable \'%s\'\n", s
tr->c_str(), name); | 251 SkDebugf("WARNING: Couldn't parse value \'%s\' for variable \'%s\'\n", |
| 252 str, name); |
262 } | 253 } |
263 return success; | 254 return success; |
264 } | 255 } |
265 | 256 |
266 // need to explicitly instantiate the parsing function for every config type we
might have... | 257 // need to explicitly instantiate the parsing function for every config type we
might have... |
267 | 258 |
268 template bool SkRTConfRegistry::parse(const char *name, bool *value); | 259 template bool SkRTConfRegistry::parse(const char *name, bool *value); |
269 template bool SkRTConfRegistry::parse(const char *name, int *value); | 260 template bool SkRTConfRegistry::parse(const char *name, int *value); |
270 template bool SkRTConfRegistry::parse(const char *name, unsigned int *value); | 261 template bool SkRTConfRegistry::parse(const char *name, unsigned int *value); |
271 template bool SkRTConfRegistry::parse(const char *name, float *value); | 262 template bool SkRTConfRegistry::parse(const char *name, float *value); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 sk_setenv("skia_nonexistent_item", "132"); | 312 sk_setenv("skia_nonexistent_item", "132"); |
322 int result = 0; | 313 int result = 0; |
323 registryWithoutContents.parse("nonexistent.item", &result); | 314 registryWithoutContents.parse("nonexistent.item", &result); |
324 SkASSERT(result == 132); | 315 SkASSERT(result == 132); |
325 } | 316 } |
326 | 317 |
327 SkRTConfRegistry::SkRTConfRegistry(bool) | 318 SkRTConfRegistry::SkRTConfRegistry(bool) |
328 : fConfs(100) { | 319 : fConfs(100) { |
329 } | 320 } |
330 #endif | 321 #endif |
OLD | NEW |