/* * fs.cpp * * Unix implementation * * Created on: 08.10.2017 * Author: julian */ #include "fs.hpp" #include namespace Path { std::tuple Split(const Path &p) { auto it = p.end(); while(it != p.begin() && *it != '/') { it--; } return std::make_tuple(Path(p.begin(), it), Path(it,p.end())); } Path Name(const Path &p) { return std::get<1>(Split(p)); } Path Dir(const Path &p) { return std::get<0>(Split(p)); } Path Clean(const Path &p) { Path temp (p); auto it = temp.begin(); bool hasSlash = false; while(it != temp.end()) { if (*it == '/') { if (hasSlash) { // remove temp.erase(it); continue; } hasSlash = true; } else { hasSlash = false; } it++; } return temp; } std::string Extension(const Path &path) { auto it = path.end(); while(it != path.begin() && *it != '.' && *it != '/') { it--; } return std::string(it, path.end()); } std::string Basename(const Path &path) { Path temp = Name(path); auto it = temp.end(); while(it != temp.begin() && *it != '.') { it--; } if (it == temp.begin()) { return path; } return std::string(temp.begin(), it); } }; // end namespace Path Path::Path fs::cwd() { ssize_t size = 1000, nsize; while(1) { char buffer[size]; if ((nsize = readlink("/proc/self/cwd", buffer, size)) < size) { buffer[nsize] = 0; return Path::Clean(Path::Path(buffer)); } size = nsize + 100; } }