Skip to content

Commit

Permalink
C++11 compatibility (#19)
Browse files Browse the repository at this point in the history
* Successfully compiled on g++ 6.4 with -std=c++11

* Resolved all MSVC compiler warnings

* Code clean up
  • Loading branch information
vincentlaucsb authored Mar 31, 2019
1 parent 749e423 commit 0041749
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 53 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ set(CMAKE_CXX_STANDARD 17)
if (MSVC)
# Make Visual Studio report accurate C++ version
# See: https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
set(CMAKE_CXX_FLAGS "/Zc:__cplusplus")
set(CMAKE_CXX_FLAGS "/EHsc /Zc:__cplusplus")

if(CMAKE_BUILD_TYPE MATCHES Debug)
# /Wall emits warnings about the C++ standard library
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif(CMAKE_BUILD_TYPE MATCHES Debug)

else()
set(CMAKE_CXX_FLAGS "-pthread")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_DEBUG "-Og -g -lgcov --coverage")
endif(MSVC)

message("CSV for C++ ${CMAKE_BUILD_TYPE} Build with ${CMAKE_CXX_COMPILER}")

set(SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/include/internal/)
set(TEST_DIR ${CMAKE_CURRENT_LIST_DIR}/tests)

Expand Down
30 changes: 30 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"configurations": [
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"inheritEnvironments": [
"msvc_x64_x64"
],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [
"msvc_x64_x64"
],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
}
]
}
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ignore:
- "include/external"
- "include/external"
- "tests"
8 changes: 7 additions & 1 deletion include/csv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
#ifndef CSV_HPP
#define CSV_HPP

// Add more compiler specific debug logic here
#define NDEBUG
#if _DEBUG
#undef NDEBUG
#endif

#include "internal/csv_reader.hpp"
#include "internal/csv_stat.hpp"
#include "internal/csv_utility.h"
#include "internal/csv_utility.hpp"
#include "internal/csv_writer.hpp"

#endif
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once
#include "../external/string_view.hpp"

#define SUPPRESS_UNUSED_WARNING(x) x

