Browse Source

Implement working directory functionality for File class (Unix)

Patrick-Christopher Mattulat 2 months ago
parent
commit
8b7b2bc444
2 changed files with 25 additions and 0 deletions
  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()