diff --git a/src/file.cpp b/src/file.cpp index f24c4da16..d38d7e4e9 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -380,12 +380,34 @@ namespace libtorrent std::string filename(std::string const& f) { - char const* sep = strrchr(f.c_str(), '/'); + if (f.empty()) return ""; + char const* first = f.c_str(); + char const* sep = strrchr(first, '/'); #ifdef TORRENT_WINDOWS - char const* altsep = strrchr(f.c_str(), '\\'); + char const* altsep = strrchr(first, '\\'); if (sep == 0 || altsep > sep) sep = altsep; #endif if (sep == 0) return f; + + if (sep - first == f.size() - 1) + { + // if the last character is a / (or \) + // ignore it + int len = 0; + while (sep > first) + { + --sep; + if (*sep == '/' +#ifdef TORRENT_WINDOWS + || *sep == '\\' +#endif + ) + return std::string(sep + 1, len); + ++len; + } + return std::string(first, len); + + } return std::string(sep + 1); } diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 9d4be485d..9832a8571 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -384,6 +384,8 @@ int test_main() TEST_EQUAL(filename("blah"), "blah"); TEST_EQUAL(filename("/blah/foo/bar"), "bar"); + TEST_EQUAL(filename("/blah/foo/bar/"), "bar"); + TEST_EQUAL(filename("blah/"), "blah"); #ifdef TORRENT_WINDOWS TEST_EQUAL(is_root_path("c:\\blah"), false);