Implement mkpath

This commit is contained in:
Cory Balaton 2023-10-08 20:27:51 +02:00
parent bb0bd8f6a0
commit d2047e43e0
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B
2 changed files with 30 additions and 0 deletions

View File

@ -118,4 +118,6 @@ static inline std::string methodName(const std::string& prettyFunction)
return prettyFunction.substr(begin,end) + "()"; return prettyFunction.substr(begin,end) + "()";
} }
bool mkpath(std::string path, int mode = 0777);
#endif #endif

View File

@ -9,6 +9,9 @@
* *
* @bug No known bugs * @bug No known bugs
* */ * */
#include <sys/stat.h>
#include "utils.hpp" #include "utils.hpp"
std::string scientific_format(double d, int width, int prec) std::string scientific_format(double d, int width, int prec)
@ -69,3 +72,28 @@ bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol)
} }
return true; return true;
} }
bool mkpath(std::string path, int mode)
{
std::string cur_dir;
std::string::size_type pos = -1;
struct stat buf;
if (path.back() != '/') {
path += '/';
}
while (true) {
pos++;
pos = path.find('/', pos);
if (pos != std::string::npos) {
cur_dir = path.substr(0, pos);
if (mkdir(cur_dir.c_str(), mode) != 0 && stat(cur_dir.c_str(), &buf) != 0) {
return -1;
}
}
else {
break;
}
}
return 0;
}