From 1aaf65abfecbde8548f90b1d0b0308b21bd0776d Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 12 Aug 2019 03:59:52 -0700 Subject: Add methods to get env vars as an optional --- common/strutils.cpp | 27 +++++++++++++++++++++++++++ common/strutils.h | 15 +++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 common/strutils.cpp (limited to 'common') 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 + + +namespace al { + +al::optional getenv(const char *envname) +{ + const char *str{std::getenv(envname)}; + if(str && str[0] != '\0') return str; + return al::nullopt; +} + +#ifdef _WIN32 +al::optional 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 + +#include "aloptional.h" +#ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include -#include 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 getenv(const char *envname); +#ifdef _WIN32 +al::optional getenv(const WCHAR *envname); +#endif + +} // namespace al + #endif /* AL_STRUTILS_H */ -- cgit v1.2.3