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

时间:2013-06-14 15:51:38

标签: c++ boost ini boost-propertytree

我有一个像

这样的INI文件
[Section1]
Value1 = /home/%USER%/Desktop
Value2 = /home/%USER%/%SOME_ENV%/Test

并希望使用Boost解析它。我尝试使用Boost property_tree,如

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;

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

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

我期待像

这样的东西
/home/Maverick/Desktop
/home/Maverick/Doc/Test

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

我很感激使用boost解析这种文件的任何提示。

2 个答案:

答案 0 :(得分:2)

这是使用旧工艺

的另一种观点
  • 不要求精神,或者确实提升
  • 不要将接口硬连接到std::string(而是允许输入迭代器输出迭代器的任意组合)
  • 正确处理%%% 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!")  << "'\n";

    // 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)) << "'\n";

    // 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("")                           << "'\n";
    std::cout << "'" << expand_env("%HOME%")                     << "'\n";
    std::cout << "'" << expand_env(" %HOME%")                    << "'\n";
    std::cout << "'" << expand_env("%HOME% ")                    << "'\n";
    std::cout << "'" << expand_env("%HOME%%HOME%")               << "'\n";
    std::cout << "'" << expand_env(" %HOME%%HOME% ")             << "'\n";
    std::cout << "'" << expand_env(" %HOME% %HOME% ")            << "'\n";
}

在我的包装盒上打印:

'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 当然,“正确”是主观的。至少,我认为这是

  • 会很有用(如何配置合法包含%的值?)
  • 是cmd.exe在Windows上执行此操作的方式

答案 1 :(得分:1)

我很确定这可以通过手写解析器轻松完成( see my newer answer ),但我个人是Spirit的粉丝:

grammar %= (*~char_("%")) % as_string ["%" >> +~char_("%") >> "%"] 
                                      [ _val += phx::bind(safe_getenv, _1) ];

含义:

  • 取所有非%字符,如果有的话
  • 然后从%内部获取任何字词,并在附加<{li>}之前将其传递给safe_getenv

现在,safe_getenv是一个简单的包装器:

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

这是一个完整的最小实现:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

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

std::string expand_env(std::string const& input) 
{
    using namespace boost::spirit::qi;
    using boost::phoenix::bind;

    static const rule<std::string::const_iterator, std::string()> compiled =
          *(~char_("%"))                         [ _val+=_1 ] 
        % as_string ["%" >> +~char_("%") >> "%"] [ _val += bind(safe_getenv, _1) ];

    std::string::const_iterator f(input.begin()), l(input.end());
    std::string result;

    parse(f, l, compiled, result);
    return result;
}

int main()
{
    std::cout << expand_env("Greeting is %HOME% world!\n");
}

打印

Greeting is /home/sehe world!

在我的盒子上

备注

  • 这不是优化的(好吧,不仅仅是编译规则一次)
  • replace_regex_copy会做得更好,效率更高(?)
  • 请参阅此答案,了解更具参与性的“扩展”引擎:Compiling a simple parser with Boost.Spirit

    • 使用输出迭代器代替std::string进行累积
    • 允许嵌套变量
    • 允许逃跑