Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parse dbc with istream #30

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}