#include "Path.hpp" #include #include "exception/FileNotFoundException.hpp" Path::Path(const vfs::VirtualFileSystem* fileSystem, const std::filesystem::path& virtualPath) { this->fileSystem = fileSystem; this->virtualPath = virtualPath; } Path Path::getParent() const { // return the parent directory return Path( this->fileSystem, this->virtualPath.parent_path() ); } std::vector> Path::getChildrens(const bool recursive, const std::string& pattern) const { // TODO(Faraphel): implement recursive and pattern // check if the path is iterable if (!this->isDirectory()) throw std::runtime_error("Can only iterate directories."); // get the corresponding real directory path const std::filesystem::path realPath = this->getRealPath(); // prepare the list of children paths auto childsPath = std::vector>(); // iterate through the childrens of the directory for (const auto& childRealEntry : std::filesystem::directory_iterator(realPath)) { // get the corresponding path object const auto& childRealPath = childRealEntry.path(); // get the virtual path std::filesystem::path childVirtualPath = this->virtualPath / childRealPath.filename(); // add it to the list of childrens auto path = std::make_shared( this->fileSystem, childVirtualPath ); childsPath.push_back(path); } return childsPath; } bool Path::exists() const { return std::filesystem::exists(this->getRealPath()); } bool Path::isFile() const { return is_regular_file(this->getRealPath()); } bool Path::isDirectory() const { return is_directory(this->getRealPath()); } std::shared_ptr Path::open(const std::string& mode, const std::string& encoding) { // create a new file object auto file = std::make_shared( this->fileSystem, this->virtualPath ); // open it with the given mode file->open(mode); return file; // TODO(Faraphel): handle encoding. Wrapper text reader like in C ? readText / writeText ? overloading with std::string ? } bool Path::mkdir(const bool parents) const { return create_directory(this->getRealPath()); } void Path::remove(const bool recursive) const { if (recursive) remove_all(this->getRealPath()); else std::filesystem::remove(this->getRealPath()); } std::string Path::getPath() const { return this->virtualPath.string(); } std::string Path::getDirname() const { return this->virtualPath.parent_path().string(); } std::string Path::getFilename() const { return this->virtualPath.filename(); } std::string Path::getBasename() const { return this->virtualPath.stem(); } std::string Path::getSuffix() const { return this->virtualPath.extension(); } std::vector Path::getSuffixes() const { const std::string filename = this->virtualPath; auto delimiter = std::regex("."); // split the string at every occurence of the "." character auto parts = std::vector( std::sregex_token_iterator(filename.begin(), filename.end(), delimiter, -1), std::sregex_token_iterator() ); // remove the first string (the basename) parts.erase(parts.begin()); return parts; } std::vector Path::getComponents() const { return {this->virtualPath.begin(), this->virtualPath.end()}; } std::filesystem::path Path::getRealPath() const { return this->fileSystem->resolve(this->virtualPath); }