在程序选项中使用hashmark值(ini文件) [英] using hashmark in program options value (ini file)

查看:214
本文介绍了在程序选项中使用hashmark值(ini文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用boost程序选项从ini文件读取数据有些麻烦。问题是包含散列符号(简单示例)的键:

I have some trouble reading from an ini file using boost program options. The problem is a key which contains hashmarks (simplyfied example):

[section]

key =xxx#yyy

[section]
key="xxx#yyy"

检索键,返回xxx,这是因为哈希标记似乎被解释为注释的开始,因此跳过其余行。不幸的是,我不能用'#'替换一些其他字符,因为值是一个正则表达式。我没有找到一个方法来引用哈希标记,并不喜欢这样做,因为它会改变我的正则表达式,使它更不可读。

Retrieving the key, returns "xxx", which is because the hashmark seems to be interpreted as start of a comment and therefore the rest of the line is skipped. Unfortunately I cannot substitute the '#' by some other character because the value is a regular expression. I didn't find a way to quote the hashmark and would prefer not to do it, because it would alter my regex and make it even more unreadable.

有方式来处理这个问题,而不重写ini文件解析器?
感谢任何帮助。

Is there a way to deal with this problem without rewriting the ini-file parser? Thanks for any help.

我检索密钥的代码如下:

My code to retrieve the key looks like:

std::string key;
boost::program_options::options_description opDesc("test");
opDesc.add_options()("section.key", po::value<string>(&key))
std::ifstream ifs("file.ini");
boost::program_options::parse_config_file(ifs, opDesc);


推荐答案

也许现在是开始使用 Boost属性树,因为您正在通过我正在解析程序options'点这里,真的。

Perhaps it is time to start using Boost Property Tree as you are way past the 'I'm parsing program options' point here, really.

http://www.boost.org/doc/libs/1_46_1/doc/html/property_tree.html

媒体资源树具有JSON,Xml,Ini(< - 你在这里)和INFO格式的解析器/格式化程序。链接页面在几行中指定了什么往返(大多数事情往返,除了J​​SON的唯一类型信息和有时候尾随的空格)。

Property tree has parsers/formatters for JSON, Xml, Ini (<-- you are here) and the INFO formats. The linked page specifies in a few lines exactly what round-trips (most things roundtrip, except for JSON sepcific type information and sometimes trailing whitespace).

像INI格式(因为它接近你使用的)和INFO设置(因为它有更多的字符串语法,除了分层嵌套的部分)。

I suppose you'd like the INI format (because it's close to what you're using) and the INFO setting (because it has more string syntax, in addition to hierarchically nested sections).

从示例:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <set>
#include <exception>
#include <iostream>

struct debug_settings
{
    std::string m_file;               // log filename
    int m_level;                      // debug level
    std::set<std::string> m_modules;  // modules where logging is enabled
    void load(const std::string &filename);
    void save(const std::string &filename);
};

void debug_settings::load(const std::string &filename)
{
    using boost::property_tree::ptree;
    ptree pt;
    read_xml(filename, pt);
    m_file = pt.get<std::string>("debug.filename");
    m_level = pt.get("debug.level", 0);
    BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.modules"))
        m_modules.insert(v.second.data());
}

void debug_settings::save(const std::string &filename)
{
    using boost::property_tree::ptree;
    ptree pt;
    pt.put("debug.filename", m_file);
    pt.put("debug.level", m_level);
    BOOST_FOREACH(const std::string &name, m_modules)
        pt.add("debug.modules.module", name);
    write_xml(filename, pt);
}

int main()
{
    try
    {
        debug_settings ds;
        ds.load("debug_settings.xml");
        ds.save("debug_settings_out.xml");
        std::cout << "Success\n";
    }
    catch (std::exception &e)
    {
        std::cout << "Error: " << e.what() << "\n";
    }
    return 0;
}

这篇关于在程序选项中使用hashmark值(ini文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