Commit 0bf2be5c authored by Paul Asmuth's avatar Paul Asmuth
Browse files

read csv until eof

parent de27edc5
Loading
Loading
Loading
Loading
+38 −12
Original line number Diff line number Diff line
@@ -43,21 +43,47 @@ TEST_CASE(CSVInputStreamTest, TestReadHeaders, [] () {
  std::vector<std::string> headers;
  csv_file->readNextRow(&headers);
  EXPECT_EQ(headers.size(), 2);
  EXPECT_EQ(headers[0], std::string("country"));
  EXPECT_EQ(headers[0], "country");
  EXPECT_EQ(headers[1], "gbp");
});

TEST_CASE(CSVInputStreamTest, TestReadSimpleRows, [] () {
  auto csv_file = CSVInputStream::openFile(
      "test/fixtures/gbp_per_country_simple.csv");
  EXPECT(csv_file.get() != nullptr);

  std::vector<std::string> headers;
  csv_file->readNextRow(&headers);
  EXPECT_EQ(headers.size(), 2);

  std::vector<std::string> row1;
  EXPECT(csv_file->readNextRow(&row1));
  EXPECT_EQ(row1.size(), 2);
  EXPECT_EQ(row1[0], "USA");
  EXPECT_EQ(row1[1], "16800000");

  std::vector<std::string> row2;
  EXPECT(csv_file->readNextRow(&row2));
  EXPECT_EQ(row2.size(), 2);
  EXPECT_EQ(row2[0], "CHN");
  EXPECT_EQ(row2[1], "9240270");
});

TEST_CASE(CSVInputStreamTest, TestReadSimpleRowsEOF, [] () {
  auto csv_file = CSVInputStream::openFile(
      "test/fixtures/gbp_per_country_simple.csv");
  EXPECT(csv_file.get() != nullptr);
  int num_rows = 0;

/*
int main() {
  try {
    csv_file_test.run();
  } catch (fnordmetric::util::RuntimeException e) {
    printf("test fail :(\n\n");
    e.debugPrint();
    exit(1);
  for (;; ++num_rows) {
    std::vector<std::string> row;

    if (!csv_file->readNextRow(&row)) {
      break;
    }

  printf("all tests passed! :)\n");
    EXPECT_EQ(row.size(), 2);
  }
*/

  EXPECT_EQ(num_rows, 192);
});
+12 −3
Original line number Diff line number Diff line
@@ -41,12 +41,19 @@ CSVInputStream::CSVInputStream(
    row_seperator_(row_seperator),
    quote_char_(quote_char) {}

void CSVInputStream::readNextRow(std::vector<std::string>* target) {
bool CSVInputStream::readNextRow(std::vector<std::string>* target) {
  bool eof = false;

  for (;;) {
    std::string column;
    char byte;

    while (input_->readNextByte(&byte)) {
    for (;;) {
      if (!input_->readNextByte(&byte)) {
        eof = true;
        break;
      }

      if (byte == column_seperator_) {
        break;
      }
@@ -60,10 +67,12 @@ void CSVInputStream::readNextRow(std::vector<std::string>* target) {

    target->emplace_back(column);

    if (byte == row_seperator_) {
    if (eof || byte == row_seperator_) {
      break;
    }
  }

  return !eof;
}

}
+3 −2
Original line number Diff line number Diff line
@@ -40,9 +40,10 @@ public:
      char quote_char = '"');

  /**
   * Read the next row from the csv file
   * Read the next row from the csv file. Returns true if a row was read and
   * false on EOF. May raise an exception.
   */
  void readNextRow(std::vector<std::string>* target);
  bool readNextRow(std::vector<std::string>* target);

protected: