3D Beam Finite Element Code  1.0
csv_parser.h
Go to the documentation of this file.
1 // Copyright 2015. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // * Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 //
12 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
16 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22 // POSSIBILITY OF SUCH DAMAGE.
23 //
24 // Author: ryan.latture@gmail.com (Ryan Latture)
25 
26 #ifndef CSV_PARSER_H
27 #define CSV_PARSER_H
28 
29 #include <boost/format.hpp>
30 #include <boost/tokenizer.hpp>
31 #include <cstdlib>
32 #include <exception>
33 #include <fstream>
34 #include <iomanip>
35 #include <iostream>
36 #include <sstream>
37 #include <string>
38 #include <vector>
39 
40 namespace fea {
47  template<typename T>
48  std::istream &operator>>(std::istream &ins, std::vector<T> &record) {
49  // make sure that the returned record contains only the stuff we read now
50  record.clear();
51 
52  // read the entire line into a string (a CSV record is terminated by a newline)
53  std::string line;
54  std::getline(ins, line);
55 
56  std::string empty;
57  std::string separator_characters(", \t");
58  boost::escaped_list_separator<char> separators(empty, separator_characters, empty);
59  boost::tokenizer<boost::escaped_list_separator<char>> tk(line, separators);
60 
61  for (boost::tokenizer<boost::escaped_list_separator<char>>::iterator i(tk.begin()); i != tk.end(); ++i) {
62  T f = std::strtod(i->c_str(), 0);
63  record.push_back(f);
64  }
65  return ins;
66  }
67 
74  template<typename T>
75  inline std::istream &operator>>(std::istream &ins, std::vector<std::vector<T> > &data) {
76  // make sure that the returned data only contains the CSV data we read here
77  data.clear();
78 
79  // For every record we can read from the file, append it to our resulting data
80  std::vector<T> record;
81  while (ins >> record) {
82  data.push_back(record);
83  }
84 
85  // Again, return the argument stream as required for this kind of input stream overload.
86  return ins;
87  }
88 
93  class CSVParser {
94  public:
95 
102  template<typename T>
103  void parseToVector(std::string filename, std::vector<T> &data) {
104 
105  std::ifstream infile;
106  infile.open(filename);
107 
108  if (infile.is_open()) {
109  try {
110  infile >> data;
111  }
112  catch (std::exception &e) {
113  throw std::runtime_error(
114  (boost::format("Error when parsing csv file %s.\nDetails from tokenizer:\n\t%s") %
115  filename % e.what()).str()
116  );
117  }
118 
119  infile.close();
120  }
121  else {
122  throw std::runtime_error(
123  (boost::format("Error opening file %s") % filename).str()
124  );
125  }
126  }
127 
136  template<typename T>
137  void write(const std::string &filename,
138  const std::vector<std::vector<T> > &data,
139  unsigned int precision,
140  const std::string &delimiter) {
141  std::ofstream output_file;
142  output_file.open(filename);
143  size_t num_cols;
144 
145  if (!output_file.is_open()) {
146  throw std::runtime_error(
147  (boost::format("Error opening file %s") % filename).str()
148  );
149  }
150  else {
151  for (size_t i = 0; i < data.size(); ++i) {
152  num_cols = data[i].size();
153  for (size_t j = 0; j < num_cols; ++j) {
154  output_file << std::fixed << std::setprecision(precision) << data[i][j];
155  if (j < num_cols - 1) {
156  output_file << delimiter;
157  }
158  }
159  output_file << "\n";
160  }
161  output_file.close();
162  }
163  }
164  };
165 } // namespace fea
166 
167 #endif //CSV_PARSER_H
void write(const std::string &filename, const std::vector< std::vector< T > > &data, unsigned int precision, const std::string &delimiter)
Definition: csv_parser.h:137
void parseToVector(std::string filename, std::vector< T > &data)
parses the contents of file_name into data.
Definition: csv_parser.h:103
std::istream & operator>>(std::istream &ins, std::vector< T > &record)
Definition: csv_parser.h:48
Reads data from a csv file into an std::vector and writes the contents of an std::vector to a file...
Definition: csv_parser.h:93
Definition: containers.h:41