diff --git a/extras/Hadrons/Global.cc b/extras/Hadrons/Global.cc index 32060460..22193770 100644 --- a/extras/Hadrons/Global.cc +++ b/extras/Hadrons/Global.cc @@ -92,3 +92,54 @@ const std::string Hadrons::resultFileExt = "h5"; #else const std::string Hadrons::resultFileExt = "xml"; #endif + +// recursive mkdir ///////////////////////////////////////////////////////////// +int Hadrons::mkdir(const std::string dirName) +{ + if (access(dirName.c_str(), R_OK|W_OK|X_OK)) + { + mode_t mode755; + char tmp[MAX_PATH_LENGTH]; + char *p = NULL; + size_t len; + + mode755 = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + + snprintf(tmp, sizeof(tmp), "%s", dirName.c_str()); + len = strlen(tmp); + if(tmp[len - 1] == '/') + { + tmp[len - 1] = 0; + } + for(p = tmp + 1; *p; p++) + { + if(*p == '/') + { + *p = 0; + ::mkdir(tmp, mode755); + *p = '/'; + } + } + + return ::mkdir(tmp, mode755); + } + else + { + return 0; + } +} + +std::string Hadrons::basename(const std::string &s) +{ + constexpr char sep = '/'; + size_t i = s.rfind(sep, s.length()); + + if (i != std::string::npos) + { + return s.substr(i+1, s.length() - i); + } + else + { + return s; + } +} diff --git a/extras/Hadrons/Global.hpp b/extras/Hadrons/Global.hpp index 12b9a029..cf3a4409 100644 --- a/extras/Hadrons/Global.hpp +++ b/extras/Hadrons/Global.hpp @@ -187,8 +187,12 @@ typedef XmlWriter ResultWriter; #define RESULT_FILE_NAME(name) \ name + "." + std::to_string(vm().getTrajectory()) + "." + resultFileExt -// default Schur convention +// recursive mkdir +#define MAX_PATH_LENGTH 512u +int mkdir(const std::string dirName); +std::string basename(const std::string &s); +// default Schur convention #ifndef HADRONS_DEFAULT_SCHUR #define HADRONS_DEFAULT_SCHUR DiagTwo #endif