File tree Expand file tree Collapse file tree 3 files changed +24
-12
lines changed
Expand file tree Collapse file tree 3 files changed +24
-12
lines changed Original file line number Diff line number Diff line change 11#include " exceptions/error.hpp"
22#include " util/utils.hpp"
33
4+ #include < regex>
5+
46namespace libdbc {
57
68 class Parser {
@@ -11,26 +13,20 @@ namespace libdbc {
1113
1214 protected:
1315
14- void verify_dbc_format (std::istream& file_stream) const {
15- std::string line;
1616
17- Utils::SafeString::get_line (file_stream, line);
18-
19- if (line.find (" VERSION" ) == std::string::npos) {
20- throw validity_error ();
21- }
22- }
2317 };
2418
2519 class DbcParser : public Parser {
2620 public:
21+ DbcParser () : version(" " ), version_re(" (VERSION)\\ s\" (.*)\" " ) {}
22+
2723 virtual ~DbcParser () = default ;
2824
2925 virtual void parse_file (const std::string& file) final override {
3026 std::ifstream s (file.c_str ());
3127 std::string line;
3228
33- verify_dbc_format (s);
29+ parse_dbc_header (s);
3430
3531 while (!s.eof ()) {
3632 Utils::SafeString::get_line (s, line);
@@ -40,11 +36,27 @@ namespace libdbc {
4036 }
4137
4238 std::string get_version () const {
43- return " " ;
39+ return version ;
4440 }
4541
4642 private:
43+ std::string version;
44+
45+ const std::regex version_re;
4746
47+ void parse_dbc_header (std::istream& file_stream) {
48+ std::string line;
49+ std::smatch match;
50+
51+ Utils::SafeString::get_line (file_stream, line);
52+
53+ if (!std::regex_search (line, match, version_re)) {
54+ throw validity_error ();
55+ }
56+
57+ version = match.str (2 );
58+
59+ }
4860
4961 };
5062
Original file line number Diff line number Diff line change 1- VERSION ""
1+ VERSION "1.0.0 "
22
33NS_ :
44 BA_
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ TEST_CASE("Testing dbc file loading", "[fileio]") {
1414
1515 parser->parse_file (DBC_FILE_2);
1616
17- REQUIRE (parser->get_version () == " " );
17+ REQUIRE (parser->get_version () == " 1.0.0 " );
1818 }
1919
2020}
You can’t perform that action at this time.
0 commit comments