aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2019-08-12 03:59:52 -0700
committerChris Robinson <[email protected]>2019-08-12 03:59:52 -0700
commit1aaf65abfecbde8548f90b1d0b0308b21bd0776d (patch)
tree767a9d1c72bd78ea572890286be92a866033f36e /common
parent50d16d2422febe2f4f56e7f29794778b10606b3a (diff)
Add methods to get env vars as an optional
Diffstat (limited to 'common')
-rw-r--r--common/strutils.cpp27
-rw-r--r--common/strutils.h15
2 files changed, 40 insertions, 2 deletions
diff --git a/common/strutils.cpp b/common/strutils.cpp
new file mode 100644
index 00000000..0163de7b
--- /dev/null
+++ b/common/strutils.cpp
@@ -0,0 +1,27 @@
+
+#include "config.h"
+
+#include "strutils.h"
+
+#include <cstdlib>
+
+
+namespace al {
+
+al::optional<std::string> getenv(const char *envname)
+{
+ const char *str{std::getenv(envname)};
+ if(str && str[0] != '\0') return str;
+ return al::nullopt;
+}
+
+#ifdef _WIN32
+al::optional<std::wstring> getenv(const WCHAR *envname)
+{
+ const WCHAR *str{_wgetenv(envname)};
+ if(str && str[0] != L'\0') return str;
+ return al::nullopt;
+}
+#endif
+
+} // namespace al
diff --git a/common/strutils.h b/common/strutils.h
index 2bfd15fc..db9b07c6 100644
--- a/common/strutils.h
+++ b/common/strutils.h
@@ -1,12 +1,14 @@
#ifndef AL_STRUTILS_H
#define AL_STRUTILS_H
-#ifdef _WIN32
+#include <string>
+
+#include "aloptional.h"
+#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
-#include <string>
inline std::string wstr_to_utf8(const WCHAR *wstr)
{
@@ -40,4 +42,13 @@ inline std::wstring utf8_to_wstr(const char *str)
#endif
+namespace al {
+
+al::optional<std::string> getenv(const char *envname);
+#ifdef _WIN32
+al::optional<std::wstring> getenv(const WCHAR *envname);
+#endif
+
+} // namespace al
+
#endif /* AL_STRUTILS_H */