如何使用 Boost 扩展 .ini 文件中的环境变量

2021-12-24 00:00:00 ini c++ boost boost-propertytree

我有一个类似

[Section1]
Value1 = /home/%USER%/Desktop
Value2 = /home/%USER%/%SOME_ENV%/Test

并想使用 Boost 解析它.我尝试使用 Boost property_tree 之类的

and want to parse it using Boost. I tried using Boost property_tree like

boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini("config.ini", pt);

std::cout << pt.get<std::string>("Section1.Value1") << std::endl;
std::cout << pt.get<std::string>("Section1.Value2") << std::endl;

但是它没有扩展环境变量.输出看起来像

But it didn't expand the environment variable. Output looks like

/home/%USER%/Desktop
/home/%USER%/%SOME_ENV%/Test

我期待类似的东西

/home/Maverick/Desktop
/home/Maverick/Doc/Test

我不确定是否可以使用 boost property_tree.

I am not sure if it is even possible with boost property_tree.

如果您有任何使用 boost 解析此类文件的提示,我将不胜感激.

I would appreciate any hint to parse this kind of file using boost.

推荐答案

这是另一种使用旧工艺的方法:

And here's another take on it, using the old crafts:

  • 不需要精神,或者确实需要提升
  • 不将接口硬连接到std::string(而是允许输入迭代器和输出迭代器的任意组合)
  • %% 作为单个 % 正确地"处理 1
  • not requiring Spirit, or indeed Boost
  • not hardwiring the interface to std::string (instead allowing any combination of input iterators and output iterator)
  • handling %% "properly" as a single % 1

本质:

#include <string>
#include <algorithm>

static std::string safe_getenv(std::string const& macro) {
    auto var = getenv(macro.c_str());
    return var? var : macro;
}

template <typename It, typename Out>
Out expand_env(It f, It l, Out o)
{
    bool in_var = false;
    std::string accum;
    while (f!=l)
    {
        switch(auto ch = *f++)
        {
            case '%':
                if (in_var || (*f!='%'))
                {
                    in_var = !in_var;
                    if (in_var) 
                        accum.clear();
                    else
                    {
                        accum = safe_getenv(accum);
                        o = std::copy(begin(accum), end(accum), o);
                    }
                    break;
                } else 
                    ++f; // %% -> %
            default:
                if (in_var)
                    accum += ch;
                else
                    *o++ = ch;
        }
    }
    return o;
}

#include <iterator>

std::string expand_env(std::string const& input)
{
    std::string result;
    expand_env(begin(input), end(input), std::back_inserter(result));
    return result;
}

#include <iostream>
#include <sstream>
#include <list>

int main()
{
    // same use case as first answer, show `%%` escape
    std::cout << "'" << expand_env("Greeti%%ng is %HOME% world!")  << "'
";

    // can be done streaming, to any container
    std::istringstream iss("Greeti%%ng is %HOME% world!");
    std::list<char> some_target;

    std::istreambuf_iterator<char> f(iss), l;
    expand_env(f, l, std::back_inserter(some_target));
    std::cout << "Streaming results: '" << std::string(begin(some_target), end(some_target)) << "'
";

    // some more edge cases uses to validate the algorithm (note `%%` doesn't
    // act as escape if the first ends a 'pending' variable)
    std::cout << "'" << expand_env("")                           << "'
";
    std::cout << "'" << expand_env("%HOME%")                     << "'
";
    std::cout << "'" << expand_env(" %HOME%")                    << "'
";
    std::cout << "'" << expand_env("%HOME% ")                    << "'
";
    std::cout << "'" << expand_env("%HOME%%HOME%")               << "'
";
    std::cout << "'" << expand_env(" %HOME%%HOME% ")             << "'
";
    std::cout << "'" << expand_env(" %HOME% %HOME% ")            << "'
";
}

我的盒子上印有:

'Greeti%ng is /home/sehe world!'
Streaming results: 'Greeti%ng is /home/sehe world!'
''
'/home/sehe'
' /home/sehe'
'/home/sehe '
'/home/sehe/home/sehe'
' /home/sehe/home/sehe '
' /home/sehe /home/sehe '

<小时>

1 当然,适当"是主观的.至少,我认为这是


1 Of course, "properly" is subjective. At the very least, I think this

  • 会很有用(您将如何配置一个合法包含 % 的值?)
  • cmd.exe 在 Windows 上的运行方式

相关文章