fixed filename function

This commit is contained in:
Arvid Norberg 2009-11-02 04:08:37 +00:00
parent 93edeee522
commit e9320371b8
2 changed files with 26 additions and 2 deletions

View File

@ -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);
}

View File

@ -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);