parsing a char[12] field that is null filled

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

parsing a char[12] field that is null filled

Jeff Scaparra
I am having difficulty figuring out how to parse a 12 byte field that is
an ascii string followed by zero or more '\0' characters. I would like to
have a parser that would store the ascii chars in a std::string and ignore
the nulls.

"Hello World!" <- 12 bytes std::string = "Hello World!"
"Hello World\0" <- 11 byte std::string = "Hello World"
"Hi World\0\0\0\0" <- 8 byte std::string = "Hi World"

I was suggested that I might use a look-ahead and qi::local and raw[] but
I can't figure out how that should work.

This will be further complicated as this is part of a header and the
contents of this string determine the rest of the structure.

Any thoughts on how I can handle this?

Thanks in advance.


------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: parsing a char[12] field that is null filled

Lee Clagett-2
On Mon, Oct 12, 2015 at 6:44 PM, Jeff Scaparra <[hidden email]> wrote:
I am having difficulty figuring out how to parse a 12 byte field that is
an ascii string followed by zero or more '\0' characters. I would like to
have a parser that would store the ascii chars in a std::string and ignore
the nulls.

"Hello World!" <- 12 bytes std::string = "Hello World!"
"Hello World\0" <- 11 byte std::string = "Hello World"
"Hi World\0\0\0\0" <- 8 byte std::string = "Hi World"

I was suggested that I might use a look-ahead and qi::local and raw[] but
I can't figure out how that should work.

This will be further complicated as this is part of a header and the
contents of this string determine the rest of the structure.

Any thoughts on how I can handle this?

Thanks in advance.


qi::repeat(0, 12)[qi::char_ - qi::lit('\0')] >> *(qi::lit('\0'))

That will get you started, but it doesn't strictly enforce a 12-byte field. I.e. "Hello World\0\0" would be valid with that rule. Something like:

qi::omit[qi::eps[qi::_a = 12]] >>
qi::repeat(0, 12)
[
(qi::char_ - qi::lit('\0')) >>
 qi::omit[qi::eps[--qi::_a]]
] >>
qi::repeat(qi::_a)[qi::lit('\0')]

Should do the job. A little complicated though, you might be better off doing:

qi::repeat(12)[qi::char_]

and then cleaning up the attribute outside the grammar. This will potentially allocate up to 12-bytes that aren't needed, but this should be trivial unless you have some serious constraints. Interested if someone else on this list has a better method.

Lee


------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general