Question

I've got a situation where I'm trying to build up an HDF compound type out from a stream of name-value pairs (for simplicity's sake, we'll say that a value can be either a double or a character string). Just to be clear, the numeric data is already binary - it's not a string. The names provide structural information (is this part of an array?, is this part of a nested compound type?).

I'm imagining making a vector of tokens, using the name information to insert tokens (e.g. '[' and ']' to delimit an array, '{' and '}' to delimit nested compounds), but otherwise using the values. It's not clear to me from the documentation if the Spirit binary parsers would be an appropriate choice to handle the numeric values.

Was it helpful?

Solution

I can't judge whether "the rest" (i.e. the non-binary data) justifies going for a PEG parser generator.

However, just to give you something to start with:

Use

  • qi::bin_float, qi::little_bin_float or qi::big_bin_float
  • qi::bin_double, qi::little_bin_double or qi::big_bin_double

Here's a 17-line sample program that exactly duplicates the behaviour of

od -w8 -A none -t f8 -v input.dat

on my box:

#include <boost/spirit/include/qi.hpp>
#include <fstream>
#include <iomanip>

namespace qi = boost::spirit::qi;

int main() {
    using namespace std;
    // read file
    ifstream ifs("input.dat", ios::binary);
    string const input { istreambuf_iterator<char>(ifs), {} };

    // parse
    vector<double> result;
    bool ret = qi::parse(begin(input), end(input), *qi::bin_double, result);

    // print
    if (ret) for (auto v : result) 
        cout << setw(28) << setprecision(16) << right << v << "\n";
}

See it Live on Coliru


Commands used:

clang++ -Os -std=c++11 -Wall -pedantic main.cpp          # compile
dd if=/dev/urandom count=32 bs=1 2>/dev/null > input.dat # generate input
./a.out                                               # spirit demo
echo 'And `od` output:'                        
od -w8 -A none -t f8 -v /tmp/input.dat                  # compare to `od` 

Disclaimer This just intended in case it helps you see how Spirit handles binary input.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top