|
@@ -3,7 +3,7 @@
|
|
* Company: Lynar Studios
|
|
* Company: Lynar Studios
|
|
* E-Mail: webmaster@lynarstudios.com
|
|
* E-Mail: webmaster@lynarstudios.com
|
|
* Created: 2020-08-15
|
|
* Created: 2020-08-15
|
|
- * Changed: 2020-11-26
|
|
|
|
|
|
+ * Changed: 2021-09-26
|
|
*
|
|
*
|
|
* */
|
|
* */
|
|
|
|
|
|
@@ -18,15 +18,21 @@
|
|
#include <sys/stat.h>
|
|
#include <sys/stat.h>
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
|
|
+
|
|
#include <unistd.h>
|
|
#include <unistd.h>
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
|
|
+
|
|
#include <direct.h>
|
|
#include <direct.h>
|
|
|
|
+#include <tchar.h>
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
-ls_std::File::File(std::string _absoluteFilePath) : ls_std::Class("File"),
|
|
|
|
-absoluteFilePath(ls_std::File::_normalizePath(std::move(_absoluteFilePath)))
|
|
|
|
|
|
+ls_std::File::File(std::string _absoluteFilePath)
|
|
|
|
+ : ls_std::Class("File"),
|
|
|
|
+ absoluteFilePath(ls_std::File::_normalizePath(std::move(_absoluteFilePath)))
|
|
{}
|
|
{}
|
|
|
|
|
|
bool ls_std::File::operator==(ls_std::File &_file)
|
|
bool ls_std::File::operator==(ls_std::File &_file)
|
|
@@ -49,10 +55,10 @@ bool ls_std::File::canRead()
|
|
bool readable;
|
|
bool readable;
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
- readable = ls_std::File::_isReadableUnix(this->absoluteFilePath);
|
|
|
|
|
|
+ readable = ls_std::File::_isReadableUnix(this->absoluteFilePath);
|
|
#endif
|
|
#endif
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
- readable = ls_std::File::_isReadableWindows(this->absoluteFilePath);
|
|
|
|
|
|
+ readable = ls_std::File::_isReadableWindows(this->absoluteFilePath);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
return readable;
|
|
return readable;
|
|
@@ -65,10 +71,13 @@ bool ls_std::File::canWrite()
|
|
|
|
|
|
void ls_std::File::createNewFile()
|
|
void ls_std::File::createNewFile()
|
|
{
|
|
{
|
|
- if(!ls_std::File::_exists(this->absoluteFilePath)) {
|
|
|
|
- std::ofstream file {this->absoluteFilePath};
|
|
|
|
|
|
+ if (!ls_std::File::_exists(this->absoluteFilePath))
|
|
|
|
+ {
|
|
|
|
+ std::ofstream file{this->absoluteFilePath};
|
|
file.close();
|
|
file.close();
|
|
- } else {
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
throw ls_std::FileOperationException{};
|
|
throw ls_std::FileOperationException{};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -78,7 +87,8 @@ bool ls_std::File::exists()
|
|
return ls_std::File::_exists(this->absoluteFilePath);
|
|
return ls_std::File::_exists(this->absoluteFilePath);
|
|
}
|
|
}
|
|
|
|
|
|
-std::string ls_std::File::getAbsoluteFilePath() {
|
|
|
|
|
|
+std::string ls_std::File::getAbsoluteFilePath()
|
|
|
|
+{
|
|
return this->absoluteFilePath;
|
|
return this->absoluteFilePath;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -88,14 +98,15 @@ std::string ls_std::File::getName()
|
|
|
|
|
|
// if it's a directory, remove separator from end, if it does exist
|
|
// if it's a directory, remove separator from end, if it does exist
|
|
|
|
|
|
- if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
|
|
|
|
|
|
+ if (ls_std::File::_isDirectory(this->absoluteFilePath))
|
|
|
|
+ {
|
|
copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
|
|
copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
|
|
}
|
|
}
|
|
|
|
|
|
// now get the file / directory name
|
|
// now get the file / directory name
|
|
|
|
|
|
auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
|
|
auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
|
|
- return std::string(base, copy.end());
|
|
|
|
|
|
+ return std::string{base, copy.end()};
|
|
}
|
|
}
|
|
|
|
|
|
std::string ls_std::File::getParent()
|
|
std::string ls_std::File::getParent()
|
|
@@ -103,11 +114,26 @@ std::string ls_std::File::getParent()
|
|
return ls_std::File::_getParent(this->absoluteFilePath);
|
|
return ls_std::File::_getParent(this->absoluteFilePath);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+std::string ls_std::File::getWorkingDirectory()
|
|
|
|
+{
|
|
|
|
+ std::string workingDirectory{};
|
|
|
|
+
|
|
|
|
+ #if defined(unix) || defined(__APPLE__)
|
|
|
|
+ workingDirectory = ls_std::File::_getWorkingDirectoryUnix();
|
|
|
|
+ #endif
|
|
|
|
+ #ifdef _WIN32
|
|
|
|
+ workingDirectory = ls_std::File::_getWorkingDirectoryWindows();
|
|
|
|
+ #endif
|
|
|
|
+
|
|
|
|
+ return workingDirectory;
|
|
|
|
+}
|
|
|
|
+
|
|
long ls_std::File::getSize()
|
|
long ls_std::File::getSize()
|
|
{
|
|
{
|
|
- std::streampos fileSize {};
|
|
|
|
|
|
+ std::streampos fileSize{};
|
|
|
|
|
|
- if(ls_std::File::_exists(this->absoluteFilePath)) {
|
|
|
|
|
|
+ if (ls_std::File::_exists(this->absoluteFilePath))
|
|
|
|
+ {
|
|
std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
|
|
std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
|
|
fileSize = fileHandler.tellg();
|
|
fileSize = fileHandler.tellg();
|
|
fileHandler.seekg(0, std::ios::end);
|
|
fileHandler.seekg(0, std::ios::end);
|
|
@@ -115,7 +141,7 @@ long ls_std::File::getSize()
|
|
fileHandler.close();
|
|
fileHandler.close();
|
|
}
|
|
}
|
|
|
|
|
|
- return fileSize;
|
|
|
|
|
|
+ return (long) fileSize;
|
|
}
|
|
}
|
|
|
|
|
|
bool ls_std::File::isDirectory()
|
|
bool ls_std::File::isDirectory()
|
|
@@ -135,9 +161,10 @@ time_t ls_std::File::lastModified()
|
|
|
|
|
|
std::list<std::string> ls_std::File::list()
|
|
std::list<std::string> ls_std::File::list()
|
|
{
|
|
{
|
|
- std::list<std::string> fileList {};
|
|
|
|
|
|
+ std::list<std::string> fileList{};
|
|
|
|
|
|
- if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
|
|
|
|
|
|
+ if (ls_std::File::_isDirectory(this->absoluteFilePath))
|
|
|
|
+ {
|
|
fileList = ls_std::File::_list(this->absoluteFilePath);
|
|
fileList = ls_std::File::_list(this->absoluteFilePath);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -146,9 +173,10 @@ std::list<std::string> ls_std::File::list()
|
|
|
|
|
|
std::list<std::string> ls_std::File::listFiles()
|
|
std::list<std::string> ls_std::File::listFiles()
|
|
{
|
|
{
|
|
- std::list<std::string> fileList {};
|
|
|
|
|
|
+ std::list<std::string> fileList{};
|
|
|
|
|
|
- if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
|
|
|
|
|
|
+ if (ls_std::File::_isDirectory(this->absoluteFilePath))
|
|
|
|
+ {
|
|
fileList = ls_std::File::_listFiles(this->absoluteFilePath);
|
|
fileList = ls_std::File::_listFiles(this->absoluteFilePath);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -157,20 +185,24 @@ std::list<std::string> ls_std::File::listFiles()
|
|
|
|
|
|
void ls_std::File::makeDirectory()
|
|
void ls_std::File::makeDirectory()
|
|
{
|
|
{
|
|
- if(ls_std::File::_mkdir(this->absoluteFilePath)) {
|
|
|
|
- throw ls_std::FileOperationException {};
|
|
|
|
|
|
+ if (ls_std::File::_mkdir(this->absoluteFilePath))
|
|
|
|
+ {
|
|
|
|
+ throw ls_std::FileOperationException{};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void ls_std::File::makeDirectories() {
|
|
|
|
|
|
+void ls_std::File::makeDirectories()
|
|
|
|
+{
|
|
std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
|
|
std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
- std::string currentHierarchy {};
|
|
|
|
|
|
+ std::string currentHierarchy{};
|
|
|
|
|
|
- for(const auto& subDirectory : subDirectories) {
|
|
|
|
|
|
+ for (const auto &subDirectory: subDirectories)
|
|
|
|
+ {
|
|
currentHierarchy += subDirectory;
|
|
currentHierarchy += subDirectory;
|
|
|
|
|
|
- if(!ls_std::File::_exists(currentHierarchy)) {
|
|
|
|
|
|
+ if (!ls_std::File::_exists(currentHierarchy))
|
|
|
|
+ {
|
|
ls_std::File::_mkdir(currentHierarchy);
|
|
ls_std::File::_mkdir(currentHierarchy);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -180,13 +212,13 @@ void ls_std::File::makeDirectories() {
|
|
|
|
|
|
void ls_std::File::remove()
|
|
void ls_std::File::remove()
|
|
{
|
|
{
|
|
- if(ls_std::File::_isFile(this->absoluteFilePath)) {
|
|
|
|
- if(std::remove(this->absoluteFilePath.c_str())) {
|
|
|
|
- throw ls_std::FileOperationException{};
|
|
|
|
- }
|
|
|
|
|
|
+ if (ls_std::File::_isFile(this->absoluteFilePath))
|
|
|
|
+ {
|
|
|
|
+ std::remove(this->absoluteFilePath.c_str());
|
|
}
|
|
}
|
|
|
|
|
|
- if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
|
|
|
|
|
|
+ if (ls_std::File::_isDirectory(this->absoluteFilePath))
|
|
|
|
+ {
|
|
ls_std::File::_remove(this->absoluteFilePath);
|
|
ls_std::File::_remove(this->absoluteFilePath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -195,7 +227,8 @@ bool ls_std::File::renameTo(const std::string &_newName)
|
|
{
|
|
{
|
|
bool renamed = ls_std::File::_renameTo(this->absoluteFilePath, _newName);
|
|
bool renamed = ls_std::File::_renameTo(this->absoluteFilePath, _newName);
|
|
|
|
|
|
- if(renamed) {
|
|
|
|
|
|
+ if (renamed)
|
|
|
|
+ {
|
|
this->absoluteFilePath = _newName;
|
|
this->absoluteFilePath = _newName;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -208,42 +241,55 @@ void ls_std::File::reset(const std::string &_newPath)
|
|
}
|
|
}
|
|
|
|
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
-void ls_std::File::_addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list)
|
|
|
|
|
|
+
|
|
|
|
+void ls_std::File::_addToFileListWindows(const std::string &_path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string> &_list)
|
|
{
|
|
{
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
std::string absolutePath = _path + separator + _data.cFileName;
|
|
std::string absolutePath = _path + separator + _data.cFileName;
|
|
|
|
|
|
- if(_withDirectories) {
|
|
|
|
|
|
+ if (_withDirectories)
|
|
|
|
+ {
|
|
_list.emplace_back(absolutePath);
|
|
_list.emplace_back(absolutePath);
|
|
- } else {
|
|
|
|
- if(ls_std::File::_isFile(absolutePath)) {
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ if (ls_std::File::_isFile(absolutePath))
|
|
|
|
+ {
|
|
_list.emplace_back(absolutePath);
|
|
_list.emplace_back(absolutePath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
-void ls_std::File::_addToFileListUnix(const std::string& _path, bool _withDirectories, dirent* directoryEntity, std::list<std::string>& _list)
|
|
|
|
|
|
+
|
|
|
|
+void ls_std::File::_addToFileListUnix(const std::string &_path, bool _withDirectories, dirent *directoryEntity, std::list<std::string> &_list)
|
|
{
|
|
{
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
std::string absolutePath = _path + separator + directoryEntity->d_name;
|
|
std::string absolutePath = _path + separator + directoryEntity->d_name;
|
|
|
|
|
|
- if(_withDirectories) {
|
|
|
|
|
|
+ if (_withDirectories)
|
|
|
|
+ {
|
|
_list.emplace_back(absolutePath);
|
|
_list.emplace_back(absolutePath);
|
|
- } else {
|
|
|
|
- if(ls_std::File::_isFile(absolutePath)) {
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ if (ls_std::File::_isFile(absolutePath))
|
|
|
|
+ {
|
|
_list.emplace_back(absolutePath);
|
|
_list.emplace_back(absolutePath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
bool ls_std::File::_equals(ls_std::File &_file, ls_std::File &_foreignFile)
|
|
bool ls_std::File::_equals(ls_std::File &_file, ls_std::File &_foreignFile)
|
|
{
|
|
{
|
|
bool isEqual = _file.getAbsoluteFilePath() == _foreignFile.getAbsoluteFilePath();
|
|
bool isEqual = _file.getAbsoluteFilePath() == _foreignFile.getAbsoluteFilePath();
|
|
|
|
|
|
- if(_file.exists() && _foreignFile.exists()) {
|
|
|
|
|
|
+ if (_file.exists() && _foreignFile.exists())
|
|
|
|
+ {
|
|
isEqual = isEqual && _file.canRead() == _foreignFile.canRead();
|
|
isEqual = isEqual && _file.canRead() == _foreignFile.canRead();
|
|
isEqual = isEqual && _file.canWrite() == _foreignFile.canWrite();
|
|
isEqual = isEqual && _file.canWrite() == _foreignFile.canWrite();
|
|
isEqual = isEqual && _file.canExecute() == _foreignFile.canExecute();
|
|
isEqual = isEqual && _file.canExecute() == _foreignFile.canExecute();
|
|
@@ -252,32 +298,76 @@ bool ls_std::File::_equals(ls_std::File &_file, ls_std::File &_foreignFile)
|
|
return isEqual;
|
|
return isEqual;
|
|
}
|
|
}
|
|
|
|
|
|
-bool ls_std::File::_exists(const std::string& _path)
|
|
|
|
|
|
+bool ls_std::File::_exists(const std::string &_path)
|
|
{
|
|
{
|
|
- struct stat _stat {};
|
|
|
|
|
|
+ struct stat _stat{};
|
|
return (stat(_path.c_str(), &_stat) == 0);
|
|
return (stat(_path.c_str(), &_stat) == 0);
|
|
}
|
|
}
|
|
|
|
|
|
std::string ls_std::File::_getParent(const std::string &_path)
|
|
std::string ls_std::File::_getParent(const std::string &_path)
|
|
{
|
|
{
|
|
- std::string parent {};
|
|
|
|
|
|
+ std::string parent{};
|
|
std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
|
|
std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
subDirectoryNames.pop_back();
|
|
subDirectoryNames.pop_back();
|
|
|
|
|
|
- for(auto const& subDirectoryName : subDirectoryNames) {
|
|
|
|
|
|
+ for (auto const &subDirectoryName: subDirectoryNames)
|
|
|
|
+ {
|
|
parent += subDirectoryName + separator;
|
|
parent += subDirectoryName + separator;
|
|
}
|
|
}
|
|
|
|
|
|
return parent;
|
|
return parent;
|
|
}
|
|
}
|
|
|
|
|
|
-bool ls_std::File::_isDirectory(const std::string& _path)
|
|
|
|
|
|
+#if defined(unix) || defined(__APPLE__)
|
|
|
|
+
|
|
|
|
+std::string ls_std::File::_getWorkingDirectoryUnix()
|
|
|
|
+{
|
|
|
|
+ std::string workingDirectory{};
|
|
|
|
+ char buffer[PATH_MAX];
|
|
|
|
+
|
|
|
|
+ if (getcwd(buffer, sizeof(buffer)) == nullptr)
|
|
|
|
+ {
|
|
|
|
+ throw ls_std::FileOperationException{};
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ workingDirectory = std::string(buffer);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return workingDirectory;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+#ifdef _WIN32
|
|
|
|
+
|
|
|
|
+std::string ls_std::File::_getWorkingDirectoryWindows()
|
|
{
|
|
{
|
|
- bool match {};
|
|
|
|
- struct stat _stat {};
|
|
|
|
|
|
+ std::string workingDirectory{};
|
|
|
|
+ TCHAR buffer[MAX_PATH];
|
|
|
|
|
|
- if(stat(_path.c_str(), &_stat) == 0) {
|
|
|
|
|
|
+ if (!GetCurrentDirectory(MAX_PATH, buffer))
|
|
|
|
+ {
|
|
|
|
+ throw ls_std::FileOperationException{};
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ workingDirectory = std::string(buffer);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return workingDirectory;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+bool ls_std::File::_isDirectory(const std::string &_path)
|
|
|
|
+{
|
|
|
|
+ bool match{};
|
|
|
|
+ struct stat _stat{};
|
|
|
|
+
|
|
|
|
+ if (stat(_path.c_str(), &_stat) == 0)
|
|
|
|
+ {
|
|
match = _stat.st_mode & (unsigned short) S_IFDIR;
|
|
match = _stat.st_mode & (unsigned short) S_IFDIR;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -286,12 +376,14 @@ bool ls_std::File::_isDirectory(const std::string& _path)
|
|
|
|
|
|
bool ls_std::File::_isExecutable(const std::string &_path)
|
|
bool ls_std::File::_isExecutable(const std::string &_path)
|
|
{
|
|
{
|
|
- bool executable {};
|
|
|
|
|
|
+ bool executable{};
|
|
|
|
|
|
- if(ls_std::File::_exists(_path)) {
|
|
|
|
- struct stat _stat {};
|
|
|
|
|
|
+ if (ls_std::File::_exists(_path))
|
|
|
|
+ {
|
|
|
|
+ struct stat _stat{};
|
|
|
|
|
|
- if(stat(_path.c_str(), &_stat) == 0) {
|
|
|
|
|
|
+ if (stat(_path.c_str(), &_stat) == 0)
|
|
|
|
+ {
|
|
executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
|
|
executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -299,12 +391,13 @@ bool ls_std::File::_isExecutable(const std::string &_path)
|
|
return executable;
|
|
return executable;
|
|
}
|
|
}
|
|
|
|
|
|
-bool ls_std::File::_isFile(const std::string& _path)
|
|
|
|
|
|
+bool ls_std::File::_isFile(const std::string &_path)
|
|
{
|
|
{
|
|
- bool match {};
|
|
|
|
- struct stat _stat {};
|
|
|
|
|
|
+ bool match{};
|
|
|
|
+ struct stat _stat{};
|
|
|
|
|
|
- if(stat(_path.c_str(), &_stat) == 0) {
|
|
|
|
|
|
+ if (stat(_path.c_str(), &_stat) == 0)
|
|
|
|
+ {
|
|
match = _stat.st_mode & (unsigned) S_IFREG;
|
|
match = _stat.st_mode & (unsigned) S_IFREG;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -312,47 +405,62 @@ bool ls_std::File::_isFile(const std::string& _path)
|
|
}
|
|
}
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
|
|
+
|
|
bool ls_std::File::_isReadableUnix(const std::string &_path)
|
|
bool ls_std::File::_isReadableUnix(const std::string &_path)
|
|
{
|
|
{
|
|
- bool readable {};
|
|
|
|
|
|
+ bool readable{};
|
|
|
|
|
|
- if(ls_std::File::_exists(_path)) {
|
|
|
|
- struct stat _stat {};
|
|
|
|
|
|
+ if (ls_std::File::_exists(_path))
|
|
|
|
+ {
|
|
|
|
+ struct stat _stat{};
|
|
|
|
|
|
- if(stat(_path.c_str(), &_stat) == 0) {
|
|
|
|
|
|
+ if (stat(_path.c_str(), &_stat) == 0)
|
|
|
|
+ {
|
|
readable = (_stat.st_mode & (unsigned) S_IREAD) != 0;
|
|
readable = (_stat.st_mode & (unsigned) S_IREAD) != 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ throw ls_std::FileOperationException{};
|
|
|
|
+ }
|
|
|
|
|
|
return readable;
|
|
return readable;
|
|
}
|
|
}
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
|
|
+
|
|
bool ls_std::File::_isReadableWindows(const std::string &_path)
|
|
bool ls_std::File::_isReadableWindows(const std::string &_path)
|
|
{
|
|
{
|
|
bool readable;
|
|
bool readable;
|
|
- WIN32_FIND_DATA data {};
|
|
|
|
|
|
+ WIN32_FIND_DATA data{};
|
|
HANDLE handleFind = FindFirstFile(_path.c_str(), &data);
|
|
HANDLE handleFind = FindFirstFile(_path.c_str(), &data);
|
|
|
|
|
|
- if(handleFind != INVALID_HANDLE_VALUE) {
|
|
|
|
|
|
+ if (handleFind != INVALID_HANDLE_VALUE)
|
|
|
|
+ {
|
|
readable = GetFileAttributes(data.cFileName) & (unsigned) FILE_ATTRIBUTE_READONLY;
|
|
readable = GetFileAttributes(data.cFileName) & (unsigned) FILE_ATTRIBUTE_READONLY;
|
|
- } else {
|
|
|
|
- throw ls_std::FileOperationException {};
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ throw ls_std::FileOperationException{};
|
|
}
|
|
}
|
|
|
|
|
|
return readable;
|
|
return readable;
|
|
}
|
|
}
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
bool ls_std::File::_isWritable(const std::string &_path)
|
|
bool ls_std::File::_isWritable(const std::string &_path)
|
|
{
|
|
{
|
|
- bool writable {};
|
|
|
|
|
|
+ bool writable{};
|
|
|
|
|
|
- if(ls_std::File::_exists(_path)) {
|
|
|
|
- struct stat _stat {};
|
|
|
|
|
|
+ if (ls_std::File::_exists(_path))
|
|
|
|
+ {
|
|
|
|
+ struct stat _stat{};
|
|
|
|
|
|
- if(stat(_path.c_str(), &_stat) == 0) {
|
|
|
|
|
|
+ if (stat(_path.c_str(), &_stat) == 0)
|
|
|
|
+ {
|
|
writable = (_stat.st_mode & (unsigned) S_IWRITE) != 0;
|
|
writable = (_stat.st_mode & (unsigned) S_IWRITE) != 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -362,10 +470,11 @@ bool ls_std::File::_isWritable(const std::string &_path)
|
|
|
|
|
|
time_t ls_std::File::_lastModified(const std::string &_path)
|
|
time_t ls_std::File::_lastModified(const std::string &_path)
|
|
{
|
|
{
|
|
- time_t lastModifiedTimeStamp {};
|
|
|
|
- struct stat _stat {};
|
|
|
|
|
|
+ time_t lastModifiedTimeStamp{};
|
|
|
|
+ struct stat _stat{};
|
|
|
|
|
|
- if(stat(_path.c_str(), &_stat) == 0) {
|
|
|
|
|
|
+ if (stat(_path.c_str(), &_stat) == 0)
|
|
|
|
+ {
|
|
lastModifiedTimeStamp = _stat.st_mtime;
|
|
lastModifiedTimeStamp = _stat.st_mtime;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -374,13 +483,13 @@ time_t ls_std::File::_lastModified(const std::string &_path)
|
|
|
|
|
|
std::list<std::string> ls_std::File::_list(const std::string &_path)
|
|
std::list<std::string> ls_std::File::_list(const std::string &_path)
|
|
{
|
|
{
|
|
- std::list<std::string> filesInDirectory {};
|
|
|
|
|
|
+ std::list<std::string> filesInDirectory{};
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
- filesInDirectory = ls_std::File::_listUnix(_path, true);
|
|
|
|
|
|
+ filesInDirectory = ls_std::File::_listUnix(_path, true);
|
|
#endif
|
|
#endif
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
- filesInDirectory = ls_std::File::_listWindows(_path, true);
|
|
|
|
|
|
+ filesInDirectory = ls_std::File::_listWindows(_path, true);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
return filesInDirectory;
|
|
return filesInDirectory;
|
|
@@ -388,65 +497,72 @@ std::list<std::string> ls_std::File::_list(const std::string &_path)
|
|
|
|
|
|
std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
|
|
std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
|
|
{
|
|
{
|
|
- std::list<std::string> filesInDirectory {};
|
|
|
|
|
|
+ std::list<std::string> filesInDirectory{};
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
- filesInDirectory = ls_std::File::_listUnix(_path, false);
|
|
|
|
|
|
+ filesInDirectory = ls_std::File::_listUnix(_path, false);
|
|
#endif
|
|
#endif
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
- filesInDirectory = ls_std::File::_listWindows(_path, false);
|
|
|
|
|
|
+ filesInDirectory = ls_std::File::_listWindows(_path, false);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
return filesInDirectory;
|
|
return filesInDirectory;
|
|
}
|
|
}
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
|
|
+
|
|
std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
|
|
std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
|
|
{
|
|
{
|
|
- std::list<std::string> filesInDirectory {};
|
|
|
|
- DIR* directory = opendir(_path.c_str());
|
|
|
|
- struct dirent* directoryEntity;
|
|
|
|
- std::string absolutePath {};
|
|
|
|
|
|
+ std::list<std::string> filesInDirectory{};
|
|
|
|
+ DIR *directory = opendir(_path.c_str());
|
|
|
|
+ struct dirent *directoryEntity;
|
|
|
|
+ std::string absolutePath{};
|
|
|
|
|
|
- while((directoryEntity = readdir(directory)) != nullptr) {
|
|
|
|
|
|
+ while ((directoryEntity = readdir(directory)) != nullptr)
|
|
|
|
+ {
|
|
ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
|
|
ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
|
|
}
|
|
}
|
|
|
|
|
|
closedir(directory);
|
|
closedir(directory);
|
|
return filesInDirectory;
|
|
return filesInDirectory;
|
|
}
|
|
}
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
|
|
+
|
|
std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
|
|
std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
|
|
{
|
|
{
|
|
- std::list<std::string> filesInDirectory {};
|
|
|
|
- WIN32_FIND_DATA data {};
|
|
|
|
|
|
+ std::list<std::string> filesInDirectory{};
|
|
|
|
+ WIN32_FIND_DATA data{};
|
|
HANDLE hFind;
|
|
HANDLE hFind;
|
|
- std::string pattern {_path + ls_std::FilePathSeparator::get() + "*"};
|
|
|
|
|
|
+ std::string pattern{_path + ls_std::FilePathSeparator::get() + "*"};
|
|
|
|
|
|
- if((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
|
|
|
|
- do {
|
|
|
|
|
|
+ if ((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE)
|
|
|
|
+ {
|
|
|
|
+ do
|
|
|
|
+ {
|
|
ls_std::File::_addToFileListWindows(_path, withDirectories, data, filesInDirectory);
|
|
ls_std::File::_addToFileListWindows(_path, withDirectories, data, filesInDirectory);
|
|
- }
|
|
|
|
- while(FindNextFile(hFind, &data) != 0);
|
|
|
|
|
|
+ } while (FindNextFile(hFind, &data) != 0);
|
|
|
|
|
|
FindClose(hFind);
|
|
FindClose(hFind);
|
|
}
|
|
}
|
|
|
|
|
|
return filesInDirectory;
|
|
return filesInDirectory;
|
|
}
|
|
}
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
-int ls_std::File::_mkdir(const std::string& _path) {
|
|
|
|
|
|
+int ls_std::File::_mkdir(const std::string &_path)
|
|
|
|
+{
|
|
int result;
|
|
int result;
|
|
|
|
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
- result = mkdir(_path.c_str());
|
|
|
|
|
|
+ result = mkdir(_path.c_str());
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
- result = mkdir(_path.c_str(), 0777);
|
|
|
|
|
|
+ result = mkdir(_path.c_str(), 0777);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
return result;
|
|
return result;
|
|
@@ -460,21 +576,25 @@ std::string ls_std::File::_normalizePath(std::string _path)
|
|
return _path;
|
|
return _path;
|
|
}
|
|
}
|
|
|
|
|
|
-std::string ls_std::File::_reduceSeparators(const std::string& _path)
|
|
|
|
|
|
+std::string ls_std::File::_reduceSeparators(const std::string &_path)
|
|
{
|
|
{
|
|
static const char separator = {ls_std::FilePathSeparator::get()};
|
|
static const char separator = {ls_std::FilePathSeparator::get()};
|
|
- std::string normalizedPath {};
|
|
|
|
- int index {};
|
|
|
|
|
|
+ std::string normalizedPath{};
|
|
|
|
+ int index{};
|
|
|
|
|
|
- while(index < _path.size()) {
|
|
|
|
- if(_path[index] == separator) {
|
|
|
|
|
|
+ while (index < _path.size())
|
|
|
|
+ {
|
|
|
|
+ if (_path[index] == separator)
|
|
|
|
+ {
|
|
normalizedPath += _path[index];
|
|
normalizedPath += _path[index];
|
|
|
|
|
|
- do {
|
|
|
|
|
|
+ do
|
|
|
|
+ {
|
|
index++;
|
|
index++;
|
|
- }
|
|
|
|
- while(_path[index] == separator);
|
|
|
|
- } else {
|
|
|
|
|
|
+ } while (_path[index] == separator);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
normalizedPath += _path[index];
|
|
normalizedPath += _path[index];
|
|
index++;
|
|
index++;
|
|
}
|
|
}
|
|
@@ -489,22 +609,26 @@ void ls_std::File::_remove(const std::string &_path)
|
|
ls_std::File::_removeUnix(_path);
|
|
ls_std::File::_removeUnix(_path);
|
|
#endif
|
|
#endif
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
- ls_std::File::_removeWindows(_path);
|
|
|
|
|
|
+ ls_std::File::_removeWindows(_path);
|
|
#endif
|
|
#endif
|
|
}
|
|
}
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
|
|
+
|
|
void ls_std::File::_removeUnix(const std::string &_path)
|
|
void ls_std::File::_removeUnix(const std::string &_path)
|
|
{
|
|
{
|
|
rmdir(_path.c_str());
|
|
rmdir(_path.c_str());
|
|
}
|
|
}
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
|
|
+
|
|
void ls_std::File::_removeWindows(const std::string &_path)
|
|
void ls_std::File::_removeWindows(const std::string &_path)
|
|
{
|
|
{
|
|
_rmdir(_path.c_str());
|
|
_rmdir(_path.c_str());
|
|
}
|
|
}
|
|
|
|
+
|
|
#endif
|
|
#endif
|
|
|
|
|
|
bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
|
|
bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
|
|
@@ -518,23 +642,25 @@ std::string ls_std::File::_replaceWrongSeparator(std::string _path)
|
|
static const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
|
|
static const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
#if defined(unix) || defined(__APPLE__)
|
|
- std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
|
|
|
|
|
|
+ std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifdef _WIN32
|
|
#ifdef _WIN32
|
|
- std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
|
|
|
|
|
|
+ std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
return _path;
|
|
return _path;
|
|
}
|
|
}
|
|
|
|
|
|
-std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string& _path) {
|
|
|
|
- std::vector<std::string> subDirectoryNames {};
|
|
|
|
- std::stringstream _stream {_path};
|
|
|
|
- std::string subDirectoryName {};
|
|
|
|
|
|
+std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string &_path)
|
|
|
|
+{
|
|
|
|
+ std::vector<std::string> subDirectoryNames{};
|
|
|
|
+ std::stringstream _stream{_path};
|
|
|
|
+ std::string subDirectoryName{};
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
const char separator = ls_std::FilePathSeparator::get();
|
|
|
|
|
|
- while(std::getline(_stream, subDirectoryName, separator)) {
|
|
|
|
|
|
+ while (std::getline(_stream, subDirectoryName, separator))
|
|
|
|
+ {
|
|
subDirectoryNames.push_back(subDirectoryName);
|
|
subDirectoryNames.push_back(subDirectoryName);
|
|
}
|
|
}
|
|
|
|
|