Browse Source

Extended File class

- added "list" method for receiving a file list
based on current directory and implemented windows
sub routine (unix version pending)
- extended tests for File class
Patrick 4 years ago
parent
commit
b4e9a3e213

+ 81 - 27
source/io/File.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2020-08-16
+ * Changed:         2020-08-17
  *
  * */
 
@@ -16,12 +16,12 @@
 #include <sstream>
 #include <vector>
 
-#ifdef unix
+#if defined(unix) || defined(__APPLE__)
 #include <unistd.h>
 #endif
 
-#ifdef __APPLE__
-#include <unistd.h>
+#ifdef _WIN32
+#include <windows.h>
 #endif
 
 ls_std::File::File(std::string _absoluteFilePath) : Class("File"),
@@ -87,16 +87,7 @@ std::string ls_std::File::getName()
 
 std::string ls_std::File::getParent()
 {
-  std::string parent {};
-  std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
-  const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
-  subDirectoryNames.pop_back();
-
-  for(auto const& subDirectoryName : subDirectoryNames) {
-    parent += subDirectoryName + separator;
-  }
-
-  return parent;
+  return ls_std::File::_getParent(this->absoluteFilePath);
 }
 
 long ls_std::File::getSize()
@@ -129,6 +120,17 @@ time_t ls_std::File::lastModified()
   return ls_std::File::_lastModified(this->absoluteFilePath);
 }
 
