Skip to content

Commit 0dbe454

Browse files
authored
feat: Parse dbc with istream (#30)
* feat: Add an istream interface to the avoid the ifstream creation given a file name * fix: formatting the code base to fix checks and clang-tidy errors
1 parent 6d62885 commit 0dbe454

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

include/libdbc/dbc.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Parser {
1515
virtual ~Parser() = default;
1616

1717
virtual void parse_file(const std::string& file) = 0;
18+
virtual void parse_file(std::istream& file) = 0;
1819

1920
protected:
2021
};
@@ -23,7 +24,8 @@ class DbcParser : public Parser {
2324
public:
2425
DbcParser();
2526

26-
void parse_file(const std::string& file) override;
27+
void parse_file(const std::string& file_name) override;
28+
void parse_file(std::istream& stream) override;
2729

2830
std::string get_version() const;
2931
std::vector<std::string> get_nodes() const;

src/dbc.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,12 @@ DbcParser::DbcParser()
7070
+ whiteSpace + receiverPattern) {
7171
}
7272

73-
void DbcParser::parse_file(const std::string& file) {
74-
std::ifstream stream(file.c_str());
73+
void DbcParser::parse_file(std::istream& stream) {
7574
std::string line;
7675
std::vector<std::string> lines;
7776

7877
messages.clear();
7978

80-
auto extension = get_extension(file);
81-
if (extension != ".dbc") {
82-
throw NonDbcFileFormatError(file, extension);
83-
}
84-
8579
parse_dbc_header(stream);
8680
parse_dbc_nodes(stream);
8781

@@ -93,6 +87,17 @@ void DbcParser::parse_file(const std::string& file) {
9387
parse_dbc_messages(lines);
9488
}
9589

90+
void DbcParser::parse_file(const std::string& file_name) {
91+
auto extension = get_extension(file_name);
92+
if (extension != ".dbc") {
93+
throw NonDbcFileFormatError(file_name, extension);
94+
}
95+
96+
std::ifstream stream(file_name.c_str());
97+
98+
parse_file(stream);
99+
}
100+
96101
std::string DbcParser::get_extension(const std::string& file_name) {
97102
std::size_t dot = file_name.find_last_of(".");
98103
if (dot != std::string::npos) {

test/single_header_testing/test_single_header.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,27 @@ TEST_CASE("Testing big endian, little endian") {
5454
REQUIRE(signal.is_bigendian == false);
5555
}
5656
}
57+
58+
TEST_CASE("Testing file stream mirrors the filename interface") {
59+
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 8 Vector__XXX
60+
SG_ Sig1 : 55|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX
61+
SG_ Sig2 : 39|16@1- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX)";
62+
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
63+
std::ifstream file(filename.c_str());
64+
65+
auto parser = Libdbc::DbcParser();
66+
parser.parse_file(file);
67+
68+
REQUIRE(parser.get_messages().size() == 1);
69+
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
70+
REQUIRE(parser.get_messages().at(0).size() == 8);
71+
REQUIRE(parser.get_messages().at(0).get_signals().size() == 2);
72+
{
73+
const auto signal = parser.get_messages().at(0).get_signals().at(0);
74+
REQUIRE(signal.is_bigendian == true);
75+
}
76+
{
77+
const auto signal = parser.get_messages().at(0).get_signals().at(1);
78+
REQUIRE(signal.is_bigendian == false);
79+
}
80+
}

0 commit comments

Comments
 (0)