Biorbd Release_1.13.0
Loading...
Searching...
No Matches
Path.cpp
1#define BIORBD_API_EXPORTS
2#include "Utils/Path.h"
3
4#include <fstream>
5#include <limits>
6#include <regex>
7
8#include "Utils/Error.h"
9#include "Utils/String.h"
10
11#ifdef _WIN32
12#include <Windows.h>
13#include <direct.h>
14#undef max
15#else
16#include <sys/stat.h>
17#include <unistd.h>
18#endif
19
20using namespace BIORBD_NAMESPACE;
21
23 : m_originalPath(std::make_shared<utils::String>()),
24 m_folder(std::make_shared<utils::String>()),
25 m_isFolderAbsolute(std::make_shared<bool>()),
26 m_filename(std::make_shared<utils::String>()),
27 m_extension(std::make_shared<utils::String>()) {}
28
29utils::Path::Path(const char* path)
30 : m_originalPath(std::make_shared<utils::String>(path)),
31 m_folder(std::make_shared<utils::String>()),
32 m_isFolderAbsolute(std::make_shared<bool>()),
33 m_filename(std::make_shared<utils::String>()),
34 m_extension(std::make_shared<utils::String>()) {
37}
38
40 : m_originalPath(std::make_shared<utils::String>(path)),
41 m_folder(std::make_shared<utils::String>()),
42 m_isFolderAbsolute(std::make_shared<bool>()),
43 m_filename(std::make_shared<utils::String>()),
44 m_extension(std::make_shared<utils::String>()) {
47}
48
49utils::Path::Path(const std::basic_string<char>& path)
50 : m_originalPath(std::make_shared<utils::String>(path)),
51 m_folder(std::make_shared<utils::String>()),
52 m_isFolderAbsolute(std::make_shared<bool>()),
53 m_filename(std::make_shared<utils::String>()),
54 m_extension(std::make_shared<utils::String>()) {
57}
58
60 utils::Path copy;
61 copy.DeepCopy(*this);
62 return copy;
63}
64
65void utils::Path::DeepCopy(const Path& other) {
67 *m_folder = *other.m_folder;
69 *m_filename = *other.m_filename;
70 *m_extension = *other.m_extension;
71}
72
74bool utils::Path::isFileExist(const Path& path) {
75 return isFileExist(path.absolutePath());
76}
78 if (FILE* file = fopen(
79#ifdef _WIN32
80 toWindowsFormat(path).c_str(),
81#else
82 path.c_str(),
83#endif
84 "r")) {
85 fclose(file);
86 return true;
87 } else {
88 return false;
89 }
90}
91
93 std::ifstream fichier(
94#ifdef _WIN32
96#else
97 absolutePath().c_str()
98#endif
99 );
100 bool isOpen(fichier.is_open());
101 fichier.close();
102 return isOpen;
103}
104
105bool utils::Path::isFolderExist() const { return isFolderExist(*this); }
106
108 return isFolderExist(path.folder());
109}
110
112#ifdef _WIN32
113 if (GetFileAttributesA(toWindowsFormat(path).c_str()) ==
114 INVALID_FILE_ATTRIBUTES) {
115 return false; // Le path est invalide
116 } else
117 // Si on est ici, c'est que quelque chose existe,
118 // s'assurer que ça ne soit pas un fichier
119 if (isFileExist(path)) {
120 return false;
121 } else {
122 return true;
123 }
124#else
125 struct stat statbuf;
126 if (stat(path.c_str(), &statbuf) != -1) {
127 return true;
128 } else {
129 return false;
130 }
131#endif
132}
133
135 const utils::String& path,
139 utils::String pathSep(toUnixFormat(path));
140
141 size_t sepPos(pathSep.rfind("/"));
142
143 // Stocker le folder
144 if (sepPos == std::string::npos)
145 // If no separator is found, then there is no separator
146 // and therefore the path is ./
147 {
148 folder = "";
149 } else {
150 folder = pathSep.substr(0, sepPos) + "/";
151 }
152
153 if (folder.find("./") == 0) { // Remove the leading ./ if necessary
154 folder = folder.substr(2);
155 }
156
157 // Stocker l'extension
158 size_t ext(pathSep.rfind("."));
159 if (ext != std::string::npos) {
160 extension = pathSep.substr(ext + 1);
161 } else {
162 extension = "";
163 }
164
165 // Stocker le nom de fichier
166 filename = pathSep.substr(sepPos + 1, ext - sepPos - 1);
167}
168
172
174 return relativePath(*this, relativeTo);
175}
176
178 const utils::Path& path,
179 const utils::String& relativeTo) {
180 utils::String me(path.absolutePath());
181 utils::String currentDir(relativeTo);
182
183 utils::String meFirstPart("");
184 utils::String currentDirFirstPart("");
185
186 // Set the separator to the 0 position
187 size_t sepMe = std::string::npos;
188 size_t sepCurrentDir = std::string::npos;
189 do {
190 // cut according to previous separator
191 me = me.substr(sepMe + 1);
192 currentDir = currentDir.substr(sepCurrentDir + 1);
193
194 // Find the next separator
195 sepMe = me.find("/"); // UNIX formalism
196 sepCurrentDir = currentDir.find("/");
197
198 // Separate first and last part
199 meFirstPart = me.substr(0, sepMe);
200 currentDirFirstPart = currentDir.substr(0, sepCurrentDir);
201
202 // While the first part are equal,
203 // we still can advance to find de closest relative part
204 } while (!meFirstPart.compare(currentDirFirstPart));
205
206 utils::String outPath;
207 while (currentDir.compare("")) {
208 // Tant que currentDir n'est pas vide, reculer
209 // Trouver le prochain séparateur
210 sepCurrentDir = currentDir.find("/");
211
212 // Séparer la première et la dernière partie
213 if (sepCurrentDir != std::string::npos) {
214 // -1 Si on est au dernier dossier
215 // et que celui-ci ne se termine pas par "/"
216 currentDirFirstPart = currentDir.substr(0, sepCurrentDir);
217 currentDir = currentDir.substr(sepCurrentDir + 1);
218 } else {
219 currentDir = "";
220 }
221
222 outPath += "../";
223 // Tant que les premières parties sont égales,
224 // continuer à avancer dans le path
225 };
226 if (!outPath.compare("") && me.find("../") != 0) {
227 outPath += "./";
228 }
229
230 outPath += me;
231
232 return outPath;
233}
234
236 if (*path.m_isFolderAbsolute) {
237 return path.folder();
238 }
239
240 utils::String base;
241#ifdef _WIN32
242 utils::String current(currentDir());
243 std::smatch matches;
244
245 if (std::regex_search(current, matches, std::regex("^([A-Z]):[\\/].*$"))) {
246 base = matches[1].str() + ":/";
247 } else {
249 "I could not find the current drive to estimate the path");
250 }
251#else
252 base = "/";
253#endif
254 return base + relativePath(path, base);
255}
256
258 if (*m_isFolderAbsolute) {
259 return *m_folder;
260 } else {
261 return currentDir() + *m_folder;
262 }
263}
264
266 if (m_filename->compare("")) {
267 if (m_extension->compare("")) {
268 return absoluteFolder() + *m_filename + "." + *m_extension;
269 } else {
270 return absoluteFolder() + *m_filename;
271 }
272 } else {
273 return absoluteFolder();
274 }
275}
276
278 utils::String pathOut(path);
279
280 // Depending on the string origin, "\\" is either the character "\"
281 // escaped or the character "\" written twice. Test for both
282 size_t pos(pathOut.rfind("\\\\"));
283 while (pos != std::string::npos) {
284 pathOut.replace(pos, 2, "/");
285 pos = pathOut.rfind("\\\\");
286 }
287
288 // However, this next hunk can create false positive each time a
289 // legitimate escape character is used (should not happen in a path?)
290 pos = pathOut.rfind("\\");
291 while (pos != std::string::npos) {
292 pathOut.replace(pos, 1, "/");
293 pos = pathOut.rfind("\\");
294 }
295 return pathOut;
296}
297
299 utils::String pathOut(path);
300 size_t pos(pathOut.rfind("/"));
301 while (pos != std::string::npos) {
302 pathOut.replace(pos, 1, "\\\\");
303 pos = pathOut.rfind("/");
304 }
305 return pathOut;
306}
307
309 return *m_originalPath;
310}
311
316
317const utils::String& utils::Path::folder() const { return *m_folder; }
318
320
322
324
326
328 utils::String base;
329#ifdef _WIN32
330 utils::String current(*m_folder);
331 std::smatch matches;
332
333 if (std::regex_search(current, matches, std::regex("^([A-Z]):[\\/].*$"))) {
334 *m_isFolderAbsolute = true;
335 } else {
336 *m_isFolderAbsolute = false;
337 }
338#else
339 base = "/";
340 size_t pos(m_folder->find(base.c_str()));
341 if (pos == 0) {
342 *m_isFolderAbsolute = true;
343 } else {
344 *m_isFolderAbsolute = false;
345 }
346#endif
347}
348
350 char buff[FILENAME_MAX];
351#ifdef _WIN32
353 _getcwd(buff, FILENAME_MAX), "Could not find the current directory");
354#else
356 getcwd(buff, FILENAME_MAX), "Could not find the current directory");
357#endif
358 return toUnixFormat(buff) + "/";
359}
360
362 const utils::String& tp(folder());
363 utils::String tp2(tp);
364
365 size_t sep = std::string::npos;
366 size_t sepTrack = 0;
367 do {
368 // Découper en fonction de la position précédente du séparateur
369 tp2 = tp2.substr(sep + 1);
370
371 // Trouver le prochain séparateur
372 sep = tp2.find("/"); // Path are stored in UNIX formalism
373 if (sep != std::string::npos) {
374 sepTrack += sep + 1;
375
376 // Séparer la première et la dernière partie
377 if (!isFolderExist(static_cast<utils::String>(tp.substr(0, sepTrack)))) {
378#ifdef _WIN32
379 _mkdir(toWindowsFormat(tp.substr(0, sepTrack)).c_str());
380#else
381 mkdir(tp.substr(0, sepTrack).c_str(), 0777);
382#endif
383 }
384 }
385 } while (sep != std::string::npos);
386}
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
Collection of methods to manipulate path.
Definition Path.h:16
void setFolder(const String &folder)
Set the folder of the file.
Definition Path.cpp:312
void setIsFolderAbsolute()
Set if folder is absolute.
Definition Path.cpp:327
void createFolder() const
To create folder on the computer.
Definition Path.cpp:361
const String & originalPath() const
Return original path as it was at constructor time.
Definition Path.cpp:308
String absolutePath() const
Return the absolute path relative to root.
Definition Path.cpp:265
Path()
Construct path.
Definition Path.cpp:22
bool isFileReadable() const
Test if file exist on the computer and is accessible.
Definition Path.cpp:92
std::shared_ptr< String > m_folder
The folder.
Definition Path.h:237
bool isFileExist() const
Test if file exist on the computer.
Definition Path.cpp:73
const String & filename() const
Return the filename.
Definition Path.cpp:321
bool isFolderExist() const
Test if folder exists on the computer.
Definition Path.cpp:105
static String currentDir()
Definition Path.cpp:349
String relativePath() const
Return relative path to current working directory.
Definition Path.cpp:169
static void parseFileName(const String &path, String &folder, String &filename, String &extension)
Parse a path in folder, filename and extension.
Definition Path.cpp:134
static String toUnixFormat(const String &path)
Return the path to Unix format.
Definition Path.cpp:277
std::shared_ptr< String > m_originalPath
The original path at construction time.
Definition Path.h:236
std::shared_ptr< bool > m_isFolderAbsolute
If folder is absolute.
Definition Path.h:238
static String absoluteFolder(const Path &path)
Return the absolute path relative to root.
Definition Path.cpp:235
const String & folder() const
Return the folder of the file.
Definition Path.cpp:317
std::shared_ptr< String > m_filename
The filename.
Definition Path.h:239
void setExtension(const String &ext)
Set the extension.
Definition Path.cpp:323
const String & extension() const
Return the extension of the file.
Definition Path.cpp:325
std::shared_ptr< String > m_extension
The extension.
Definition Path.h:240
String absoluteFolder() const
Return the absolute folder relative to root.
Definition Path.cpp:257
static String toWindowsFormat(const String &path)
Return the path to Windows format.
Definition Path.cpp:298
void setFilename(const String &name)
Set the filename.
Definition Path.cpp:319
Path DeepCopy() const
Deep copy of path.
Definition Path.cpp:59
Wrapper around the std::string class with augmented functionality.
Definition String.h:18