forked from premiere/premiere-libtorrent
fixed filename function
This commit is contained in:
parent
93edeee522
commit
e9320371b8
26
src/file.cpp
26
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue