|
@@ -3,7 +3,7 @@
|
|
|
* Company: Lynar Studios
|
|
|
* E-Mail: webmaster@lynarstudios.com
|
|
|
* Created: 2020-08-15
|
|
|
- * Changed: 2023-04-13
|
|
|
+ * Changed: 2023-05-16
|
|
|
*
|
|
|
* */
|
|
|
|
|
@@ -56,22 +56,22 @@ File::File(string _absoluteFilePath) : Class("File"), absoluteFilePath(File::_no
|
|
|
|
|
|
File::~File() noexcept = default;
|
|
|
|
|
|
-bool File::operator==(File &_file)
|
|
|
+bool File::operator==(const File &_file) const
|
|
|
{
|
|
|
return File::_equals(*this, _file);
|
|
|
}
|
|
|
|
|
|
-bool File::operator!=(File &_file)
|
|
|
+bool File::operator!=(const File &_file) const
|
|
|
{
|
|
|
return !File::_equals(*this, _file);
|
|
|
}
|
|
|
|
|
|
-bool File::canExecute()
|
|
|
+bool File::canExecute() const
|
|
|
{
|
|
|
return File::_isExecutable(this->absoluteFilePath);
|
|
|
}
|
|
|
|
|
|
-bool File::canRead()
|
|
|
+bool File::canRead() const
|
|
|
{
|
|
|
bool readable{};
|
|
|
|
|
@@ -85,12 +85,12 @@ bool File::canRead()
|
|
|
return readable;
|
|
|
}
|
|
|
|
|
|
-bool File::canWrite()
|
|
|
+bool File::canWrite() const
|
|
|
{
|
|
|
return File::_isWritable(this->absoluteFilePath);
|
|
|
}
|
|
|
|
|
|
-void File::createNewFile()
|
|
|
+void File::createNewFile() const
|
|
|
{
|
|
|
if (!File::_exists(this->absoluteFilePath))
|
|
|
{
|
|
@@ -103,17 +103,17 @@ void File::createNewFile()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-bool File::exists()
|
|
|
+bool File::exists() const
|
|
|
{
|
|
|
return File::_exists(this->absoluteFilePath);
|
|
|
}
|
|
|
|
|
|
-string File::getAbsoluteFilePath()
|
|
|
+string File::getAbsoluteFilePath() const
|
|
|
{
|
|
|
return this->absoluteFilePath;
|
|
|
}
|
|
|
|
|
|
-string File::getName()
|
|
|
+string File::getName() const
|
|
|
{
|
|
|
string copy = this->absoluteFilePath;
|
|
|
|
|
@@ -130,7 +130,7 @@ string File::getName()
|
|
|
return string{base, copy.end()};
|
|
|
}
|
|
|
|
|
|
-string File::getParent()
|
|
|
+string File::getParent() const
|
|
|
{
|
|
|
return File::_getParent(this->absoluteFilePath);
|
|
|
}
|
|
@@ -149,7 +149,7 @@ string File::getWorkingDirectory()
|
|
|
return workingDirectory;
|
|
|
}
|
|
|
|
|
|
-long File::getSize()
|
|
|
+long File::getSize() const
|
|
|
{
|
|
|
streampos fileSize{};
|
|
|
|
|
@@ -165,22 +165,22 @@ long File::getSize()
|
|
|
return (long) fileSize;
|
|
|
}
|
|
|
|
|
|
-bool File::isDirectory()
|
|
|
+bool File::isDirectory() const
|
|
|
{
|
|
|
return File::_isDirectory(this->absoluteFilePath);
|
|
|
}
|
|
|
|
|
|
-bool File::isFile()
|
|
|
+bool File::isFile() const
|
|
|
{
|
|
|
return File::_isFile(this->absoluteFilePath);
|
|
|
}
|
|
|
|
|
|
-time_t File::lastModified()
|
|
|
+time_t File::lastModified() const
|
|
|
{
|
|
|
return File::_lastModified(this->absoluteFilePath);
|
|
|
}
|
|
|
|
|
|
-::list<string> File::list()
|
|
|
+::list<string> File::list() const
|
|
|
{
|
|
|
::list<string> fileList{};
|
|
|
|
|
@@ -192,7 +192,7 @@ time_t File::lastModified()
|
|
|
return fileList;
|
|
|
}
|
|
|
|
|
|
-::list<string> File::listFiles()
|
|
|
+::list<string> File::listFiles() const
|
|
|
{
|
|
|
::list<string> fileList{};
|
|
|
|
|
@@ -204,7 +204,7 @@ time_t File::lastModified()
|
|
|
return fileList;
|
|
|
}
|
|
|
|
|
|
-void File::makeDirectory()
|
|
|
+void File::makeDirectory() const
|
|
|
{
|
|
|
if (!File::_makeDirectory(this->absoluteFilePath))
|
|
|
{
|
|
@@ -212,7 +212,7 @@ void File::makeDirectory()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void File::makeDirectories()
|
|
|
+void File::makeDirectories() const
|
|
|
{
|
|
|
vector<string> subDirectories = File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
|
|
|
const char separator = FilePathSeparator::get();
|
|
@@ -222,19 +222,16 @@ void File::makeDirectories()
|
|
|
{
|
|
|
currentHierarchy += subDirectory;
|
|
|
|
|
|
- if (!File::_exists(currentHierarchy + separator) && !currentHierarchy.empty())
|
|
|
+ if (!File::_exists(currentHierarchy + separator) && !currentHierarchy.empty() && !File::_makeDirectory(currentHierarchy))
|
|
|
{
|
|
|
- if (!File::_makeDirectory(currentHierarchy))
|
|
|
- {
|
|
|
- throw FileOperationException{"operation: create directory"};
|
|
|
- }
|
|
|
+ throw FileOperationException{"operation: create directory"};
|
|
|
}
|
|
|
|
|
|
currentHierarchy += separator;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void File::remove()
|
|
|
+void File::remove() const
|
|
|
{
|
|
|
if (File::_isFile(this->absoluteFilePath))
|
|
|
{
|
|
@@ -288,7 +285,7 @@ void File::_addToFileListWindows(const string &_path, bool _withDirectories, WIN
|
|
|
|
|
|
#if defined(unix) || defined(__APPLE__)
|
|
|
|
|
|
-void File::_addToFileListUnix(const string &_path, bool _withDirectories, dirent *directoryEntity, ::list<string> &_list)
|
|
|
+void File::_addToFileListUnix(const string &_path, bool _withDirectories, const dirent *directoryEntity, ::list<string> &_list)
|
|
|
{
|
|
|
const char separator = FilePathSeparator::get();
|
|
|
string absolutePath = _path + separator + directoryEntity->d_name;
|
|
@@ -308,7 +305,7 @@ void File::_addToFileListUnix(const string &_path, bool _withDirectories, dirent
|
|
|
|
|
|
#endif
|
|
|
|
|
|
-bool File::_equals(File &_file, File &_foreignFile)
|
|
|
+bool File::_equals(const File &_file, const File &_foreignFile)
|
|
|
{
|
|
|
bool isEqual = _file.getAbsoluteFilePath() == _foreignFile.getAbsoluteFilePath();
|
|
|
|
|
@@ -344,9 +341,8 @@ string File::_getParent(const string &_path)
|
|
|
string File::_getWorkingDirectoryUnix()
|
|
|
{
|
|
|
string workingDirectory{};
|
|
|
- char buffer[PATH_MAX];
|
|
|
|
|
|
- if (getcwd(buffer, sizeof(buffer)) == nullptr)
|
|
|
+ if (string buffer(PATH_MAX, 'x'); getcwd(buffer.data(), buffer.size()) == nullptr)
|
|
|
{
|
|
|
throw FileOperationException{"operation: get working directory"};
|
|
|
}
|
|
@@ -384,11 +380,8 @@ string File::_getWorkingDirectoryWindows()
|
|
|
bool File::_isDirectory(const string &_path)
|
|
|
{
|
|
|
bool match{};
|
|
|
- struct stat _stat
|
|
|
- {
|
|
|
- };
|
|
|
|
|
|
- if (stat(_path.c_str(), &_stat) == 0)
|
|
|
+ if (struct stat _stat{}; stat(_path.c_str(), &_stat) == 0)
|
|
|
{
|
|
|
match = _stat.st_mode & (unsigned short) S_IFDIR;
|
|
|
}
|
|
@@ -418,11 +411,8 @@ bool File::_isExecutable(const string &_path)
|
|
|
bool File::_isFile(const string &_path)
|
|
|
{
|
|
|
bool match{};
|
|
|
- struct stat _stat
|
|
|
- {
|
|
|
- };
|
|
|
|
|
|
- if (stat(_path.c_str(), &_stat) == 0)
|
|
|
+ if (struct stat _stat{}; stat(_path.c_str(), &_stat) == 0)
|
|
|
{
|
|
|
match = _stat.st_mode & (unsigned) S_IFREG;
|
|
|
}
|
|
@@ -501,11 +491,8 @@ bool File::_isWritable(const string &_path)
|
|
|
time_t File::_lastModified(const string &_path)
|
|
|
{
|
|
|
time_t lastModifiedTimeStamp{};
|
|
|
- struct stat _stat
|
|
|
- {
|
|
|
- };
|
|
|
|
|
|
- if (stat(_path.c_str(), &_stat) == 0)
|
|
|
+ if (struct stat _stat{}; stat(_path.c_str(), &_stat) == 0)
|
|
|
{
|
|
|
lastModifiedTimeStamp = _stat.st_mtime;
|
|
|
}
|
|
@@ -547,7 +534,7 @@ time_t File::_lastModified(const string &_path)
|
|
|
{
|
|
|
::list<string> filesInDirectory{};
|
|
|
DIR *directory = opendir(_path.c_str());
|
|
|
- struct dirent *directoryEntity;
|
|
|
+ const struct dirent *directoryEntity;
|
|
|
string absolutePath{};
|
|
|
|
|
|
while ((directoryEntity = readdir(directory)) != nullptr)
|