Commit e94b9c29 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

WIP

parent 3a34fbdf
Loading
Loading
Loading
Loading
+23 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 * Licensed under the MIT license (see LICENSE).
 */
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include "csvfile.h"
#include "../../util/runtimeexception.h"
@@ -37,12 +38,33 @@ CSVFile::CSVFile(
    char column_seperator /* = ',' */,
    char row_seperator /* = '\n' */,
    char quote_char /* = '"' */) :
    fd_(fd) {
    fd_(fd),
    buf_len_(0),
    buf_pos_(0) {
  assert(fd > 0);
}

void CSVFile::readNextRow(std::vector<std::string>* target) {
  target->emplace_back(readNextColumn());
}

std::string CSVFile::readNextColumn() {
  if (buf_pos_ < buf_len_) {
    readNextChunk();
  }
}

bool CSVFile::readNextChunk() {
  int bytes_read = read(fd_, buf_, sizeof(buf_));

  if (bytes_read < 0) {
    throw RUNTIME_EXCEPTION_ERRNO(
        &typeid(CSVFile),
        ERR_CSV_READ_ERROR,
        "read() failed");
  }

  return bytes_read == 0;
}

}
+19 −1
Original line number Diff line number Diff line
@@ -19,7 +19,8 @@ class CSVFile {
public:

  enum ErrorCodes {
    ERR_CSV_CANNOT_OPEN_FILE = 4000
    ERR_CSV_CANNOT_OPEN_FILE = 4000,
    ERR_CSV_READ_ERROR = 4001
  };

  /**
@@ -55,7 +56,24 @@ public:
  void readNextRow(std::vector<std::string>* target);

protected:

  /**
   * Read the next column from the csv file
   */
  std::string readNextColumn();


  /**
   * Read the next chunk from disk. Returns true on success and false on EOF.
   * Raises an exception on error.
   */
  bool readNextChunk();


  int fd_;
  char buf_[4];
  size_t buf_len_;
  size_t buf_pos_;
};

}