Skip to content

Commit

Permalink
feat: Parse dbc with istream (#30)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LinuxDevon authored May 26, 2024
1 parent 6d62885 commit 0dbe454
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
4 changes: 3 additions & 1 deletion include/libdbc/dbc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Parser {
virtual ~Parser() = default;

virtual void parse_file(const std::string& file) = 0;
virtual void parse_file(std::istream& file) = 0;

protected:
};
Expand All @@ -23,7 +24,8 @@ class DbcParser : public Parser {
public:
DbcParser();

void parse_file(const std::string& file) override;
void parse_file(const std::string& file_name) override;
void parse_file(std::istream& stream) override;

std::string get_version() const;
std::vector<std::string> get_nodes() const;
Expand Down
19 changes: 12 additions & 7 deletions src/dbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,12 @@ DbcParser::DbcParser()
+ whiteSpace + receiverPattern) {
}

void DbcParser::parse_file(const std::string& file) {
std::ifstream stream(file.c_str());
void DbcParser::parse_file(std::istream& stream) {
std::string line;
std::vector<std::string> lines;

messages.clear();

auto extension = get_extension(file);
if (extension != ".dbc") {
throw NonDbcFileFormatError(file, extension);
}

parse_dbc_header(stream);
parse_dbc_nodes(stream);

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

void DbcParser::parse_file(const std::string& file_name) {
auto extension = get_extension(file_name);
if (extension != ".dbc") {
throw NonDbcFileFormatError(file_name, extension);
}

std::ifstream stream(file_name.c_str());

parse_file(stream);
}

std::string DbcParser::get_extension(const std::string& file_name) {
std::size_t dot = file_name.find_last_of(".");
if (dot != std::string::npos) {
Expand Down
24 changes: 24 additions & 0 deletions test/single_header_testing/test_single_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,27 @@ TEST_CASE("Testing big endian, little endian") {
REQUIRE(signal.is_bigendian == false);
}
}

TEST_CASE("Testing file stream mirrors the filename interface") {
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 8 Vector__XXX
SG_ Sig1 : 55|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX
SG_ Sig2 : 39|16@1- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX)";
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
std::ifstream file(filename.c_str());

auto parser = Libdbc::DbcParser();
parser.parse_file(file);

REQUIRE(parser.get_messages().size() == 1);
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
REQUIRE(parser.get_messages().at(0).size() == 8);
REQUIRE(parser.get_messages().at(0).get_signals().size() == 2);
{
const auto signal = parser.get_messages().at(0).get_signals().at(0);
REQUIRE(signal.is_bigendian == true);
}
{
const auto signal = parser.get_messages().at(0).get_signals().at(1);
REQUIRE(signal.is_bigendian == false);
}
}

0 comments on commit 0dbe454

Please sign in to comment.