string.cpp
Go to the documentation of this file.
2 
3 namespace can {
4 
5 bool hex2dec(uint8_t& d, const char& h) {
6  if ('0' <= h && h <= '9')
7  d = h - '0';
8  else if ('a' <= h && h <= 'f')
9  d = h - 'a' + 10;
10  else if ('A' <= h && h <= 'F')
11  d = h - 'A' + 10;
12  else
13  return false;
14 
15  return true;
16 }
17 
18 bool hex2buffer(std::string& out, const std::string& in_raw, bool pad) {
19  std::string in(in_raw);
20  if ((in.size() % 2) != 0) {
21  if (pad)
22  in.insert(0, "0");
23  else
24  return false;
25  }
26  out.resize(in.size() >> 1);
27  for (size_t i = 0; i < out.size(); ++i) {
28  uint8_t hi, lo;
29  if (!hex2dec(hi, in[i << 1]) || !hex2dec(lo, in[(i << 1) + 1]))
30  return false;
31 
32  out[i] = (hi << 4) | lo;
33  }
34  return true;
35 }
36 
37 bool dec2hex(char& h, const uint8_t& d, bool lc) {
38  if (d < 10) {
39  h = '0' + d;
40  } else if (d < 16 && lc) {
41  h = 'a' + (d - 10);
42  } else if (d < 16 && !lc) {
43  h = 'A' + (d - 10);
44  } else {
45  h='?';
46  return false;
47  }
48  return true;
49 }
50 
51 std::string byte2hex(const uint8_t& d, bool pad, bool lc) {
52  uint8_t hi = d >> 4;
53  char c=0;
54  std::string s;
55  if (hi || pad) {
56  dec2hex(c, hi, lc);
57  s += c;
58  }
59  dec2hex(c, d & 0xf, lc);
60  s += c;
61  return s;
62 }
63 
64 std::string buffer2hex(const std::string& in, bool lc) {
65  std::string s;
66  s.reserve(in.size() * 2);
67  for (size_t i = 0; i < in.size(); ++i) {
68  std::string b = byte2hex(in[i], true, lc);
69  if (b.empty())
70  return b;
71 
72  s += b;
73  }
74  return s;
75 }
76 
77 std::string tostring(const Header& h, bool lc) {
78  std::string s;
79  s.reserve(8);
80  std::stringstream buf;
81  buf << std::hex;
82  if (lc)
83  buf << std::nouppercase;
84  else
85  buf << std::uppercase;
86 
87  buf << (int) (h);
88  return buf.str();
89 }
90 
91 uint32_t tohex(const std::string& s) {
92  unsigned int h = 0;
93  std::stringstream stream;
94  stream << std::hex << s;
95  stream >> h;
96  return h;
97 }
98 
99 Header toheader(const std::string& s) {
100  unsigned int h = tohex(s);
103 }
104 
105 std::string tostring(const Frame& f, bool lc) {
106  std::string s;
107  s.resize(f.dlc);
108  for (uint8_t i = 0; i < f.dlc; ++i) {
109  s[i] = f.data[i];
110  }
111  return tostring((const Header&) (f), lc) + '#' + buffer2hex(s, lc);
112 }
113 
114 Frame toframe(const std::string& s) {
115  size_t sep = s.find('#');
116  if (sep == std::string::npos)
117  return MsgHeader(0xfff);
118 
119  Header header = toheader(s.substr(0, sep));
120  Frame frame(header);
121  std::string buffer;
122  if (header.isValid() && hex2buffer(buffer, s.substr(sep + 1), false)) {
123  if (buffer.size() > 8)
124  return MsgHeader(0xfff);
125 
126  for (size_t i = 0; i < buffer.size(); ++i) {
127  frame.data[i] = buffer[i];
128  }
129  frame.dlc = buffer.size();
130  }
131  return frame;
132 }
133 
134 template<> FrameFilterSharedPtr tofilter(const std::string &s){
135  FrameFilter * filter = 0;
136  size_t delim = s.find_first_of(":~-_");
137 
138  uint32_t second = FrameMaskFilter::MASK_RELAXED;
139  bool invert = false;
140  char type = ':';
141 
142  if(delim != std::string::npos) {
143  type = s.at(delim);
144  second = tohex(s.substr(delim +1));
145  }
146  uint32_t first = toheader(s.substr(0, delim));
147  switch (type) {
148  case '~':
149  invert = true;
150  case ':':
151  filter = new FrameMaskFilter(first, second, invert);
152  break;
153  case '_':
154  invert = true;
155  case '-':
156  filter = new FrameRangeFilter(first, second, invert);
157  break;
158  }
159  return FrameFilterSharedPtr(filter);
160 }
161 template<> FrameFilterSharedPtr tofilter(const uint32_t &id){
162  return FrameFilterSharedPtr(new FrameMaskFilter(id));
163 }
164 
166  return tofilter<std::string>(s);
167 }
168 
169 }
170 
171 std::ostream& operator <<(std::ostream& stream, const can::Header& h) {
172  return stream << can::tostring(h, true);
173 }
174 
175 std::ostream& operator <<(std::ostream& stream, const can::Frame& f) {
176  return stream << can::tostring(f, true);
177 }
bool dec2hex(char &h, const uint8_t &d, bool lc)
Definition: string.cpp:37
std::string tostring(const Header &h, bool lc)
Definition: string.cpp:77
Definition: asio_base.h:11
FrameFilterSharedPtr tofilter(const T &ct)
bool hex2dec(uint8_t &d, const char &h)
Definition: string.cpp:5
Header toheader(const std::string &s)
Definition: string.cpp:99
static const unsigned int RTR_MASK
Definition: interface.h:16
std::ostream & operator<<(std::ostream &stream, const can::Header &h)
Definition: string.cpp:171
static const unsigned int ID_MASK
Definition: interface.h:14
static const unsigned int EXTENDED_MASK
Definition: interface.h:17
bool isValid() const
Definition: interface.h:24
std::string buffer2hex(const std::string &in, bool lc)
Definition: string.cpp:64
boost::array< value_type, 8 > data
array for 8 data bytes with bounds checking
Definition: interface.h:58
Frame toframe(const std::string &s)
Definition: string.cpp:114
std::string byte2hex(const uint8_t &d, bool pad, bool lc)
Definition: string.cpp:51
static const unsigned int ERROR_MASK
Definition: interface.h:15
uint32_t tohex(const std::string &s)
Definition: string.cpp:91
boost::shared_ptr< FrameFilter > FrameFilterSharedPtr
Definition: filter.h:15
static const uint32_t MASK_RELAXED
Definition: filter.h:20
bool hex2buffer(std::string &out, const std::string &in_raw, bool pad)
Definition: string.cpp:18
unsigned char dlc
len of data
Definition: interface.h:59


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Sat May 4 2019 02:40:41