forked from downtimes/can-dbcparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.cpp
129 lines (111 loc) · 2.9 KB
/
signal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* signal.cpp
*
* Created on: 04.10.2013
* Author: downtimes
*/
#include "header/signal.hpp"
#include <istream>
#include <sstream>
#include <limits>
#include <iterator>
#include <algorithm>
std::string& trim(std::string& str, const std::string& toTrim = " ") {
std::string::size_type pos = str.find_last_not_of(toTrim);
if (pos == std::string::npos) {
str.clear();
} else {
str.erase(pos + 1);
str.erase(0, str.find_first_not_of(toTrim));
}
return str;
}
std::vector<std::string>& split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
std::istream& operator>>(std::istream& in, Signal& sig) {
std::string line;
std::getline(in, line);
if (!line.empty() && *line.rbegin() == '\r') line.erase(line.length() - 1, 1);
if (line.empty()) {
in.setstate(std::ios_base::failbit);
return in;
}
std::istringstream sstream(line);
std::string preamble;
sstream >> preamble;
//Check if we are actually reading a Signal otherwise fail the stream
if (preamble != "SG_") {
sstream.setstate(std::ios_base::failbit);
return in;
}
//Parse the Signal Name
sstream >> sig.name;
std::string multi;
sstream >> multi;
//This case happens if there is no Multiplexor present
if (multi == ":") {
sig.multiplexor = Multiplexor::NONE;
//Case with multiplexor
} else {
if (multi == "M") {
sig.multiplexor = Multiplexor::MULTIPLEXOR;
} else {
//The multiplexor looks like that 'm12' so we ignore the m and parse it as integer
std::istringstream multstream(multi);
multstream.ignore(1);
unsigned short multiNum;
multstream >> multiNum;
sig.multiplexor = Multiplexor::MULTIPLEXED;
sig.multiplexNum = multiNum;
}
//ignore the next thing which is a ':'
sstream >> multi;
}
sstream >> sig.startBit;
sstream.ignore(1);
sstream >> sig.length;
sstream.ignore(1);
int order;
sstream >> order;
if (order == 0) {
sig.order = ByteOrder::MOTOROLA;
} else {
sig.order = ByteOrder::INTEL;
}
char sign;
sstream >> sign;
if (sign == '+') {
sig.sign = Sign::UNSIGNED;
} else {
sig.sign = Sign::SIGNED;
}
sstream.ignore(std::numeric_limits<std::streamsize>::max(), '(');
sstream >> sig.factor;
sstream.ignore(1);
sstream >> sig.offset;
sstream.ignore(1);
sstream.ignore(std::numeric_limits<std::streamsize>::max(), '[');
sstream >> sig.minimum;
sstream.ignore(1);
sstream >> sig.maximum;
sstream.ignore(1);
std::string unit;
sstream >> unit;
sig.unit = trim(unit, "\"");
std::string to;
sstream >> to;
std::vector<std::string> toStrings = split(to, ',');
std::move(toStrings.begin(), toStrings.end(), std::inserter(sig.to, sig.to.begin()));
return in;
}