bytes_parser.cpp
Go to the documentation of this file.
1 // Parse memory specifications like "100 MB" orr "100MiB"
2 // Authors: Adrien Barral, Max Schwarz
3 
4 #include "bytes_parser.h"
5 
6 #include <boost/spirit/include/classic.hpp>
7 #include <boost/spirit/include/phoenix_core.hpp>
8 #include <boost/spirit/include/phoenix_operator.hpp>
9 #include <boost/spirit/include/qi.hpp>
10 
11 #include <ros/console.h>
12 
13 namespace rosmon
14 {
15 namespace launch
16 {
17 
18 std::tuple<uint64_t, bool> parseMemory(const std::string& memory)
19 {
20  using boost::phoenix::ref;
21  using boost::spirit::qi::_1;
22  using boost::spirit::qi::double_;
23  using boost::spirit::qi::no_case;
24  using boost::spirit::qi::phrase_parse;
25 
26  struct bytes_decades_ : boost::spirit::qi::symbols<char, uint64_t>
27  {
28  bytes_decades_(){
29  add("kB", 1e3)("mB", 1e6)("gB", 1e9)("tB", 1e12)("KB", 1e3)("MB", 1e6)("GB", 1e9)(
30  "TB", 1e12)("B", 1)("KiB", static_cast<uint64_t>(1ull<<10))("MiB",
31  static_cast<uint64_t>(1ull<<20))("GiB", static_cast<uint64_t>(1ull<<30))("TiB",
32  static_cast<uint64_t>(1ull<<40));
33  }
34  } byte_decades;
35 
36  auto it = memory.begin();
37  double res = 0.0;
38  bool ok = phrase_parse(it, memory.end(),
39  (double_[ref(res) = _1] >> -(byte_decades[ref(res) *= _1])),
40  boost::spirit::ascii::space
41  );
42 
43  return std::make_tuple(static_cast<uint64_t>(res), ok);
44 }
45 
46 }
47 }
ROSCPP_DECL bool ok()
std::tuple< uint64_t, bool > parseMemory(const std::string &memory)


rosmon_core
Author(s): Max Schwarz
autogenerated on Sat Jan 9 2021 03:35:43