Skip to content

Commit 467ffa6

Browse files
committed
Got a base file input test going. I am checking the safe getline function
1 parent 2c63ea8 commit 467ffa6

File tree

8 files changed

+165
-2
lines changed

8 files changed

+165
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
1212

1313
# add where to find the source files
1414
include_directories(src)
15+
include_directories(test)
1516
add_subdirectory(test)
1617

src/dbc.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#include <string>
3+
#include <fstream>
4+
#include <iostream>
5+
6+
namespace SafeStr {
7+
8+
/**
9+
* This is a safe non line ending specific getline function. This is to help with files
10+
* carried over from different systems. i.e Unix file comes to Windows with LF endings
11+
* instead of CRLF.
12+
*
13+
* @param stream [description]
14+
* @param line [description]
15+
* @return [description]
16+
*/
17+
std::istream & getline( std::istream & stream, std::string & line ) {
18+
std::string newline;
19+
20+
std::getline( stream, newline );
21+
22+
// Windows CRLF (\r\n)
23+
if ( newline.size() && newline[newline.size()-1] == '\r' ) {
24+
line = newline.substr( 0, newline.size() - 1 );
25+
// MacOS LF (\r)
26+
} else if (newline.size() && newline[newline.size()] == '\r') {
27+
line = newline.replace(newline.size(), 1, "\n");
28+
} else {
29+
line = newline;
30+
}
31+
32+
return stream;
33+
}
34+
35+
}
36+

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_executable(dbc_test main.cpp)
1+
add_executable(dbc_test main.cpp file_loading.cpp)

test/dbcs/Sample1.dbc

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
VERSION ""
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+
38+
BO_ 100 DRIVER_HEARTBEAT: 1 DRIVER
39+
SG_ DRIVER_HEARTBEAT_cmd : 0|8@1+ (1,0) [0|0] "" SENSOR,MOTOR
40+
41+
BO_ 500 IO_DEBUG: 4 IO
42+
SG_ IO_DEBUG_test_unsigned : 0|8@1+ (1,0) [0|0] "" DBG
43+
SG_ IO_DEBUG_test_enum : 8|8@1+ (1,0) [0|0] "" DBG
44+
SG_ IO_DEBUG_test_signed : 16|8@1- (1,0) [0|0] "" DBG
45+
SG_ IO_DEBUG_test_float : 24|8@1+ (0.5,0) [0|0] "" DBG
46+
47+
BO_ 101 MOTOR_CMD: 1 DRIVER
48+
SG_ MOTOR_CMD_steer : 0|4@1- (1,-5) [-5|5] "" MOTOR
49+
SG_ MOTOR_CMD_drive : 4|4@1+ (1,0) [0|9] "" MOTOR
50+
51+
BO_ 400 MOTOR_STATUS: 3 MOTOR
52+
SG_ MOTOR_STATUS_wheel_error : 0|1@1+ (1,0) [0|0] "" DRIVER,IO
53+
SG_ MOTOR_STATUS_speed_kph : 8|16@1+ (0.001,0) [0|0] "kph" DRIVER,IO
54+
55+
BO_ 200 SENSOR_SONARS: 8 SENSOR
56+
SG_ SENSOR_SONARS_mux M : 0|4@1+ (1,0) [0|0] "" DRIVER,IO
57+
SG_ SENSOR_SONARS_err_count : 4|12@1+ (1,0) [0|0] "" DRIVER,IO
58+
SG_ SENSOR_SONARS_left m0 : 16|12@1+ (0.1,0) [0|0] "" DRIVER,IO
59+
SG_ SENSOR_SONARS_middle m0 : 28|12@1+ (0.1,0) [0|0] "" DRIVER,IO
60+
SG_ SENSOR_SONARS_right m0 : 40|12@1+ (0.1,0) [0|0] "" DRIVER,IO
61+
SG_ SENSOR_SONARS_rear m0 : 52|12@1+ (0.1,0) [0|0] "" DRIVER,IO
62+
SG_ SENSOR_SONARS_no_filt_left m1 : 16|12@1+ (0.1,0) [0|0] "" DBG
63+
SG_ SENSOR_SONARS_no_filt_middle m1 : 28|12@1+ (0.1,0) [0|0] "" DBG
64+
SG_ SENSOR_SONARS_no_filt_right m1 : 40|12@1+ (0.1,0) [0|0] "" DBG
65+
SG_ SENSOR_SONARS_no_filt_rear m1 : 52|12@1+ (0.1,0) [0|0] "" DBG
66+
67+
68+
69+
70+
CM_ BU_ DRIVER "The driver controller driving the car";
71+
CM_ BU_ MOTOR "The motor controller of the car";
72+
CM_ BU_ SENSOR "The sensor controller of the car";
73+
CM_ BO_ 100 "Sync message used to synchronize the controllers";
74+
75+
BA_DEF_ "BusType" STRING ;
76+
BA_DEF_ BO_ "GenMsgCycleTime" INT 0 0;
77+
BA_DEF_ SG_ "FieldType" STRING ;
78+
79+
BA_DEF_DEF_ "BusType" "CAN";
80+
BA_DEF_DEF_ "FieldType" "";
81+
BA_DEF_DEF_ "GenMsgCycleTime" 0;
82+
83+
BA_ "GenMsgCycleTime" BO_ 100 1000;
84+
BA_ "GenMsgCycleTime" BO_ 500 100;
85+
BA_ "GenMsgCycleTime" BO_ 101 100;
86+
BA_ "GenMsgCycleTime" BO_ 400 100;
87+
BA_ "GenMsgCycleTime" BO_ 200 100;
88+
BA_ "FieldType" SG_ 100 DRIVER_HEARTBEAT_cmd "DRIVER_HEARTBEAT_cmd";
89+
BA_ "FieldType" SG_ 500 IO_DEBUG_test_enum "IO_DEBUG_test_enum";
90+
91+
92+
VAL_ 100 DRIVER_HEARTBEAT_cmd 2 "DRIVER_HEARTBEAT_cmd_REBOOT" 1 "DRIVER_HEARTBEAT_cmd_SYNC" 0 "DRIVER_HEARTBEAT_cmd_NOOP" ;
93+
VAL_ 500 IO_DEBUG_test_enum 2 "IO_DEBUG_test2_enum_two" 1 "IO_DEBUG_test2_enum_one" ;

test/dbcs/TextFile.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is a non dbc formatted file.
2+
3+
Make sure things pass with this
4+
Who knows what might happen.

test/defines.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <string>
2+
3+
static const std::string TEXT_FILE = "./dbcs/TextFile.txt";

test/file_loading.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "catch.hpp"
2+
#include "defines.hpp"
3+
#include "dbc.hpp"
4+
5+
TEST_CASE("Basic file input with safe getline", "") {
6+
7+
SECTION("Verify line inputs") {
8+
std::ifstream TextFile;
9+
std::string test;
10+
11+
TextFile.open(TEXT_FILE, std::ios::in);
12+
CHECK(TextFile.is_open());
13+
14+
if(TextFile.is_open()) {
15+
SafeStr::getline(TextFile, test);
16+
REQUIRE(test == "This is a non dbc formatted file.");
17+
SafeStr::getline(TextFile, test);
18+
REQUIRE(test == "");
19+
SafeStr::getline(TextFile, test);
20+
REQUIRE(test == "Make sure things pass with this");
21+
SafeStr::getline(TextFile, test);
22+
REQUIRE(test == "Who knows what might happen.");
23+
24+
TextFile.close();
25+
}
26+
}
27+
}

test/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
22
#include "catch.hpp"
3-
#include "dbc.hpp"

0 commit comments

Comments
 (0)