use string_view in create_torrent (#1120)

use string_view in create_torrent
This commit is contained in:
Arvid Norberg 2016-09-21 19:59:43 -07:00 committed by GitHub
parent f712caa80d
commit 269e384c2c
4 changed files with 26 additions and 25 deletions

View File

@ -38,7 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/storage.hpp" #include "libtorrent/storage.hpp"
#include "libtorrent/hasher.hpp" #include "libtorrent/hasher.hpp"
#include "libtorrent/file.hpp" // for combine_path etc. #include "libtorrent/string_view.hpp"
#include <vector> #include <vector>
#include <string> #include <string>
@ -233,8 +233,8 @@ namespace libtorrent
// files of the torrent in it. // files of the torrent in it.
// //
// The second function, ``add_http_seed()`` adds an HTTP seed instead. // The second function, ``add_http_seed()`` adds an HTTP seed instead.
void add_url_seed(std::string const& url); void add_url_seed(string_view url);
void add_http_seed(std::string const& url); void add_http_seed(string_view url);
// This adds a DHT node to the torrent. This especially useful if you're creating a // This adds a DHT node to the torrent. This especially useful if you're creating a
// tracker less torrent. It can be used by clients to bootstrap their DHT node from. // tracker less torrent. It can be used by clients to bootstrap their DHT node from.
@ -248,16 +248,16 @@ namespace libtorrent
// info-hash. The tier is the fallback priority of the tracker. All trackers with tier 0 are // info-hash. The tier is the fallback priority of the tracker. All trackers with tier 0 are
// tried first (in any order). If all fail, trackers with tier 1 are tried. If all of those // tried first (in any order). If all fail, trackers with tier 1 are tried. If all of those
// fail, trackers with tier 2 are tried, and so on. // fail, trackers with tier 2 are tried, and so on.
void add_tracker(std::string const& url, int tier = 0); void add_tracker(string_view url, int tier = 0);
// This function sets an X.509 certificate in PEM format to the torrent. This makes the // This function sets an X.509 certificate in PEM format to the torrent. This makes the
// torrent an *SSL torrent*. An SSL torrent requires that each peer has a valid certificate // torrent an *SSL torrent*. An SSL torrent requires that each peer has a valid certificate
// signed by this root certificate. For SSL torrents, all peers are connecting over SSL // signed by this root certificate. For SSL torrents, all peers are connecting over SSL
// connections. For more information, see the section on ssl-torrents_. // connections. For more information, see the section on ssl-torrents_.
// //
// The string is not the path to the cert, it's the actual content of the certificate, // The string is not the path to the cert, it's the actual content of the
// loaded into a std::string. // certificate.
void set_root_cert(std::string const& pem); void set_root_cert(string_view pem);
// Sets and queries the private flag of the torrent. // Sets and queries the private flag of the torrent.
// Torrents with the private flag set ask clients to not use any other // Torrents with the private flag set ask clients to not use any other
@ -292,7 +292,7 @@ namespace libtorrent
// //
// .. _`BEP 38`: http://www.bittorrent.org/beps/bep_0038.html // .. _`BEP 38`: http://www.bittorrent.org/beps/bep_0038.html
void add_similar_torrent(sha1_hash ih); void add_similar_torrent(sha1_hash ih);
void add_collection(std::string c); void add_collection(string_view c);
private: private:

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/announce_entry.hpp" #include "libtorrent/announce_entry.hpp"
#include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/performance_counters.hpp" // for counters
#include "libtorrent/alert_manager.hpp" #include "libtorrent/alert_manager.hpp"
#include "libtorrent/file.hpp" // for combine_path etc.
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -59,15 +60,15 @@ namespace libtorrent
inline bool ignore_subdir(std::string const& leaf) inline bool ignore_subdir(std::string const& leaf)
{ return leaf == ".." || leaf == "."; } { return leaf == ".." || leaf == "."; }
int get_file_attributes(std::string const& p) int get_file_attributes(string_view p)
{ {
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
WIN32_FILE_ATTRIBUTE_DATA attr; WIN32_FILE_ATTRIBUTE_DATA attr;
#if TORRENT_USE_WSTRING #if TORRENT_USE_WSTRING
std::wstring path = convert_to_wstring(p); std::wstring path = convert_to_wstring(p.to_string());
GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &attr); GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &attr);
#else #else
std::string path = convert_to_native(p); std::string path = convert_to_native(p.to_string());
GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &attr); GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &attr);
#endif // TORRENT_USE_WSTRING #endif // TORRENT_USE_WSTRING
if (attr.dwFileAttributes == INVALID_FILE_ATTRIBUTES) return 0; if (attr.dwFileAttributes == INVALID_FILE_ATTRIBUTES) return 0;
@ -75,7 +76,7 @@ namespace libtorrent
return 0; return 0;
#else #else
struct stat s; struct stat s;
if (lstat(convert_to_native(p).c_str(), &s) < 0) return 0; if (lstat(convert_to_native(p.to_string()).c_str(), &s) < 0) return 0;
int file_attr = 0; int file_attr = 0;
if (s.st_mode & S_IXUSR) if (s.st_mode & S_IXUSR)
file_attr += file_storage::attribute_executable; file_attr += file_storage::attribute_executable;
@ -665,18 +666,18 @@ namespace libtorrent
return dict; return dict;
} }
void create_torrent::add_tracker(std::string const& url, int tier) void create_torrent::add_tracker(string_view url, int const tier)
{ {
m_urls.push_back(announce_entry(url, tier)); m_urls.push_back(announce_entry(url.to_string(), tier));
std::sort(m_urls.begin(), m_urls.end() std::sort(m_urls.begin(), m_urls.end()
, [] (announce_entry const& lhs, announce_entry const& rhs) , [] (announce_entry const& lhs, announce_entry const& rhs)
{ return lhs.second < rhs.second; } ); { return lhs.second < rhs.second; } );
} }
void create_torrent::set_root_cert(std::string const& cert) void create_torrent::set_root_cert(string_view cert)
{ {
m_root_cert = cert; m_root_cert.assign(cert.data(), cert.size());
} }
void create_torrent::add_similar_torrent(sha1_hash ih) void create_torrent::add_similar_torrent(sha1_hash ih)
@ -684,9 +685,9 @@ namespace libtorrent
m_similar.push_back(ih); m_similar.push_back(ih);
} }
void create_torrent::add_collection(std::string c) void create_torrent::add_collection(string_view c)
{ {
m_collections.push_back(c); m_collections.emplace_back(c);
} }
void create_torrent::set_hash(int index, sha1_hash const& h) void create_torrent::set_hash(int index, sha1_hash const& h)
@ -709,14 +710,14 @@ namespace libtorrent
m_nodes.push_back(node); m_nodes.push_back(node);
} }
void create_torrent::add_url_seed(std::string const& url) void create_torrent::add_url_seed(string_view url)
{ {
m_url_seeds.push_back(url); m_url_seeds.emplace_back(url);
} }
void create_torrent::add_http_seed(std::string const& url) void create_torrent::add_http_seed(string_view url)
{ {
m_http_seeds.push_back(url); m_http_seeds.emplace_back(url);
} }
void create_torrent::set_comment(char const* str) void create_torrent::set_comment(char const* str)