Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
csv_writer.hpp
Go to the documentation of this file.
1 #ifndef __STAN__IO__CSV_WRITER_HPP__
2 #define __STAN__IO__CSV_WRITER_HPP__
3 
4 #include <ostream>
5 #include <stan/math/matrix.hpp>
6 
7 namespace stan {
8 
9  namespace io {
10 
19  class csv_writer {
20  private:
21  std::ostream& o_;
22  bool at_bol_;
23 
24  public:
25 
32  void comma() {
33  if (at_bol_) {
34  at_bol_ = false;
35  return;
36  }
37  o_ << ",";
38  }
39 
40 
47  csv_writer(std::ostream& o)
48  : o_(o), at_bol_(true) {
49  }
50 
59  void newline() {
60  o_ << "\n";
61  at_bol_ = true;
62  }
63 
72  void write(int n) {
73  comma();
74  o_ << n;
75  }
76 
85  void write(double x) {
86  comma();
87  o_ << x;
88  }
89 
98  template<int R, int C>
99  void write(const Eigen::Matrix<double,R,C>& m) {
101  for (size_type i = 0; i < m.rows(); ++i)
102  for (size_type j = 0; j < m.cols(); ++j)
103  write(m(i,j));
104  }
105 
114  template<int R, int C>
115  void write_col_major(const Eigen::Matrix<double,R,C>& m) {
117  for (size_type j = 0; j < m.cols(); ++j)
118  for (size_type i = 0; i < m.rows(); ++i)
119  write(m(i,j));
120  }
121 
131  void write(const std::string& s) {
132  comma();
133 
134  o_ << '"';
135  for (size_t i = 0; i < s.size(); ++i) {
136  if (s.at(i) == '"') {
137  o_ << '"' << '"'; // double quotes
138  } else {
139  o_ << s.at(i);
140  }
141  }
142  o_ << '"';
143  }
144 
145 
146  };
147 
148  }
149 
150 }
151 
152 #endif

     [ Stan Home Page ] © 2011–2013, Stan Development Team.