<i id='fDRTQ'><tr id='fDRTQ'><dt id='fDRTQ'><q id='fDRTQ'><span id='fDRTQ'><b id='fDRTQ'><form id='fDRTQ'><ins id='fDRTQ'></ins><ul id='fDRTQ'></ul><sub id='fDRTQ'></sub></form><legend id='fDRTQ'></legend><bdo id='fDRTQ'><pre id='fDRTQ'><center id='fDRTQ'></center></pre></bdo></b><th id='fDRTQ'></th></span></q></dt></tr></i><div id='fDRTQ'><tfoot id='fDRTQ'></tfoot><dl id='fDRTQ'><fieldset id='fDRTQ'></fieldset></dl></div>

<small id='fDRTQ'></small><noframes id='fDRTQ'>

<tfoot id='fDRTQ'></tfoot>
      <bdo id='fDRTQ'></bdo><ul id='fDRTQ'></ul>

      1. <legend id='fDRTQ'><style id='fDRTQ'><dir id='fDRTQ'><q id='fDRTQ'></q></dir></style></legend>
      2. 如何使用 Boost 扩展 .ini 文件中的环境变量

        时间:2023-07-20

          <tfoot id='JG6zg'></tfoot>
          <i id='JG6zg'><tr id='JG6zg'><dt id='JG6zg'><q id='JG6zg'><span id='JG6zg'><b id='JG6zg'><form id='JG6zg'><ins id='JG6zg'></ins><ul id='JG6zg'></ul><sub id='JG6zg'></sub></form><legend id='JG6zg'></legend><bdo id='JG6zg'><pre id='JG6zg'><center id='JG6zg'></center></pre></bdo></b><th id='JG6zg'></th></span></q></dt></tr></i><div id='JG6zg'><tfoot id='JG6zg'></tfoot><dl id='JG6zg'><fieldset id='JG6zg'></fieldset></dl></div>
          • <bdo id='JG6zg'></bdo><ul id='JG6zg'></ul>
                <tbody id='JG6zg'></tbody>
            • <small id='JG6zg'></small><noframes id='JG6zg'>

                <legend id='JG6zg'><style id='JG6zg'><dir id='JG6zg'><q id='JG6zg'></q></dir></style></legend>

                  本文介绍了如何使用 Boost 扩展 .ini 文件中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个类似

                  [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 上的运行方式

                  这篇关于如何使用 Boost 扩展 .ini 文件中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std::tuple get() 成员函数 下一篇:ACE vs Boost vs POCO

                  相关文章

                        <bdo id='cnkpW'></bdo><ul id='cnkpW'></ul>
                    1. <i id='cnkpW'><tr id='cnkpW'><dt id='cnkpW'><q id='cnkpW'><span id='cnkpW'><b id='cnkpW'><form id='cnkpW'><ins id='cnkpW'></ins><ul id='cnkpW'></ul><sub id='cnkpW'></sub></form><legend id='cnkpW'></legend><bdo id='cnkpW'><pre id='cnkpW'><center id='cnkpW'></center></pre></bdo></b><th id='cnkpW'></th></span></q></dt></tr></i><div id='cnkpW'><tfoot id='cnkpW'></tfoot><dl id='cnkpW'><fieldset id='cnkpW'></fieldset></dl></div>
                      <legend id='cnkpW'><style id='cnkpW'><dir id='cnkpW'><q id='cnkpW'></q></dir></style></legend>
                      <tfoot id='cnkpW'></tfoot>
                    2. <small id='cnkpW'></small><noframes id='cnkpW'>