#include <string>
#include <iostream>
#include <regex>
void replaceSystemEnviromentTokens(std::string& var)
{
static std::string tokenOpening = "\\[\\[";
static std::string tokenClosure = "\\]\\]";
auto getEnvVal = [](const std::string& var) {
char* val = getenv(var.c_str());
return std::string(val ? val : "");
};
std::regex re(tokenOpening + "(\\S+)" + tokenClosure);
std::smatch m;
while (std::regex_search(var, m, re)) {
auto value = getEnvVal(m.str(1));
if (!value.empty()) {
std::regex re(tokenOpening + m.str(1) + tokenClosure);
var = regex_replace(var, re, value);
}
}
}
int main(int argc, char** argv)
{
putenv(R"(VULKAN_SDK=C:\VulkanSDK\1.2.148.1)");
std::string test = "this should be converted: [[VULKAN_SDK]] this should not be converted: VULKAN_SDK]] this: [[VULKAN_SDK]]";
replaceSystemEnviromentTokens(test);
std::cout << test << std::endl;
test = "nothing to convert here";
replaceSystemEnviromentTokens(test);
std::cout << test << std::endl;
test = "something to convert here: [[PATH]]";
replaceSystemEnviromentTokens(test);
std::cout << test << std::endl;
}