aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/mpt/environment/environment.hpp
blob: 4fb021b15b0b91837d5f561f3f10168984ae5141 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */

#ifndef MPT_ENVIRONMENT_ENVIRONMENT_HPP
#define MPT_ENVIRONMENT_ENVIRONMENT_HPP

#include "mpt/base/detect.hpp"
#include "mpt/base/macros.hpp"
#include "mpt/base/namespace.hpp"
#include "mpt/string/types.hpp"
#include "mpt/string_transcode/transcode.hpp"
#include "mpt/system_error/system_error.hpp"

#include <optional>
#if MPT_OS_WINDOWS
#if defined(UNICODE) && !MPT_OS_WINDOWS_WINRT
#include <vector>
#endif // !MPT_OS_WINDOWS_WINRT
#endif // MPT_OS_WINDOWS

#include <cstdlib>

#if MPT_OS_WINDOWS
#include <windows.h>
#endif // MPT_OS_WINDOWS



namespace mpt {
inline namespace MPT_INLINE_NS {



inline std::optional<mpt::ustring> getenv(const mpt::ustring & env_var) {
#if MPT_OS_WINDOWS && MPT_OS_WINDOWS_WINRT
	MPT_UNUSED(env_var);
	return std::nullopt;
#elif MPT_OS_WINDOWS && defined(UNICODE)
	std::vector<WCHAR> buf(32767);
	DWORD size = GetEnvironmentVariable(mpt::transcode<std::wstring>(env_var).c_str(), buf.data(), 32767);
	if (size == 0) {
		mpt::windows::ExpectError(ERROR_ENVVAR_NOT_FOUND);
		return std::nullopt;
	}
	return mpt::transcode<mpt::ustring>(buf.data());
#else
	const char * val = std::getenv(mpt::transcode<std::string>(mpt::environment_encoding, env_var).c_str());
	if (!val) {
		return std::nullopt;
	}
	return mpt::transcode<mpt::ustring>(mpt::environment_encoding, val);
#endif
}



} // namespace MPT_INLINE_NS
} // namespace mpt



#endif // MPT_ENVIRONMENT_ENVIRONMENT_HPP