How to expand environment variables in .ini files using Boost

我的未来我决定 提交于 2019-11-26 18:35:24

问题


I have a INI file like

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

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

I was expecting something like

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

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

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


回答1:


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

  • 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

The essence:

#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";
}

Which, on my box, prints:

'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 Of course, "properly" is subjective. At the very least, I think this

  • would be useful (how else would you configure a value legitimitely containing %?)
  • is how cmd.exe does it on Windows



回答2:


I'm pretty sure that this could be done trivally (see my newer answer) using a handwritten parser, but I'm personally a fan of Spirit:

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

Meaning:

  • take all non-% chars, if any
  • then take any word from inside %s and pass it through safe_getenv before appending

Now, safe_getenv is a trivial wrapper:

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

Here's a complete minimal implementation:

#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");
}

This prints

Greeting is /home/sehe world!

on my box

Notes

  • this is not optimized (well, not beyond compiling the rule once)
  • replace_regex_copy would do as nicely and more efficient (?)
  • see this answer for a slightly more involved 'expansion' engine: Compiling a simple parser with Boost.Spirit

    • using output iterator instead of std::string for accumulation
    • allowing nested variables
    • allowing escapes


来源:https://stackoverflow.com/questions/17112494/how-to-expand-environment-variables-in-ini-files-using-boost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!