Skip to content

Commit 192104e

Browse files
committed
Added in parsing the nodes of the DBC.
Now you can get a vector of nodes from the file. Maybe this should become generic and allow other containers. A split function was needed so this was added to the utils to be used else where if needed.
1 parent d499bf7 commit 192104e

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

include/dbc.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ namespace libdbc {
2929
virtual void parse_file(const std::string& file) final override;
3030

3131
std::string get_version() const;
32+
std::vector<std::string> get_nodes() const;
3233

3334
private:
3435
std::string version;
36+
std::vector<std::string> nodes;
3537

3638
const std::regex version_re;
3739
const std::regex bit_timing_re;
3840
const std::regex name_space_re;
41+
const std::regex node_re;
3942

4043
void parse_dbc_header(std::istream& file_stream);
44+
void parse_dbc_nodes(std::istream& file_stream);
4145

4246
};
4347

include/utils/utils.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <string>
66
#include <fstream>
77
#include <iostream>
8+
#include <sstream>
9+
#include <algorithm>
10+
#include <iterator>
811

912
namespace utils {
1013

@@ -33,6 +36,17 @@ namespace utils {
3336

3437
static std::string trim(const std::string& line);
3538

39+
template <class Container>
40+
static void split(const std::string& str, Container& cont, char delim = ' ') {
41+
std::stringstream ss(str);
42+
std::string token;
43+
44+
while (std::getline(ss, token, delim)) {
45+
cont.push_back(token);
46+
}
47+
}
48+
49+
3650
};
3751

3852
}

src/dbc.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
namespace libdbc {
88

9-
DbcParser::DbcParser() : version(""), version_re("^(VERSION)\\s\"(.*)\""),
10-
bit_timing_re("^(BS_:)"), name_space_re("^(NS_)\\s\\:") {
9+
DbcParser::DbcParser() : version(""), nodes(),
10+
version_re("^(VERSION)\\s\"(.*)\""), bit_timing_re("^(BS_:)"),
11+
name_space_re("^(NS_)\\s\\:"), node_re("^(BU_:)\\s((?:[\\w]+?\\s?)*)") {
1112

1213
}
1314

@@ -17,9 +18,10 @@ namespace libdbc {
1718

1819
parse_dbc_header(s);
1920

20-
while(!s.eof()) {
21-
utils::StreamHandler::get_line(s, line);
21+
parse_dbc_nodes(s);
2222

23+
while(!s.eof()) {
24+
utils::StreamHandler::get_line( s, line );
2325
}
2426

2527
}
@@ -28,11 +30,14 @@ namespace libdbc {
2830
return version;
2931
}
3032

33+
std::vector<std::string> DbcParser::get_nodes() const {
34+
return nodes;
35+
}
36+
37+
3138
void DbcParser::parse_dbc_header(std::istream& file_stream) {
3239
std::string line;
3340
std::smatch match;
34-
bool is_blank = true;
35-
bool not_blank = true;
3641

3742
utils::StreamHandler::get_line(file_stream, line);
3843

@@ -53,4 +58,22 @@ namespace libdbc {
5358

5459
}
5560

61+
void DbcParser::parse_dbc_nodes(std::istream& file_stream) {
62+
std::string line;
63+
std::smatch match;
64+
65+
utils::StreamHandler::get_next_non_blank_line( file_stream, line );
66+
67+
if(!std::regex_search(line, match, node_re))
68+
throw validity_error();
69+
70+
if(match.length() > 2) {
71+
std::string n = match.str(2);
72+
utils::String::split(n, nodes);
73+
}
74+
75+
}
76+
77+
78+
5679
}

test/test_dbc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ TEST_CASE("Testing dbc file loading", "[fileio]") {
2828
auto parser = std::unique_ptr<libdbc::DbcParser>(new libdbc::DbcParser());
2929

3030
SECTION("Loading a single simple dbc file", "[dbc]") {
31-
std::vector<libdbc::Message> messages;
31+
std::vector<std::string> nodes = {"DBG", "DRIVER", "IO", "MOTOR", "SENSOR"};
3232

3333
parser->parse_file(SIMPLE_DBC_FILE);
3434

3535
REQUIRE(parser->get_version() == "1.0.0");
36+
37+
REQUIRE(parser->get_nodes() == nodes);
3638
}
3739

3840
}

test/test_utils.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,14 @@ TEST_CASE("Test the string trim feature", "[string]") {
8181
REQUIRE(String::trim(s) == "there might be some white space....");
8282
}
8383

84+
TEST_CASE("Test string split feature", "[string]") {
85+
std::string s = "name1 name2 name3 name4 name5 ";
86+
std::vector<std::string> vs = {"name1", "name2", "name3", "name4", "name5"};
87+
88+
std::vector<std::string> v;
89+
90+
String::split(s, v);
91+
92+
REQUIRE(v == vs);
93+
}
94+

0 commit comments

Comments
 (0)