Skip to content

Commit

Permalink
EndOfLine returns always CellContent
Browse files Browse the repository at this point in the history
  • Loading branch information
ferkulat committed Sep 4, 2019
1 parent 96c73e9 commit 9e92bf7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
21 changes: 10 additions & 11 deletions csv2xlslib.Test/ReadCellTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ bool operator==(EndOfBuffer const& c1, EndOfBuffer const& c2)
{
return (c1.unfinished_cell == c2.unfinished_cell);
}
bool operator==(EndOfLine const& c1, EndOfLine const& c2)
{
return (c1.cell == c2.cell);
}
namespace ReadCellTest
{

Expand Down Expand Up @@ -57,8 +61,8 @@ TEST_CASE("Given the last cell in buffer with new line returns EndOfBuffer with

strncpy(buffer.mem.get(), content, buffer.m_size);

auto const actual = std::get<CellContent>(read(buffer, CsvSeparator(';')));
auto const expected = CellContent{buffer.mem.get() + 12, 4};
auto const actual = std::get<EndOfLine>(read(buffer, CsvSeparator(';')));
auto const expected = EndOfLine{CellContent{buffer.mem.get() + 12, 4}};

REQUIRE(actual == expected);
}
Expand All @@ -71,8 +75,8 @@ TEST_CASE(R"(Given the last cell in buffer with \r\n returns EndOfBuffer with th

strncpy(buffer.mem.get(), content, buffer.m_size);

auto const actual = std::get<CellContent>(read(buffer, CsvSeparator(';')));
auto const expected = CellContent{buffer.mem.get() + 12, 4};
auto const actual = std::get<EndOfLine>(read(buffer, CsvSeparator(';')));
auto const expected = EndOfLine{CellContent{buffer.mem.get() + 12, 4}};

REQUIRE(actual == expected);
}
Expand Down Expand Up @@ -115,16 +119,11 @@ TEST_CASE("Given a cell ending with \n, returns Cell, but the next read returns
strncpy(buffer.mem.get(), content, buffer.m_size);

{
auto const actual = std::get<CellContent>(read(buffer, CsvSeparator(';')));
auto const expected = CellContent{buffer.mem.get() + 6, 5};
auto const actual = std::get<EndOfLine>(read(buffer, CsvSeparator(';')));
auto const expected = EndOfLine{CellContent{buffer.mem.get() + 6, 5}};

REQUIRE(actual == expected);
}
{
auto const actual = read(buffer, CsvSeparator(';'));

REQUIRE(std::holds_alternative<EndOfLine>(actual));
}
}

}
Expand Down
16 changes: 9 additions & 7 deletions csv2xlslib.Test/convertCsvTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ namespace ConvertCsvTest{

Given_a_Csv_file_with_3_lines_and_no_newline_at_end_of_file()
{
file << "1111;2222;3333\n";
file << "dog;cat;duck\n";
file << ";2222;3333\n";
file << "dog;cat;\n";
file << ";shark;bird\n";
file << "milk;tea;coffee";
}
};
Expand All @@ -72,12 +73,13 @@ namespace ConvertCsvTest{
{
Buffer buffer(10);
auto outfile = OutFile(OutputRowLimit(3), OutputColumnLimit(4),data);
auto const actual = convertCsv(OutputDoc(OutFile(outfile)), buffer, OutputRowLimit(3), file);
REQUIRE(data.size() == 3 );
auto const actual = convertCsv(OutputDoc(OutFile(outfile)), buffer, OutputRowLimit(4), file);
REQUIRE(data.size() == 4 );
using ROW = std::vector<std::string>;
REQUIRE(data[0] == ROW{"1111","2222","3333"});
REQUIRE(data[1] == ROW{"dog","cat","duck"});
REQUIRE(data[2] == ROW{"milk","tea","coffee"});
REQUIRE(data[0] == ROW{"","2222","3333"});
REQUIRE(data[1] == ROW{"dog","cat",""});
REQUIRE(data[2] == ROW{"","shark","bird"});
REQUIRE(data[3] == ROW{"milk","tea","coffee"});

}
}
Expand Down
27 changes: 14 additions & 13 deletions csv2xlslib/convertCsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ constexpr auto AllOf(Preds... preds){
return (... && preds(value));
};
}


using chaining::operator|;
using chaining::repeatUntil;

