added first file abstraction interface.

This commit is contained in:
Arvid Norberg 2004-01-15 23:57:11 +00:00
parent da5b07ecd8
commit 85ed5e1593
8 changed files with 123 additions and 22 deletions

View File

@ -413,9 +413,9 @@ public:
<p>This class will need some explanation. First of all, to get a list of all files
in the torrent, you can use <tt class="literal"><span class="pre">begin_files()</span></tt>, <tt class="literal"><span class="pre">end_files()</span></tt>,
<tt class="literal"><span class="pre">rbegin_files()</span></tt> and <tt class="literal"><span class="pre">rend_files()</span></tt>. These will give you standard vector
iterators with the type <tt class="literal"><span class="pre">file</span></tt>.</p>
iterators with the type <tt class="literal"><span class="pre">file_entry</span></tt>.</p>
<pre class="literal-block">
struct file
struct file_entry
{
std::string path;
std::string filename;
@ -548,7 +548,7 @@ struct torrent_status
int num_peers;
std::vector&lt;bool&gt; pieces;
const std::vector&lt;bool&gt;* pieces;
std::size_t total_done;
};
</pre>
@ -592,8 +592,9 @@ uploaded to all peers, accumulated, <em>this session</em> only.</p>
<p><tt class="literal"><span class="pre">total_payload_download</span></tt> and <tt class="literal"><span class="pre">total_payload_upload</span></tt> counts the amount of bytes
send and received this session, but only the actual oayload data (i.e the interesting
data), these counters ignore any protocol overhead.</p>
<p><tt class="literal"><span class="pre">pieces</span></tt> is the bitmask that representw which pieces we have (set to true) and
the pieces we don't have.</p>
<p><tt class="literal"><span class="pre">pieces</span></tt> is the bitmask that represents which pieces we have (set to true) and
the pieces we don't have. It's a pointer and may be set to 0 if the torrent isn't
downloading or seeding.</p>
<p><tt class="literal"><span class="pre">download_rate</span></tt> and <tt class="literal"><span class="pre">upload_rate</span></tt> are the total rates for all peers for this
torrent. These will usually have better precision than summing the rates from
all peers. The rates are given as the number of bytes per second.</p>

View File

