Bläddra i källkod

Improved File class

- implemented TODO by extending "_normalizePath" method
 which is now reducing multiple occurrences of path
 separators
- extended tests for File class
Patrick 4 år sedan
förälder
incheckning
1f7d75392e
3 ändrade filer med 48 tillägg och 9 borttagningar
  1. 38 8
      source/io/File.cpp
  2. 3 0
      source/io/File.hpp
  3. 7 1
      test/cases/io/FileTest.cpp

+ 38 - 8
source/io/File.cpp

@@ -358,11 +358,46 @@ int ls_std::File::_mkdir(const std::string& _path) {
   return result;
 }
 
-// TODO: also consider "/////" formatted paths, which would have to be converted to "/"
 std::string ls_std::File::_normalizePath(std::string _path)
 {
-  const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
-  const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
+  _path = ls_std::File::_replaceWrongSeparator(_path);
+  _path = ls_std::File::_reduceSeparators(_path);
+
+  return _path;
+}
+
+std::string ls_std::File::_reduceSeparators(const std::string& _path)
+{
+  static const char separator = {ls_std::FilePathSeparator::get()};
+  std::string normalizedPath {};
+  int index {};
+
+  while(index  < _path.size()) {
+    if(_path[index] == separator) {
+      normalizedPath += _path[index];
+
+      do {
+        index++;
+      }
+      while(_path[index] == separator);
+    } else {
+      normalizedPath += _path[index];
+      index++;
+    }
+  }
+
+  return normalizedPath;
+}
+
+bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
+{
+  return std::rename(_oldName.c_str(), _newName.c_str()) == 0;
+}
+
+std::string ls_std::File::_replaceWrongSeparator(std::string _path)
+{
+  static const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
+  static const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
 
   #if defined(unix) || defined(__APPLE__)
     std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
@@ -375,11 +410,6 @@ std::string ls_std::File::_normalizePath(std::string _path)
   return _path;
 }
 
-bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
-{
-  return std::rename(_oldName.c_str(), _newName.c_str()) == 0;
-}
-
 std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string& _path) {
   std::vector<std::string> subDirectoryNames {};
   std::stringstream _stream {_path};

+ 3 - 0
source/io/File.hpp

@@ -15,6 +15,7 @@
 #include <vector>
 #include <list>
 #include <ctime>
+#include <regex>
 
 #if defined(unix) || defined(__APPLE__)
 #include <dirent.h>
@@ -80,7 +81,9 @@ namespace ls_std {
       #endif
       static int _mkdir(const std::string& _path);
       static std::string _normalizePath(std::string _path);
+      static std::string _reduceSeparators(const std::string& _path);
       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);
   };
 }

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

@@ -100,7 +100,13 @@ namespace {
   TEST_F(FileTest, getAbsoluteFilePath)
   {
     ls_std::File file {this->fileLocation};
-    ASSERT_STREQ(fileLocation.c_str(), file.getAbsoluteFilePath().c_str());
+    ASSERT_STREQ(this->fileLocation.c_str(), file.getAbsoluteFilePath().c_str());
+    std::string s = {ls_std::FilePathSeparator::get()};
+
+    std::string wrongFilePath = "home" + s + s + s + "user" + s + "bla" + s + s + "sub_folder";
+    std::string expectedFilePath = "home" + s + "user" + s + "bla" + s + "sub_folder";
+    ls_std::File anotherFile {wrongFilePath};
+    ASSERT_STREQ(expectedFilePath.c_str(), anotherFile.getAbsoluteFilePath().c_str());
   }
 
   TEST_F(FileTest, getName)