1// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "commandlineflags.h"
16
17#include <cctype>
18#include <cstdlib>
19#include <cstring>
20#include <iostream>
21#include <limits>
22
23namespace benchmark {
24namespace {
25
26// Parses 'str' for a 32-bit signed integer. If successful, writes
27// the result to *value and returns true; otherwise leaves *value
28// unchanged and returns false.
29bool ParseInt32(const std::string& src_text, const char* str, int32_t* value) {
30 // Parses the environment variable as a decimal integer.
31 char* end = nullptr;
32 const long long_value = strtol(str, &end, 10); // NOLINT
33
34 // Has strtol() consumed all characters in the string?
35 if (*end != '\0') {
36 // No - an invalid character was encountered.
37 std::cerr << src_text << " is expected to be a 32-bit integer, "
38 << "but actually has value \"" << str << "\".\n";
39 return false;
40 }
41
42 // Is the parsed value in the range of an Int32?
43 const int32_t result = static_cast<int32_t>(long_value);
44 if (long_value == std::numeric_limits<long>::max() ||
45 long_value == std::numeric_limits<long>::min() ||
46 // The parsed value overflows as a long. (strtol() returns
47 // LONG_MAX or LONG_MIN when the input overflows.)
48 result != long_value
49 // The parsed value overflows as an Int32.
50 ) {
51 std::cerr << src_text << " is expected to be a 32-bit integer, "
52 << "but actually has value \"" << str << "\", "
53 << "which overflows.\n";
54 return false;
55 }
56
57 *value = result;
58 return true;
59}
60
61// Parses 'str' for a double. If successful, writes the result to *value and
62// returns true; otherwise leaves *value unchanged and returns false.
63bool ParseDouble(const std::string& src_text, const char* str, double* value) {
64 // Parses the environment variable as a decimal integer.
65 char* end = nullptr;
66 const double double_value = strtod(str, &end); // NOLINT
67
68 // Has strtol() consumed all characters in the string?
69 if (*end != '\0') {
70 // No - an invalid character was encountered.
71 std::cerr << src_text << " is expected to be a double, "
72 << "but actually has value \"" << str << "\".\n";
73 return false;
74 }
75
76 *value = double_value;
77 return true;
78}
79
80// Returns the name of the environment variable corresponding to the
81// given flag. For example, FlagToEnvVar("foo") will return
82// "BENCHMARK_FOO" in the open-source version.
83static std::string FlagToEnvVar(const char* flag) {
84 const std::string flag_str(flag);
85
86 std::string env_var;
87 for (size_t i = 0; i != flag_str.length(); ++i)
88 env_var += static_cast<char>(::toupper(flag_str.c_str()[i]));
89
90 return "BENCHMARK_" + env_var;
91}
92
93} // namespace
94
95// Reads and returns the Boolean environment variable corresponding to
96// the given flag; if it's not set, returns default_value.
97//
98// The value is considered true iff it's not "0".
99bool BoolFromEnv(const char* flag, bool default_value) {
100 const std::string env_var = FlagToEnvVar(flag);
101 const char* const string_value = getenv(env_var.c_str());
102 return string_value == nullptr ? default_value
103 : strcmp(string_value, "0") != 0;
104}
105
106// Reads and returns a 32-bit integer stored in the environment
107// variable corresponding to the given flag; if it isn't set or
108// doesn't represent a valid 32-bit integer, returns default_value.
109int32_t Int32FromEnv(const char* flag, int32_t default_value) {
110 const std::string env_var = FlagToEnvVar(flag);
111 const char* const string_value = getenv(env_var.c_str());
112 if (string_value == nullptr) {
113 // The environment variable is not set.
114 return default_value;
115 }
116
117 int32_t result = default_value;
118 if (!ParseInt32(std::string("Environment variable ") + env_var, string_value,
119 &result)) {
120 std::cout << "The default value " << default_value << " is used.\n";
121 return default_value;
122 }
123
124 return result;
125}
126
127// Reads and returns the string environment variable corresponding to
128// the given flag; if it's not set, returns default_value.
129const char* StringFromEnv(const char* flag, const char* default_value) {
130 const std::string env_var = FlagToEnvVar(flag);
131 const char* const value = getenv(env_var.c_str());
132 return value == nullptr ? default_value : value;
133}
134
135// Parses a string as a command line flag. The string should have
136// the format "--flag=value". When def_optional is true, the "=value"
137// part can be omitted.
138//
139// Returns the value of the flag, or nullptr if the parsing failed.
140const char* ParseFlagValue(const char* str, const char* flag,
141 bool def_optional) {
142 // str and flag must not be nullptr.
143 if (str == nullptr || flag == nullptr) return nullptr;
144
145 // The flag must start with "--".
146 const std::string flag_str = std::string("--") + std::string(flag);
147 const size_t flag_len = flag_str.length();
148 if (strncmp(str, flag_str.c_str(), flag_len) != 0) return nullptr;
149
150 // Skips the flag name.
151 const char* flag_end = str + flag_len;
152
153 // When def_optional is true, it's OK to not have a "=value" part.
154 if (def_optional && (flag_end[0] == '\0')) return flag_end;
155
156 // If def_optional is true and there are more characters after the
157 // flag name, or if def_optional is false, there must be a '=' after
158 // the flag name.
159 if (flag_end[0] != '=') return nullptr;
160
161 // Returns the string after "=".
162 return flag_end + 1;
163}
164
165bool ParseBoolFlag(const char* str, const char* flag, bool* value) {
166 // Gets the value of the flag as a string.
167 const char* const value_str = ParseFlagValue(str, flag, true);
168
169 // Aborts if the parsing failed.
170 if (value_str == nullptr) return false;
171
172 // Converts the string value to a bool.
173 *value = IsTruthyFlagValue(value_str);
174 return true;
175}
176
177bool ParseInt32Flag(const char* str, const char* flag, int32_t* value) {
178 // Gets the value of the flag as a string.
179 const char* const value_str = ParseFlagValue(str, flag, false);
180
181 // Aborts if the parsing failed.
182 if (value_str == nullptr) return false;
183
184 // Sets *value to the value of the flag.
185 return ParseInt32(std::string("The value of flag --") + flag, value_str,
186 value);
187}
188
189bool ParseDoubleFlag(const char* str, const char* flag, double* value) {
190 // Gets the value of the flag as a string.
191 const char* const value_str = ParseFlagValue(str, flag, false);
192
193 // Aborts if the parsing failed.
194 if (value_str == nullptr) return false;
195
196 // Sets *value to the value of the flag.
197 return ParseDouble(std::string("The value of flag --") + flag, value_str,
198 value);
199}
200
201bool ParseStringFlag(const char* str, const char* flag, std::string* value) {
202 // Gets the value of the flag as a string.
203 const char* const value_str = ParseFlagValue(str, flag, false);
204
205 // Aborts if the parsing failed.
206 if (value_str == nullptr) return false;
207
208 *value = value_str;
209 return true;
210}
211
212bool IsFlag(const char* str, const char* flag) {
213 return (ParseFlagValue(str, flag, true) != nullptr);
214}
215
216bool IsTruthyFlagValue(const std::string& value) {
217 if (value.empty()) return true;
218 char ch = value[0];
219 return isalnum(ch) &&
220 !(ch == '0' || ch == 'f' || ch == 'F' || ch == 'n' || ch == 'N');
221}
222} // end namespace benchmark
223