Parsing a binary envelope text file using boost Spririt

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

Basic file structure: <0x02> <"File payload"> <0x03> <16bit CRC>

and I want to extract the payload on another line to the next parser.

The parser expression that I use to parse this shell looks like this:

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

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

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

But the problem is that every new line, space, etc. omitted!

Now my questions are:

  • How to extract contents between bytes 0x02 / 0x03 (ETX / STX) correctly, without omitting spaces, newlines, etc.

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

+1
source share
1 answer

Use for example. qi :: seek / qi :: confix to get you started (both parts 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 the problem is that every new line, space, etc. omitted!

Good thing the skipper does . Do not use it, or:

Use qi::raw[]

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

Seed Rule:

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

You can add checksums:

 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 may prefer to keep checksums outside the parser.

+1
source

Source: https://habr.com/ru/post/1216475/


All Articles