• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2018 Adam Butcher, Antony Polukhin
2 // Copyright (c) 2019-2021 Antony Polukhin
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <boost/pfr/core.hpp>
8 #include <boost/core/lightweight_test.hpp>
9 
parseHex(char const * p,size_t limit=~0u)10 auto parseHex(char const* p, size_t limit = ~0u) {
11     struct { size_t val; char const* rest; } res = { 0, p };
12     while (limit) {
13         int v = *res.rest;
14         if (v >= '0' && v <= '9')
15             v = v - '0';
16         else if (v >= 'A' && v <= 'F')
17             v = 10 + v - 'A';
18         else if (v >= 'a' && v <= 'f')
19             v = 10 + v - 'a';
20         else
21             break;
22         res.val = (res.val << 4) + v;
23         --limit;
24         ++res.rest;
25     }
26     return res;
27 }
28 
parseLinePrefix(char const * line)29 auto parseLinePrefix(char const* line) {
30      struct {
31           size_t byteCount, address, recordType; char const* rest;
32      } res;
33      using namespace boost::pfr;
34      tie_from_structure (res.byteCount, line) = parseHex(line, 2);
35      tie_from_structure (res.address, line) = parseHex(line, 4);
36      tie_from_structure (res.recordType, line) = parseHex(line, 2);
37      res.rest = line;
38      return res;
39 }
40 
main()41 int main() {
42     auto line = "0860E000616263646566000063";
43     auto meta = parseLinePrefix(line);
44     BOOST_TEST_EQ(meta.byteCount, 8);
45     BOOST_TEST_EQ(meta.address, 24800);
46     BOOST_TEST_EQ(meta.recordType, 0);
47     BOOST_TEST_EQ(meta.rest, line + 8);
48 
49     size_t val;
50     using namespace boost::pfr;
51 
52     tie_from_structure (val, std::ignore) = parseHex("a73b");
53     BOOST_TEST_EQ(val, 42811);
54 
55     tie_from_structure (std::ignore, line) = parseHex(line, 8);
56     BOOST_TEST_EQ(line, meta.rest);
57 
58     return boost::report_errors();
59 }
60 
61