+std::list<std::string> ls_std::File::list()
+{
+  std::list<std::string> fileList {};
+
+  if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
+    fileList = ls_std::File::_list(this->absoluteFilePath);
+  }
+
+  return fileList;
+}
+
 void ls_std::File::makeDirectory()
 {
   if(ls_std::File::_mkdir(this->absoluteFilePath)) {
@@ -173,6 +175,20 @@ bool ls_std::File::_exists(const std::string& _path)
   return (stat(_path.c_str(), &_stat) == 0);
 }
 
+std::string ls_std::File::_getParent(const std::string &_path)
+{
+  std::string parent {};
+  std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
+  const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
+  subDirectoryNames.pop_back();
+
+  for(auto const& subDirectoryName : subDirectoryNames) {
+    parent += subDirectoryName + separator;
+  }
+
+  return parent;
+}
+
 bool ls_std::File::_isDirectory(const std::string& _path)
 {
   bool match {};
@@ -209,6 +225,52 @@ time_t ls_std::File::_lastModified(const std::string &_path)
   return lastModifiedTimeStamp;
 }
 
+std::list<std::string> ls_std::File::_list(const std::string &_path)
+{
+  std::list<std::string> filesInDirectory {};
+
+  #if defined(unix) || defined(__APPLE__)
+    filesInDirectory = ls_std::File::_listUnix(_path);
+  #endif
+  #ifdef _WIN32
+    filesInDirectory = ls_std::File::_listWindows(_path);
+  #endif
+
+  return filesInDirectory;
+}
+
+#if defined(unix) || defined(__APPLE__)
+std::list<std::string> ls_std::File::_listUnix(const std::string &_path)
+{
+  std::list<std::string> filesInDirectory {};
+
+
+  return filesInDirectory;
+}
+#endif
+
+#ifdef _WIN32
+std::list<std::string> ls_std::File::_listWindows(const std::string &_path)
+{
+  std::list<std::string> filesInDirectory {};
+  WIN32_FIND_DATA data {};
+  HANDLE hFind;
+  std::string parent = ls_std::File::_getParent(_path);
+  std::string pattern {_path + ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator() + "*"};
+
+  if((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
+    do {
+      filesInDirectory.emplace_back(parent + data.cFileName);
+    }
+    while(FindNextFile(hFind, &data) != 0);
+
+    FindClose(hFind);
+  }
+
+  return filesInDirectory;
+}
+#endif
+
 int ls_std::File::_mkdir(const std::string& _path) {
   int result;
 
@@ -216,11 +278,7 @@ int ls_std::File::_mkdir(const std::string& _path) {
     result = mkdir(_path.c_str());
   #endif
 
-  #ifdef unix
-    result = mkdir(_path.c_str(), 0777);
-  #endif
-
-  #ifdef __APPLE__
+  #if defined(unix) || defined(__APPLE__)
     result = mkdir(_path.c_str(), 0777);
   #endif
 
@@ -229,19 +287,15 @@ int ls_std::File::_mkdir(const std::string& _path) {
 
 std::string ls_std::File::_normalizePath(std::string _path)
 {
-  const char linuxSeparator = ls_std::FilePathSeparator::getLinuxFilePathSeparator();
+  const char unixSeparator = ls_std::FilePathSeparator::getLinuxFilePathSeparator();
   const char windowsSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
 
-  #ifdef unix
-    std::replace(_path.begin(), _path.end(), windowsSeparator, linuxSeparator);
-  #endif
-
-  #ifdef __APPLE__
-    std::replace(_path.begin(), _path.end(), windowsSeparator, linuxSeparator);
+  #if defined(unix) || defined(__APPLE__)
+    std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
   #endif
 
   #ifdef _WIN32
-    std::replace(_path.begin(), _path.end(), linuxSeparator, windowsSeparator);
+    std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
   #endif
 
   return _path;

+ 11 - 1
source/io/File.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2020-08-16
+ * Changed:         2020-08-17
  *
  * */
 
@@ -13,6 +13,7 @@
 #include "../base/Class.hpp"
 #include <string>
 #include <vector>
+#include <list>
 #include <ctime>
 
 namespace ls_std {
@@ -39,6 +40,7 @@ namespace ls_std {
       bool isDirectory();
       bool isFile();
       time_t lastModified();
+      std::list<std::string> list();
       void makeDirectory();
       void makeDirectories();
       void remove();
@@ -48,9 +50,17 @@ namespace ls_std {
       std::string absoluteFilePath {};
 
       static bool _exists(const std::string& _path);
+      static std::string _getParent(const std::string& _path);
       static bool _isDirectory(const std::string& _path);
       static bool _isFile(const std::string& _path);
       static time_t _lastModified(const std::string& _path);
+      static std::list<std::string> _list(const std::string& _path);
+      #if defined(unix) || defined(__APPLE__)
+        static std::list<std::string> _listUnix(const std::string& _path);
+      #endif
+      #ifdef _WIN32
+        static std::list<std::string> _listWindows(const std::string& _path);
+      #endif
       static int _mkdir(const std::string& _path);
       static std::string _normalizePath(std::string _path);
       static std::vector<std::string> _splitIntoSubDirectoryNames(const std::string& _path);

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

@@ -9,6 +9,7 @@
 
 #include <gtest/gtest.h>
 #include "../../../source/io/File.hpp"
+#include "../../../source/io/FilePathSeparator.hpp"
 #include "../../TestHelper.hpp"
 
 namespace {
@@ -162,6 +163,28 @@ namespace {
     ASSERT_EQ(1597475488, file.lastModified());
   }
 
+  TEST_F(FileTest, list)
+  {
+    ls_std::File file {TestHelper::getResourcesFolderLocation() + "list_test"};
+    std::list<std::string> filesInDirectory = file.list();
+    auto filesIterator = filesInDirectory.begin();
+
+    ASSERT_FALSE(filesInDirectory.empty());
+    ASSERT_EQ(6, filesInDirectory.size());
+
+    ASSERT_STREQ(std::string(file.getParent() + ".").c_str(), filesIterator->c_str());
+    filesIterator++;
+    ASSERT_STREQ(std::string(file.getParent() + "..").c_str(), filesIterator->c_str());
+    filesIterator++;
+    ASSERT_STREQ(std::string(file.getParent() + "another_file.txt").c_str(), filesIterator->c_str());
+    filesIterator++;
+    ASSERT_STREQ(std::string(file.getParent() + "bla.txt").c_str(), filesIterator->c_str());
+    filesIterator++;
+    ASSERT_STREQ(std::string(file.getParent() + "hello.txt").c_str(), filesIterator->c_str());
+    filesIterator++;
+    ASSERT_STREQ(std::string(file.getParent() + "list_test_sub").c_str(), filesIterator->c_str());
+  }
+
   TEST_F(FileTest, makeDirectory)
   {
     ls_std::File directory {TestHelper::getResourcesFolderLocation() + "testDir"};

+ 1 - 0
test/resources/list_test/another_file.txt

@@ -0,0 +1 @@
+Hello again!

+ 1 - 0
test/resources/list_test/bla.txt

@@ -0,0 +1 @@
+nothing to say!

+ 1 - 0
test/resources/list_test/hello.txt

@@ -0,0 +1 @@
+Blub!

+ 1 - 0
test/resources/list_test/list_test_sub/tmp.txt

@@ -0,0 +1 @@
+not important!