gfx/skia/trunk/src/utils/SkRTConf.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/utils/SkRTConf.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,350 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkRTConf.h"
    1.12 +#include "SkOSFile.h"
    1.13 +
    1.14 +SkRTConfRegistry::SkRTConfRegistry(): fConfs(100) {
    1.15 +
    1.16 +    SkFILE *fp = sk_fopen(configFileLocation(), kRead_SkFILE_Flag);
    1.17 +
    1.18 +    if (!fp) {
    1.19 +        return;
    1.20 +    }
    1.21 +
    1.22 +    char line[1024];
    1.23 +
    1.24 +    while (!sk_feof(fp)) {
    1.25 +
    1.26 +        if (!sk_fgets(line, sizeof(line), fp)) {
    1.27 +            break;
    1.28 +        }
    1.29 +
    1.30 +        char *commentptr = strchr(line, '#');
    1.31 +        if (commentptr == line) {
    1.32 +            continue;
    1.33 +        }
    1.34 +        if (NULL != commentptr) {
    1.35 +            *commentptr = '\0';
    1.36 +        }
    1.37 +
    1.38 +        char sep[] = " \t\r\n";
    1.39 +
    1.40 +        char *keyptr = strtok(line, sep);
    1.41 +        if (!keyptr) {
    1.42 +            continue;
    1.43 +        }
    1.44 +
    1.45 +        char *valptr = strtok(NULL, sep);
    1.46 +        if (!valptr) {
    1.47 +            continue;
    1.48 +        }
    1.49 +
    1.50 +        SkString* key = SkNEW_ARGS(SkString,(keyptr));
    1.51 +        SkString* val = SkNEW_ARGS(SkString,(valptr));
    1.52 +
    1.53 +        fConfigFileKeys.append(1, &key);
    1.54 +        fConfigFileValues.append(1, &val);
    1.55 +    }
    1.56 +    sk_fclose(fp);
    1.57 +}
    1.58 +
    1.59 +SkRTConfRegistry::~SkRTConfRegistry() {
    1.60 +    ConfMap::Iter iter(fConfs);
    1.61 +    SkTDArray<SkRTConfBase *> *confArray;
    1.62 +
    1.63 +    while (iter.next(&confArray)) {
    1.64 +        delete confArray;
    1.65 +    }
    1.66 +
    1.67 +    for (int i = 0 ; i < fConfigFileKeys.count() ; i++) {
    1.68 +        SkDELETE(fConfigFileKeys[i]);
    1.69 +        SkDELETE(fConfigFileValues[i]);
    1.70 +    }
    1.71 +}
    1.72 +
    1.73 +const char *SkRTConfRegistry::configFileLocation() const {
    1.74 +    return "skia.conf"; // for now -- should probably do something fancier like home directories or whatever.
    1.75 +}
    1.76 +
    1.77 +// dump all known runtime config options to the file with their default values.
    1.78 +// to trigger this, make a config file of zero size.
    1.79 +void SkRTConfRegistry::possiblyDumpFile() const {
    1.80 +    const char *path = configFileLocation();
    1.81 +    SkFILE *fp = sk_fopen(path, kRead_SkFILE_Flag);
    1.82 +    if (!fp) {
    1.83 +        return;
    1.84 +    }
    1.85 +    size_t configFileSize = sk_fgetsize(fp);
    1.86 +    if (configFileSize == 0) {
    1.87 +        printAll(path);
    1.88 +    }
    1.89 +    sk_fclose(fp);
    1.90 +}
    1.91 +
    1.92 +// Run through every provided configuration option and print a warning if the user hasn't
    1.93 +// declared a correponding configuration object somewhere.
    1.94 +void SkRTConfRegistry::validate() const {
    1.95 +    for (int i = 0 ; i < fConfigFileKeys.count() ; i++) {
    1.96 +        if (!fConfs.find(fConfigFileKeys[i]->c_str())) {
    1.97 +            SkDebugf("WARNING: You have config value %s in your configuration file, but I've never heard of that.\n", fConfigFileKeys[i]->c_str());
    1.98 +        }
    1.99 +    }
   1.100 +}
   1.101 +
   1.102 +void SkRTConfRegistry::printAll(const char *fname) const {
   1.103 +    SkWStream *o;
   1.104 +
   1.105 +    if (NULL != fname) {
   1.106 +        o = new SkFILEWStream(fname);
   1.107 +    } else {
   1.108 +        o = new SkDebugWStream();
   1.109 +    }
   1.110 +
   1.111 +    ConfMap::Iter iter(fConfs);
   1.112 +    SkTDArray<SkRTConfBase *> *confArray;
   1.113 +
   1.114 +    while (iter.next(&confArray)) {
   1.115 +        if (confArray->getAt(0)->isDefault()) {
   1.116 +            o->writeText("# ");
   1.117 +        }
   1.118 +        confArray->getAt(0)->print(o);
   1.119 +        o->newline();
   1.120 +    }
   1.121 +
   1.122 +    delete o;
   1.123 +}
   1.124 +
   1.125 +bool SkRTConfRegistry::hasNonDefault() const {
   1.126 +    ConfMap::Iter iter(fConfs);
   1.127 +    SkTDArray<SkRTConfBase *> *confArray;
   1.128 +    while (iter.next(&confArray)) {
   1.129 +        if (!confArray->getAt(0)->isDefault()) {
   1.130 +            return true;
   1.131 +        }
   1.132 +    }
   1.133 +    return false;
   1.134 +}
   1.135 +
   1.136 +void SkRTConfRegistry::printNonDefault(const char *fname) const {
   1.137 +    SkWStream *o;
   1.138 +
   1.139 +    if (NULL != fname) {
   1.140 +        o = new SkFILEWStream(fname);
   1.141 +    } else {
   1.142 +        o = new SkDebugWStream();
   1.143 +    }
   1.144 +    ConfMap::Iter iter(fConfs);
   1.145 +    SkTDArray<SkRTConfBase *> *confArray;
   1.146 +
   1.147 +    while (iter.next(&confArray)) {
   1.148 +        if (!confArray->getAt(0)->isDefault()) {
   1.149 +            confArray->getAt(0)->print(o);
   1.150 +            o->newline();
   1.151 +        }
   1.152 +    }
   1.153 +
   1.154 +    delete o;
   1.155 +}
   1.156 +
   1.157 +// register a configuration variable after its value has been set by the parser.
   1.158 +// we maintain a vector of these things instead of just a single one because the
   1.159 +// user might set the value after initialization time and we need to have
   1.160 +// all the pointers lying around, not just one.
   1.161 +void SkRTConfRegistry::registerConf(SkRTConfBase *conf) {
   1.162 +    SkTDArray<SkRTConfBase *> *confArray;
   1.163 +    if (fConfs.find(conf->getName(), &confArray)) {
   1.164 +        if (!conf->equals(confArray->getAt(0))) {
   1.165 +            SkDebugf("WARNING: Skia config \"%s\" was registered more than once in incompatible ways.\n", conf->getName());
   1.166 +        } else {
   1.167 +            confArray->append(1, &conf);
   1.168 +        }
   1.169 +    } else {
   1.170 +        confArray = new SkTDArray<SkRTConfBase *>;
   1.171 +        confArray->append(1, &conf);
   1.172 +        fConfs.set(conf->getName(),confArray);
   1.173 +    }
   1.174 +}
   1.175 +
   1.176 +template <typename T> T doParse(const char *, bool *success ) {
   1.177 +    SkDebugf("WARNING: Invoked non-specialized doParse function...\n");
   1.178 +    if (success) {
   1.179 +        *success = false;
   1.180 +    }
   1.181 +    return (T) 0;
   1.182 +}
   1.183 +
   1.184 +template<> bool doParse<bool>(const char *s, bool *success) {
   1.185 +    if (success) {
   1.186 +        *success = true;
   1.187 +    }
   1.188 +    if (!strcmp(s,"1") || !strcmp(s,"true")) {
   1.189 +        return true;
   1.190 +    }
   1.191 +    if (!strcmp(s,"0") || !strcmp(s,"false")) {
   1.192 +        return false;
   1.193 +    }
   1.194 +    if (success) {
   1.195 +        *success = false;
   1.196 +    }
   1.197 +    return false;
   1.198 +}
   1.199 +
   1.200 +template<> const char * doParse<const char *>(const char * s, bool *success) {
   1.201 +    if (success) {
   1.202 +        *success = true;
   1.203 +    }
   1.204 +    return s;
   1.205 +}
   1.206 +
   1.207 +template<> int doParse<int>(const char * s, bool *success) {
   1.208 +    if (success) {
   1.209 +        *success = true;
   1.210 +    }
   1.211 +    return atoi(s);
   1.212 +}
   1.213 +
   1.214 +template<> unsigned int doParse<unsigned int>(const char * s, bool *success) {
   1.215 +    if (success) {
   1.216 +        *success = true;
   1.217 +    }
   1.218 +    return (unsigned int) atoi(s);
   1.219 +}
   1.220 +
   1.221 +template<> float doParse<float>(const char * s, bool *success) {
   1.222 +    if (success) {
   1.223 +        *success = true;
   1.224 +    }
   1.225 +    return (float) atof(s);
   1.226 +}
   1.227 +
   1.228 +template<> double doParse<double>(const char * s, bool *success) {
   1.229 +    if (success) {
   1.230 +        *success = true;
   1.231 +    }
   1.232 +    return atof(s);
   1.233 +}
   1.234 +
   1.235 +static inline void str_replace(char *s, char search, char replace) {
   1.236 +    for (char *ptr = s ; *ptr ; ptr++) {
   1.237 +        if (*ptr == search) {
   1.238 +            *ptr = replace;
   1.239 +        }
   1.240 +    }
   1.241 +}
   1.242 +
   1.243 +template<typename T> bool SkRTConfRegistry::parse(const char *name, T* value) {
   1.244 +    const char *str = NULL;
   1.245 +
   1.246 +    for (int i = fConfigFileKeys.count() - 1 ; i >= 0; i--) {
   1.247 +        if (fConfigFileKeys[i]->equals(name)) {
   1.248 +            str = fConfigFileValues[i]->c_str();
   1.249 +            break;
   1.250 +        }
   1.251 +    }
   1.252 +
   1.253 +    SkString environment_variable("skia.");
   1.254 +    environment_variable.append(name);
   1.255 +
   1.256 +    const char *environment_value = getenv(environment_variable.c_str());
   1.257 +    if (environment_value) {
   1.258 +        str = environment_value;
   1.259 +    } else {
   1.260 +        // apparently my shell doesn't let me have environment variables that
   1.261 +        // have periods in them, so also let the user substitute underscores.
   1.262 +        SkAutoTMalloc<char> underscore_name(SkStrDup(environment_variable.c_str()));
   1.263 +        str_replace(underscore_name.get(), '.', '_');
   1.264 +        environment_value = getenv(underscore_name.get());
   1.265 +        if (environment_value) {
   1.266 +            str = environment_value;
   1.267 +        }
   1.268 +    }
   1.269 +
   1.270 +    if (!str) {
   1.271 +        return false;
   1.272 +    }
   1.273 +
   1.274 +    bool success;
   1.275 +    T new_value = doParse<T>(str, &success);
   1.276 +    if (success) {
   1.277 +        *value = new_value;
   1.278 +    } else {
   1.279 +        SkDebugf("WARNING: Couldn't parse value \'%s\' for variable \'%s\'\n",
   1.280 +                 str, name);
   1.281 +    }
   1.282 +    return success;
   1.283 +}
   1.284 +
   1.285 +// need to explicitly instantiate the parsing function for every config type we might have...
   1.286 +
   1.287 +template bool SkRTConfRegistry::parse(const char *name, bool *value);
   1.288 +template bool SkRTConfRegistry::parse(const char *name, int *value);
   1.289 +template bool SkRTConfRegistry::parse(const char *name, unsigned int *value);
   1.290 +template bool SkRTConfRegistry::parse(const char *name, float *value);
   1.291 +template bool SkRTConfRegistry::parse(const char *name, double *value);
   1.292 +template bool SkRTConfRegistry::parse(const char *name, const char **value);
   1.293 +
   1.294 +template <typename T> void SkRTConfRegistry::set(const char *name,
   1.295 +                                                 T value,
   1.296 +                                                 bool warnIfNotFound) {
   1.297 +    SkTDArray<SkRTConfBase *> *confArray;
   1.298 +    if (!fConfs.find(name, &confArray)) {
   1.299 +        if (warnIfNotFound) {
   1.300 +            SkDebugf("WARNING: Attempting to set configuration value \"%s\","
   1.301 +                     " but I've never heard of that.\n", name);
   1.302 +        }
   1.303 +        return;
   1.304 +    }
   1.305 +    SkASSERT(confArray != NULL);
   1.306 +    for (SkRTConfBase **confBase = confArray->begin(); confBase != confArray->end(); confBase++) {
   1.307 +        // static_cast here is okay because there's only one kind of child class.
   1.308 +        SkRTConf<T> *concrete = static_cast<SkRTConf<T> *>(*confBase);
   1.309 +
   1.310 +        if (concrete) {
   1.311 +            concrete->set(value);
   1.312 +        }
   1.313 +    }
   1.314 +}
   1.315 +
   1.316 +template void SkRTConfRegistry::set(const char *name, bool value, bool);
   1.317 +template void SkRTConfRegistry::set(const char *name, int value, bool);
   1.318 +template void SkRTConfRegistry::set(const char *name, unsigned int value, bool);
   1.319 +template void SkRTConfRegistry::set(const char *name, float value, bool);
   1.320 +template void SkRTConfRegistry::set(const char *name, double value, bool);
   1.321 +template void SkRTConfRegistry::set(const char *name, char * value, bool);
   1.322 +
   1.323 +SkRTConfRegistry &skRTConfRegistry() {
   1.324 +    static SkRTConfRegistry r;
   1.325 +    return r;
   1.326 +}
   1.327 +
   1.328 +
   1.329 +#ifdef SK_SUPPORT_UNITTEST
   1.330 +
   1.331 +#ifdef SK_BUILD_FOR_WIN32
   1.332 +static void sk_setenv(const char* key, const char* value) {
   1.333 +    _putenv_s(key, value);
   1.334 +}
   1.335 +#else
   1.336 +static void sk_setenv(const char* key, const char* value) {
   1.337 +    setenv(key, value, 1);
   1.338 +}
   1.339 +#endif
   1.340 +
   1.341 +void SkRTConfRegistry::UnitTest() {
   1.342 +    SkRTConfRegistry registryWithoutContents(true);
   1.343 +
   1.344 +    sk_setenv("skia_nonexistent_item", "132");
   1.345 +    int result = 0;
   1.346 +    registryWithoutContents.parse("nonexistent.item", &result);
   1.347 +    SkASSERT(result == 132);
   1.348 +}
   1.349 +
   1.350 +SkRTConfRegistry::SkRTConfRegistry(bool)
   1.351 +    : fConfs(100) {
   1.352 +}
   1.353 +#endif

mercurial