2

qi::repeat(1,2) funtion in rule is giving me warrning and i dont want to ignore that warning so i want to optimized this code like separating the rule from the parsering method.

qi::phrase_parse is doing the same thing which is in the the rule but i want to sperate the rule and give rule to the pharse_parse funtion.

std::ifstream ifs("f:/test.txt");
 std::string line;

//In header in my code
std::vector<unsigned long long> v_BF_Char;


//qi::int_parser<uintmax_t, 16> hex_int;
static qi::uint_parser<unsigned long long, 16, 16, 16> hex_int;

while (std::getline(ifs, line))
{
    typedef std::string::const_iterator It;
    It begin = line.begin(), end = line.end();

    // rule for grammer
    qi::rule<It, unsigned long long()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';


    bool ok = qi::phrase_parse(begin, end,
           *('<' >> qi::repeat(1,2)[ hex_int ] >> '>'),  qi::space, v_BF_Char);

and want to do somthing like this

bool ok = qi::phrase_parse(begin, end,
           braced_hex ,  qi::space, v_BF_Char);

test.txt Contain

<51dc> <04001C0180000000000000000EE317BC>
<05001C0180000000> <04001C0180000000000000000EE317BC>
<51dc> <30ea30f330ae30c330c8>
<0000> <fffd>
<003d> <00a5>
<005d> <00a6>
<005e> <007d>
<005f> <0303>
<0060> <2019>
<0061> <005c>
<0062> <2018>
<0063> <007c>
<0064> <007e>
<0068> <2044>
<0069> <0192>
<006a> <00a7>
<006b> <00a4>
<006c> <201c>

My code is working for only 2nd line and extract values in vector but not other line values.

  • To the edit since my answer: You need to decide what it means when you parse less than 16 hex digits. Does that imply leading zeros? What is the byte ordering assumed to be? Did you actually want to parse just `std::vector`? – sehe Jun 17 '15 at 11:52
  • Also, this question seems eerily similar, perhaps it can help you: http://stackoverflow.com/questions/30365628/function-or-functor-calling-using-sprit-boost-parser-library-rule-to-save-values/30366329#30366329 – sehe Jun 17 '15 at 11:53

1 Answers1

2

Fix the rule's exposed attribute:

qi::rule<It, std::vector<unsigned long long>()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';

Repeat exposes a vector of hex_int's exposed attribute type

Live demo:

Live On Coliru

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

namespace qi = boost::spirit::qi;

int main() {
    std::ifstream ifs("input.txt");

    //In header in my code
    std::vector<unsigned long long> v_BF_Char;

    //qi::int_parser<uintmax_t, 16> hex_int;
    static qi::uint_parser<uint64_t, 16, 16, 16> hex_int;

    std::string line;
    while (std::getline(ifs, line))
    {
        typedef std::string::const_iterator It;
        It begin = line.begin(), end = line.end();

        // rule for grammer
        qi::rule<It, std::vector<uint64_t>()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';

        bool ok = qi::phrase_parse(begin, end, *braced_hex,  qi::space, v_BF_Char);
        assert(ok);
    }

    std::cout << "Parsed: " << v_BF_Char.size() << " 64bit elements\n";
}

Prints:

Parsed: 50 64bit elements

E.g. for this input:

<1353cd278dd1f003636bc155006ac5ce> <1e83b053032565f0> <d1e97c841e68153a5d82d57df3074a21>
<92adee538fd147a337ebc8a4fc8d0ad3> <0ed9fb22ab42b3a4> <756ad64486054d22c62329e8dcaef0c5>
<16eeaec1108b1159b49c6bf884564519> <b4b87d1fd1aa10af> <1f710495fd863a1d191355adf1b33d5a>
<947ac523b4450ec26446840ccde3965b> <faa860f7763b23dc> <571decbfd0fcfe9a4047f72c101b9d87>
<50d726028b79b1a531a2c3752a4fdde7> <644e057721fa7fe1> <6bf66d2e1ae50351db53eddcee5fae41>
<6916580258e94f2be66eb71f103d3023> <a427df9bd05edd6d> <d896cfe92e6634867fcab5c6fc2de60b>
<9e50d5c9cda9e9a2fbf78eeb10f3a6bd> <9cff72edea319328> <0aabc7f36fcd058a2dfa7bb94602919a>
<923832f107c94d4a04b1de96241fda14> <003c7554390cabaf> <c43d58504fc6659bb226707efc0221b8>
<1040a8d23eac10e9e4b6abb2efcde1bd> <f38ac3906542529d> <ffbd836c54b0f498d358e4ea50170c94>
<7c4c6fd86a60cf7b4ac62faa0395c06b> <61156478683a6b01> <fb92ef7030068f25471e8049fb0f7cd3>

random data generated with

od -A none -t x8 -w256 < /dev/urandom | head | awk '{ print "<" $1 $2 "> <" $3 "> <" $4 $5 ">" }'
sehe
  • 374,641
  • 47
  • 450
  • 633
  • **test.txt contain the following values.** <51dc> <04001C0180000000000000000EE317BC> <05001C0180000000> <04001C0180000000000000000EE317BC> <51dc> <30ea30f330ae30c330c8> <0000> <003d> <00a5> **vector can store only 2nd line values not working working for line 1 ,3, 4** – max william Jun 17 '15 at 09:51
  • @maxwilliam I don't know what you're trying to say. Please see my live sample. Consider clarifying your question instead of using the comments for details like this. – sehe Jun 17 '15 at 09:56
  • 1
    @maxwilliam Hint `int_parser` expressly says: exactly 16 hex digits must appear. Of course `` doesn't match – sehe Jun 17 '15 at 09:57
  • kindly check my data file how should i write the general solution of this ? – max william Jun 17 '15 at 10:01
  • Kindly check my previous comment. I'll be back later tonight – sehe Jun 17 '15 at 10:03
  • You need to decide what it means when you parse less than 16 hex digits. Does that imply leading zeros? What is the byte ordering assumed to be? Did you actually want to parse just `std::vector`? – sehe Jun 17 '15 at 11:52
  • 1
    @maxwilliam in the absense of more information: http://coliru.stacked-crooked.com/a/fa27fa6bd9b5c55c – sehe Jun 17 '15 at 21:20