|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cerrno> |
| 4 | +#include <cstdlib> |
| 5 | +#include <cstring> |
| 6 | +#include <string_view> |
| 7 | + |
| 8 | +#include <runtime-base/logger.hh> |
| 9 | + |
| 10 | +struct AppEnvironmentVariable; |
| 11 | + |
| 12 | +namespace xamarin::android { |
| 13 | + class HostEnvironment |
| 14 | + { |
| 15 | + public: |
| 16 | + static void init () noexcept; |
| 17 | + |
| 18 | + [[gnu::flatten, gnu::always_inline]] |
| 19 | + static void set_variable (const char *name, const char *value) noexcept |
| 20 | + { |
| 21 | + log_debug (LOG_DEFAULT, " Variable {} = '{}'", optional_string (name), optional_string (value)); |
| 22 | + if (::setenv (name, value, 1) < 0) { |
| 23 | + log_warn (LOG_DEFAULT, "Failed to set environment variable '{}': {}", name, ::strerror (errno)); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + [[gnu::flatten, gnu::always_inline]] |
| 28 | + static void set_variable (std::string_view const& name, std::string_view const& value) noexcept |
| 29 | + { |
| 30 | + set_variable (name.data (), value.data ()); |
| 31 | + } |
| 32 | + |
| 33 | + [[gnu::flatten, gnu::always_inline]] |
| 34 | + static void set_system_property (const char *name, const char *value) noexcept |
| 35 | + { |
| 36 | + // TODO: should we **actually** try to set the system property here? Would that even work? Needs testing |
| 37 | + log_debug (LOG_DEFAULT, " System property {} = '{}'", optional_string (name), optional_string (value)); |
| 38 | + } |
| 39 | + |
| 40 | + [[gnu::flatten, gnu::always_inline]] |
| 41 | + static auto lookup_system_property (std::string_view const& name, size_t &value_len, |
| 42 | + uint32_t const count, AppEnvironmentVariable const (&entries)[], |
| 43 | + const char (&contents)[]) noexcept -> const char* |
| 44 | + { |
| 45 | + if (count == 0) { |
| 46 | + return nullptr; |
| 47 | + } |
| 48 | + |
| 49 | + for (size_t i = 0; i < count; i++) { |
| 50 | + AppEnvironmentVariable const& sys_prop = entries[i]; |
| 51 | + const char *prop_name = &contents[sys_prop.name_index]; |
| 52 | + if (name.compare (prop_name) != 0) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + const char *prop_value = &contents[sys_prop.value_index]; |
| 57 | + value_len = strlen (prop_value); |
| 58 | + return prop_value; |
| 59 | + } |
| 60 | + |
| 61 | + return nullptr; |
| 62 | + } |
| 63 | + |
| 64 | + template<void (*setter)(const char *name, const char *value) noexcept> [[gnu::flatten, gnu::always_inline]] |
| 65 | + static void set_values (uint32_t const& count, AppEnvironmentVariable const (&entries)[], const char (&contents)[]) noexcept |
| 66 | + { |
| 67 | + for (size_t i = 0; i < count; i++) { |
| 68 | + AppEnvironmentVariable const& env_var = entries[i]; |
| 69 | + const char *var_name = &contents[env_var.name_index]; |
| 70 | + const char *var_value = &contents[env_var.value_index]; |
| 71 | + |
| 72 | + setter (var_name, var_value); |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | +} |
0 commit comments