diff --git a/docs/manual.rst b/docs/manual.rst index fc8c0a868..372715846 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -1089,6 +1089,7 @@ The ``torrent_info`` has the following synopsis:: torrent_info(); torrent_info(sha1_hash const& info_hash); torrent_info(entry const& torrent_file); + torrent_info(char const* filename); entry create_torrent() const; void set_comment(char const* str); @@ -1153,6 +1154,7 @@ torrent_info() torrent_info(); torrent_info(sha1_hash const& info_hash); torrent_info(entry const& torrent_file); + torrent_info(char const* filename); The default constructor of ``torrent_info`` is used when creating torrent files. It will initialize the object to an empty torrent, containing no files. The info hash will be set @@ -1166,10 +1168,15 @@ exception that it will initialize the info-hash to the given value. This is used when downloading torrents without the metadata. The metadata will be created by libtorrent as soon as it has been downloaded from the swarm. -The last constructor is the one that is used in most cases. It will create a ``torrent_info`` -object from the information found in the given torrent_file. The ``entry`` represents a tree -node in an bencoded file. To load an ordinary .torrent file into an ``entry``, use bdecode(), -see `bdecode() bencode()`_. +The constructor that takes an entry, is the one that is used in most cases. It will create +a ``torrent_info`` object from the information found in the given torrent_file. The +``entry`` represents a tree node in an bencoded file. To load an ordinary .torrent file +into an ``entry``, use bdecode(), see `bdecode() bencode()`_. + +The version that takes a filename will simply load the torrent file and decode it inside +the constructor, for convenience. This might not be the most suitable for applications that +want to be able to report detailed errors on what might go wrong. + set_comment() set_piece_size() set_creator() set_hash() add_tracker() add_file() -------------------------------------------------------------------------------- diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index 4a520300b..83873c17f 100755 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -111,6 +111,7 @@ namespace libtorrent torrent_info(); torrent_info(sha1_hash const& info_hash); torrent_info(entry const& torrent_file); + torrent_info(char const* filename); ~torrent_info(); entry create_torrent() const; diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index f1d3e50b2..c6e2e0e8c 100755 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include #include #include @@ -286,6 +287,37 @@ namespace libtorrent { } + torrent_info::torrent_info(char const* filename) + : m_num_pieces(0) + , m_creation_date(pt::ptime(pt::not_a_date_time)) + , m_multifile(false) + , m_private(false) + , m_extra_info(entry::dictionary_t) +#ifndef NDEBUG + , m_half_metadata(false) +#endif + { + size_type s = fs::file_size(fs::path(filename)); + // don't load torrent files larger than 2 MB + if (s > 2000000) return; + std::vector buf(s); + std::ifstream f(filename); + f.read(&buf[0], s); + /* + lazy_entry e; + int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e); + if (ret != 0) return; + */ + entry e = bdecode(&buf[0], &buf[0] + buf.size()); + std::string error; +#ifndef BOOST_NO_EXCEPTIONS + if (!read_torrent_info(e, error)) + throw invalid_torrent_file(); +#else + read_torrent_info(e, error); +#endif + } + torrent_info::~torrent_info() {}