RAVENNAKIT
Loading...
Searching...
No Matches
env.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3 * Project: RAVENNAKIT (RAVENNA / AES67 / ST2110-30 SDK)
4 * Copyright (c) 2024-2025 Sound on Digital
5 *
6 * This file is part of RAVENNAKIT.
7 *
8 * RAVENNAKIT is dual-licensed:
9 * 1) Under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version (the "AGPL License"); and
12 * 2) Under a commercial license from Sound on Digital, for customers who
13 * cannot (or do not wish to) comply with the AGPL License terms.
14 *
15 * If you obtained this file under the AGPL License, you may redistribute it
16 * and/or modify it under the terms of the AGPL License. See the LICENSE
17 * file in the project root for details.
18 *
19 * For commercial licensing, support, and other inquiries, please visit:
20 *
21 * https://ravennakit.com
22 *
23 */
24
25#pragma once
26
27#include "platform.hpp"
28
29#if RAV_WINDOWS
30 #include <windows.h>
31 #include <processenv.h>
32#endif
33
34#include <string>
35#include <optional>
36#include <cassert>
37
38namespace rav {
39
45inline std::optional<std::string> get_env(const char* name) {
46#if RAV_WINDOWS
47 std::string value(64, 0);
48 auto length = GetEnvironmentVariableA(name, value.data(), static_cast<DWORD>(value.size()));
49 if (length == 0) {
50 return {};
51 }
52 if (length > value.size() - 1) {
53 value.resize(length, 0);
54 length = GetEnvironmentVariableA(name, value.data(), static_cast<DWORD>(value.size()));
55 assert(length == value.size() - 1);
56 }
57 value.resize(length);
58 return value;
59#else
60 if (auto* value = std::getenv(name)) {
61 return value;
62 }
63 return {};
64#endif
65}
66
67} // namespace rav
std::optional< std::string > get_env(const char *name)
Gets the value of an environment variable.
Definition env.hpp:45