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().
pull/2/head
Tatsuhiro Tsujikawa 2011-08-18 17:27:41 +09:00
parent 58c5dc7928
commit dce0667c0b
4 changed files with 45 additions and 6 deletions

View File

@ -167,7 +167,8 @@ mode_t File::mode()
std::string File::getBasename() const 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) { if(lastSlashIndex == std::string::npos) {
return name_; return name_;
} else { } else {
@ -177,7 +178,8 @@ std::string File::getBasename() const
std::string File::getDirname() 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(lastSlashIndex == std::string::npos) {
if(name_.empty()) { if(name_.empty()) {
return A2STR::NIL; return A2STR::NIL;
@ -252,4 +254,14 @@ std::string File::getCurrentDir()
#endif // !__MINGW32__ #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 } // namespace aria2

View File

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

View File

@ -1420,13 +1420,22 @@ bool saveAs
std::string applyDir(const std::string& dir, const std::string& relPath) std::string applyDir(const std::string& dir, const std::string& relPath)
{ {
std::string s;
if(dir.empty()) { 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) { } else if(dir == A2STR::SLASH_C) {
return strconcat(A2STR::SLASH_C, relPath); s = strconcat(A2STR::SLASH_C, relPath);
} else { } 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) std::string fixTaintedBasename(const std::string& src)

View File

@ -178,6 +178,12 @@ void FileTest::testGetDirname()
File f(""); File f("");
CPPUNIT_ASSERT_EQUAL(std::string(""), f.getDirname()); 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() void FileTest::testGetBasename()
@ -210,6 +216,16 @@ void FileTest::testGetBasename()
File f(""); File f("");
CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename()); 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() void FileTest::testRenameTo()