ocra-recipes
Doxygen documentation for the ocra-recipes repository
FileOperations.h
Go to the documentation of this file.
1 
13 #ifndef OCRA_UTIL_FILE_OPERATIONS_H
14 #define OCRA_UTIL_FILE_OPERATIONS_H
15 
16 #include <ocra/util/MathTypes.h>
17 
18 #include <string>
19 #include <fstream>
20 #include <iostream>
21 
22 namespace ocra
23 {
24  namespace utils
25  {
26  template <class Derived>
27  inline void writeInFile(const MatrixBase<Derived>& m, const std::string& fileName, bool app=false)
28  {
29  std::ofstream aof;
30  if (app)
31  {
32  aof.open(fileName.c_str(), std::ios_base::app);
33  }
34  else
35  {
36  aof.open(fileName.c_str());
37  }
38  if (aof.is_open())
39  {
40  if (m.cols() != 1)
41  aof << m << std::endl;
42  else
43  aof << m.transpose() << std::endl;
44  aof.close();
45  }
46  }
47 
48 
49  template<class Derived>
50  inline std::ostream& writeInFile(std::ostream & s, const MatrixBase<Derived>& m, int precision = -1, const std::string& coeffSeparator = " ",
51  const std::string& rowSeparator = "\n", const std::string& rowPrefix="", const std::string& rowSuffix="",
52  const std::string& matPrefix="", const std::string& matSuffix="")
53  {
54  std::string rowSpacer = "";
55  int i = int(matSuffix.length())-1;
56  while (i>=0 && matSuffix[i]!='\n')
57  {
58  rowSpacer += ' ';
59  i--;
60  }
61 
62  std::streamsize old_precision = 0;
63  if(precision > 0) old_precision = s.precision(precision);
64  s << matPrefix;
65  for(int i = 0; i < (int)m.get_nrows(); ++i)
66  {
67  if (i)
68  s << rowSpacer;
69  s << rowPrefix;
70  s << m(i, 0);
71  for(int j = 1; j < (int)m.get_ncols(); ++j)
72  {
73  s << coeffSeparator;
74  s << m(i, j);
75  }
76  s << rowSuffix;
77  if( i <(int) m.get_nrows() - 1)
78  s << rowSeparator;
79  }
80  s << matSuffix;
81  if(precision>0) s.precision(old_precision);
82  return s;
83  }
84 
85 
87  template<class Derived>
88  inline std::ostream& writeInFile(std::ostream & s, const MatrixBase<Derived>& v, int precision = -1, const std::string& coeffSeparator = " ",
89  const std::string& vectPrefix="", const std::string& vectSuffix="")
90  {
91  assert(v.rows() == 1 || v.cols() == 1);
92  std::streamsize old_precision = 0;
93  if(precision > 0) old_precision = s.precision(precision);
94  s << vectPrefix;
95  s << v[0];
96  for(int j = 1; j < (int)v.getSize(); ++j)
97  {
98  s << coeffSeparator;
99  s << v[j];
100  }
101  s << vectSuffix;
102  if(precision>0) s.precision(old_precision);
103  return s;
104  }
105 
106  template<class Derived>
107  inline std::ostream& writeInFileForScilab(std::ostream & s, const MatrixBase<Derived>& m, int precision = -1)
108  {
109  if (m.rows() == 1 || m.cols() == 1)
110  return writeInFile(s, m, precision, ", ", "[", "];");
111  else
112  return writeInFile(s, m, precision, ", ", ";\n", "[", "]", "[", "];");
113  }
114  }
115 
116 
117 }
118 
119 #endif //OCRA_UTIL_FILE_OPERATIONS_H
120 
121 // cmake:sourcegroup=utils
void writeInFile(const MatrixBase< Derived > &m, const std::string &fileName, bool app=false)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
std::ostream & writeInFileForScilab(std::ostream &s, const MatrixBase< Derived > &m, int precision=-1)
Declaration file of the OptimizationVariable class.