Biorbd
IfStream.cpp
1 #define BIORBD_API_EXPORTS
2 #include "Utils/IfStream.h"
3 
4 #include <clocale>
5 #include <fstream>
6 #include "Utils/Error.h"
7 #include "Utils/Equation.h"
8 
9 // Constructor
11  m_isOpen(std::make_shared<bool>(false)),
12  m_ifs(std::make_shared<std::ifstream>()),
13  m_path(std::make_shared<biorbd::utils::Path>())
14 {
15  setlocale(LC_ALL, "C");
16 }
18  const biorbd::utils::Path& path,
19  std::ios_base::openmode mode = std::ios_base::in ) :
20  m_isOpen(std::make_shared<bool>(false)),
21  m_ifs(std::make_shared<std::ifstream>()),
22  m_path(std::make_shared<biorbd::utils::Path>(path))
23 {
24  open(m_path->absolutePath().c_str(), mode);
25  setlocale(LC_ALL, "C");
26 }
28  const char* path,
29  std::ios_base::openmode mode = std::ios_base::in ) :
30  m_isOpen(std::make_shared<bool>(false)),
31  m_ifs(std::make_shared<std::ifstream>()),
32  m_path(std::make_shared<biorbd::utils::Path>(path))
33 {
34  open(m_path->absolutePath().c_str(), mode);
35  setlocale(LC_ALL, "C");
36 }
37 
38 
39 // Open a file
41  const biorbd::utils::Path& path,
42  std::ios_base::openmode mode = std::ios_base::in )
43 {
44  biorbd::utils::Error::check(path.isFileExist(), path.absolutePath() + " could not be loaded");
45  *m_ifs = std::ifstream(path.absolutePath().c_str(), mode);
46  *m_isOpen = true;
47  return *m_isOpen;
48 }
49 
50 // Read a file
52  const biorbd::utils::String& tag,
53  biorbd::utils::String& text){
54  reachSpecificTag(tag);
55  read(text);
56  return true;
57 }
60  while (read(text))
61  if (!text.tolower().compare(tag))
62  return true;
63 
64  biorbd::utils::String outMessage(tag + " parameter could not be found in Data file..");
65  biorbd::utils::Error::raise(outMessage);
66 #ifdef _WIN32
67  // It is impossible to get here, but it's better to have a return for the compiler
68  return false;
69 #endif
70 }
71 
73 {
74  // Remember where we were in the file
75  std::streamoff positionInFile(m_ifs->tellg());
77  int nMarkers(0);
78 
79  // Read the first word of the line
80  while (read(text))
81  // If same as tag, skip to the other line and restart
82  if (!text.compare(tag)){
83  getline(text);
84  ++nMarkers;
85  }
86  else
87  break;
88 
89  // Reset in file to the origine point
90  m_ifs->seekg(positionInFile);
91  return nMarkers;
92 }
93 
95  bool out(*m_ifs >> text);
96 
97  if (out == 0)
98  return out;
99 
100  if (!text(0,1).compare("//")){ // If it's a comment by //
101  getline(text);
102  read(text);
103  }
104  else if (!text(0,1).compare("/*")){ // If it's a comment by / *
105  while (readAWord(text)){
106  if (!text(0,1).compare("*/") || (text.length()>=2 && !text(static_cast<unsigned int>(text.length()-2),static_cast<unsigned int>(text.length()-1)).compare("*/")))
107  break;
108  }
109  read(text);
110  }
111  return out;
112 }
114  bool out(*m_ifs >> text);
115  return out;
116 }
118  double& val){
119  std::map<biorbd::utils::Equation, double> dumb;
120  return read(val, dumb);
121 }
122 #ifdef BIORBD_USE_CASADI_MATH
124  RBDLCasadiMath::MX_Xd_SubMatrix val){
125  std::map<biorbd::utils::Equation, double> dumb;
126  return read(val, dumb);
127 }
128 #endif
130  double& result,
131  const std::map<biorbd::utils::Equation, double> &variables){
133  bool out(read(tp));
134  // Manage in case of an equation
135  try {
136  result = biorbd::utils::Equation::evaluateEquation(tp, variables);
137  } catch (std::runtime_error) {
138  biorbd::utils::Error::raise("The following expression cannot be parsed properly: \"" + tp + "\"");
139  }
140  return out;
141 }
142 #ifdef BIORBD_USE_CASADI_MATH
144  RBDLCasadiMath::MX_Xd_SubMatrix result,
145  const std::map<biorbd::utils::Equation, double> &variables){
147  bool out(read(tp));
148  // Manage in case of an equation
149  try {
150  result = biorbd::utils::Equation::evaluateEquation(tp, variables);
151  } catch (std::runtime_error) {
152  biorbd::utils::Error::raise("The following expression cannot be parsed properly: \"" + tp + "\"");
153  }
154  return out;
155 }
156 #endif
158  int& val){
160  bool out(read(tp));
161  val = std::stoi(tp);
162  return out;
163 }
165  unsigned int& val){
167  bool out(read(tp));
168  val = static_cast<unsigned int>(std::stoul(tp));
169  return out;
170 }
172  bool& val){
174  bool out(read(tp));
175  val = std::stoi(tp) != 0;
176  return out;
177 }
178 // Read the entire line
180  std::getline(*m_ifs, text);
181 }
182 
183 
184 // Close the file
186  m_ifs->close();
187  return 1;
188 }
189 
191 {
192  return m_ifs->eof();
193 }
biorbd::utils::Path::absolutePath
biorbd::utils::String absolutePath() const
Return the absolute path relative to root.
Definition: Path.cpp:290
biorbd::utils::IfStream::getline
void getline(biorbd::utils::String &text)
Read a whole line.
Definition: IfStream.cpp:179
biorbd::utils::IfStream::read
bool read(biorbd::utils::String &text)
Read a word in the file skipping the word if it is c-like commented.
Definition: IfStream.cpp:94
biorbd::utils::IfStream::open
bool open(const biorbd::utils::Path &path, std::ios_base::openmode mode)
Open the file.
Definition: IfStream.cpp:40
biorbd::utils::IfStream::close
bool close()
Close the file.
Definition: IfStream.cpp:185
biorbd::utils::Error::raise
static void raise(const biorbd::utils::String &message)
Throw an error message.
Definition: Error.cpp:4
biorbd::utils::IfStream::IfStream
IfStream()
Construct IfStream.
Definition: IfStream.cpp:10
biorbd::utils::String::tolower
static biorbd::utils::String tolower(const biorbd::utils::String &str)
Convert a string to a lower case string.
Definition: String.cpp:97
biorbd::utils::IfStream::eof
bool eof()
Return if the file is at the end.
Definition: IfStream.cpp:190
biorbd::utils::IfStream::readSpecificTag
bool readSpecificTag(const biorbd::utils::String &tag, biorbd::utils::String &text)
Advance in the file to a specific tag.
Definition: IfStream.cpp:51
biorbd::utils::IfStream::countTagsInAConsecutiveLines
int countTagsInAConsecutiveLines(const biorbd::utils::String &tag)
Counts the number of consecutive lines starting with the same tag and then brings it back to the init...
Definition: IfStream.cpp:72
biorbd::utils::IfStream::readAWord
bool readAWord(biorbd::utils::String &text)
Read a word in the file.
Definition: IfStream.cpp:113
biorbd::utils::Path::isFileExist
bool isFileExist() const
Test if file exist on the computer.
Definition: Path.cpp:82
biorbd::utils::String
Wrapper around the std::string class with augmented functionality.
Definition: String.h:17
biorbd::utils::Equation
Strings that are to be interpreted as equation that can be evaluated.
Definition: Equation.h:16
biorbd::utils::Equation::evaluateEquation
static double evaluateEquation(std::vector< biorbd::utils::Equation > wholeEq)
Evaluate and return an equation.
Definition: Equation.cpp:161
biorbd::utils::Path
Collection of methods to manipulate path.
Definition: Path.h:17
biorbd::utils::IfStream::reachSpecificTag
bool reachSpecificTag(const biorbd::utils::String &tag)
Advance in the file to a specific tag.
Definition: IfStream.cpp:58
biorbd::utils::Error::check
static void check(bool cond, const biorbd::utils::String &message)
Assert that raises the error message if false.
Definition: Error.cpp:10