Просмотр исходного кода

Implement working directory functionality for File class (Win)

Patrick-Christopher Mattulat 3 лет назад
Родитель
Сommit
3645b6b8be
3 измененных файлов с 54 добавлено и 7 удалено
  1. 11 5
      include/ls_std/io/File.hpp
  2. 36 1
      source/ls_std/io/File.cpp
  3. 7 1
      test/cases/io/FileTest.cpp

+ 11 - 5
include/ls_std/io/File.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2021-05-01
+ * Changed:         2021-09-26
  *
  * */
 
@@ -24,7 +24,9 @@
 #endif
 
 #ifdef _WIN32
+
 #include <windows.h>
+
 #endif
 
 namespace ls_std
@@ -51,6 +53,7 @@ namespace ls_std
       std::string getAbsoluteFilePath();
       std::string getName();
       std::string getParent();
+      static std::string getWorkingDirectory();
       long getSize();
       bool isDirectory();
       bool isFile();
@@ -71,11 +74,14 @@ namespace ls_std
       static void _addToFileListUnix(const std::string &_path, bool _withDirectories, dirent *directoryEntity, std::list<std::string> &_list);
       #endif
       #ifdef _WIN32
-      static void _addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list);
+      static void _addToFileListWindows(const std::string &_path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string> &_list);
       #endif
       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);
+      #ifdef _WIN32
+      static std::string _getWorkingDirectoryWindows();
+      #endif
       static bool _isDirectory(const std::string &_path);
       static bool _isExecutable(const std::string &_path);
       static bool _isFile(const std::string &_path);
@@ -83,7 +89,7 @@ namespace ls_std
       static bool _isReadableUnix(const std::string &_path);
       #endif
       #ifdef _WIN32
-      static bool _isReadableWindows(const std::string& _path);
+      static bool _isReadableWindows(const std::string &_path);
       #endif
       static bool _isWritable(const std::string &_path);
       static time_t _lastModified(const std::string &_path);
@@ -93,7 +99,7 @@ namespace ls_std
       static std::list<std::string> _listUnix(const std::string &_path, bool withDirectories);
       #endif
       #ifdef _WIN32
-      static std::list<std::string> _listWindows(const std::string& _path, bool withDirectories);
+      static std::list<std::string> _listWindows(const std::string &_path, bool withDirectories);
       #endif
       static int _mkdir(const std::string &_path);
       static std::string _normalizePath(std::string _path);
@@ -103,7 +109,7 @@ namespace ls_std
       static void _removeUnix(const std::string &_path);
       #endif
       #ifdef _WIN32
-      static void _removeWindows(const std::string& _path);
+      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);

+ 36 - 1
source/ls_std/io/File.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2021-09-24
+ * Changed:         2021-09-26
  *
  * */
 
@@ -26,6 +26,7 @@
 #ifdef _WIN32
 
 #include <direct.h>
+#include <tchar.h>
 
 #endif
 
@@ -113,6 +114,19 @@ std::string ls_std::File::getParent()
   return ls_std::File::_getParent(this->absoluteFilePath);
 }
 
+std::string ls_std::File::getWorkingDirectory()
+{
+  std::string workingDirectory{};
+
+  #if defined(unix) || defined(__APPLE__)
+  #endif
+  #ifdef _WIN32
+  workingDirectory = ls_std::File::_getWorkingDirectoryWindows();
+  #endif
+
+  return workingDirectory;
+}
+
 long ls_std::File::getSize()
 {
   std::streampos fileSize{};
@@ -304,6 +318,27 @@ std::string ls_std::File::_getParent(const std::string &_path)
   return parent;
 }
 
+#ifdef _WIN32
+
+std::string ls_std::File::_getWorkingDirectoryWindows()
+{
+  std::string workingDirectory{};
+  TCHAR buffer[MAX_PATH];
+
+  if (!GetCurrentDirectory(MAX_PATH, buffer))
+  {
+    throw ls_std::FileOperationException{};
+  }
+  else
+  {
+    workingDirectory = std::string(buffer);
+  }
+
+  return workingDirectory;
+}
+
+#endif
+
 bool ls_std::File::_isDirectory(const std::string &_path)
 {
   bool match{};

+ 7 - 1
test/cases/io/FileTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2021-09-17
+ * Changed:         2021-09-26
  *
  * */
 
@@ -187,6 +187,12 @@ namespace
     ASSERT_STREQ(TestHelper::getResourcesFolderLocation().c_str(), file.getParent().c_str());
   }
 
+  TEST_F(FileTest, getWorkingDirectory)
+  {
+    std::string workingDirectory = ls_std::File::getWorkingDirectory();
+    ASSERT_FALSE(workingDirectory.empty());
+  }
+
   TEST_F(FileTest, getSize)
   {
     ls_std::File file{this->fileLocation};