Skip to content

Commit a3e3323

Browse files
committed
Refactored code; fixed tests.
1 parent 2afd3a1 commit a3e3323

File tree

4 files changed

+23
-48
lines changed

4 files changed

+23
-48
lines changed

include/libdbc/dbc.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class DbcParser : public Parser {
4040
const std::vector<std::string>& unused_lines() const;
4141

4242
#if __cplusplus >= 201703L
43-
static bool is_valid_dbc_file(const std::filesystem::path&);
43+
static void validate_dbc_file(const std::filesystem::path&);
4444
#else
45-
static bool is_valid_dbc_file(const std::string&);
45+
static void validate_dbc_file(const std::string&);
4646
#endif // __cplusplus >= 201703L
47-
static bool is_valid_dbc_file(std::istream& stream);
47+
static void validate_dbc_file(std::istream& stream);
4848

4949
private:
5050
std::string version{};

include/libdbc/exceptions/error.hpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ class ValidityError : public Exception {
2222

2323
class NonDbcFileFormatError : public ValidityError {
2424
public:
25-
NonDbcFileFormatError(const std::string& path, const std::string& extension) {
26-
error_msg = {"File is not of DBC format. Expected a .dbc extension. Cannot read this type of file (" + path + "). Found the extension (" + extension
27-
+ ")."};
28-
}
25+
explicit NonDbcFileFormatError(const std::string& path, const std::string& extension):
26+
error_msg("File is not of DBC format. Expected a .dbc extension. Cannot read this type of file ("
27+
+ path + "). Found the extension (" + extension + ")."
28+
) { }
29+
explicit NonDbcFileFormatError(const std::string& error):
30+
error_msg("File is not of DBC format. Cannot read this type of file (" + error + ").") { }
2931

3032
const char* what() const throw() override {
3133
return error_msg.c_str();
@@ -35,32 +37,16 @@ class NonDbcFileFormatError : public ValidityError {
3537
std::string error_msg;
3638
};
3739

38-
class DbcFileIsMissingVersion : public ValidityError {
40+
class DbcFileIsMissingVersion : public NonDbcFileFormatError {
3941
public:
40-
DbcFileIsMissingVersion(const std::string& line) {
41-
error_msg = {"Invalid dbc file. Missing the required version header. Attempting to read line: (" + line + ")."};
42-
}
43-
44-
const char* what() const throw() override {
45-
return error_msg.c_str();
46-
}
47-
48-
private:
49-
std::string error_msg;
42+
explicit DbcFileIsMissingVersion(const std::string& line):
43+
NonDbcFileFormatError("Invalid dbc file. Missing the required version header. Attempting to read line: (" + line + ").") { }
5044
};
5145

52-
class DbcFileIsMissingBitTiming : public ValidityError {
46+
class DbcFileIsMissingBitTiming : public NonDbcFileFormatError {
5347
public:
54-
DbcFileIsMissingBitTiming(const std::string& line) {
55-
error_msg = {"Invalid dbc file. Missing required bit timing in the header. Attempting to read line: (" + line + ")."};
56-
}
57-
58-
const char* what() const throw() override {
59-
return error_msg.c_str();
60-
}
61-
62-
private:
63-
std::string error_msg;
48+
explicit DbcFileIsMissingBitTiming(const std::string& line):
49+
NonDbcFileFormatError("Invalid dbc file. Missing required bit timing in the header. Attempting to read line: (" + line + ").") { }
6450
};
6551

6652
} // libdbc

src/dbc.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,21 @@ DbcParser::DbcParser()
7171
}
7272

7373
#if __cplusplus >= 201703L
74-
bool DbcParser::is_valid_dbc_file(const std::filesystem::path& file_path) {
74+
void DbcParser::validate_dbc_file(const std::filesystem::path& file_path) {
7575
#else
76-
bool DbcParser::is_valid_dbc_file(const std::string& file_path) {
76+
void DbcParser::validate_dbc_file(const std::string& file_path) {
7777
#endif // __cplusplus >= 201703L
7878

7979
std::ifstream testStream{file_path};
80-
if (!testStream.is_open()) { return false; }
8180

82-
return is_valid_dbc_file(testStream);
81+
return validate_dbc_file(testStream);
8382
}
8483

85-
bool DbcParser::is_valid_dbc_file(std::istream& stream) {
84+
void DbcParser::validate_dbc_file(std::istream& stream) {
8685
stream.seekg(0, std::ios::beg);
8786

88-
try {
89-
DbcParser parser{};
90-
parser.parse_dbc_header(stream);
91-
} catch (const Exception& e) {
92-
// If the file is missing the version, it is not a valid DBC file
93-
return false;
94-
}
95-
96-
return true;
87+
DbcParser parser{};
88+
parser.parse_dbc_header(stream);
9789
}
9890

9991
void DbcParser::parse_file(std::istream& stream) {
@@ -114,9 +106,7 @@ void DbcParser::parse_file(std::istream& stream) {
114106
}
115107

116108
void DbcParser::parse_file(const std::string& file_name) {
117-
if (!is_valid_dbc_file(file_name)) {
118-
throw NonDbcFileFormatError(file_name, get_extension(file_name));
119-
}
109+
validate_dbc_file(file_name);
120110

121111
std::ifstream stream(file_name);
122112

test/test_dbc.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ TEST_CASE("Testing dbc file loading error issues", "[fileio][error]") {
1414
auto parser = std::unique_ptr<Libdbc::DbcParser>(new Libdbc::DbcParser());
1515

1616
SECTION("Loading a non dbc file should throw an error", "[error]") {
17-
REQUIRE_THROWS_AS(parser->parse_file(TEXT_FILE), Libdbc::NonDbcFileFormatError);
18-
REQUIRE_THROWS_WITH(parser->parse_file(TEXT_FILE), ContainsSubstring("TextFile.txt"));
17+
REQUIRE_THROWS_AS(parser->validate_dbc_file(TEXT_FILE), Libdbc::NonDbcFileFormatError);
1918
}
2019

2120
SECTION("Loading a dbc with missing version header throws an error (VERSION)", "[error]") {

0 commit comments

Comments
 (0)