Skip to content

Commit 4ba5f8f

Browse files
committed
Merge branch 'starting-library'
2 parents 72226f3 + 56dd6ee commit 4ba5f8f

File tree

13 files changed

+166
-24
lines changed

13 files changed

+166
-24
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
cmake_minimum_required(VERSION 3.10)
22

3-
project(DBC)
3+
project(dbc)
44

55
# specify the C++ standard
66
set(CMAKE_CXX_STANDARD 11)
77
set(CMAKE_CXX_STANDARD_REQUIRED True)
88

9-
# Add in the debug flag
9+
# Add in the debug flag0.
1010
set(GCC_COVERAGE_COMPILE_FLAGS "-g")
1111
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
1212

@@ -19,3 +19,4 @@ include_directories(include)
1919
add_subdirectory(test)
2020
add_subdirectory(doc)
2121

22+
add_library(${PROJECT_NAME} STATIC ${SOURCE})

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
# C++ DBC Header Only File Parser
1+
# C++ DBC Parser
22

33
This is to provide a library header only file to read in DBC files. I was looking around and couldn't
44
find a simple library that didn't have dependencies. So here we are making one. I got some inspiration
55
from the python dbc library here: https://pypi.org/project/cantools/
66

77
## Testing
88

9-
I am trying to alwasy make sure that this is very well tested code. I am using Catch2 to do this
9+
I am trying to always make sure that this is very well tested code. I am using Catch2 to do this
1010
testing and if you aren't familiar here is the documentation: https://github.com/catchorg/Catch2/blob/master/docs/Readme.md#top
1111

12+
To run the tests locally you can use the following:
13+
```bash
14+
mkdir build
15+
cd build
16+
cmake ..
17+
make test -j
18+
```
19+
1220
## Building
1321

14-
I am using Cmake to be able to build the tests. I plan on doing more with it but this is what it
22+
I am using Cmake to be able to build the tests and the lib. I plan on doing more with it but this is what it
1523
is for now. I am doing developement on the WSL Ubuntu 18.04 kernel. This doesn't mean that IDEs aren't
1624
welcome but the build process might not be suited for this. You will need to modify it for your
1725
needs. Feel free to submit changes so the building process will be more robust.
@@ -24,8 +32,6 @@ cmake ..
2432
make
2533
```
2634

27-
These steps produce an executable in the bin folder.
28-
2935
## Contributing
3036

3137
I welcome all help! Please feel free to fork and start some pull requests!

doc/Doxyfile.in

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ INPUT = @doxy_main_page@ \
66
@PROJECT_SOURCE_DIR@ \
77
@PROJECT_BINARY_DIR@
88
FILE_PATTERNS = *.h \
9-
*.cc
9+
*.hpp \
10+
*.cc \
11+
*.ccp
1012
RECURSIVE = YES
11-
USE_MDFILE_AS_MAINPAGE = @doxy_main_page@
13+
USE_MDFILE_AS_MAINPAGE = @doxy_main_page@
14+
15+
EXCLUDE_PATTERNS = */third_party/*
16+
EXCLUDE_PATTERNS += */test/*
17+
18+
HAVE_DOT = YES

src/dbc.hpp

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,67 @@
1+
#include "exceptions/error.hpp"
2+
#include "util/utils.hpp"
13

2-
namespace dbc {
4+
#include <regex>
35

4-
class parser {
6+
namespace libdbc {
57

6-
};
8+
class Parser {
9+
public:
10+
virtual ~Parser() = default;
11+
12+
virtual void parse_file(const std::string& file) = 0;
13+
14+
protected:
15+
16+
17+
};
18+
19+
class DbcParser : public Parser {
20+
public:
21+
DbcParser() : version(""), version_re("(VERSION)\\s\"(.*)\"") {}
22+
23+
virtual ~DbcParser() = default;
24+
25+
virtual void parse_file(const std::string& file) final override {
26+
std::ifstream s(file.c_str());
27+
std::string line;
28+
29+
parse_dbc_header(s);
30+
31+
while(!s.eof()) {
32+
utils::SafeString::get_line(s, line);
33+
34+
}
35+
36+
}
37+
38+
std::string get_version() const {
39+
return version;
40+
}
41+
42+
private:
43+
std::string version;
44+
45+
const std::regex version_re;
46+
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+
}
60+
61+
};
62+
63+
struct Message {
64+
65+
};
766

867
}

src/exceptions/error.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
namespace libdbc {
3+
4+
class exception : public std::exception {
5+
const char * what() const throw() {
6+
return "libdbc exception occurred";
7+
}
8+
};
9+
10+
class validity_error : public exception {
11+
const char * what() const throw() {
12+
return "invalid file issue";
13+
}
14+
};
15+
16+
} // libdbc

src/util/utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "util/utils.hpp"
22

3-
namespace Utils {
3+
namespace utils {
44

55
std::istream & SafeString::get_line( std::istream & stream, std::string & line ) {
66
std::string newline;

src/util/utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <fstream>
33
#include <iostream>
44

5-
namespace Utils {
5+
namespace utils {
66

77
class SafeString {
88
public:

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project(tests VERSION 0.1.0)
22

33
list(APPEND TEST_SOURCES main.cpp
4-
test_file_loading.cpp
4+
test_dbc.cpp
55
test_utils.cpp)
66

77
include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/third_party/Catch2/single_include)

test/dbcs/Sample2.dbc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
VERSION "1.0.0"
2+
3+
NS_ :
4+
BA_
5+
BA_DEF_
6+
BA_DEF_DEF_
7+
BA_DEF_DEF_REL_
8+
BA_DEF_REL_
9+
BA_DEF_SGTYPE_
10+
BA_REL_
11+
BA_SGTYPE_
12+
BO_TX_BU_
13+
BU_BO_REL_
14+
BU_EV_REL_
15+
BU_SG_REL_
16+
CAT_
17+
CAT_DEF_
18+
CM_
19+
ENVVAR_DATA_
20+
EV_DATA_
21+
FILTER
22+
NS_DESC_
23+
SGTYPE_
24+
SGTYPE_VAL_
25+
SG_MUL_VAL_
26+
SIGTYPE_VALTYPE_
27+
SIG_GROUP_
28+
SIG_TYPE_REF_
29+
SIG_VALTYPE_
30+
VAL_
31+
VAL_TABLE_
32+
33+
BS_:
34+
35+
BU_: DBG DRIVER IO MOTOR SENSOR
36+
37+
BO_ 500 IO_DEBUG: 4 IO
38+
SG_ IO_DEBUG_test_unsigned : 0|8@1+ (1,0) [0|0] "" DBG

test/defines.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#include <string>
22

3-
static const std::string TEXT_FILE = "./dbcs/TextFile.txt";
3+
static const std::string TEXT_FILE = "./dbcs/TextFile.txt";
4+
static const std::string DBC_FILE_1 = "./dbcs/Sample1.dbc";
5+
static const std::string DBC_FILE_2 = "./dbcs/Sample2.dbc";

0 commit comments

Comments
 (0)