使用 Boost.Program_options 处理 INI 文件的各个部分

发布于 2024-10-07 04:02:29 字数 617 浏览 0 评论 0原文

我正在尝试解析 Linux 中的配置 INI 文件。 我想使用 Boost,有人向我指出了程序选项库。

问题是我可以读取具有语法 field=value 的行,但是如何处理不同的部分,即其中具有 [Section_Name] 的行? 对于下面的代码,我

在我尝试的代码下面总是有一个例外。 谢谢AFG

 const char* testFileName = "file.ini";
 std::ifstream s;
 s.open(  testFileName );

 namespace pod = boost::program_options::detail;
 std::set<std::string> options;
 options.insert("a");
 options.insert("b");
 options.insert("c");

 //parser
 for (pod::config_file_iterator i(s, options), e ; i != e; ++i)
 {
    std::cout << i->value[0] << std::endl;
 }

I am trying to parse configuration INI files in Linux.
I would like to use Boost and someone pointed me the program options library.

The thing is that I can read lines having the syntax field=value, but how to deal with different sections i.e. lines having [Section_Name] in it?
With the code below I have always an exception

Below the code I tried.
Thanks AFG

 const char* testFileName = "file.ini";
 std::ifstream s;
 s.open(  testFileName );

 namespace pod = boost::program_options::detail;
 std::set<std::string> options;
 options.insert("a");
 options.insert("b");
 options.insert("c");

 //parser
 for (pod::config_file_iterator i(s, options), e ; i != e; ++i)
 {
    std::cout << i->value[0] << std::endl;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤 2024-10-14 04:02:29

我正在使用program_options中的parse_config_file,所以它可能会有所不同,但如果你有类似name=value的内容,选项的名称是SectionName.name[SectionName] 中。

std::string config_filename = "foo.ini";
namespace po = boost::program_options;
po::options_description my_options("Options");
int my_opt;
my_options.add_options()
    ("SectionName.my_opt", po::value(&my_opt)->default_value(64), "My option");
std::ifstream config_stream(config_filename.c_str());
po::variables_map vm;
po::store(po::parse_config_file(config_stream, my_options), vm);
po::notify(vm);
// value is now in my_opt, also accessible by vm["SectionName.my_opt"].as<int>()

I'm using parse_config_file from program_options, so it may be different, but there the name of the option is SectionName.name if you have something like name=value in [SectionName].

std::string config_filename = "foo.ini";
namespace po = boost::program_options;
po::options_description my_options("Options");
int my_opt;
my_options.add_options()
    ("SectionName.my_opt", po::value(&my_opt)->default_value(64), "My option");
std::ifstream config_stream(config_filename.c_str());
po::variables_map vm;
po::store(po::parse_config_file(config_stream, my_options), vm);
po::notify(vm);
// value is now in my_opt, also accessible by vm["SectionName.my_opt"].as<int>()
心头的小情儿 2024-10-14 04:02:29

正如 etarion 之前所述,选项的标识符必须以其封闭部分为前缀。这是对代码的简单修改来演示:

int main()
{
    std::stringstream s(
        "[Test]\n"
        "a = 1\n"
        "b = 2\n"
        "c = test option\n");

    std::set<std::string> options;
    options.insert("Test.a");
    options.insert("Test.b");
    options.insert("Test.c");

    for (boost::program_options::detail::config_file_iterator i(s, options), e ; i != e; ++i)
        std::cout << i->value[0] << std::endl;
}

该程序输出:

1
2
test option

As stated earlier by etarion, the identifier of the option must be prefixed by their enclosing section. Here is a simple modification on your code to demonstrate :

int main()
{
    std::stringstream s(
        "[Test]\n"
        "a = 1\n"
        "b = 2\n"
        "c = test option\n");

    std::set<std::string> options;
    options.insert("Test.a");
    options.insert("Test.b");
    options.insert("Test.c");

    for (boost::program_options::detail::config_file_iterator i(s, options), e ; i != e; ++i)
        std::cout << i->value[0] << std::endl;
}

This program outputs :

1
2
test option
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文