Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
dom_map.cpp
Go to the documentation of this file.
1#include "dom_map.hpp"
2
3#include <stdexcept>
4#include <cstdlib>
5
6#include <boost/tokenizer.hpp>
7#include <boost/foreach.hpp>
8#include <boost/lexical_cast.hpp>
9#include <boost/algorithm/string/trim.hpp>
10
11/**
12 * \author cpellegrino
13 */
14
15uint32_t getDomId(std::string const& s_domid)
16{
17 if (s_domid.find(":") == std::string::npos) {
18 return boost::lexical_cast<uint32_t>(s_domid);
19 } else {
20 boost::char_separator<char> const args_sep(":");
21 boost::tokenizer<boost::char_separator<char> > tok(s_domid, args_sep);
22
23 std::stringstream ss;
24 int count = 0;
25
26 BOOST_FOREACH(std::string byte, tok) {
27 if (byte.size() != 2) {
28 throw std::runtime_error("Error: malformed MAC address");
29 }
30
31 if (count > 1) {
32 ss << byte;
33 } else {
34 if (count == 0 && byte == "08") {
35 } else if (count == 1 && byte == "00") {
36 } else {
37 throw std::runtime_error(
38 "Error: the provided MAC address is not a CLB MAC."
39 );
40 }
41 }
42 ++count;
43 }
44
45 return std::strtol(ss.str().c_str(), 0, 16);
46 }
47}
48
49dom_map_type load_dom_map(std::istream& map)
50{
51 boost::char_separator<char> const args_sep(";");
52 boost::char_separator<char> const fields_sep("=");
53 boost::char_separator<char> const white_spaces("\t ");
54
55 dom_map_type dom_map;
56
57 while (map.peek() != EOF) {
58 std::string line;
59 getline(map, line);
60
61 std::string::size_type const pound = line.find('#');
62
63 if (pound != std::string::npos) {
64 line = line.substr(0, pound);
65 }
66
67 boost::algorithm::trim(line);
68 boost::tokenizer<boost::char_separator<char> > tok(line, args_sep);
69
70 BOOST_FOREACH(std::string s, tok) {
71 boost::algorithm::trim(s);
72 boost::tokenizer<boost::char_separator<char> > fields(s, fields_sep);
73 typedef boost::tokenizer<boost::char_separator<char> >::iterator Iter;
74
75 Iter it = fields.begin();
76
77 std::string s_domid = *it;
78 boost::algorithm::trim(s_domid);
79 uint32_t const domid = getDomId(s_domid);
80
81 ++it;
82 if (it != fields.end()) {
83 std::string name = *it;
84 boost::algorithm::trim(name);
85
86 dom_map.insert(std::make_pair(domid, name));
87 } else {
88 throw std::runtime_error("Error: malformed assignment");
89 }
90 }
91 }
92
93 return dom_map;
94}
uint32_t getDomId(std::string const &s_domid)
Definition dom_map.cpp:15
dom_map_type load_dom_map(std::istream &map)
Definition dom_map.cpp:49
const char * map
Definition elog.cc:87