Browse Source

Fix rmdir dependency in File class

- fix rmdir dependency for Microsoft VS compiler
Patrick 3 years ago
parent
commit
2f9be186db
2 changed files with 37 additions and 4 deletions
  1. 30 4
      source/ls_std/io/File.cpp
  2. 7 0
      source/ls_std/io/File.hpp

+ 30 - 4
source/ls_std/io/File.cpp

@@ -11,16 +11,20 @@
 #include "../exception/FileOperationException.hpp"
 #include "FilePathSeparatorMatch.hpp"
 #include <fstream>
-#include <sys/stat.h>
 #include <algorithm>
 #include <sstream>
 #include <vector>
 #include <cstdio>
+#include <sys/stat.h>
 
 #if defined(unix) || defined(__APPLE__)
 #include <unistd.h>
 #endif
 
+#ifdef _WIN32
+#include <direct.h>
+#endif
+
 ls_std::File::File(std::string _absoluteFilePath) : Class("File"),
 absoluteFilePath(ls_std::File::_normalizePath(std::move(_absoluteFilePath)))
 {}
@@ -183,9 +187,7 @@ void ls_std::File::remove()
   }
 
   if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
-    if(rmdir(this->absoluteFilePath.c_str())) {
-      throw ls_std::FileOperationException{};
-    }
+    ls_std::File::_remove(this->absoluteFilePath);
   }
 }
 
@@ -481,6 +483,30 @@ std::string ls_std::File::_reduceSeparators(const std::string& _path)
   return normalizedPath;
 }
 
+void ls_std::File::_remove(const std::string &_path)
+{
+  #if defined(unix) || defined(__APPLE__)
+  ls_std::File::_removeUnix(_path);
+  #endif
+  #ifdef _WIN32
+    ls_std::File::_removeWindows(_path);
+  #endif
+}
+
+#if defined(unix) || defined(__APPLE__)
+void ls_std::File::_removeUnix(const std::string &_path)
+{
+  rmdir(_path.c_str());
+}
+#endif
+
+#ifdef _WIN32
+void ls_std::File::_removeWindows(const std::string &_path)
+{
+  _rmdir(_path.c_str());
+}
+#endif
+
 bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
 {
   return std::rename(_oldName.c_str(), _newName.c_str()) == 0;

+ 7 - 0
source/ls_std/io/File.hpp

@@ -94,6 +94,13 @@ namespace ls_std {
       static int _mkdir(const std::string& _path);
       static std::string _normalizePath(std::string _path);
       static std::string _reduceSeparators(const std::string& _path);
+      static void _remove(const std::string& _path);
+      #if defined(unix) || defined(__APPLE__)
+        static void _removeUnix(const std::string& _path);
+      #endif
+      #ifdef _WIN32
+        static void _removeWindows(const std::string& _path);
+      #endif
       static bool _renameTo(const std::string& _oldName, const std::string& _newName);
       static std::string _replaceWrongSeparator(std::string _path);
       static std::vector<std::string> _splitIntoSubDirectoryNames(const std::string& _path);