diff --git a/src/File.cc b/src/File.cc index e0e8c5e5..1fd77ab0 100644 --- a/src/File.cc +++ b/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 diff --git a/src/File.h b/src/File.h index 8e4d0f3f..37b1ba49 100644 --- a/src/File.h +++ b/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 diff --git a/src/util.cc b/src/util.cc index 1f9e6d5c..b1ed0c6a 100644 --- a/src/util.cc +++ b/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) diff --git a/test/FileTest.cc b/test/FileTest.cc index 7055ec2f..effa0ab2 100644 --- a/test/FileTest.cc +++ b/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()