123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-15
- * Changed: 2020-08-15
- *
- * */
- #include "File.hpp"
- #include "../exception/FileOperationException.hpp"
- #include "FilePathSeparatorMatch.hpp"
- #include <fstream>
- #include <sys/stat.h>
- #include <algorithm>
- ls_std::File::File(std::string _absoluteFilePath) : Class("File"),
- absoluteFilePath(std::move(_absoluteFilePath))
- {}
- bool ls_std::File::operator==(File &_file)
- {
- std::string normalizedFilePath = ls_std::File::normalizePath(this->absoluteFilePath);
- std::string normalizedForeignFilePath = ls_std::File::normalizePath(_file.getAbsoluteFilePath());
- return normalizedFilePath == normalizedForeignFilePath;
- }
- bool ls_std::File::operator!=(File &_file)
- {
- std::string normalizedFilePath = ls_std::File::normalizePath(this->absoluteFilePath);
- std::string normalizedForeignFilePath = ls_std::File::normalizePath(_file.getAbsoluteFilePath());
- return normalizedFilePath != normalizedForeignFilePath;
- }
- bool ls_std::File::canExecute()
- {
- bool executable {};
- struct stat _stat {};
- if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
- executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
- }
- return executable;
- }
- void ls_std::File::create()
- {
- if(!this->_exists()) {
- std::ofstream file {this->absoluteFilePath};
- file.close();
- } else {
- throw ls_std::FileOperationException{this->absoluteFilePath};
- }
- }
- bool ls_std::File::exists()
- {
- return this->_exists();
- }
- std::string ls_std::File::getAbsoluteFilePath() {
- return this->absoluteFilePath;
- }
- std::string ls_std::File::getName()
- {
- std::string copy = this->absoluteFilePath;
- // if it's a directory, remove separator from end, if it does exist
- if(this->_isDirectory()) {
- copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
- }
- // now get the file / directory name
- auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
- return std::string(base, copy.end());
- }
- long ls_std::File::getSize()
- {
- std::streampos fileSize {};
- if(this->_exists()) {
- std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
- fileSize = fileHandler.tellg();
- fileHandler.seekg(0, std::ios::end);
- fileSize = fileHandler.tellg() - fileSize;
- fileHandler.close();
- }
- return fileSize;
- }
- bool ls_std::File::isDirectory()
- {
- return this->_isDirectory();
- }
- bool ls_std::File::isFile()
- {
- return this->_isFile();
- }
- void ls_std::File::makeDirectory()
- {
- if(mkdir(this->absoluteFilePath.c_str())) {
- throw ls_std::FileOperationException {this->absoluteFilePath};
- }
- }
- void ls_std::File::remove()
- {
- if(this->_isFile()) {
- if(std::remove(this->absoluteFilePath.c_str())) {
- throw ls_std::FileOperationException{this->absoluteFilePath};
- }
- }
- if(this->_isDirectory()) {
- if(rmdir(this->absoluteFilePath.c_str())) {
- throw ls_std::FileOperationException{this->absoluteFilePath};
- }
- }
- }
- bool ls_std::File::_exists()
- {
- struct stat _stat {};
- return (stat(this->absoluteFilePath.c_str(), &_stat) == 0);
- }
- bool ls_std::File::_isDirectory()
- {
- bool match {};
- struct stat _stat {};
- if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
- match = _stat.st_mode & (unsigned short) S_IFDIR;
- }
- return match;
- }
- bool ls_std::File::_isFile()
- {
- bool match {};
- struct stat _stat {};
- if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
- match = _stat.st_mode & (unsigned short) S_IFREG;
- }
- return match;
- }
- std::string ls_std::File::normalizePath(std::string path)
- {
- std::replace(path.begin(), path.end(), '\\', '/');
- return path;
- }
|