#include "Path.h" #include #include #include #include std::string Path::dirname(const std::string & s) { size_t pos = s.find_last_of("/\\"); if (pos == std::string::npos) { return "."; } if ((pos == 0u) || (pos == 2u && s[1] == ':')) { return std::string(s, 0, pos + 1u); } return std::string(s, 0, pos); } std::string Path::_join(const std::string & first, const std::string & second) { if (first == "") { return second; } if (second == "") { return first; } char last = first[first.size() - 1u]; if ((last == '/') || (last == '\\')) { return first + second; } return first + "/" + second; } bool Path::is_file(const std::string & s) { struct stat st; if (stat(s.c_str(), &st) != 0) { return false; } return S_ISREG(st.st_mode); } bool Path::is_dir(const std::string & s) { struct stat st; if (stat(s.c_str(), &st) != 0) { return false; } return S_ISDIR(st.st_mode); } std::shared_ptr>> Path::listdir(const std::string & path) { std::shared_ptr>> result = std::make_shared>>(); DIR * dir = opendir(path.c_str()); if (dir != nullptr) { struct dirent * ent; while ((ent = readdir(dir)) != nullptr) { result->push_back(std::make_shared(ent->d_name)); } closedir(dir); } return result; } std::string Path::clean(const std::string & s) { std::string p = s; if (p.substr(0, 2) == "~/") { char * home = getenv("HOME"); if (home != nullptr) { p = home + p.substr(1, p.size()); } } return p; }