Browse Source

Improved File class

- reduced complexity in "_listUnix" method
patrick 4 years ago
parent
commit
1aaedbdc5a
2 changed files with 24 additions and 12 deletions
  1. 17 12
      source/io/File.cpp
  2. 7 0
      source/io/File.hpp

+ 17 - 12
source/io/File.cpp

@@ -18,7 +18,6 @@
 
 #if defined(unix) || defined(__APPLE__)
 #include <unistd.h>
-#include <dirent.h>
 #endif
 
 ls_std::File::File(std::string _absoluteFilePath) : Class("File"),
@@ -192,6 +191,22 @@ void ls_std::File::_addToFileListWindows(const std::string& _path, bool _withDir
 }
 #endif
 
+#if defined(unix) || defined(__APPLE__)
+void ls_std::File::_addToFileListUnix(const std::string& _path, bool _withDirectories, dirent* directoryEntity, std::list<std::string>& _list)
+{
+  const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
+  std::string absolutePath = _path + separator + directoryEntity->d_name;
+
+  if(_withDirectories) {
+    _list.emplace_back(absolutePath);
+  } else {
+    if(ls_std::File::_isFile(absolutePath)) {
+      _list.emplace_back(absolutePath);
+    }
+  }
+}
+#endif
+
 bool ls_std::File::_exists(const std::string& _path)
 {
   struct stat _stat {};
@@ -282,20 +297,10 @@ std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool wi
   std::list<std::string> filesInDirectory {};
   DIR* directory = opendir(_path.c_str());
   struct dirent* directoryEntity;
-  std::string parent = ls_std::File::_getParent(_path);
   std::string absolutePath {};
-  const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
 
   while((directoryEntity = readdir(directory)) != nullptr) {
-    absolutePath = _path + separator + directoryEntity->d_name;
-
-    if(withDirectories) {
-      filesInDirectory.emplace_back(absolutePath);
-    } else {
-      if(ls_std::File::_isFile(absolutePath)) {
-        filesInDirectory.emplace_back(absolutePath);
-      }
-    }
+    ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
   }
 
   return filesInDirectory;

+ 7 - 0
source/io/File.hpp

@@ -16,6 +16,10 @@
 #include <list>
 #include <ctime>
 
+#if defined(unix) || defined(__APPLE__)
+#include <dirent.h>
+#endif
+
 #ifdef _WIN32
 #include <windows.h>
 #endif
@@ -57,6 +61,9 @@ namespace ls_std {
       #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
       static bool _exists(const std::string& _path);
       static std::string _getParent(const std::string& _path);
       static bool _isDirectory(const std::string& _path);