enum class ChainStatus{Break, Ok, EndOfStream};
enum class Status{Break, Ok, EndOfStream};
namespace Domain
{

Expand All @@ -95,7 +97,7 @@ auto appendToOutputDoc(OutputDoc& output_doc, CsvType csv_type)
using R = std::variant<Row, Column, EndOfBuffer, EndOfStream>;
return MatchType(csv_type
, [&](CellContent cell) -> R { return output_doc.appendCell(cell); }
, [&](EndOfLine ) -> R { return output_doc.newLine(); }
, [&](EndOfLine eol ) -> R { output_doc.appendCell(eol.cell);return output_doc.newLine(); }
, [ ](auto anything ) -> R { return anything; }
);
}
Expand All @@ -104,7 +106,7 @@ auto appendToOutputDoc(OutputDoc& output_doc, CsvType csv_type)
auto fill(Buffer& buffer, std::istream& csv_input)
{
if (!csv_input.good())
return false;
return Status::EndOfStream;

auto const bytes_left = buffer.end - buffer.mem.get();
auto const bytes_to_read = ConvertTo<long>(buffer.m_size) - bytes_left;
Expand All @@ -118,7 +120,7 @@ auto fill(Buffer& buffer, std::istream& csv_input)
*buffer.end = '\0';
++(buffer.end);
}
return true;
return Status::Ok;
}

auto init(Buffer& buffer, EndOfBuffer eob)
Expand All @@ -134,6 +136,7 @@ auto init(Buffer& buffer, EndOfBuffer eob)
{
buffer.end = buffer.mem.get();
}
return Status::Ok;
}

}
Expand All @@ -149,21 +152,19 @@ auto appendToOutputDoc(OutputDoc& output_doc)

auto fill(Buffer& buffer, std::istream& csv_input)
{
return [&](ChainStatus status) {
return (status == ChainStatus::Break)? ChainStatus::Break:
(status == ChainStatus::EndOfStream)? ChainStatus::EndOfStream:
(Domain::fill(buffer, csv_input)) ? ChainStatus::Ok : ChainStatus::EndOfStream;
return [&](Status status) {
return (status == Status::Ok )? Domain::fill(buffer, csv_input): status;
};
}

auto init (Buffer& buffer)
{
return [&](std::variant<Row, Column, EndOfBuffer, EndOfStream> status){
return MatchType(status
,[&](EndOfBuffer eob){Domain::init(buffer, eob);return ChainStatus::Ok;}
,[ ](EndOfStream ){return ChainStatus::EndOfStream;}
,[ ](Row ){return ChainStatus::Break;}
,[ ](auto ){return ChainStatus::Ok;}
,[&](EndOfBuffer eob){return Domain::init(buffer, eob);}
,[ ](EndOfStream ){return Status::EndOfStream;}
,[ ](Row ){return Status::Break;}
,[ ](auto ){return Status::Ok;}
);
};
}
Expand Down Expand Up @@ -207,7 +208,7 @@ OutputDoc convertCsv(OutputDoc output_doc, Buffer& buffer, OutputRowLimit output
| repeatUntil(matchesOneOf(isEndOfBuffer, isEndOfStreamType, isRowLimit(output_row_limit)))
| init(buffer)
| fill(buffer, stream)
| repeatUntil(isEitherOf(ChainStatus::Break, ChainStatus::EndOfStream))
| repeatUntil(isEitherOf(Status::Break, Status::EndOfStream))
)();
return output_doc;

Expand Down
7 changes: 1 addition & 6 deletions csv2xlslib/readBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ CsvType read(Buffer& buffer, CsvSeparator csv_separator)
if (*current_position == '\n')
{
auto const length = cellLengthForLineEnd(start, current_position);
if (length)
{
buffer.current_position = current_position;
return CellContent{start, length};
}
buffer.current_position = current_position + 1;
return EndOfLine{};
return EndOfLine{CellContent{start, length}};
}
if (*current_position == '\0')
{
Expand Down
8 changes: 3 additions & 5 deletions csv2xlslib/readBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ struct Buffer
}
};

struct EndOfLine{};
struct EndOfBuffer{std::optional<CellContent> unfinished_cell;};
struct EndOfLine{CellContent cell;};
struct EndOfStream{};
struct EndOfBuffer
{
std::optional<CellContent> unfinished_cell;
};

using CsvType = std::variant<CellContent, EndOfLine, EndOfBuffer, EndOfStream>;

CsvType read(Buffer& buffer, CsvSeparator csv_separator);
Expand Down

0 comments on commit 9e92bf7

Please sign in to comment.