90 lines
2.3 KiB
C++
90 lines
2.3 KiB
C++
#include "filesystem.hpp"
|
|
#include <sys/stat.h>
|
|
#include <stdlib.h>
|
|
#include <linux/limits.h>
|
|
|
|
#ifndef GUI
|
|
|
|
bool FSLib::exists(const std::string &path) {
|
|
struct stat path_stat;
|
|
return stat( path.c_str(), &path_stat ) == 0;
|
|
}
|
|
|
|
std::string FSLib::canonical(const std::string &path) {
|
|
char *canonical_path = static_cast<char *>(malloc(PATH_MAX));
|
|
if( realpath(path.c_str(), canonical_path) == nullptr ) {
|
|
free(canonical_path);
|
|
return "";
|
|
}
|
|
std::string canonical_string{canonical_path};
|
|
free(canonical_path);
|
|
return canonical_string;
|
|
}
|
|
|
|
#endif
|
|
|
|
bool FSLib::isDirectory(const std::string &path) {
|
|
struct stat path_stat;
|
|
stat( path.c_str(), &path_stat );
|
|
return S_ISDIR(path_stat.st_mode);
|
|
}
|
|
|
|
bool FSLib::rename(const std::string &file_a, const std::string &file_b) {
|
|
return ::rename( file_a.c_str(), file_b.c_str() ) == 0;
|
|
}
|
|
|
|
FSLib::Directory::iterator FSLib::Directory::begin() {
|
|
return Iterator(dir_path);
|
|
}
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::begin() const {
|
|
return Iterator(dir_path);
|
|
}
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::cbegin() const {
|
|
return begin();
|
|
}
|
|
|
|
FSLib::Directory::iterator FSLib::Directory::end() {
|
|
return Iterator(dir_path, nullptr);
|
|
}
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::end() const {
|
|
return Iterator(dir_path, nullptr);
|
|
}
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::cend() const {
|
|
return end();
|
|
}
|
|
|
|
const char *FSLib::Directory::path() const {
|
|
return dir_path.c_str();
|
|
}
|
|
|
|
char const *FSLib::Directory::Iterator::operator*() const {
|
|
return current_entry->d_name;
|
|
}
|
|
|
|
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
|
|
if( current_entry == nullptr )
|
|
return *this;
|
|
current_entry = readdir(d);
|
|
if( current_entry != nullptr && ( !strcmp(current_entry->d_name, ".") || !strcmp(current_entry->d_name, "..") ) )
|
|
return operator++();
|
|
return *this;
|
|
}
|
|
|
|
FSLib::Directory::Iterator FSLib::Directory::Iterator::operator++(int) {
|
|
Iterator ret(*this);
|
|
operator++();
|
|
return ret;
|
|
}
|
|
|
|
bool FSLib::Directory::Iterator::operator==(const Iterator &i_other) const {
|
|
return i_other.current_entry == current_entry;
|
|
}
|
|
|
|
bool FSLib::Directory::Iterator::operator!=(const Iterator &i_other) const {
|
|
return i_other.current_entry != current_entry;
|
|
}
|