Commit c11efc1a authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Adding simple csvfile reader.

parent 09c49f5d
......@@ -2,6 +2,7 @@ TRIBITS_SUBPACKAGE(io)
SET(SOURCE
csvfile.cc
eafstream.cc
endian.cc
gfsfile.cc
......@@ -9,6 +10,7 @@ SET(SOURCE
)
SET(HEADERS
csvfile.hh
eafstream.hh
endian.hh
gfsfile.hh
......@@ -20,6 +22,9 @@ SET(HEADERS
TRIBITS_ADD_LIBRARY(radixio
SOURCES ${SOURCE}
)
#
# Add testing directory
TRIBITS_ADD_TEST_DIRECTORIES(tests)
INSTALL(FILES ${HEADERS} DESTINATION "include/radixio/")
TRIBITS_SUBPACKAGE_POSTPROCESS()
TRIBITS_PACKAGE_DEFINE_DEPENDENCIES(
LIB_REQUIRED_PACKAGES
LIB_OPTIONAL_PACKAGES
TEST_REQUIRED_PACKAGES
TEST_REQUIRED_PACKAGES testframework
TEST_OPTIONAL_PACKAGES
LIB_REQUIRED_TPLS
LIB_OPTIONAL_TPLS
......
#include <fstream>
#include <string>
#include <vector>
#include "radixio/csvfile.hh"
namespace radix
{
std::vector<std::string> CSVFile::row(const std::string &row) const
{
State state = State::Unquoted;
std::vector<std::string> fields = {""};
size_t i = 0; // index of the current field
for (char c : row) {
switch (state) {
case State::Unquoted:
switch (c) {
case ',': // end of field
fields.push_back(""); i++;
break;
case '"': state = State::Quoted;
break;
default: fields[i].push_back(c);
break; }
break;
case State::Quoted:
switch (c) {
case '"': state = State::QuotedQuote;
break;
default: fields[i].push_back(c);
break; }
break;
case State::QuotedQuote:
switch (c) {
case ',': // , after closing quote
fields.push_back(""); i++;
state = State::Unquoted;
break;
case '"': // "" -> "
fields[i].push_back('"');
state = State::Quoted;
break;
default: // end of quote
state = State::Unquoted;
break; }
break;
}
}
return fields;
} // readCSVRow
bool radix::CSVFile::data(std::vector<std::vector<std::string>> &data) const
{
std::ifstream in;
in.open(mFile, std::ios::in);
if(!in.is_open()) return false;
//
// clear any existing data
data.clear();
std::string line;
while(std::getline(in, line))
{
data.push_back(row(line));
}
in.close();
}
radix::CSVFile::CSVFile(const std::string &file)
: mFile(file)
{
}
} // namespace aptool
#ifndef RADIXIO_CSVFILE_HH_
#define RADIXIO_CSVFILE_HH_
#include <string>
#include <vector>
namespace radix
{
/**
* @brief The CSVFile class
* @author Credit is due to http://stackoverflow.com/users/59557/quant-dev
*/
class CSVFile
{
private:
std::string mFile;
enum class State
{
Unquoted,
Quoted,
QuotedQuote
};
std::vector<std::string> row(const std::string& row) const;
public:
CSVFile(const std::string& file);
bool data(std::vector<std::vector<std::string>>& data) const;
}; // class CSVFile
} // namespace radix
#endif /** RADIXIO_CSVFILE_HH_ */
INCLUDE(GoogleTest)
ADD_GOOGLE_TEST(tstCSVFile.cc NP 1)
#include "gtest/gtest.h"
#include "radixio/csvfile.hh"
#include <fstream>
TEST(Radix, CSVFile)
{
//
// write an example csv file to disk
std::ofstream out;
out.open("tstcsvfile.csv");
ASSERT_TRUE(out.is_open());
out << R"(
#Created by ,"QUOTED, Version 2,3,3"
#Calculation executed on 20160629
#The columns are organized as follows:.
# [Line description] , [Time Units] , [Time], [Mass Chain] , Data_1, Data_2, etc.
#--------------------------------------------------
Particle Diameter, (m), , , 4.6189E-03, 2.7058E-03, 1.9869E-03, 1.6103E-03, 1.3673E-03, 1.1937E-03, 1.0616E-03, 9.5693E-04, 8.7138E-04, 7.9984E-04, 7.3891E-04, 6.8627E-04, 6.4022E-04, 5.9953E-04, 5.6326E-04, 5.3069E-04, 5.0125E-04, 4.7449E-04, 4.5003E-04, 4.2759E-04, 4.0691E-04, 3.8777E-04, 3.7001E-04, 3.5348E-04, 3.3805E-04, 3.2360E-04, 3.1005E-04, 2.9731E-04, 2.8530E-04, 2.7397E-04, 2.6325E-04, 2.5310E-04, 2.4347E-04, 2.3433E-04, 2.2562E-04, 2.1733E-04, 2.0943E-04, 2.0188E-04, 1.9466E-04, 1.8775E-04, 1.8114E-04, 1.7480E-04, 1.6871E-04, 1.6287E-04, 1.5725E-04, 1.5184E-04, 1.4664E-04, 1.4163E-04, 1.3680E-04, 1.3213E-04, 1.2763E-04, 1.2328E-04, 1.1908E-04, 1.1501E-04, 1.1107E-04, 1.0725E-04, 1.0355E-04, 9.9963E-05, 9.6483E-05, 9.3105E-05, 8.9825E-05, 8.6638E-05, 8.3541E-05, 8.0529E-05, 7.7599E-05, 7.4748E-05, 7.1972E-05, 6.9268E-05, 6.6632E-05, 6.4063E-05, 6.1558E-05, 5.9112E-05, 5.6725E-05, 5.4394E-05, 5.2116E-05, 4.9889E-05, 4.7711E-05, 4.5579E-05, 4.3492E-05, 4.1447E-05, 3.9442E-05, 3.7475E-05, 3.5543E-05, 3.3646E-05, 3.1779E-05, 2.9942E-05, 2.8130E-05, 2.6342E-05, 2.4575E-05, 2.2824E-05, 2.1085E-05, 1.9354E-05, 1.7624E-05, 1.5886E-05, 1.4129E-05, 1.2334E-05, 1.0473E-05, 8.4882E-06, 6.2329E-06, 3.6513E-06
Total Particle Mass, (kg), , , 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04, 1.0331E+04
[ 3] In-Plume Air Dose Rate, (rem/hr), 6.000, 0, 3.4764E+08, 3.5474E+08, 3.5980E+08, 3.6377E+08, 3.6721E+08, 3.7034E+08, 3.7325E+08, 3.7601E+08, 3.7867E+08, 3.8125E+08, 3.8376E+08, 3.8623E+08, 3.8867E+08, 3.9109E+08, 3.9349E+08, 3.9588E+08, 3.9827E+08, 4.0066E+08, 4.0306E+08, 4.0547E+08, 4.0789E+08, 4.1033E+08, 4.1279E+08, 4.1527E+08, 4.1778E+08, 4.2032E+08, 4.2289E+08, 4.2550E+08, 4.2814E+08, 4.3083E+08, 4.3355E+08, 4.3632E+08, 4.3914E+08, 4.4201E+08, 4.4493E+08, 4.4791E+08, 4.5095E+08, 4.5405E+08, 4.5722E+08, 4.6046E+08, 4.6377E+08, 4.6716E+08, 4.7063E+08, 4.7418E+08, 4.7782E+08, 4.8156E+08, 4.8539E+08, 4.8933E+08, 4.9337E+08, 4.9753E+08, 5.0182E+08, 5.0623E+08, 5.1078E+08, 5.1548E+08, 5.2033E+08, 5.2534E+08, 5.3052E+08, 5.3589E+08, 5.4145E+08, 5.4722E+08, 5.5321E+08, 5.5944E+08, 5.6591E+08, 5.7265E+08, 5.7968E+08, 5.8702E+08, 5.9469E+08, 6.0272E+08, 6.1113E+08, 6.1995E+08, 6.2923E+08, 6.3901E+08, 6.4931E+08, 6.6021E+08, 6.7175E+08, 6.8399E+08, 6.9702E+08, 7.1092E+08, 7.2578E+08, 7.4173E+08, 7.5889E+08, 7.7744E+08, 7.9756E+08, 8.1948E+08, 8.4349E+08, 8.6994E+08, 8.9925E+08, 9.3199E+08, 9.6887E+08, 1.0108E+09, 1.0591E+09, 1.1156E+09, 1.1827E+09, 1.2644E+09, 1.3668E+09, 1.5007E+09, 1.6866E+09, 1.9724E+09, 2.5124E+09, 3.9289E+09
[14] Equivalent Fissions, (eqf), 0.000, 89, 5.9436E+20, 6.0370E+20, 6.1179E+20, 6.1887E+20, 6.2549E+20, 6.3186E+20, 6.3807E+20, 6.4421E+20, 6.5031E+20, 6.5640E+20, 6.6250E+20, 6.6865E+20, 6.7484E+20, 6.8109E+20, 6.8742E+20, 6.9383E+20, 7.0034E+20, 7.0695E+20, 7.1366E+20, 7.2050E+20, 7.2746E+20, 7.3455E+20, 7.4179E+20, 7.4917E+20, 7.5670E+20, 7.6440E+20, 7.7227E+20, 7.8031E+20, 7.8854E+20, 7.9697E+20, 8.0559E+20, 8.1443E+20, 8.2349E+20, 8.3278E+20, 8.4231E+20, 8.5209E+20, 8.6213E+20, 8.7244E+20, 8.8304E+20, 8.9394E+20, 9.0515E+20, 9.1669E+20, 9.2857E+20, 9.4081E+20, 9.5342E+20, 9.6642E+20, 9.7984E+20, 9.9368E+20, 1.0080E+21, 1.0228E+21, 1.0380E+21, 1.0539E+21, 1.0702E+21, 1.0872E+21, 1.1048E+21, 1.1231E+21, 1.1421E+21, 1.1619E+21, 1.1824E+21, 1.2038E+21, 1.2261E+21, 1.2494E+21, 1.2737E+21, 1.2991E+21, 1.3257E+21, 1.3536E+21, 1.3828E+21, 1.4135E+21, 1.4458E+21, 1.4798E+21, 1.5157E+21, 1.5536E+21, 1.5937E+21, 1.6363E+21, 1.6815E+21, 1.7297E+21, 1.7811E+21, 1.8362E+21, 1.8952E+21, 1.9588E+21, 2.0275E+21, 2.1019E+21, 2.1830E+21, 2.2715E+21, 2.3689E+21, 2.4765E+21, 2.5961E+21, 2.7302E+21, 2.8817E+21, 3.0547E+21, 3.2547E+21, 3.4890E+21, 3.7688E+21, 4.1106E+21, 4.5407E+21, 5.1051E+21, 5.8925E+21, 7.1081E+21, 9.4177E+21, 1.5512E+22
)";
out.close();
radix::CSVFile csv("tstcsvfile.csv");
std::vector<std::vector<std::string>> data;
csv.data(data);
EXPECT_EQ(data.size(), 10);
EXPECT_EQ(data[0].size(), 1);
EXPECT_EQ(data[1].size(), 2);
EXPECT_EQ(data[1][1], "QUOTED, Version 2,3,3");
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment