Planetary Orbital Evolution due to Tides
Orbital evolution of two objects experiencing tides
IOUtil.h
1 #ifndef __IO_UTIL_H
2 #define __IO_UTIL_H
3 
4 #include <iostream>
5 #include <valarray>
6 #include <list>
7 
9 template<typename COLNUM_STRUCTURE>
10 std::vector< std::list<double> > parse_columns(
14  std::istream &is,
15 
18  const COLNUM_STRUCTURE &column_numbers,
19 
22  bool csv = true
23 )
24 {
25  int last_column=*max_element(column_numbers.begin(),
26  column_numbers.end());
27  std::string line;
28  std::vector< std::list<double> > result(column_numbers.size());
29  unsigned line_number=0;
30  for(std::getline(is, line); !is.eof(); std::getline(is, line)) {
31  ++line_number;
32  if(!is.good()) {
33  std::ostringstream msg;
34  msg << "Failed to read line " << line_number
35  << " of the data section of an input stream in "
36  << "parse_columns.";
37  throw Core::Error::IO(msg.str());
38  }
39  if(line[0]=='#') continue;
40  std::istringstream line_stream(line);
41  for(int column_number=0; column_number<=last_column;
42  ++column_number) {
43  std::ostringstream msg;
44  msg << "Failed to parse column " << column_number
45  << " on line " << line_number << " of the data section of an"
46  " input stream in parse_columns.";
47  if(!line_stream.good()) throw Core::Error::IO(msg.str());
48 
49  std::string word;
50  if(csv) std::getline(line_stream, word, ',');
51  else line_stream >> word;
52  typename COLNUM_STRUCTURE::const_iterator
53  colnum_iter=column_numbers.begin();
54  for(size_t result_index=0; result_index<column_numbers.size();
55  ++result_index, ++colnum_iter)
56  if(*colnum_iter==column_number) {
57  double value;
58  if(word == "NaN") value = Core::NaN;
59  else std::istringstream(word) >> value;
60  result[result_index].push_back(value);
61  break;
62  }
63  if(line_stream.bad()) throw Core::Error::IO(msg.str());
64  }
65  }
66  return result;
67 }
68 
69 #endif
Input/Output exception.
Definition: Error.h:118
const double NaN
Not a number.
Definition: Common.h:33