From 2c131dbbc434cc30f2f356e24c3b20ecd9816e11 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 25 Mar 2017 00:01:47 -0400 Subject: [PATCH] optimize client_test path handling (#1845) optimize client_test path handling --- examples/client_test.cpp | 59 ++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 4d9815f6c..0b7225aa9 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -623,8 +623,31 @@ void add_torrent(libtorrent::session& ses files.insert(std::pair(torrent, torrent_handle())); } +std::string make_absolute_path(char const* p) +{ + std::string ret; +#if defined TORRENT_WINDOWS + if (p[0] != '\0' && !(p[1] == ':' && p[2] == '\\')) + { + char* cwd = ::_getcwd(nullptr, 0); + ret = path_append(cwd, p); + std::free(cwd); + } + else ret = p; +#else + if (p[0] != '/') + { + char* cwd = ::getcwd(nullptr, 0); + ret = path_append(cwd, p); + std::free(cwd); + } + else ret = p; +#endif + return ret; +} + std::vector list_dir(std::string path - , bool (*filter_fun)(std::string const&) + , bool (*filter_fun)(lt::string_view) , libtorrent::error_code& ec) { std::vector ret; @@ -642,9 +665,9 @@ std::vector list_dir(std::string path do { - std::string p = fd.cFileName; + lt::string_view p = fd.cFileName; if (filter_fun(p)) - ret.push_back(p); + ret.push_back(p.to_string()); } while (FindNextFileA(handle, &fd)); FindClose(handle); @@ -666,32 +689,21 @@ std::vector list_dir(std::string path { if (dummy == nullptr) break; - std::string p = de.d_name; + lt::string_view p(de.d_name); if (filter_fun(p)) - ret.push_back(p); + ret.push_back(p.to_string()); } closedir(handle); #endif return ret; } -bool filter_fun(std::string const& p) +bool filter_fun(lt::string_view p) { - for (int i = int(p.size()) - 1; i >= 0; --i) - { - if (p[i] == '/') break; -#ifdef TORRENT_WINDOWS - if (p[i] == '\\') break; -#endif - if (p[i] != '.') continue; - return p.compare(i, 8, ".torrent") == 0; - } - return false; + return p.size() > 8 && p.substr(p.size() - 8) == ".torrent"; } -void scan_dir(std::string const& dir_path - , libtorrent::session& ses - , handles_t& files) +void scan_dir(std::string const& dir_path, libtorrent::session& ses, handles_t& files) { std::set valid; @@ -706,10 +718,9 @@ void scan_dir(std::string const& dir_path return; } - for (std::vector::iterator i = ents.begin() - , end(ents.end()); i != end; ++i) + for (auto const& e : ents) { - std::string file = path_append(dir_path, *i); + std::string const file = path_append(dir_path, e); handles_t::iterator k = files.find(file); if (k != files.end()) @@ -1253,10 +1264,10 @@ int main(int argc, char* argv[]) case 'f': g_log_file = std::fopen(arg, "w+"); break; case 'k': settings = high_performance_seed(); --i; break; case 'G': seed_mode = true; --i; break; - case 's': save_path = arg; break; + case 's': save_path = make_absolute_path(arg); break; case 'U': torrent_upload_limit = atoi(arg) * 1000; break; case 'D': torrent_download_limit = atoi(arg) * 1000; break; - case 'm': monitor_dir = arg; break; + case 'm': monitor_dir = make_absolute_path(arg); break; case 'Q': share_mode = true; --i; break; case 't': poll_interval = atoi(arg); break; case 'F': refresh_delay = atoi(arg); break;