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

csvfile -> csvinputstream

parent e94b9c29
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ set(FNORDMETRIC_QUERY_SOURCES
    src/query/tokenize.cc)

set(FNORDMETRIC_CSV_BACKEND_SOURCES
    src/backends/csv/csvfile.cc)
    src/backends/csv/csvinputstream.cc)

include_directories(./src)
set(CMAKE_CXX_FLAGS "-std=c++0x -stdlib=libc++")
+12 −8
Original line number Diff line number Diff line
@@ -10,32 +10,36 @@
#include <assert.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include "csvfile.h"
#include "csvinputstream.h"
//#include "csv_backend.h"
#include "../../util/runtimeexception.h"
#include "../../util/unittest.h"

using namespace fnordmetric::csv_backend;

UNIT_TEST(CSVFileTest);
UNIT_TEST(CSVInputStreamTest);

TEST_CASE(CSVInputStreamTest, TestOpenFile, [] () {
  auto csv_file = CSVInputStream::openFile(
      "test/fixtures/gbp_per_country.csv");

TEST_CASE(CSVFileTest, TestOpenFile, [] () {
  auto csv_file = CSVFile::openFile("test/fixtures/gbp_per_country.csv");
  EXPECT(csv_file.get() != nullptr);
});

TEST_CASE(CSVFileTest, TestInvalidFileName, [] () {
TEST_CASE(CSVInputStreamTest, TestInvalidFileName, [] () {
  auto errmsg = "error opening file 'test/fixtures/invalid.csv': "
      "No such file or directory";

  EXPECT_EXCEPTION(errmsg, [] () {
    auto csv_file = CSVFile::openFile("test/fixtures/invalid.csv");
    auto csv_file = CSVInputStream::openFile("test/fixtures/invalid.csv");
    EXPECT(csv_file.get() != nullptr);
  });
});

TEST_CASE(CSVFileTest, TestReadHeaders, [] () {
  auto csv_file = CSVFile::openFile("test/fixtures/gbp_per_country_simple.csv");
TEST_CASE(CSVInputStreamTest, TestReadHeaders, [] () {
  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);
+47 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * Licensed under the MIT license (see LICENSE).
 */
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include "csvinputstream.h"
#include "../../util/inputstream.h"
#include "../../util/runtimeexception.h"

namespace fnordmetric {
namespace csv_backend {

std::unique_ptr<CSVInputStream> CSVInputStream::openFile(
    const std::string& file_path,
    char column_seperator /* = ',' */,
    char row_seperator /* = '\n' */,
    char quote_char /* = '"' */) {
  auto csv_file = new CSVInputStream(
      fnordmetric::util::FileInputStream::openFile(file_path));

  return std::unique_ptr<CSVInputStream>(csv_file);
}

CSVInputStream::CSVInputStream(
    std::unique_ptr<fnordmetric::util::InputStream>&& input_stream,
    char column_seperator /* = ',' */,
    char row_seperator /* = '\n' */,
    char quote_char /* = '"' */) :
    input_(std::move(input_stream)) {}

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

// FIXPAUL optimize?
std::string CSVInputStream::readNextColumn() {
  std::string column;

  return column;
}

}
}
+59 −0
Original line number Diff line number Diff line
@@ -4,48 +4,37 @@
 *
 * Licensed under the MIT license (see LICENSE).
 */
#ifndef _FNORDMETRIC_CSVFILE_H
#define _FNORDMETRIC_CSVFILE_H
#ifndef _FNORDMETRIC_CSVINPUTSTREAM_H
#define _FNORDMETRIC_CSVINPUTSTREAM_H
#include <string>
#include <vector>
#include <memory>
#include "../../util/inputstream.h"
#include "../../util/unittest.h"

namespace fnordmetric {
namespace csv_backend {

class CSVFile {
  friend class fnordmetric::util::UnitTest::TestCase;
class CSVInputStream {
public:

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

  /**
   * Open a new csv file from the provided file path. Throws an exception if
   * the file cannot be opened.
   * Open a new csv file input stream from the provided file path. Throws an
   * exception if the file cannot be opened.
   *
   * @param file_path the path to the csv file
   */
  static std::unique_ptr<CSVFile> openFile(
  static std::unique_ptr<CSVInputStream> openFile(
      const std::string& file_path,
      char column_seperator = ',',
      char row_seperator = '\n',
      char quote_char = '"');

  CSVFile(const CSVFile& other) = delete;
  CSVFile& operator=(const CSVFile& other) = delete;

  /**
   * Create a new CSVFile instance with the provided filedescriptor. The csv
   * file takes ownership of the fd and will close() it when destructed.
   *
   * @param fd a valid an opened fd, transfers ownership and closes on destruct
   * Create a new CSVInputStream from the provided InputStream.
   */
  explicit CSVFile(
      int fd,
  explicit CSVInputStream(
      std::unique_ptr<fnordmetric::util::InputStream>&& input_stream,
      char column_seperator = ',',
      char row_seperator = '\n',
      char quote_char = '"');
@@ -62,18 +51,7 @@ protected:
   */
  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_;
  std::unique_ptr<fnordmetric::util::InputStream> input_;
};

}
+47 −0
Original line number Diff line number Diff line
@@ -4,16 +4,11 @@
 *
 * Licensed under the MIT license (see LICENSE).
 */
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include "csvfile.h"
#include "../../util/runtimeexception.h"

#include "inputstream.h"
namespace fnordmetric {
namespace csv_backend {
namespace util {

std::unique_ptr<CSVFile> CSVFile::openFile(
std::unique_ptr<InputStream> InputStream::openFile(
    const std::string& file_path,
    char column_seperator /* = ',' */,
    char row_seperator /* = '\n' */,
@@ -23,49 +18,30 @@ std::unique_ptr<CSVFile> CSVFile::openFile(

  if (fd < 1) {
    throw RUNTIME_EXCEPTION_ERRNO(
        &typeid(CSVFile),
        &typeid(InputStream),
        ERR_CSV_CANNOT_OPEN_FILE,
        "error opening file '%s'",
        fp);
  }

  auto csv_file = new CSVFile(fd);
  return std::unique_ptr<CSVFile>(csv_file);
}

CSVFile::CSVFile(
    int fd,
    char column_seperator /* = ',' */,
    char row_seperator /* = '\n' */,
    char quote_char /* = '"' */) :
    fd_(fd),
    buf_len_(0),
    buf_pos_(0) {
  assert(fd > 0);
  auto csv_file = new InputStream(fd);
  return std::unique_ptr<InputStream>(csv_file);
}

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() {
/*
bool CSVInputStream::readNextChunk() {
  int bytes_read = read(fd_, buf_, sizeof(buf_));

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

  return bytes_read == 0;
}
*/

}
}
Loading