你最好避免依赖于实现细节,所以我建议你预处理你的配置文件只是为了剥离 cmets。
将"//" 简单地替换为"; " 可能就足够了。
基于上一个答案:
std::string tmp;
{
std::ifstream ifs(file_name.c_str());
tmp.assign(std::istreambuf_iterator<char>(ifs), {});
} // closes file
boost::algorithm::replace_all(tmp, "//", ";");
std::istringstream preprocessed(tmp);
read_info(preprocessed, pt);
现在,如果您将输入更改为包含 cmets:
Resnet50 {
Layer CONV1 {
Type: CONV // this is a comment
Stride { X: 2, Y: 2 } ; this too
Dimensions { K: 64, C: 3, R: 7, S: 7, Y:224, X:224 }
}
// don't forget the CONV2_1_1 layer
Layer CONV2_1_1 {
Type: CONV
Stride { X: 1, Y: 1 }
Dimensions { K: 64, C: 64, R: 1, S: 1, Y: 56, X: 56 }
}
}
如果我们还扩展调试输出进行验证,它仍然可以按预期解析:
ptree const& resnet50 = pt.get_child("Resnet50");
for (auto& entry : resnet50) {
std::cout << entry.first << " " << entry.second.get_value("") << "\n";
std::cout << " --- Echoing the complete subtree:\n";
write_info(std::cout, entry.second);
}
打印
Layer CONV1
--- Echoing the complete subtree:
Type: CONV
Stride
{
X: 2,
Y: 2
}
Dimensions
{
K: 64,
C: 3,
R: 7,
S: 7,
Y:224, X:224
}
Layer CONV2_1_1
--- Echoing the complete subtree:
Type: CONV
Stride
{
X: 1,
Y: 1
}
Dimensions
{
K: 64,
C: 64,
R: 1,
S: 1,
Y: 56,
X: 56
}
看Live On Coliru
是的,但是...?
如果 '//' 出现在字符串文字中怎么办?不会也换了吧。是的。
这不是图书馆质量的解决方案。您不应该期望它,因为您不必付出任何努力来解析您的定制配置文件格式。
您是唯一可以判断这种方法的缺点是否对您造成问题的一方。
但是,除了复制和修改 Boost 的解析器或从头开始实现自己的解析器之外,没有什么可以做的。
对于受虐狂
如果您不想重新实现整个解析器,但仍希望“智能”跳过字符串文字,这里有一个 pre_process 函数可以完成所有这些。这一次,是真正使用了Boost Spirit
#include <boost/spirit/home/x3.hpp>
std::string pre_process(std::string const& input) {
std::string result;
using namespace boost::spirit::x3;
auto static string_literal
= raw[ '"' >> *('\\'>> char_ | ~char_('"')) >> '"' ];
auto static comment
= char_(';') >> *~char_("\r\n")
| "//" >> attr(';') >> *~char_("\r\n")
| omit["/*" >> *(char_ - "*/") >> "*/"];
auto static other
= +(~char_(";\"") - "//" - "/*");
auto static content
= *(string_literal | comment | other) >> eoi;
if (!parse(begin(input), end(input), content, result)) {
throw std::invalid_argument("pre_process");
}
return result;
}
如您所见,它识别字符串文字(带有转义),它处理“//”和“;”将 linewise cmets 样式设置为等效。为了“炫耀”,我加入了 /block cmets/,它不能用正确的 INFO 语法表示,所以我们只是 omit[] 他们。
现在让我们用一个时髦的例子进行测试(从文档中的"Complicated example demonstrating all INFO features" 扩展):
#include <boost/property_tree/info_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
int main() {
boost::property_tree::ptree pt;
std::istringstream iss(
pre_process(R"~~( ; A comment
key1 value1 // Another comment
key2 "value with /* no problem */ special // characters in it {};#\n\t\"\0"
{
subkey "value split "\
"over three"\
"lines"
{
a_key_without_value ""
"a key with special characters in it {};#\n\t\"\0" ""
"" value /* Empty key with a value */
"" /*also empty value: */ "" ; Empty key with empty value!
}
})~~"));
read_info(iss, pt);
std::cout << " --- Echoing the parsed tree:\n";
write_info(std::cout, pt);
}
打印 (Live On Coliru)
--- Echoing the parsed tree:
key1 value1
key2 "value with /* no problem */ special // characters in it {};#\n \"\0"
{
subkey "value split over threelines"
{
a_key_without_value ""
"a key with special characters in it {};#\n \"\0" ""
"" value
"" ""
}
}