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 inline const char* EnvToString(const char* envname, const char* dflt) { | |
125 static char system_property_value[PROP_VALUE_MAX]; | |
bulach
2013/05/01 17:55:30
nit: s/static//
Dai Mikurube (NOT FULLTIME)
2013/05/02 16:26:40
It actually needs static since it returns the stri
| |
126 if (__system_property_get(envname, system_property_value) > 0) | |
127 return system_property_value; | |
128 return dflt; | |
129 } | |
130 | |
131 inline bool EnvToBool(const char* envname, bool dflt) { | |
132 char system_property_value[PROP_VALUE_MAX]; | |
bulach
2013/05/01 17:55:30
nit: const char kTrueValues[] = "tTyY1"; then size
Dai Mikurube (NOT FULLTIME)
2013/05/02 16:26:40
Done.
| |
133 if (__system_property_get(envname, system_property_value) > 0) | |
134 return memchr("tTyY1\0", system_property_value[0], 6); | |
135 return dflt; | |
136 } | |
137 | |
138 inline int EnvToInt(const char* envname, int dflt) { | |
139 char system_property_value[PROP_VALUE_MAX]; | |
140 if (__system_property_get(envname, system_property_value) > 0) | |
141 return strtol(system_property_value, NULL, 10); | |
142 return dflt; | |
143 } | |
144 | |
145 inline int64 EnvToInt64(const char* envname, int64 dflt) { | |
146 char system_property_value[PROP_VALUE_MAX]; | |
147 if (__system_property_get(envname, system_property_value) > 0) | |
148 return strtoll(system_property_value, NULL, 10); | |
149 return dflt; | |
150 } | |
151 | |
152 inline double EnvToDouble(const char* envname, double dflt) { | |
153 char system_property_value[PROP_VALUE_MAX]; | |
154 if (__system_property_get(envname, system_property_value) > 0) | |
155 return strtod(system_property_value, NULL); | |
156 return dflt; | |
157 } | |
158 | |
159 #else // defined(__ANDROID__) || defined(ANDROID) | |
115 | 160 |
116 #define EnvToString(envname, dflt) \ | 161 #define EnvToString(envname, dflt) \ |
117 (!getenv(envname) ? (dflt) : getenv(envname)) | 162 (!getenv(envname) ? (dflt) : getenv(envname)) |
118 | 163 |
119 #define EnvToBool(envname, dflt) \ | 164 #define EnvToBool(envname, dflt) \ |
120 (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL) | 165 (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL) |
121 | 166 |
122 #define EnvToInt(envname, dflt) \ | 167 #define EnvToInt(envname, dflt) \ |
123 (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10)) | 168 (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10)) |
124 | 169 |
125 #define EnvToInt64(envname, dflt) \ | 170 #define EnvToInt64(envname, dflt) \ |
126 (!getenv(envname) ? (dflt) : strtoll(getenv(envname), NULL, 10)) | 171 (!getenv(envname) ? (dflt) : strtoll(getenv(envname), NULL, 10)) |
127 | 172 |
128 #define EnvToDouble(envname, dflt) \ | 173 #define EnvToDouble(envname, dflt) \ |
129 (!getenv(envname) ? (dflt) : strtod(getenv(envname), NULL)) | 174 (!getenv(envname) ? (dflt) : strtod(getenv(envname), NULL)) |
130 | 175 |
176 #endif // defined(__ANDROID__) || defined(ANDROID) | |
177 | |
131 #endif // BASE_COMMANDLINEFLAGS_H_ | 178 #endif // BASE_COMMANDLINEFLAGS_H_ |
OLD | NEW |