csv_format.h
Go to the documentation of this file.
1 
8 #pragma once
10 #include "interop/util/math.h"
11 
12 
13 namespace illumina { namespace interop { namespace io { namespace table
14 {
21  template<typename T>
22  void read_csv_line(std::istream& in, std::vector<T>& values, const T missing=T())
23  {
24  values.clear();
25  std::string line;
26  std::getline(in, line);
27  std::istringstream sin(line);
28  std::string cell;
29  while(std::getline(sin, cell, ','))
30  {
31  if(cell=="") values.push_back(missing);
32  else values.push_back(util::lexical_cast<T>(cell));
33  }
34  }
42  template<typename T>
43  T read_value(std::istream& in, std::string& buf, const char delim=',')
44  {
45  std::getline(in, buf, delim);
46  return util::lexical_cast<T>(buf);
47  }
55  template<typename T>
56  void read_value(std::istream& in, T& dest, std::string& buf, const char delim=',')
57  {
58  std::getline(in, buf, delim);
59  dest = util::lexical_cast<T>(buf);
60  }
68  template<typename I>
69  void read_csv(std::istream& in, I beg, I end, const char delim=',')
70  {
71  std::string buf;
72  for(;beg != end;++beg)
73  {
74  read_value(in, *beg, buf, delim);
75  }
76  }
83  template<class T>
84  inline const T& handle_nan(const T& val)
85  {
86  return val;
87  }
93  inline float handle_nan(const float val)
94  {
95  if(std::isnan(val)) return std::numeric_limits<float>::quiet_NaN();
96  return val;
97  }
103  inline double handle_nan(const double val)
104  {
105  if(std::isnan(val)) return std::numeric_limits<double>::quiet_NaN();
106  return val;
107  }
116  template<typename I>
117  void write_csv(std::ostream& out, I beg, I end, const char eol, const size_t precision=10)
118  {
119  if(beg == end) return;
120  std::ios::fmtflags previous_state( out.flags() );
121  out << handle_nan(*beg);
122  ++beg;
123  for(;beg != end;++beg)
124  {
125  if(precision>0)
126  out << "," << std::setprecision(precision) << handle_nan(*beg);
127  else
128  out << "," << handle_nan(*beg);
129  }
130  if(eol != '\0') out << eol;
131  out.flags(previous_state);
132  }
140  template<typename T>
141  void write_csv_line(std::ostream& out, const std::vector<T>& values, const size_t beg=0, size_t last=0)
142  {
143  if(values.empty())return;
144  if(last == 0) last = values.size();
145  INTEROP_ASSERT(beg < values.size());
146  INTEROP_ASSERT(last <= values.size());
147  write_csv(out, values.begin()+beg, values.begin()+std::min(last, values.size()), '\n');
148  }
149 
150 }}}}
151 
T read_value(std::istream &in, std::string &buf, const char delim=',')
Definition: csv_format.h:43
Definition: enum_description.h:15
void write_csv(std::ostream &out, I beg, I end, const char eol, const size_t precision=10)
Definition: csv_format.h:117
void read_csv_line(std::istream &in, std::vector< T > &values, const T missing=T())
Definition: csv_format.h:22
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
const T & handle_nan(const T &val)
Definition: csv_format.h:84
void write_csv_line(std::ostream &out, const std::vector< T > &values, const size_t beg=0, size_t last=0)
Definition: csv_format.h:141
Definition: enums.h:301
void read_csv(std::istream &in, I beg, I end, const char delim=',')
Definition: csv_format.h:69
Destination lexical_cast(const Source &src)
Definition: lexical_cast.h:264