mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 13:05:20 +01:00
add support for CRLF
This commit is contained in:
parent
83bbab4112
commit
0d84fcf7e8
@ -14,7 +14,7 @@ class parser {
|
|||||||
public:
|
public:
|
||||||
parser(const std::string& file_name, const std::string& delimiter)
|
parser(const std::string& file_name, const std::string& delimiter)
|
||||||
: file_name_{file_name}, delim_{delimiter},
|
: file_name_{file_name}, delim_{delimiter},
|
||||||
file_{fopen(file_name_.c_str(), "r")} {
|
file_{fopen(file_name_.c_str(), "rb")} {
|
||||||
if (file_) {
|
if (file_) {
|
||||||
read_line();
|
read_line();
|
||||||
} else {
|
} else {
|
||||||
@ -82,11 +82,17 @@ class parser {
|
|||||||
|
|
||||||
bool read(FILE* file) {
|
bool read(FILE* file) {
|
||||||
ssize_t size = getline(&buffer_, &size_, file);
|
ssize_t size = getline(&buffer_, &size_, file);
|
||||||
|
size_t string_end = size - 1;
|
||||||
|
|
||||||
if (size == -1) {
|
if (size == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer_[size - 1] = '\0';
|
if (size >= 2 && buffer_[size - 2] == '\r') {
|
||||||
|
string_end--;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer_[string_end] = '\0';
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,14 +43,16 @@ template <typename T>
|
|||||||
static void make_and_write(const std::string& file_name,
|
static void make_and_write(const std::string& file_name,
|
||||||
const std::vector<T>& data) {
|
const std::vector<T>& data) {
|
||||||
std::ofstream out{file_name};
|
std::ofstream out{file_name};
|
||||||
for (const auto& i : data) {
|
std::vector<const char*> new_lines = {"\n", "\r\n"};
|
||||||
out << i.to_string() << std::endl;
|
for (size_t i = 0; i < data.size(); ++i) {
|
||||||
|
out << data[i].to_string() << new_lines[i % new_lines.size()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("testing parser") {
|
TEST_CASE("testing parser") {
|
||||||
unique_file_name f;
|
unique_file_name f;
|
||||||
std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"}};
|
std::vector<X> 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);
|
make_and_write(f.name, data);
|
||||||
{
|
{
|
||||||
ss::parser p{f.name, ","};
|
ss::parser p{f.name, ","};
|
||||||
@ -101,17 +103,20 @@ TEST_CASE("testing parser") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
constexpr int excluded = 3;
|
||||||
ss::parser p{f.name, ","};
|
ss::parser p{f.name, ","};
|
||||||
std::vector<X> i;
|
std::vector<X> i;
|
||||||
|
|
||||||
while (!p.eof()) {
|
while (!p.eof()) {
|
||||||
auto a = p.get_struct<X, ss::ax<int, 3>, double,
|
auto a = p.get_struct<X, ss::ax<int, excluded>, double,
|
||||||
std::string>();
|
std::string>();
|
||||||
if (p.valid()) {
|
if (p.valid()) {
|
||||||
i.push_back(a);
|
i.push_back(a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::vector<X> expected = {{1, 2, "x"}, {5, 6, "z"}};
|
std::vector<X> 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()));
|
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user