Grok 20.3.2
EnvVarManager.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <cstdlib>
21#include <cstring>
22#include <cctype>
23#include <string>
24#include <optional>
25
26namespace grk
27{
28
30{
31public:
32 // Retrieve an environment variable as a string (returns empty optional if unset)
33 static std::optional<std::string> get(const char* name)
34 {
35 const char* value = std::getenv(name);
36 if(value && *value)
37 {
38 return std::string(value);
39 }
40 return std::nullopt;
41 }
42
43 // Test if a variable is a boolean (mimics GDAL's CPLTestBool)
44 static bool test_bool(const char* name, bool default_value = false)
45 {
46 auto value = get(name);
47 if(!value)
48 {
49 return default_value;
50 }
51 const char* str = value->c_str();
52 if(!str || !*str)
53 {
54 return default_value;
55 }
56
57 // Case-insensitive comparison
58 char buffer[16];
59 size_t i = 0;
60 for(; str[i] && i < sizeof(buffer) - 1; ++i)
61 {
62 buffer[i] = static_cast<char>(std::tolower(static_cast<unsigned char>(str[i])));
63 }
64 buffer[i] = '\0';
65
66 return std::strcmp(buffer, "true") == 0 || std::strcmp(buffer, "on") == 0 ||
67 std::strcmp(buffer, "yes") == 0 || std::strcmp(buffer, "1") == 0;
68 }
69
70 // Get an environment variable as an integer (returns default_value if unset or invalid)
71 static long get_int(const char* name, long default_value = 0)
72 {
73 auto value = get(name);
74 if(!value)
75 {
76 return default_value;
77 }
78 try
79 {
80 return std::stol(*value);
81 }
82 catch(...)
83 {
84 return default_value;
85 }
86 }
87
88 // Get an environment variable as a string (returns default_value if unset)
89 static std::string get_string(const char* name, const std::string& default_value = "")
90 {
91 auto value = get(name);
92 return value.value_or(default_value);
93 }
94};
95
96} // namespace grk
Definition EnvVarManager.h:30
static bool test_bool(const char *name, bool default_value=false)
Definition EnvVarManager.h:44
static std::optional< std::string > get(const char *name)
Definition EnvVarManager.h:33
static long get_int(const char *name, long default_value=0)
Definition EnvVarManager.h:71
static std::string get_string(const char *name, const std::string &default_value="")
Definition EnvVarManager.h:89
ResWindow.
Definition CompressedChunkCache.h:36