From 61026418d7794b47543366dca1c1961603780c99 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9D=B4=ED=95=9C=EC=A2=85/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84?= =?utf8?q?=EC=9E=90?= Date: Fri, 6 Jul 2018 11:09:14 +0900 Subject: [PATCH] Introduce EnvVar class (#1884) * Introduce EnvVar class EnvVar reads environment variables from system and can return the value as string, bool or int. Signed-off-by: Hanjoung Lee * Move EnvVar.h to nnfw/include/util Move the file and add namespace Signed-off-by: Hanjoung Lee --- include/util/EnvVar.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 include/util/EnvVar.h diff --git a/include/util/EnvVar.h b/include/util/EnvVar.h new file mode 100644 index 0000000..ec18678 --- /dev/null +++ b/include/util/EnvVar.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NNFW_UTIL_ENV_VAR__ +#define __NNFW_UTIL_ENV_VAR__ + +#include +#include +#include + + +namespace nnfw +{ +namespace util +{ + +class EnvVar +{ +public: + EnvVar(const std::string &key) + { + const char *value = std::getenv(key.c_str()); + if (value == nullptr) + { + // An empty string is considered as an empty value + _value = ""; + } + else + { + _value = value; + } + } + + std::string asString(const std::string &def) const + { + if (_value.empty()) + return def; + return _value; + } + + bool asBool(bool def) const + { + if (_value.empty()) + return def; + static const std::array false_list{"0", "OFF", "FALSE", "N", "NO"}; + return std::find(false_list.begin(), false_list.end(), _value); + } + + int asInt(int def) const + { + if (_value.empty()) + return def; + return std::stoi(_value); + } + +private: + std::string _value; +}; + +} // namespace util +} // namespace nnfw + +#endif // __NNFW_UTIL_ENV_VAR__ -- 2.7.4