DbcSignal.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2015-2020, Dataspeed Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Dataspeed Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #include "DbcSignal.hpp"
36 
37 #include <vector>
38 #include <istream>
39 #include <sstream>
40 #include <limits>
41 #include <iterator>
42 #include <algorithm>
43 
44 std::string& trim(std::string& str, const std::string& toTrim = " ") {
45  std::string::size_type pos = str.find_last_not_of(toTrim);
46  if (pos == std::string::npos) {
47  str.clear();
48  } else {
49  str.erase(pos + 1);
50  str.erase(0, str.find_first_not_of(toTrim));
51  }
52  return str;
53 }
54 
55 std::vector<std::string>& split(const std::string &s, char delim, std::vector<std::string> &elems) {
56  std::stringstream ss(s);
57  std::string item;
58  while (std::getline(ss, item, delim)) {
59  elems.push_back(item);
60  }
61  return elems;
62 }
63 
64 std::vector<std::string> split(const std::string &s, char delim) {
65  std::vector<std::string> elems;
66  split(s, delim, elems);
67  return elems;
68 }
69 
70 std::istream& operator>>(std::istream& in, Signal& sig) {
71  std::string line;
72  std::getline(in, line);
73  if (!line.empty() && *line.rbegin() == '\r') {
74  line.erase(line.length() - 1, 1);
75  }
76  if (line.empty()) {
77  in.setstate(std::ios_base::failbit);
78  return in;
79  }
80 
81  // Check if we are actually reading a Signal otherwise fail the stream
82  std::istringstream sstream(line);
83  std::string preamble;
84  sstream >> preamble;
85  if (preamble != "SG_") {
86  in.setstate(std::ios_base::failbit);
87  return in;
88  }
89 
90  // Parse the Signal Name
91  sstream >> sig.name;
92 
93  std::string multi;
94  sstream >> multi;
95 
96  // This case happens if there is no Multiplexor present
97  if (multi == ":") {
98  sig.multiplexor = NONE;
99  } else {
100  // Case with multiplexor
101  if (multi == "M") {
102  sig.multiplexor = MULTIPLEXOR;
103  } else {
104  // The multiplexor looks like that 'm12' so we ignore the m and parse it as integer
105  std::istringstream multstream(multi);
106  multstream.ignore(1);
107  unsigned short multiNum;
108  multstream >> multiNum;
109  sig.multiplexor = MULTIPLEXED;
110  sig.multiplexNum = multiNum;
111  }
112  //ignore the next thing which is a ':'
113  sstream >> multi;
114  }
115 
116  sstream >> sig.startBit;
117  sstream.ignore(1);
118  sstream >> sig.length;
119  sstream.ignore(1);
120 
121  int order;
122  sstream >> order;
123  if (order == 0) {
124  sig.order = MOTOROLA;
125  } else {
126  sig.order = INTEL;
127  }
128 
129  char sign;
130  sstream >> sign;
131  if (sign == '+') {
132  sig.sign = UNSIGNED;
133  } else {
134  sig.sign = SIGNED;
135  }
136 
137  // Factor and offset
138  sstream.ignore(std::numeric_limits<std::streamsize>::max(), '(');
139  sstream >> sig.factor;
140  sstream.ignore(1);
141  sstream >> sig.offset;
142  sstream.ignore(1);
143 
144  // Min and max
145  sstream.ignore(std::numeric_limits<std::streamsize>::max(), '[');
146  sstream >> sig.minimum;
147  sstream.ignore(1);
148  sstream >> sig.maximum;
149  sstream.ignore(1);
150 
151  // Unit
152  std::string unit;
153  sstream >> unit;
154  sig.unit = trim(unit, "\"");
155 
156  // Receivers
157  std::string to;
158  sstream >> to;
159  std::vector<std::string> toStrings = split(to, ',');
160  for (size_t i = 0; i < toStrings.size(); i++) {
161  sig.to.insert(sig.to.end(), toStrings[i]);
162  }
163 
164  return in;
165 }
166 
std::string & trim(std::string &str, const std::string &toTrim=" ")
Definition: DbcSignal.cpp:44
std::string name
Definition: DbcSignal.hpp:68
std::string unit
Definition: DbcSignal.hpp:86
Multiplexor multiplexor
Definition: DbcSignal.hpp:88
std::istream & operator>>(std::istream &in, Signal &sig)
Definition: DbcSignal.cpp:70
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Definition: DbcSignal.cpp:55
unsigned short length
Definition: DbcSignal.hpp:74
ByteOrder order
Definition: DbcSignal.hpp:70
double offset
Definition: DbcSignal.hpp:84
double minimum
Definition: DbcSignal.hpp:78
double factor
Definition: DbcSignal.hpp:82
unsigned short startBit
Definition: DbcSignal.hpp:72
Sign sign
Definition: DbcSignal.hpp:76
unsigned short multiplexNum
Definition: DbcSignal.hpp:90
double maximum
Definition: DbcSignal.hpp:80
toList to
Definition: DbcSignal.hpp:92


dataspeed_can_tools
Author(s): Micho Radovnikovich
autogenerated on Thu Jul 9 2020 03:41:59