Introduce EnvVar class (#1884)
author이한종/동작제어Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Fri, 6 Jul 2018 02:09:14 +0000 (11:09 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Fri, 6 Jul 2018 02:09:14 +0000 (11:09 +0900)
* Introduce EnvVar class

EnvVar reads environment variables from system and can return the
value as string, bool or int.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
* Move EnvVar.h to nnfw/include/util

Move the file and add namespace

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
include/util/EnvVar.h [new file with mode: 0644]

diff --git a/include/util/EnvVar.h b/include/util/EnvVar.h
new file mode 100644 (file)
index 0000000..ec18678
--- /dev/null
@@ -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 <array>
+#include <cstdlib>
+#include <string>
+
+
+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<std::string, 5> 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__