added constructor to torrent_info that loads a file directly

This commit is contained in:
Arvid Norberg 2008-05-12 06:35:24 +00:00
parent 2fbe53f880
commit 149fa28586
3 changed files with 44 additions and 4 deletions

View File

@ -1089,6 +1089,7 @@ The ``torrent_info`` has the following synopsis::
torrent_info(); torrent_info();
torrent_info(sha1_hash const& info_hash); torrent_info(sha1_hash const& info_hash);
torrent_info(entry const& torrent_file); torrent_info(entry const& torrent_file);
torrent_info(char const* filename);
entry create_torrent() const; entry create_torrent() const;
void set_comment(char const* str); void set_comment(char const* str);
@ -1153,6 +1154,7 @@ torrent_info()
torrent_info(); torrent_info();
torrent_info(sha1_hash const& info_hash); torrent_info(sha1_hash const& info_hash);
torrent_info(entry const& torrent_file); 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 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 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 when downloading torrents without the metadata. The metadata will be created by libtorrent
as soon as it has been downloaded from the swarm. 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`` The constructor that takes an entry, is the one that is used in most cases. It will create
object from the information found in the given torrent_file. The ``entry`` represents a tree a ``torrent_info`` object from the information found in the given torrent_file. The
node in an bencoded file. To load an ordinary .torrent file into an ``entry``, use bdecode(), ``entry`` represents a tree node in an bencoded file. To load an ordinary .torrent file
see `bdecode() bencode()`_. 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() set_comment() set_piece_size() set_creator() set_hash() add_tracker() add_file()
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -111,6 +111,7 @@ namespace libtorrent
torrent_info(); torrent_info();
torrent_info(sha1_hash const& info_hash); torrent_info(sha1_hash const& info_hash);
torrent_info(entry const& torrent_file); torrent_info(entry const& torrent_file);
torrent_info(char const* filename);
~torrent_info(); ~torrent_info();
entry create_torrent() const; entry create_torrent() const;

View File

@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include <boost/filesystem.hpp>
#include <boost/next_prior.hpp> #include <boost/next_prior.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
@ -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<char> 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() torrent_info::~torrent_info()
{} {}