I am studying how to use boost spirit Qi binary endian parser. I write a small test parser program according to here and basics examples, but it doesn't work proper. It gave me the msg:"Error:no match".
Here is my code.
#include "boost/spirit/include/qi.hpp"
#include "boost/spirit/include/phoenix_core.hpp"
#include "boost/spirit/include/phoenix_operator.hpp"
#include "boost/spirit/include/qi_binary.hpp" // parsing binary data in various endianness
template "<"typename P, typename T>
void binary_parser( char const* input, P const& endian_word_type,
T& voxel, bool full_match = true)
{
using boost::spirit::qi::parse;
char const* f(input);
char const* l(f + strlen(f));
bool result1 = parse(f,l,endian_word_type,voxel);
bool result2 =((!full_match) || (f ==l));
if ( result1 && result2) {
//doing nothing, parsing data is pass to voxel alreay
} else {
std::cerr << "Error: not match!!" << std::endl;
exit(1);
}
}
typedef boost::uint16_t bs_int16;
typedef boost::uint32_t bs_int32;
int main ( int argc, char *argv[] ){
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;开发者_如何学运维
using qi::big_word;
using qi::big_dword;
boost::uint32_t ui;
float uf;
binary_parser("\x01\x02\x03\x04",big_word,ui); assert(ui=0x01020304);
binary_parser("\x01\x02\x03\x04",big_word,uf); assert(uf=0x01020304);
return 0;
}
I almost copy the example, but why this binary parser doesn't work. I use Mac OS 10.5.8 and gcc 4.01 compiler.
The big_word
parser matches a big-endian word (16 bits), while big_dword
will match what you want (big-endian dword, 32 bits). But I don't think it to be a good idea to use a float as the attribute to the binary parser, you might not get what you expect.
I used wrong parse param. I should not use big_word
instead of big_dword
精彩评论