1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/tsl/util/env_var.h"
17
18#include <stdlib.h>
19
20#include "tensorflow/tsl/platform/errors.h"
21#include "tensorflow/tsl/platform/logging.h"
22#include "tensorflow/tsl/platform/numbers.h"
23#include "tensorflow/tsl/platform/str_util.h"
24#include "tensorflow/tsl/platform/strcat.h"
25
26namespace tsl {
27
28Status ReadBoolFromEnvVar(StringPiece env_var_name, bool default_val,
29 bool* value) {
30 *value = default_val;
31 const char* tf_env_var_val = getenv(string(env_var_name).c_str());
32 if (tf_env_var_val == nullptr) {
33 return OkStatus();
34 }
35 string str_value = absl::AsciiStrToLower(tf_env_var_val);
36 if (str_value == "0" || str_value == "false") {
37 *value = false;
38 return OkStatus();
39 } else if (str_value == "1" || str_value == "true") {
40 *value = true;
41 return OkStatus();
42 }
43 return errors::InvalidArgument(strings::StrCat(
44 "Failed to parse the env-var ${", env_var_name, "} into bool: ",
45 tf_env_var_val, ". Use the default value: ", default_val));
46}
47
48Status ReadInt64FromEnvVar(StringPiece env_var_name, int64_t default_val,
49 int64_t* value) {
50 *value = default_val;
51 const char* tf_env_var_val = getenv(string(env_var_name).c_str());
52 if (tf_env_var_val == nullptr) {
53 return OkStatus();
54 }
55 if (strings::safe_strto64(tf_env_var_val, value)) {
56 return OkStatus();
57 }
58 return errors::InvalidArgument(strings::StrCat(
59 "Failed to parse the env-var ${", env_var_name, "} into int64: ",
60 tf_env_var_val, ". Use the default value: ", default_val));
61}
62
63Status ReadFloatFromEnvVar(StringPiece env_var_name, float default_val,
64 float* value) {
65 *value = default_val;
66 const char* tf_env_var_val = getenv(string(env_var_name).c_str());
67 if (tf_env_var_val == nullptr) {
68 return OkStatus();
69 }
70 if (strings::safe_strtof(tf_env_var_val, value)) {
71 return OkStatus();
72 }
73 return errors::InvalidArgument(strings::StrCat(
74 "Failed to parse the env-var ${", env_var_name, "} into float: ",
75 tf_env_var_val, ". Use the default value: ", default_val));
76}
77
78Status ReadStringFromEnvVar(StringPiece env_var_name, StringPiece default_val,
79 string* value) {
80 const char* tf_env_var_val = getenv(string(env_var_name).c_str());
81 if (tf_env_var_val != nullptr) {
82 *value = tf_env_var_val;
83 } else {
84 *value = string(default_val);
85 }
86 return OkStatus();
87}
88
89Status ReadStringsFromEnvVar(StringPiece env_var_name, StringPiece default_val,
90 std::vector<string>* value) {
91 string str_val;
92 TF_RETURN_IF_ERROR(ReadStringFromEnvVar(env_var_name, default_val, &str_val));
93 *value = str_util::Split(str_val, ',');
94 return OkStatus();
95}
96
97} // namespace tsl
98