diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index b33c6cd..ba3d300 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -14,7 +14,7 @@ class parser { public: parser(const std::string& file_name, const std::string& delimiter) : file_name_{file_name}, delim_{delimiter}, - file_{fopen(file_name_.c_str(), "r")} { + file_{fopen(file_name_.c_str(), "rb")} { if (file_) { read_line(); } else { @@ -82,11 +82,17 @@ class parser { bool read(FILE* file) { ssize_t size = getline(&buffer_, &size_, file); + size_t string_end = size - 1; + if (size == -1) { return false; } - buffer_[size - 1] = '\0'; + if (size >= 2 && buffer_[size - 2] == '\r') { + string_end--; + } + + buffer_[string_end] = '\0'; return true; } diff --git a/test/test_parser.cpp b/test/test_parser.cpp index f7db772..e84fe83 100644 --- a/test/test_parser.cpp +++ b/test/test_parser.cpp @@ -43,14 +43,16 @@ template static void make_and_write(const std::string& file_name, const std::vector& data) { std::ofstream out{file_name}; - for (const auto& i : data) { - out << i.to_string() << std::endl; + std::vector new_lines = {"\n", "\r\n"}; + for (size_t i = 0; i < data.size(); ++i) { + out << data[i].to_string() << new_lines[i % new_lines.size()]; } } TEST_CASE("testing parser") { unique_file_name f; - std::vector data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"}}; + std::vector data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"}, + {7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}}; make_and_write(f.name, data); { ss::parser p{f.name, ","}; @@ -101,17 +103,20 @@ TEST_CASE("testing parser") { } { + constexpr int excluded = 3; ss::parser p{f.name, ","}; std::vector i; while (!p.eof()) { - auto a = p.get_struct, double, + auto a = p.get_struct, double, std::string>(); if (p.valid()) { i.push_back(a); } } - std::vector expected = {{1, 2, "x"}, {5, 6, "z"}}; + std::vector expected = data; + std::remove_if(expected.begin(), expected.end(), + [](const X& x) { return x.i == excluded; }); CHECK(std::equal(i.begin(), i.end(), expected.begin())); }