Ver Fonte

Improved File class

- implemented TODO by extending "equals" and "not
equals" functionality. Both check for file
permissions now, too
patrickmattulat há 4 anos atrás
pai
commit
12b9e4b053
2 ficheiros alterados com 13 adições e 5 exclusões
  1. 12 5
      source/io/File.cpp
  2. 1 0
      source/io/File.hpp

+ 12 - 5
source/io/File.cpp

@@ -25,17 +25,14 @@ ls_std::File::File(std::string _absoluteFilePath) : Class("File"),
 absoluteFilePath(ls_std::File::_normalizePath(std::move(_absoluteFilePath)))
 {}
 
-// TODO: also compare executable, readable, writable flag
 bool ls_std::File::operator==(File &_file)
 {
-  std::string normalizedForeignFilePath = ls_std::File::_normalizePath(_file.getAbsoluteFilePath());
-  return this->absoluteFilePath == normalizedForeignFilePath;
+  return ls_std::File::_equals(*this, _file);
 }
 
 bool ls_std::File::operator!=(File &_file)
 {
-  std::string normalizedForeignFilePath = ls_std::File::_normalizePath(_file.getAbsoluteFilePath());
-  return this->absoluteFilePath != normalizedForeignFilePath;
+  return !ls_std::File::_equals(*this, _file);
 }
 
 bool ls_std::File::canExecute()
@@ -235,6 +232,16 @@ void ls_std::File::_addToFileListUnix(const std::string& _path, bool _withDirect
 }
 #endif
 
+bool ls_std::File::_equals(File &_file, File &_foreignFile)
+{
+  bool pathEquals = _file.getAbsoluteFilePath() == _foreignFile.getAbsoluteFilePath();
+  bool readableEquals = _file.canRead() == _foreignFile.canRead();
+  bool writableEquals = _file.canWrite() == _foreignFile.canWrite();
+  bool executableEquals = _file.canExecute() == _foreignFile.canExecute();
+
+  return pathEquals && readableEquals && writableEquals && executableEquals;
+}
+
 bool ls_std::File::_exists(const std::string& _path)
 {
   struct stat _stat {};

+ 1 - 0
source/io/File.hpp

@@ -68,6 +68,7 @@ namespace ls_std {
       #ifdef _WIN32
         static void _addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list);
       #endif
+      static bool _equals(File& _file, File& _foreignFile);
       static bool _exists(const std::string& _path);
       static std::string _getParent(const std::string& _path);
       static bool _isDirectory(const std::string& _path);