Kaynağa Gözat

MinGW32 build: Replace all '\' in path with '/' in util::applyDir()

In MinGW32 build, replace all '\' in path with '/' in
util::applyDir().  Take into account '\' in File::getBasename() and
File::getDirname().
Tatsuhiro Tsujikawa 14 yıl önce
ebeveyn
işleme
dce0667c0b
4 değiştirilmiş dosya ile 45 ekleme ve 6 silme
  1. 14 2
      src/File.cc
  2. 2 0
      src/File.h
  3. 12 3
      src/util.cc
  4. 17 1
      test/FileTest.cc

+ 14 - 2
src/File.cc

@@ -167,7 +167,8 @@ mode_t File::mode()
 
 std::string File::getBasename() const
 {
-  std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
+  std::string::size_type lastSlashIndex =
+    name_.find_last_of(getPathSeparators());
   if(lastSlashIndex == std::string::npos) {
     return name_;
   } else {
@@ -177,7 +178,8 @@ std::string File::getBasename() const
 
 std::string File::getDirname() const
 {
-  std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
+  std::string::size_type lastSlashIndex =
+    name_.find_last_of(getPathSeparators());
   if(lastSlashIndex == std::string::npos) {
     if(name_.empty()) {
       return A2STR::NIL;
@@ -252,4 +254,14 @@ std::string File::getCurrentDir()
 #endif // !__MINGW32__
 }
 
+const std::string& File::getPathSeparators()
+{
+#ifdef __MINGW32__
+  static std::string s = "/\\";
+#else // !__MINGW32__
+  static std::string s = "/";
+#endif // !__MINGW32__
+  return s;
+}
+
 } // namespace aria2

+ 2 - 0
src/File.h

@@ -119,6 +119,8 @@ public:
   // directory cannot be retrieved or its length is larger than 2048,
   // returns ".".
   static std::string getCurrentDir();
+  // Returns possible path separators for the underlining platform.
+  static const std::string& getPathSeparators();
 };
 
 } // namespace aria2

+ 12 - 3
src/util.cc

@@ -1420,13 +1420,22 @@ bool saveAs
 
 std::string applyDir(const std::string& dir, const std::string& relPath)
 {
+  std::string s;
   if(dir.empty()) {
-    return strconcat(A2STR::DOT_C, A2STR::SLASH_C, relPath);
+    s = strconcat(A2STR::DOT_C, A2STR::SLASH_C, relPath);
   } else if(dir == A2STR::SLASH_C) {
-    return strconcat(A2STR::SLASH_C, relPath);
+    s = strconcat(A2STR::SLASH_C, relPath);
   } else {
-    return strconcat(dir, A2STR::SLASH_C, relPath);
+    s = strconcat(dir, A2STR::SLASH_C, relPath);
   }
+#ifdef __MINGW32__
+  for(std::string::iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
+    if(*i == '\\') {
+      *i = '/';
+    }
+  }
+#endif // __MINGW32__
+  return s;
 }
 
 std::string fixTaintedBasename(const std::string& src)

+ 17 - 1
test/FileTest.cc

@@ -177,7 +177,13 @@ void FileTest::testGetDirname()
   {
     File f("");
     CPPUNIT_ASSERT_EQUAL(std::string(""), f.getDirname());
-  }  
+  }
+#ifdef __MINGW32__
+  {
+    File f("c:\\foo\\bar");
+    CPPUNIT_ASSERT_EQUAL(std::string("c:\\foo"), f.getDirname());
+  }
+#endif // __MINGW32__
 }
 
 void FileTest::testGetBasename()
@@ -210,6 +216,16 @@ void FileTest::testGetBasename()
     File f("");
     CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
   }
+#ifdef __MINGW32__
+  {
+    File f("c:\\foo\\bar");
+    CPPUNIT_ASSERT_EQUAL(std::string("bar"), f.getBasename());
+  }
+  {
+    File f("c:\\foo\\");
+    CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
+  }
+#endif // __MINGW32__
 }
 
 void FileTest::testRenameTo()