| OLD | NEW |
| 1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // elsewhere. | 47 // elsewhere. |
| 48 #ifndef BASE_COMMANDLINEFLAGS_H_ | 48 #ifndef BASE_COMMANDLINEFLAGS_H_ |
| 49 #define BASE_COMMANDLINEFLAGS_H_ | 49 #define BASE_COMMANDLINEFLAGS_H_ |
| 50 | 50 |
| 51 #include <config.h> | 51 #include <config.h> |
| 52 #include <string> | 52 #include <string> |
| 53 #include <string.h> // for memchr | 53 #include <string.h> // for memchr |
| 54 #include <stdlib.h> // for getenv | 54 #include <stdlib.h> // for getenv |
| 55 #include "base/basictypes.h" | 55 #include "base/basictypes.h" |
| 56 | 56 |
| 57 #if defined(__ANDROID__) || defined(ANDROID) |
| 58 #include <sys/system_properties.h> |
| 59 #endif |
| 60 |
| 57 #define DECLARE_VARIABLE(type, name) \ | 61 #define DECLARE_VARIABLE(type, name) \ |
| 58 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead {
\ | 62 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead {
\ |
| 59 extern PERFTOOLS_DLL_DECL type FLAGS_##name; \ | 63 extern PERFTOOLS_DLL_DECL type FLAGS_##name; \ |
| 60 } \ | 64 } \ |
| 61 using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_
##name | 65 using FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead::FLAGS_
##name |
| 62 | 66 |
| 63 #define DEFINE_VARIABLE(type, name, value, meaning) \ | 67 #define DEFINE_VARIABLE(type, name, value, meaning) \ |
| 64 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead {
\ | 68 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##type##_instead {
\ |
| 65 PERFTOOLS_DLL_DECL type FLAGS_##name(value); \ | 69 PERFTOOLS_DLL_DECL type FLAGS_##name(value); \ |
| 66 char FLAGS_no##name; \ | 70 char FLAGS_no##name; \ |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##
name | 109 using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##
name |
| 106 #define DEFINE_string(name, value, meaning) \ | 110 #define DEFINE_string(name, value, meaning) \ |
| 107 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \ | 111 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \ |
| 108 std::string FLAGS_##name(value);
\ | 112 std::string FLAGS_##name(value);
\ |
| 109 char FLAGS_no##name; \ | 113 char FLAGS_no##name; \ |
| 110 } \ | 114 } \ |
| 111 using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##
name | 115 using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##
name |
| 112 | 116 |
| 113 // These macros (could be functions, but I don't want to bother with a .cc | 117 // These macros (could be functions, but I don't want to bother with a .cc |
| 114 // file), make it easier to initialize flags from the environment. | 118 // file), make it easier to initialize flags from the environment. |
| 119 // They are functions in Android because __system_property_get() doesn't |
| 120 // return a string. |
| 121 |
| 122 #if defined(__ANDROID__) || defined(ANDROID) |
| 123 |
| 124 // Returns a pointer to a static variable. The string pointed by the returned |
| 125 // pointer must not be modified. |
| 126 inline const char* const EnvToString(const char* envname, const char* dflt) { |
| 127 static char system_property_value[PROP_VALUE_MAX]; |
| 128 if (__system_property_get(envname, system_property_value) > 0) |
| 129 return system_property_value; |
| 130 return dflt; |
| 131 } |
| 132 |
| 133 inline bool EnvToBool(const char* envname, bool dflt) { |
| 134 static const char kTrueValues[] = "tTyY1"; |
| 135 char system_property_value[PROP_VALUE_MAX]; |
| 136 if (__system_property_get(envname, system_property_value) > 0) |
| 137 return memchr(kTrueValues, system_property_value[0], sizeof(kTrueValues)); |
| 138 return dflt; |
| 139 } |
| 140 |
| 141 inline int EnvToInt(const char* envname, int dflt) { |
| 142 char system_property_value[PROP_VALUE_MAX]; |
| 143 if (__system_property_get(envname, system_property_value) > 0) |
| 144 return strtol(system_property_value, NULL, 10); |
| 145 return dflt; |
| 146 } |
| 147 |
| 148 inline int64 EnvToInt64(const char* envname, int64 dflt) { |
| 149 char system_property_value[PROP_VALUE_MAX]; |
| 150 if (__system_property_get(envname, system_property_value) > 0) |
| 151 return strtoll(system_property_value, NULL, 10); |
| 152 return dflt; |
| 153 } |
| 154 |
| 155 inline double EnvToDouble(const char* envname, double dflt) { |
| 156 char system_property_value[PROP_VALUE_MAX]; |
| 157 if (__system_property_get(envname, system_property_value) > 0) |
| 158 return strtod(system_property_value, NULL); |
| 159 return dflt; |
| 160 } |
| 161 |
| 162 #else // defined(__ANDROID__) || defined(ANDROID) |
| 115 | 163 |
| 116 #define EnvToString(envname, dflt) \ | 164 #define EnvToString(envname, dflt) \ |
| 117 (!getenv(envname) ? (dflt) : getenv(envname)) | 165 (!getenv(envname) ? (dflt) : getenv(envname)) |
| 118 | 166 |
| 119 #define EnvToBool(envname, dflt) \ | 167 #define EnvToBool(envname, dflt) \ |
| 120 (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL) | 168 (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL) |
| 121 | 169 |
| 122 #define EnvToInt(envname, dflt) \ | 170 #define EnvToInt(envname, dflt) \ |
| 123 (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10)) | 171 (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10)) |
| 124 | 172 |
| 125 #define EnvToInt64(envname, dflt) \ | 173 #define EnvToInt64(envname, dflt) \ |
| 126 (!getenv(envname) ? (dflt) : strtoll(getenv(envname), NULL, 10)) | 174 (!getenv(envname) ? (dflt) : strtoll(getenv(envname), NULL, 10)) |
| 127 | 175 |
| 128 #define EnvToDouble(envname, dflt) \ | 176 #define EnvToDouble(envname, dflt) \ |
| 129 (!getenv(envname) ? (dflt) : strtod(getenv(envname), NULL)) | 177 (!getenv(envname) ? (dflt) : strtod(getenv(envname), NULL)) |
| 130 | 178 |
| 179 #endif // defined(__ANDROID__) || defined(ANDROID) |
| 180 |
| 131 #endif // BASE_COMMANDLINEFLAGS_H_ | 181 #endif // BASE_COMMANDLINEFLAGS_H_ |
| OLD | NEW |