瀏覽代碼

Extended File class (incomplete)

- added "canRead" method to File class (Unix implementation
pending)
- added test for File class
Patrick 3 年之前
父節點
當前提交
15f49a78d9
共有 3 個文件被更改,包括 47 次插入3 次删除
  1. 31 0
      source/io/File.cpp
  2. 10 3
      source/io/File.hpp
  3. 6 0
      test/cases/io/FileTest.cpp

+ 31 - 0
source/io/File.cpp

@@ -43,6 +43,20 @@ bool ls_std::File::canExecute()
   return ls_std::File::_isExecutable(this->absoluteFilePath);
 }
 
+bool ls_std::File::canRead()
+{
+  bool readable;
+
+  #if defined(unix) || defined(__APPLE__)
+    readable = ls_std::File::_isReadableUnix(this->absoluteFilePath);
+  #endif
+  #ifdef _WIN32
+    readable = ls_std::File::_isReadableWindows(this->absoluteFilePath);
+  #endif
+
+  return readable;
+}
+
 void ls_std::File::createNewFile()
 {
   if(!ls_std::File::_exists(this->absoluteFilePath)) {
@@ -275,6 +289,23 @@ bool ls_std::File::_isFile(const std::string& _path)
   return match;
 }
 
+#ifdef _WIN32
+bool ls_std::File::_isReadableWindows(const std::string &_path)
+{
+  bool readable;
+  WIN32_FIND_DATA data {};
+  HANDLE handleFind = FindFirstFile(_path.c_str(), &data);
+
+  if(handleFind != INVALID_HANDLE_VALUE) {
+    readable = GetFileAttributes(data.cFileName) & (unsigned) FILE_ATTRIBUTE_READONLY;
+  } else {
+    throw ls_std::FileOperationException {};
+  }
+
+  return readable;
+}
+#endif
+
 time_t ls_std::File::_lastModified(const std::string &_path)
 {
   time_t lastModifiedTimeStamp {};

+ 10 - 3
source/io/File.hpp

@@ -40,6 +40,7 @@ namespace ls_std {
       // additional functionality
 
       bool canExecute();
+      bool canRead();
       void createNewFile();
       bool exists();
       std::string getAbsoluteFilePath();
@@ -60,17 +61,23 @@ namespace ls_std {
 
       std::string absoluteFilePath {};
 
-      #ifdef _WIN32
-        static void _addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list);
-      #endif
       #if defined(unix) || defined(__APPLE__)
         static void _addToFileListUnix(const std::string& _path, bool _withDirectories, dirent* directoryEntity, std::list<std::string>& _list);
       #endif
+      #ifdef _WIN32
+        static void _addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list);
+      #endif
       static bool _exists(const std::string& _path);
       static std::string _getParent(const std::string& _path);
       static bool _isDirectory(const std::string& _path);
       static bool _isExecutable(const std::string& _path);
       static bool _isFile(const std::string& _path);
+      #if defined(unix) || defined(__APPLE__)
+        static bool _isReadableUnix(const std::string& _path);
+      #endif
+      #ifdef _WIN32
+        static bool _isReadableWindows(const std::string& _path);
+      #endif
       static time_t _lastModified(const std::string& _path);
       static std::list<std::string> _list(const std::string& _path);
       static std::list<std::string> _listFiles(const std::string& _path);

+ 6 - 0
test/cases/io/FileTest.cpp

@@ -70,6 +70,12 @@ namespace {
     ASSERT_FALSE(file.canExecute());
   }
 
+  TEST_F(FileTest, canRead)
+  {
+    ls_std::File readableFile {this->fileLocation};
+    ASSERT_TRUE(readableFile.canRead());
+  }
+
   TEST_F(FileTest, createNewFileAndRemove)
   {
     ls_std::File file {TestHelper::getResourcesFolderLocation() + "tmp.txt"};