33namespace fs = std::filesystem;
51template<ns_concept::StringRepresentable T, ns_concept::StringRepresentable U>
52void set(T&& name, U&& value, Replace replace)
69 const char * var = std::getenv(name.data());
70 return (var !=
nullptr)?
72 : Error(fmt.data, name);
82inline bool exists(std::string_view name, std::string_view value)
84 const char* value_real = getenv(name.data());
85 return_if(not value_real,
false);
86 return std::string_view{value_real} == value;
102 if (
int ret = wordexp(expanded.c_str(), &data, 0); ret == 0)
104 if (data.we_wordc > 0)
106 expanded = data.we_wordv[0];
115 case WRDE_BADCHAR: error =
"WRDE_BADCHAR";
break;
116 case WRDE_BADVAL: error =
"WRDE_BADVAL";
break;
117 case WRDE_CMDSUB: error =
"WRDE_CMDSUB";
break;
118 case WRDE_NOSPACE: error =
"WRDE_NOSPACE";
break;
119 case WRDE_SYNTAX: error =
"WRDE_SYNTAX";
break;
120 default: error =
"unknown";
122 return Error(
"E::{}", error);
134template<
typename T = std::
string>
137 const char* var = std::getenv(
"XDG_DATA_HOME");
139 const char* home = std::getenv(
"HOME");
140 return_if(not home, Error(
"E::HOME is undefined"));
141 return std::string{home} +
"/.local/share";
154 if ( fs::path{query}.is_absolute() )
156 return Error(
"E::Query should be a file name, not an absolute path");
159 for(fs::path directory : env_path
160 | std::views::split(
':')
161 | std::ranges::to<std::vector<std::string>>())
163 fs::path path_full = directory / query;
164 return_if(fs::exists(path_full), path_full);
166 return Error(
"E::File not found in PATH");
Common utility functions and helpers used throughout FlatImage.
Concept for types that can be represented as a string.
Enhanced error handling framework built on std::expected.
Simplified macros for common control flow patterns with optional logging.
Environment variable management utilities.
Value< std::string > expand(ns_concept::StringRepresentable auto &&var)
Performs variable expansion analogous to a POSIX shell.
Value< std::string > get_expected(std::string_view name)
Get the value of an environment variable.
Value< fs::path > search_path(std::string const &query)
Search the directories in the PATH variable for the given input file name.
bool exists(std::string_view name, std::string_view value)
Checks if variable exists and equals value.
Value< T > xdg_data_home() noexcept
Returns or computes the value of XDG_DATA_HOME.
void set(T &&name, U &&value, Replace replace)
Sets an environment variable.
std::string to_string(T &&t) noexcept
Converts a type to a string.
Enhanced expected type with integrated logging capabilities.
Compile-time string container with fixed size.