Biorbd Release_1.13.0
Loading...
Searching...
No Matches
IfStream.cpp
1#define BIORBD_API_EXPORTS
2#include "Utils/IfStream.h"
3
4#include <clocale>
5#include <fstream>
6
7#include "Utils/Equation.h"
8#include "Utils/Error.h"
9#include "Utils/Vector3d.h"
10
11using namespace BIORBD_NAMESPACE;
12
13// Constructor
15 : m_isOpen(std::make_shared<bool>(false)),
16 m_ifs(std::make_shared<std::ifstream>()),
17 m_path(std::make_shared<utils::Path>()) {
18 setlocale(LC_ALL, "C");
19}
21 const utils::Path& path,
22 std::ios_base::openmode mode = std::ios_base::in)
23 : m_isOpen(std::make_shared<bool>(false)),
24 m_ifs(std::make_shared<std::ifstream>()),
25 m_path(std::make_shared<utils::Path>(path)) {
26 open(m_path->absolutePath().c_str(), mode);
27 setlocale(LC_ALL, "C");
28}
30 const char* path,
31 std::ios_base::openmode mode = std::ios_base::in)
32 : m_isOpen(std::make_shared<bool>(false)),
33 m_ifs(std::make_shared<std::ifstream>()),
34 m_path(std::make_shared<utils::Path>(path)) {
35 open(m_path->absolutePath().c_str(), mode);
36 setlocale(LC_ALL, "C");
37}
38
39// Open a file
41 const utils::Path& path,
42 std::ios_base::openmode mode = std::ios_base::in) {
44 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 utils::String& tag,
53 utils::String& text) {
55 read(text);
56 return true;
57}
59 const utils::String& tag,
60 size_t maxTag) {
61 utils::String text;
62 size_t i = 0;
63 while (read(text) && i < maxTag) {
64 if (!text.tolower().compare(tag)) {
65 return true;
66 }
67 ++i;
68 }
69
70 utils::String outMessage(
71 tag + " parameter could not be found in Data file..");
72 utils::Error::raise(outMessage);
73#ifdef _WIN32
74 // It is impossible to get here, but it's better to have a return for the
75 // compiler
76 return false;
77#endif
78}
79
81 // Remember where we were in the file
82 std::streamoff positionInFile(m_ifs->tellg());
83 utils::String text;
84 int nMarkers(0);
85
86 // Read the first word of the line
87 while (read(text))
88 // If same as tag, skip to the other line and restart
89 if (!text.compare(tag)) {
90 getline(text);
91 ++nMarkers;
92 } else {
93 break;
94 }
95
96 // Reset in file to the origine point
97 m_ifs->seekg(positionInFile);
98 return nMarkers;
99}
100
102 bool out(*m_ifs >> text);
103
104 if (out == 0) {
105 return out;
106 }
107
108 if (!text(0, 1).compare("//")) { // If it's a comment by //
109 getline(text);
110 read(text);
111 } else if (!text(0, 1).compare("/*")) { // If it's a comment by / *
112 while (readAWord(text)) {
113 if (!text(0, 1).compare("*/") ||
114 (text.length() >= 2 &&
115 !text(text.length() - 2, text.length() - 1).compare("*/"))) {
116 break;
117 }
118 }
119 read(text);
120 }
121 return out;
122}
124 bool out(*m_ifs >> text);
125 return out;
126}
127bool utils::IfStream::read(double& val) {
128 std::map<utils::Equation, double> dumb;
129 return read(val, dumb);
130}
131#ifdef BIORBD_USE_CASADI_MATH
132bool utils::IfStream::read(RBDLCasadiMath::MX_Xd_SubMatrix val) {
133 std::map<utils::Equation, double> dumb;
134 return read(val, dumb);
135}
136#endif
138 double& result,
139 const std::map<Equation, double>& variables) {
141 bool out(read(tp));
142 // Manage in case of an equation
143 try {
144 result = utils::Equation::evaluateEquation(tp, variables);
145 } catch (std::runtime_error) {
147 "The following expression cannot be parsed properly: \"" + tp + "\"");
148 }
149 return out;
150}
151
152bool utils::IfStream::readFromBinary(char* output, int n_elements) {
153 m_ifs->read(output, n_elements);
154 return true;
155}
156
158 m_ifs->read(m_floatBuffer, 4);
159 output = *((float*)m_floatBuffer);
160 return true;
161}
162
164 m_ifs->read(m_floatBuffer, 4);
165 v[0] = *((float*)m_floatBuffer);
166
167 m_ifs->read(m_floatBuffer, 4);
168 v[1] = *((float*)m_floatBuffer);
169
170 m_ifs->read(m_floatBuffer, 4);
171 v[2] = *((float*)m_floatBuffer);
172 return true;
173}
174
175#ifdef BIORBD_USE_CASADI_MATH
177 RBDLCasadiMath::MX_Xd_SubMatrix result,
178 const std::map<utils::Equation, double>& variables) {
180 bool out(read(tp));
181 // Manage in case of an equation
182 try {
183 result = utils::Equation::evaluateEquation(tp, variables);
184 } catch (std::runtime_error) {
186 "The following expression cannot be parsed properly: \"" + tp + "\"");
187 }
188 return out;
189}
190#endif
191bool utils::IfStream::read(int& val) {
192 utils::String tp;
193 bool out(read(tp));
194 val = std::stoi(tp);
195 return out;
196}
197bool utils::IfStream::read(size_t& val) {
198 utils::String tp;
199 bool out(read(tp));
200 val = static_cast<size_t>(std::stoul(tp));
201 return out;
202}
203bool utils::IfStream::read(bool& val) {
204 utils::String tp;
205 bool out(read(tp));
206 val = std::stoi(tp) != 0;
207 return out;
208}
209// Read the entire line
211 std::getline(*m_ifs, text);
212}
213
215 m_ifs->clear();
216 m_ifs->seekg(0);
217}
218
219// Close the file
221 m_ifs->close();
222 return 1;
223}
224
225bool utils::IfStream::eof() { return m_ifs->eof(); }
Strings that are to be interpreted as equation that can be evaluated.
Definition Equation.h:17
static double evaluateEquation(std::vector< Equation > wholeEq)
Evaluate and return an equation.
Definition Equation.cpp:149
static void raise(const String &message)
Throw an error message.
Definition Error.cpp:6
static void check(bool cond, const String &message)
Assert that raises the error message if false.
Definition Error.cpp:10
bool reachSpecificTag(const String &tag, size_t maxTag=-1)
Advance in the file to a specific tag.
Definition IfStream.cpp:58
bool close()
Close the file.
Definition IfStream.cpp:220
bool eof()
Return if the file is at the end.
Definition IfStream.cpp:225
int countTagsInAConsecutiveLines(const String &tag)
Counts the number of consecutive lines starting with the same tag and then brings it back to the init...
Definition IfStream.cpp:80
char m_floatBuffer[sizeof(float)]
Buffer for reading float in binaries.
Definition IfStream.h:189
bool readFromBinary(char *output, int n_elements)
Read a certain amounts of bits in binary.
Definition IfStream.cpp:152
std::shared_ptr< bool > m_isOpen
If file is open.
Definition IfStream.h:188
bool readSpecificTag(const String &tag, String &text)
Advance in the file to a specific tag.
Definition IfStream.cpp:51
IfStream()
Construct IfStream.
Definition IfStream.cpp:14
void getline(String &text)
Read a whole line.
Definition IfStream.cpp:210
bool open(const Path &path, std::ios_base::openmode mode)
Open the file.
Definition IfStream.cpp:40
bool readAWord(String &text)
Read a word in the file.
Definition IfStream.cpp:123
bool read(String &text)
Read a word in the file skipping the word if it is c-like commented.
Definition IfStream.cpp:101
void resetCursor()
Reset the file cursor to the 0 position.
Definition IfStream.cpp:214
Collection of methods to manipulate path.
Definition Path.h:16
String absolutePath() const
Return the absolute path relative to root.
Definition Path.cpp:265
bool isFileExist() const
Test if file exist on the computer.
Definition Path.cpp:73
Wrapper around the std::string class with augmented functionality.
Definition String.h:18
static String tolower(const String &str)
Convert a string to a lower case string.
Definition String.cpp:76
Wrapper around Eigen Vector3d and attach it to a parent.
Definition Vector3d.h:27