namespace csv {
using namespace std::literals;
using namespace nonstd::literals;
using namespace nonstd;

#if __cplusplus == 201703L
#if __cplusplus >= 201703L
#include <string_view>
using string_view = std::string_view;
#else
Expand Down
24 changes: 7 additions & 17 deletions include/internal/csv_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

namespace csv {
namespace internals {
bool is_equal(double a, double b, double epsilon) {
/** Returns true if two doubles are about the same */
return std::abs(a - b) < epsilon;
}

std::string format_row(const std::vector<std::string>& row, const std::string& delim) {
/** Print a CSV row */
std::stringstream ret;
Expand Down Expand Up @@ -46,10 +41,6 @@ namespace csv {
return ret;
}

void GiantStringBuffer::operator+=(const char ch) {
*(this->buffer) += ch;
}

size_t GiantStringBuffer::size() const {
// Return size of current row
return (this->buffer->size() - this->current_end);
Expand Down Expand Up @@ -266,7 +257,7 @@ namespace csv {
strict = format.strict;

// Read first 500KB of CSV
read_csv(filename, 500000, false);
read_csv(filename, 500000);
}

/** @brief Return the format of the original raw CSV */
Expand All @@ -288,9 +279,9 @@ namespace csv {
* csv::CSV_NOT_FOUND otherwise.
*/
int CSVReader::index_of(const std::string& col_name) const {
auto col_names = this->get_col_names();
for (size_t i = 0; i < col_names.size(); i++)
if (col_names[i] == col_name) return (int)i;
auto _col_names = this->get_col_names();
for (size_t i = 0; i < _col_names.size(); i++)
if (_col_names[i] == col_name) return (int)i;

return CSV_NOT_FOUND;
}
Expand Down Expand Up @@ -454,12 +445,11 @@ namespace csv {
* @brief Parse a CSV file using multiple threads
*
* @param[in] nrows Number of rows to read. Set to -1 to read entire file.
* @param[in] close Close file after reading?
*
* @see CSVReader::read_row()
*
*/
void CSVReader::read_csv(const std::string& filename, const size_t& bytes, bool close) {
void CSVReader::read_csv(const std::string& filename, const size_t& bytes) {
if (!this->infile) {
#ifdef _MSC_BUILD
// Silence compiler warnings in Microsoft Visual C++
Expand Down Expand Up @@ -489,7 +479,7 @@ namespace csv {
this->feed_buffer.push_back(std::move(buffer));
this->feed_cond.notify_one();

buffer = std::make_unique<char[]>(BUFFER_UPPER_LIMIT); // New pointer
buffer = std::unique_ptr<char[]>(new char[BUFFER_UPPER_LIMIT]); // New pointer
line_buffer = buffer.get();
}
}
Expand Down Expand Up @@ -532,7 +522,7 @@ namespace csv {
bool CSVReader::read_row(CSVRow &row) {
if (this->records.empty()) {
if (!this->eof()) {
this->read_csv("", ITERATION_CHUNK_SIZE, false);
this->read_csv("", ITERATION_CHUNK_SIZE);
}
else return false; // Stop reading
}
Expand Down
13 changes: 5 additions & 8 deletions include/internal/csv_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
#include "data_type.h"
#include "csv_format.hpp"
#include "csv_row.hpp"
#include "string_view.hpp"
#include "compatibility.hpp"

namespace csv {
/** @brief Integer indicating a requested column wasn't found. */
const int CSV_NOT_FOUND = -1;

/** @namespace csv::internals
* @brief Stuff that is generally not of interest to end-users
*/
* @brief Stuff that is generally not of interest to end-users
*/
namespace internals {
bool is_equal(double a, double b, double epsilon = 0.001);
std::string type_name(const DataType& dtype);
std::string format_row(const std::vector<std::string>& row, const std::string& delim = ", ");

Expand All @@ -32,7 +31,6 @@ namespace csv {
size_t size() const;
std::string* get();
std::string* operator->();
void operator+=(const char);

std::shared_ptr<std::string> buffer = nullptr;
size_t current_end = 0;
Expand Down Expand Up @@ -125,7 +123,7 @@ namespace csv {
std::vector<std::string> get_col_names() const;
int index_of(const std::string& col_name) const;
///@}

/** @name CSV Metadata: Attributes */
///@{
RowCount row_num = 0; /**< @brief How many lines have
Expand Down Expand Up @@ -208,8 +206,7 @@ namespace csv {
void feed(std::unique_ptr<char[]>&&); /**< @brief Helper for read_csv_worker() */
void read_csv(
const std::string& filename,
const size_t& bytes = ITERATION_CHUNK_SIZE,
bool close = true
const size_t& bytes = ITERATION_CHUNK_SIZE
);
void read_csv_worker();
///@}
Expand Down
5 changes: 3 additions & 2 deletions include/internal/csv_row.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cassert>
#include <functional>
#include "csv_row.hpp"

Expand Down Expand Up @@ -151,11 +152,11 @@ namespace csv {
}

CSVRow::reverse_iterator CSVRow::rbegin() const {
return std::make_reverse_iterator<CSVRow::iterator>(this->end());
return std::reverse_iterator<CSVRow::iterator>(this->end());
}

CSVRow::reverse_iterator CSVRow::rend() const {
return std::make_reverse_iterator<CSVRow::iterator>(this->begin());
return std::reverse_iterator<CSVRow::iterator>(this->begin());
}

CSVRow::iterator::iterator(const CSVRow* _reader, int _i)
Expand Down
8 changes: 6 additions & 2 deletions include/internal/csv_row.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Auxiliary data structures for CSV parser

#include "data_type.h"
#include "string_view.hpp"
#include "compatibility.hpp"

#include <math.h>
#include <vector>
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace csv {

private:
long double value = 0;
csv::string_view sv;
csv::string_view sv = "";
int _type = -1;
void get_value();
};
Expand Down Expand Up @@ -160,6 +160,10 @@ namespace csv {
bool operator==(const iterator&) const;
bool operator!=(const iterator& other) const { return !operator==(other); }

#ifndef NDEBUG
friend CSVRow;
#endif

private:
const CSVRow * daddy = nullptr; // Pointer to parent
std::shared_ptr<CSVField> field = nullptr; // Current field pointed at
Expand Down
2 changes: 1 addition & 1 deletion include/internal/csv_stat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace csv {
* methods like get_mean(), get_counts(), etc... can be used to retrieve statistics.
*/
while (!this->eof()) {
this->read_csv("", ITERATION_CHUNK_SIZE, false);
this->read_csv("", ITERATION_CHUNK_SIZE);
this->calc();
}

Expand Down
8 changes: 6 additions & 2 deletions include/internal/csv_utility.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <vector>

#include "constants.hpp"
#include "csv_utility.h"
#include "csv_utility.hpp"
#include "csv_reader.hpp"

namespace csv {
Expand Down Expand Up @@ -65,7 +65,11 @@ namespace csv {
CSVFileInfo get_file_info(const std::string& filename) {
CSVReader reader(filename);
CSVFormat format = reader.get_format();
for (auto& row : reader);
for (auto& row : reader) {
#ifndef NDEBUG
SUPPRESS_UNUSED_WARNING(row);
#endif
}

CSVFileInfo info = {
filename,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ namespace csv {
int get_col_pos(const std::string filename, const std::string col_name,
const CSVFormat format = GUESS_CSV);
///@}

namespace internals {
template<typename T>
inline bool is_equal(T a, T b, T epsilon = 0.001) {
/** Returns true if two doubles are about the same */
return std::abs(a - b) < epsilon;
}
}
}
2 changes: 1 addition & 1 deletion include/internal/csv_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace csv {
*/

for (size_t i = 0, ilen = record.size(); i < ilen; i++) {
out << csv_escape<Delim, Quote>(record[i]);
out << csv_escape<Delim, Quote>(record[i], quote_minimal);
if (i + 1 != ilen) out << Delim;
}

Expand Down
2 changes: 1 addition & 1 deletion include/internal/data_type.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "data_type.h"
#include "string_view.hpp"
#include "compatibility.hpp"

namespace csv {
namespace internals {
Expand Down
2 changes: 1 addition & 1 deletion include/internal/data_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cctype>
#include <string>

#include "string_view.hpp"
#include "compatibility.hpp"

namespace csv {
/** Enumerates the different CSV field types that are
Expand Down
3 changes: 2 additions & 1 deletion programs/csv_bench.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Calculate benchmarks for CSV parser

#include "csv_parser.hpp"
#include "csv.hpp"
#include <chrono>
#include <iostream>
#include <sstream>

int main(int argc, char** argv) {
using namespace csv;
Expand Down
2 changes: 1 addition & 1 deletion programs/csv_info.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "csv_parser.hpp"
#include "csv.hpp"
#include <iostream>

int main(int argc, char** argv) {
Expand Down
4 changes: 4 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
#define CATCH_CONFIG_MAIN

// For Catch + MSVC
#define _SILENCE_CXX17_UNCAUGHT_EXCEPTION_DEPRECATION_WARNING

#include "catch.hpp"
4 changes: 2 additions & 2 deletions tests/test_csv_row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST_CASE("CSVRow Test", "[test_csv_row]") {
try {
auto dne = row[4].get<>();
}
catch (std::runtime_error& err) {
catch (std::runtime_error&) {
error_caught = true;
}

Expand All @@ -53,7 +53,7 @@ TEST_CASE("CSVRow Test", "[test_csv_row]") {
try {
row["Col5"].get<>();
}
catch (std::runtime_error& err) {
catch (std::runtime_error&) {
error_caught = true;
}

Expand Down
Loading

0 comments on commit 0041749

Please sign in to comment.