summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-08-18 17:27:41 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-08-18 17:27:41 +0900
commitdce0667c0be31a8dbaee3f6ec7e223d11ec9f7dd (patch)
treea737910f43f2f4533e205fe19accb05031d3c560
parent58c5dc792887fb53ad7718d09567e7c72ef1fc7a (diff)
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().
-rw-r--r--src/File.cc16
-rw-r--r--src/File.h2
-rw-r--r--src/util.cc15
-rw-r--r--test/FileTest.cc18
4 files changed, 45 insertions, 6 deletions
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()