cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
env.cpp
Go to the documentation of this file.
1/*
2* cpp-terminal
3* C++ library for writing multi-platform terminal applications.
4*
5* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
6*
7* SPDX-License-Identifier: MIT
8*/
9
11
13
14std::pair<bool, std::string> Term::Private::getenv(const std::string& key)
15{
16#if defined(_WIN32)
17 std::size_t size{0};
18 _wgetenv_s(&size, nullptr, 0, Term::Private::to_wide(key).c_str());
19 std::wstring ret;
20 if(size == 0 || size > ret.max_size()) return {false, std::string()};
21 ret.reserve(size);
22 _wgetenv_s(&size, &ret[0], size, Term::Private::to_wide(key).c_str());
23 return {true, Term::Private::to_narrow(ret)};
24#else
25 if(std::getenv(key.c_str()) != nullptr) { return {true, static_cast<std::string>(std::getenv(key.c_str()))}; }
26 return {false, std::string()};
27#endif
28}
std::wstring to_wide(const std::string &str)
Definition unicode.cpp:40
std::string to_narrow(const std::wstring &wstr)
Definition unicode.cpp:26
std::pair< bool, std::string > getenv(const std::string &env)
Value of an environment variables.
Definition env.cpp:14