Sfoglia il codice sorgente

Implement working directory functionality for File class (Unix)

Patrick-Christopher Mattulat 3 anni fa
parent
commit
8b7b2bc444
2 ha cambiato i file con 25 aggiunte e 0 eliminazioni
  1. 3 0
      include/ls_std/io/File.hpp
  2. 22 0
      source/ls_std/io/File.cpp

+ 3 - 0
include/ls_std/io/File.hpp

@@ -79,6 +79,9 @@ namespace ls_std
       static bool _equals(ls_std::File &_file, ls_std::File &_foreignFile);
       static bool _exists(const std::string &_path);
       static std::string _getParent(const std::string &_path);
+      #if defined(unix) || defined(__APPLE__)
+      static std::string _getWorkingDirectoryUnix();
+      #endif
       #ifdef _WIN32
       static std::string _getWorkingDirectoryWindows();
       #endif

+ 22 - 0
source/ls_std/io/File.cpp

@@ -119,6 +119,7 @@ std::string ls_std::File::getWorkingDirectory()
   std::string workingDirectory{};
 
   #if defined(unix) || defined(__APPLE__)
+  workingDirectory = ls_std::File::_getWorkingDirectoryUnix();
   #endif
   #ifdef _WIN32
   workingDirectory = ls_std::File::_getWorkingDirectoryWindows();
@@ -318,6 +319,27 @@ std::string ls_std::File::_getParent(const std::string &_path)
   return parent;
 }
 
+#if defined(unix) || defined(__APPLE__)
+
+std::string ls_std::File::_getWorkingDirectoryUnix()
+{
+  std::string workingDirectory{};
+  char buffer[PATH_MAX];
+
+  if (getcwd(buffer, sizeof(buffer)) == nullptr)
+  {
+    throw ls_std::FileOperationException{};
+  }
+  else
+  {
+    workingDirectory = std::string(buffer);
+  }
+
+  return workingDirectory;
+}
+
+#endif
+
 #ifdef _WIN32
 
 std::string ls_std::File::_getWorkingDirectoryWindows()