@ -395,11 +395,11 @@ The ``torrent_info`` has the following synopsis::
This class will need some explanation. First of all, to get a list of all files
in the torrent, you can use ``begin_files()``, ``end_files()``,
``rbegin_files()`` and ``rend_files()``. These will give you standard vector
iterators with the type ``file``.
iterators with the type ``file_entry``.
::
struct file
struct file_entry
{
std::string path;
std::string filename;

99
include/libtorrent/file.hpp Executable file
View File

@ -0,0 +1,99 @@
/*
Copyright (c) 2003, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_FILE_HPP_INCLUDED
#define TORRENT_FILE_HPP_INCLUDED
#include <memory>
#include <boost/noncopyable.hpp>
#include <boost/filesystem/path.hpp>
namespace libtorrent
{
struct file_error: std::runtime_error
{
file_error(std::string const& msg): std::runtime_error(msg) {}
};
class file: public boost::noncopyable
{
public:
#ifdef _MSC_VER
typedef _int64 size_type;
#else
typedef long long int size_type;
#endif
class open_mode
{
friend file;
public:
open_mode operator|(open_mode m)
{ return open_mode(m.m_mask | m_mask); }
private:
open_mode(int val): m_mask(val) {}
int m_mask;
};
static open_mode in;
static open_mode out;
file();
file(boost::filesystem::path const& p, open_mode m);
~file();
void open(boost::filesystem::path const& p, open_mode m);
void close();
size_type write(const char*, size_type num_bytes);
size_type read(char*, size_type num_bytes);
void seek(size_type pos);
size_type tell();
private:
struct impl;
const std::auto_ptr<impl> m_impl;
};
}
#endif // TORRENT_FILE_HPP_INCLUDED

View File

@ -242,5 +242,5 @@ namespace libtorrent
}
#endif // TORRENT_SOCKET_WIN_INCLUDED
#endif // TORRENT_SOCKET_HPP_INCLUDED

View File

@ -54,7 +54,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
struct file
struct file_entry
{
std::string path;
std::string filename;
@ -96,8 +96,8 @@ namespace libtorrent
}
}
typedef std::vector<file>::const_iterator file_iterator;
typedef std::vector<file>::const_reverse_iterator reverse_file_iterator;
typedef std::vector<file_entry>::const_iterator file_iterator;
typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
// list the files in the torrent file
file_iterator begin_files() const { return m_files.begin(); }
@ -106,7 +106,7 @@ namespace libtorrent
reverse_file_iterator rend_files() const { return m_files.rend(); }
std::size_t num_files() const { return m_files.size(); }
const file& file_at(int index) const { assert(index >= 0 && index < (int)m_files.size()); return m_files[index]; }
const file_entry& file_at(int index) const { assert(index >= 0 && index < (int)m_files.size()); return m_files[index]; }
const std::vector<announce_entry>& trackers() const { return m_urls; }
@ -161,7 +161,7 @@ namespace libtorrent
std::vector<sha1_hash> m_piece_hash;
// the list of files that this torrent consists of
std::vector<file> m_files;
std::vector<file_entry> m_files;
// the sum of all filesizes
entry::integer_type m_total_size;

View File

@ -1477,7 +1477,7 @@ namespace libtorrent
assert(r.length > 0 && r.start >= 0);
#ifndef NDEBUG
assert(m_torrent->verify_piece(r.piece) && "internal error");
// assert(m_torrent->verify_piece(r.piece) && "internal error");
#endif
const int send_buffer_offset = m_send_buffer.size();
const int packet_size = 4 + 5 + 4 + r.length;

View File

@ -50,6 +50,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/hasher.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/peer_id.hpp"
#include "libtorrent/file.hpp"
#if defined(_MSC_VER)
#define for if (false) {} else for
@ -185,7 +186,7 @@ namespace libtorrent {
// find the file iterator and file offset
size_type file_offset = start;
std::vector<file>::const_iterator file_iter;
std::vector<file_entry>::const_iterator file_iter;
for (file_iter = m_pimpl->info.begin_files();;)
{
@ -261,7 +262,7 @@ namespace libtorrent {
// find the file iterator and file offset
size_type file_offset = start;
std::vector<file>::const_iterator file_iter;
std::vector<file_entry>::const_iterator file_iter;
for (file_iter = m_pimpl->info.begin_files();;)
{

View File

@ -50,7 +50,7 @@ using namespace libtorrent;
namespace
{
void extract_single_file(const entry::dictionary_type& dict, file& target)
void extract_single_file(const entry::dictionary_type& dict, file_entry& target)
{
entry::dictionary_type::const_iterator i = dict.find("length");
if (i == dict.end()) throw invalid_torrent_file();
@ -69,11 +69,11 @@ namespace
target.filename = list.back().string();
}
void extract_files(const entry::list_type& list, std::vector<file>& target, const std::string& root_directory)
void extract_files(const entry::list_type& list, std::vector<file_entry>& target, const std::string& root_directory)
{
for (entry::list_type::const_iterator i = list.begin(); i != list.end(); ++i)
{
target.push_back(file());
target.push_back(file_entry());
target.back().path = root_directory;
extract_single_file(i->dict(), target.back());
}
@ -161,7 +161,7 @@ namespace libtorrent
i = info.dict().find("length");
if (i == info.dict().end()) throw invalid_torrent_file();
m_files.push_back(file());
m_files.push_back(file_entry());
m_files.back().filename = m_name;
m_files.back().size = i->second.integer();
}
@ -186,7 +186,7 @@ namespace libtorrent
// calculate total size of all pieces
m_total_size = 0;
for (std::vector<file>::iterator i = m_files.begin(); i != m_files.end(); ++i)
for (std::vector<file_entry>::iterator i = m_files.begin(); i != m_files.end(); ++i)
m_total_size += i->size;
// extract sha-1 hashes for all pieces
@ -206,7 +206,7 @@ namespace libtorrent
void torrent_info::convert_file_names()
{
for (std::vector<file>::iterator i = m_files.begin(); i != m_files.end(); ++i)
for (std::vector<file_entry>::iterator i = m_files.begin(); i != m_files.end(); ++i)
{
// replace all dots in directory names with underscores
std::string& path = i->path;