使用boost Spririt解析文本文件,二进制包 [英] Parsing text file with binary envelope using boost Spririt

查看:531
本文介绍了使用boost Spririt解析文本文件,二进制包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在尝试写一个由小信封,校验包围ASCII文本文件解析器。

该文件的基本结构是:< 0X02><文件有效载荷><×03>< 16位CRC>

和我想提取有效载荷在另一个字符串给它喂
下一个解析器。

我用解析这个包分析器前pression是:

 齐:: phrase_parse(
    第一个,最后,
    烧焦_('\\ X02')>> *打印>>烧焦_('\\ X02')>> * xdigit,
    空间
);

输入消耗......我已经尝试过倾倒了有效载荷:

 齐:: phrase_parse(
    第一个,最后,
    烧焦_('\\ X02')>> *打印[COUT<< _1>>烧焦_('\\ X02')>> * xdigit,
    空间
);

但问题是,省略每换行,空白等!

现在我的问题:


  1. 我如何提取0X02 /×03(ETX / STX)字节之间的内容
    不正确忽略空格,换行等。


  2. 和是我的方法先取出信封,然后解析
    有效载荷好或有另一种更好的方法,我应该使用?



解决方案

使用例如齐::谋求/气:: confix让你开始(库的<一个部分都href=\"http://www.boost.org/doc/libs/1_57_0/libs/spirit/repository/doc/html/spirit_repository/qi_components/directives/confix.html\" rel=\"nofollow\">http://www.boost.org/doc/libs/1_57_0/libs/spirit/repository/doc/html/spirit_repository/qi_components/directives/confix.html).


  

但问题是,省略每换行,空白等!


嗯,这是<一个href=\"http://stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965\">what一个船长确实。不要使用,或者:

使用补气生:: []

要提取文本介入,我建议使用补气生:: 。虽然我不知道你其实想将它复制到一个字符串(复制听起来昂贵)。当源是一个流(或输入迭代器的其他来源),你能做到这一点可能。

精囊炎的规则:

  MyRule的='\\ X02'&GT;原料[*(char_  - '\\ X03')]≥ '\\ X03';

您可以添加校验:

  MyRule的='\\ X02'&GT;原料[*(char_  - '\\ X03')] [_a = _checksum(_1)]≥ '\\ X03'&GT;&GT;齐::字(_a);

假设


  • 齐::当地人&LT; uint16_t&GT;

  • _checksum 是一个合适的凤凰仿函数,它接受一个对源迭代器返回的 uint16_t

当然,你可能preFER保持校验解析器之外。

I'm currently trying to write a parser for an ASCII text file that is surrounded by a small envelope with checksum.

The basic structure of the file is: <0x02><"File payload"><0x03><16bit CRC>

and I want to extract the payload in another string to feed it to the next parser.

The parser expression I use to parse this envelope is:

qi::phrase_parse(
    first, last,
    char_('\x02') >> *print >> char_('\x02') >> *xdigit,
    space
);

The input is consumed... and I already tried to dump out the payload:

qi::phrase_parse(
    first, last,
    char_('\x02') >> *print[cout << _1] >> char_('\x02') >> *xdigit,
    space
);

But problem is that every newline, blank etc. is omitted!

Now my questions:

  1. How do I extract the content between the 0x02/0x03 (ETX/STX) bytes correctly without omitting spaces, newlines etc.

  2. And is my approach to first remove the envelope and then parse the payload good or is there another better approach I should use?

解决方案

Use e.g. qi::seek/qi::confix to get you started (both part of the repository http://www.boost.org/doc/libs/1_57_0/libs/spirit/repository/doc/html/spirit_repository/qi_components/directives/confix.html).

But problem is that every newline, blank etc. is omitted!

Well, that's what a skipper does. Don't use one, or:

Use qi::raw[]

To extract the intervening text, I suggest using qi::raw. Although I'm not sure you actually want to copy it to a string (copying sounds expensive). You could do this probably when the source is a stream (or other source of input iterators).

Seminal rule:

myrule = '\x02' > raw [ *(char_ - '\x03') ] > '\x03';

You could add the checksumming:

myrule = '\x02' > raw [ *(char_ - '\x03') ] [ _a = _checksum(_1) ] > '\x03' >> qi::word(_a);

Assuming

  • qi::locals<uint16_t>
  • _checksum is a suitable Phoenix functor that takes a pair of source iterators and returns uint16_t

Of course you might prefer to keep checksumming outside the parser.

这篇关于使用boost Spririt解析文本文件,二进制包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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