11#include < libdbc/exceptions/error.hpp>
22#include < libdbc/utils/utils.hpp>
33#include < libdbc/dbc.hpp>
4+ #include < fast_float/fast_float.h>
45
56#include < regex>
67
@@ -28,12 +29,12 @@ const auto whiteSpace = "\\s";
2829
2930namespace libdbc {
3031
31- DbcParser::DbcParser () : version(" " ), nodes(),
32+ DbcParser::DbcParser () : version(" " ), nodes(),
3233 version_re (" ^(VERSION)\\ s\" (.*)\" " ), bit_timing_re(" ^(BS_:)" ),
3334 name_space_re(" ^(NS_)\\ s\\ :" ), node_re(" ^(BU_:)\\ s((?:[\\ w]+?\\ s?)*)" ),
3435 message_re(" ^(BO_)\\ s(\\ d+)\\ s(\\ w+)\\ :\\ s(\\ d+)\\ s(\\ w+|Vector__XXX)" ),
3536 // NOTE: No multiplex support yet
36- signal_re(std::string(whiteSpace) +
37+ signal_re(std::string(" ^ " ) + whiteSpace +
3738 signalIdentifierPattern +
3839 whiteSpace +
3940 namePattern +
@@ -57,11 +58,13 @@ namespace libdbc {
5758
5859 }
5960
60- void DbcParser::parse_file (const std::string& file) {
61+ void DbcParser::parse_file (const std::string& file) {
6162 std::ifstream s (file.c_str ());
6263 std::string line;
6364 std::vector<std::string> lines;
6465
66+ messages.clear ();
67+
6568 parse_dbc_header (s);
6669
6770 parse_dbc_nodes (s);
@@ -71,8 +74,7 @@ namespace libdbc {
7174 lines.push_back (line);
7275 }
7376
74- parse_dbc_messages (lines);
75-
77+ parse_dbc_messages (lines);
7678 }
7779
7880 std::string DbcParser::get_version () const {
@@ -87,6 +89,13 @@ namespace libdbc {
8789 return messages;
8890 }
8991
92+ Message::ParseSignalsStatus DbcParser::parseMessage (const uint32_t id, const std::vector<uint8_t >& data, std::vector<double >& out_values) {
93+ for (const auto & message: messages) {
94+ if (message.id () == id)
95+ return message.parseSignals (data, out_values);
96+ }
97+ return Message::ParseSignalsStatus::ErrorUnknownID;
98+ }
9099
91100 void DbcParser::parse_dbc_header (std::istream& file_stream) {
92101 std::string line;
@@ -150,17 +159,21 @@ namespace libdbc {
150159 bool is_bigendian = (std::stoul (match.str (5 )) == 0 );
151160 bool is_signed = (match.str (6 ) == " -" );
152161 // Alternate groups because a group is for the decimal portion
153- double factor = std::stod (match.str (7 ));
154- double offset = std::stod (match.str (9 ));
155- double min = std::stod (match.str (11 ));
156- double max = std::stod (match.str (13 ));
162+ double factor;
163+ fast_float::from_chars (match.str (7 ).data (), match.str (7 ).data () + match.str (7 ).size (), factor);
164+ double offset;
165+ fast_float::from_chars (match.str (9 ).data (), match.str (9 ).data () + match.str (9 ).size (), offset);
166+ double min;
167+ fast_float::from_chars (match.str (11 ).data (), match.str (11 ).data () + match.str (11 ).size (), min);
168+ double max;
169+ fast_float::from_chars (match.str (13 ).data (), match.str (13 ).data () + match.str (13 ).size (), max);
157170 std::string unit = match.str (15 );
158171
159172 std::vector<std::string> receivers;
160173 utils::String::split (match.str (16 ), receivers, ' ,' );
161174
162175 Signal sig (name, is_multiplexed, start_bit, size, is_bigendian, is_signed, factor, offset, min, max, unit, receivers);
163- messages.back ().signals . push_back (sig);
176+ messages.back ().appendSignal (sig);
164177 }
165178 }
166179
0 commit comments