diff --git a/include/utils.hpp b/include/utils.hpp index 7a03149..2210880 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -118,4 +118,6 @@ static inline std::string methodName(const std::string& prettyFunction) return prettyFunction.substr(begin,end) + "()"; } +bool mkpath(std::string path, int mode = 0777); + #endif diff --git a/src/utils.cpp b/src/utils.cpp index b55fa34..934e50f 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -9,6 +9,9 @@ * * @bug No known bugs * */ + +#include + #include "utils.hpp" 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; } + +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; +}