Skip to content

Commit 73d18b6

Browse files
committed
Moved utility functions to the appropriate folder. setup some folder structure to help split up the project.
Testing is working at the point. I have the basic utility function working as a test. There is not output of any library or single header yet. For now all development will happen in the test folder until first release.
1 parent 89edda2 commit 73d18b6

File tree

7 files changed

+60
-46
lines changed

7 files changed

+60
-46
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ set(GCC_COVERAGE_COMPILE_FLAGS "-g")
1111
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
1212

1313
# add where to find the source files
14+
list(APPEND SOURCE ${PROJECT_SOURCE_DIR}/src/util/utils.cpp)
15+
1416
include_directories(src)
15-
include_directories(test)
17+
include_directories(include)
18+
1619
add_subdirectory(test)
1720
add_subdirectory(doc)
1821

src/dbc.hpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,4 @@
11

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-
37-
382
namespace dbc {
393

404
class parser {

src/util/utils.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "util/utils.hpp"
2+
3+
namespace Utils {
4+
5+
std::istream & SafeString::getline( std::istream & stream, std::string & line ) {
6+
std::string newline;
7+
8+
std::getline( stream, newline );
9+
10+
// Windows CRLF (\r\n)
11+
if ( newline.size() && newline[newline.size()-1] == '\r' ) {
12+
line = newline.substr( 0, newline.size() - 1 );
13+
// MacOS LF (\r)
14+
} else if (newline.size() && newline[newline.size()] == '\r') {
15+
line = newline.replace(newline.size(), 1, "\n");
16+
} else {
17+
line = newline;
18+
}
19+
20+
return stream;
21+
}
22+
23+
} // Namespace Utils

src/util/utils.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <string>
2+
#include <fstream>
3+
#include <iostream>
4+
5+
namespace Utils {
6+
7+
class SafeString {
8+
public:
9+
static std::istream & getline( std::istream & stream, std::string & line );
10+
11+
};
12+
13+
}

test/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
project(tests VERSION 0.1.0)
22

3+
list(APPEND TEST_SOURCES main.cpp
4+
test_file_loading.cpp
5+
test_utils.cpp)
6+
37
include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/third_party/Catch2/single_include)
48

5-
add_executable(tests main.cpp file_loading.cpp)
9+
add_executable(tests ${TEST_SOURCES} ${SOURCE})
610

711
add_custom_target(test
812
COMMAND ${PROJECT_NAME}

test/test_file_loading.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <catch2/catch.hpp>
2+
#include "defines.hpp"
3+
4+
TEST_CASE("Loading a basic file", "[fileio]") {
5+
6+
7+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// #include "catch.hpp"
21
#include <catch2/catch.hpp>
32
#include "defines.hpp"
4-
#include "dbc.hpp"
3+
#include "util/utils.hpp"
54

6-
TEST_CASE("Basic file input with safe getline", "") {
5+
using namespace Utils;
76

7+
TEST_CASE("Basic file input with safe getline", "") {
88
SECTION("Verify line inputs") {
99
std::ifstream TextFile;
1010
std::string test;
@@ -13,16 +13,16 @@ TEST_CASE("Basic file input with safe getline", "") {
1313
CHECK(TextFile.is_open());
1414

1515
if(TextFile.is_open()) {
16-
SafeStr::getline(TextFile, test);
16+
SafeString::getline(TextFile, test);
1717
REQUIRE(test == "This is a non dbc formatted file.");
18-
SafeStr::getline(TextFile, test);
18+
SafeString::getline(TextFile, test);
1919
REQUIRE(test == "");
20-
SafeStr::getline(TextFile, test);
20+
SafeString::getline(TextFile, test);
2121
REQUIRE(test == "Make sure things pass with this");
22-
SafeStr::getline(TextFile, test);
22+
SafeString::getline(TextFile, test);
2323
REQUIRE(test == "Who knows what might happen.");
2424

2525
TextFile.close();
2626
}
2727
}
28-
}
28+
}

0 commit comments

Comments
 (0)