diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/strutils.cpp | 27 | ||||
-rw-r--r-- | common/strutils.h | 15 |
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 */ |