带有Boost Spirit的OBJ解析器-忽略评论 [英] OBJ Parser with Boost Spirit - Ignoring comments
问题描述
我正在尝试使用Boost Spirit库编写一个基本的OBJ文件加载器.尽管我使用标准std :: ifstreams使其工作,但我想知道是否可以使用内存映射文件对整个文件执行phrase_parse,因为它似乎提供了最佳性能,如
I'm trying to write a basic OBJ file loader using the Boost Spirit library. Although I got it working using the standard std::ifstreams, I'm wondering if it's possible to do a phrase_parse on the entire file using a memory mapped file, since it seems to provide the best performance as posted here.
我有以下代码,看起来似乎不错,但是当文件中有注释时,它就会中断.因此,我的问题是,如何使用Spririt忽略OBJ文件中以#"开头的注释?
I have the following code, which seems to work well, but it breaks when there is a comment in the file. So, my question is how do you ignore a comment that starts with a '#' in the OBJ file using Spririt?
struct vertex {
double x, y, z;
};
BOOST_FUSION_ADAPT_STRUCT(
vertex,
(double, x)
(double, y)
(double, z)
)
std::vector<vertex> b_vertices
boost::iostreams::mapped_file mmap(
path,
boost::iostreams::mapped_file::readonly);
const char* f = mmap.const_data();
const char* l = f + mmap.size();
using namespace boost::spirit::qi;
bool ok = phrase_parse(f,l,(("v" >> double_ >> double_ >> double_) |
("vn" >> double_ >> double_>> double_)) % eol ,
blank, b_vertices);
当没有注释或任何其他数据(顶点/法线除外)时,上述代码可以很好地工作.但是,当存在不同类型的数据时,解析器将失败(应该如此),我想知道是否有一种方法可以使它工作而又不重新解析每一行,因为它速度较慢(在我的测试中几乎是2.5倍) .谢谢!
The above code works well when there are no comments or any other data except vertices/normals. But when there is a different type of data the parser fails (as it should) and I'm wondering if there is a way to make it work without going back to parsing every line as it is slower (almost 2.5x in my tests). Thank you!
推荐答案
想到的最简单的方法是简单地使注释可跳过:
The simplest way that comes to mind is to simply make comments skippable:
bool ok = qi::phrase_parse(
f,l,
(
("v" >> qi::double_ >> qi::double_ >> qi::double_) |
("vn" >> qi::double_ >> qi::double_ >> qi::double_)
)
% qi::eol,
('#' >> *(qi::char_ - qi::eol) >> qi::eol | qi::blank), b_vertices);
请注意,如果#
出现在行内某处,这也会识别"注释.这可能很好(因为它将使解析失败,除非它是在其他有效输入行上尾随的注释).
Note that this also 'recognizes' comments if #
appears somewhere inside the line. This is probably just fine (as it would make the parsing fail, unless it was a comment trailing on an otherwise valid input line).
查看 在Coliru上直播
See it Live on Coliru
或者,使用一些凤凰魔法来处理注释行",就像处理"vn"或"v"行一样.
Alternatively, use some phoenix magic to handle "comment lines" just as you handle a "vn" or "v" line.
这篇关于带有Boost Spirit的OBJ解析器-忽略评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!