added wpath and wstring overloads for functions dealing with paths for more complete wide character support. #432

This commit is contained in:
Arvid Norberg 2008-11-30 08:12:26 +00:00
parent aab4876ec3
commit 8e48e81ead
7 changed files with 139 additions and 14 deletions

View File

@ -59,8 +59,11 @@ add_files
template <class Pred>
void add_files(file_storage& fs, boost::filesystem::path const& path, Pred p);
template <class Pred>
void add_files(file_storage& fs, boost::filesystem::wpath const& path, Pred p);
void add_files(file_storage& fs, boost::filesystem::path const& path);
void add_files(file_storage& fs, boost::filesystem::wpath const& path);
Adds the file specified by ``path`` to the ``file_storage`` object. In case ``path``
refers to a diretory, files will be added recursively from the directory.
@ -71,6 +74,10 @@ which ``p`` returns true are traversed. ``p`` must have the following signature:
bool Pred(boost::filesystem::path const& p);
and for the wpath version::
bool Pred(boost::filesystem::wpath const& p);
The path that is passed in to the predicate is the full path of the file or
directory. If no predicate is specified, all files are added, and all directories
are traveresed.
@ -84,8 +91,11 @@ set_piece_hashes()
template <class Fun>
void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f);
template <class Fun>
void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f);
void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p);
void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p);
This function will assume that the files added to the torrent file exists at path
``p``, read those files and hash the content and set the hashes in the ``create_torrent``
@ -109,7 +119,9 @@ file structure. Its synopsis::
void add_file(file_entry const& e);
void add_file(fs::path const& p, size_type size);
void add_file(fs::wpath const& p, size_type size);
void rename_file(int index, std::string const& new_filename);
void rename_file(int index, std::wstring const& new_filename);
std::vector<file_slice> map_block(int piece, size_type offset
, int size) const;
@ -134,6 +146,7 @@ file structure. Its synopsis::
int piece_size(int index) const;
void set_name(std::string const& n);
void set_name(std::wstring const& n);
const std::string& name() const;
void swap(file_storage& ti);

View File

@ -1710,7 +1710,9 @@ Its declaration looks like this::
boost::filesystem::path save_path() const;
void move_storage(boost::filesystem::path const& save_path) const;
void move_storage(boost::filesystem::wpath const& save_path) const;
void rename_file(int index, boost::filesystem::path) const;
void rename_file(int index, boost::filesystem::wpath) const;
storage_interface* get_storage_impl() const;
sha1_hash info_hash() const;
@ -1831,6 +1833,7 @@ move_storage()
::
void move_storage(boost::filesystem::path const& save_path) const;
void move_storage(boost::filesystem::wpath const& save_path) const;
Moves the file(s) that this torrent are currently seeding from or downloading to. If
the given ``save_path`` is not located on the same drive as the original save path,
@ -1848,6 +1851,7 @@ rename_file()
::
void rename_file(int index, boost::filesystem::path) const;
void rename_file(int index, boost::filesystem::wpath) const;
Renames the file with the given index asynchronously. The rename operation is complete
when either a ``file_renamed_alert`` or ``file_rename_failed_alert`` is posted.

View File

@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/utf8.hpp"
#include <vector>
#include <string>
@ -140,26 +141,33 @@ namespace libtorrent
namespace detail
{
inline bool default_pred(boost::filesystem::path const&) { return true; }
inline bool wdefault_pred(boost::filesystem::wpath const&) { return true; }
inline bool ignore_subdir(std::string const& leaf)
{ return leaf == ".." || leaf == "."; }
inline bool ignore_subdir(std::wstring const& leaf)
{ return leaf == L".." || leaf == L"."; }
inline void nop(int i) {}
template <class Pred>
void add_files_impl(file_storage& fs, boost::filesystem::path const& p
, boost::filesystem::path const& l, Pred pred)
template <class Pred, class Str, class PathTraits>
void add_files_impl(file_storage& fs, boost::filesystem::basic_path<Str, PathTraits> const& p
, boost::filesystem::basic_path<Str, PathTraits> const& l, Pred pred)
{
using boost::filesystem::path;
using boost::filesystem::directory_iterator;
using boost::filesystem::basic_path;
using boost::filesystem::basic_directory_iterator;
#if BOOST_VERSION < 103600
std::string const& leaf = l.leaf();
Str const& leaf = l.leaf();
#else
std::string const& leaf = l.filename();
Str const& leaf = l.filename();
#endif
if (leaf == ".." || leaf == ".") return;
if (ignore_subdir(leaf)) return;
if (!pred(l)) return;
path f(p / l);
basic_path<Str, PathTraits> f(p / l);
if (is_directory(f))
{
for (directory_iterator i(f), end; i != end; ++i)
for (basic_directory_iterator<basic_path<Str, PathTraits> > i(f), end; i != end; ++i)
#if BOOST_VERSION < 103600
add_files_impl(fs, p, l / i->path().leaf(), pred);
#else
@ -173,22 +181,26 @@ namespace libtorrent
}
}
// path versions
template <class Pred>
void add_files(file_storage& fs, boost::filesystem::path const& file, Pred p)
{
using boost::filesystem::path;
#if BOOST_VERSION < 103600
detail::add_files_impl(fs, complete(file).branch_path(), file.leaf(), p);
detail::add_files_impl(fs, complete(file).branch_path(), path(file.leaf()), p);
#else
detail::add_files_impl(fs, complete(file).parent_path(), file.filename(), p);
detail::add_files_impl(fs, complete(file).parent_path(), path(file.filename()), p);
#endif
}
inline void add_files(file_storage& fs, boost::filesystem::path const& file)
{
using boost::filesystem::path;
#if BOOST_VERSION < 103600
detail::add_files_impl(fs, complete(file).branch_path(), file.leaf(), detail::default_pred);
detail::add_files_impl(fs, complete(file).branch_path(), path(file.leaf()), detail::default_pred);
#else
detail::add_files_impl(fs, complete(file).parent_path(), file.filename(), detail::default_pred);
detail::add_files_impl(fs, complete(file).parent_path(), path(file.filename()), detail::default_pred);
#endif
}
@ -218,6 +230,56 @@ namespace libtorrent
set_piece_hashes(t, p, detail::nop);
}
// wpath versions
template <class Pred>
void add_files(file_storage& fs, boost::filesystem::wpath const& file, Pred p)
{
using boost::filesystem::wpath;
#if BOOST_VERSION < 103600
detail::add_files_impl(fs, complete(file).branch_path(), wpath(file.leaf()), p);
#else
detail::add_files_impl(fs, complete(file).parent_path(), wpath(file.filename()), p);
#endif
}
inline void add_files(file_storage& fs, boost::filesystem::wpath const& file)
{
using boost::filesystem::wpath;
#if BOOST_VERSION < 103600
detail::add_files_impl(fs, complete(file).branch_path(), wpath(file.leaf()), detail::wdefault_pred);
#else
detail::add_files_impl(fs, complete(file).parent_path(), wpath(file.filename()), detail::wdefault_pred);
#endif
}
template <class Fun>
void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p, Fun f)
{
file_pool fp;
std::string utf8;
wchar_utf8(p.string(), utf8);
boost::scoped_ptr<storage_interface> st(
default_storage_constructor(const_cast<file_storage&>(t.files()), utf8, fp));
// calculate the hash for all pieces
int num = t.num_pieces();
std::vector<char> buf(t.piece_length());
for (int i = 0; i < num; ++i)
{
// read hits the disk and will block. Progress should
// be updated in between reads
st->read(&buf[0], i, 0, t.piece_size(i));
hasher h(&buf[0], t.piece_size(i));
t.set_hash(i, h.final());
f(i);
}
}
inline void set_piece_hashes(create_torrent& t, boost::filesystem::wpath const& p)
{
set_piece_hashes(t, p, detail::nop);
}
}
#endif

View File

@ -86,7 +86,9 @@ namespace libtorrent
void add_file(file_entry const& e);
void add_file(fs::path const& p, size_type size, bool pad_file = false);
void add_file(fs::wpath const& p, size_type size, bool pad_file = false);
void rename_file(int index, std::string const& new_filename);
void rename_file(int index, std::wstring const& new_filename);
std::vector<file_slice> map_block(int piece, size_type offset
, int size) const;
@ -117,6 +119,7 @@ namespace libtorrent
int piece_size(int index) const;
void set_name(std::string const& n) { m_name = n; }
void set_name(std::wstring const& n);
const std::string& name() const { TORRENT_ASSERT(m_piece_length > 0); return m_name; }
void swap(file_storage& ti)

View File

@ -472,7 +472,9 @@ namespace libtorrent
// post condition: save_path() == save_path if true is returned
void move_storage(fs::path const& save_path) const;
void move_storage(fs::wpath const& save_path) const;
void rename_file(int index, fs::path const& new_name) const;
void rename_file(int index, fs::wpath const& new_name) const;
sha1_hash info_hash() const;

View File

@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/pch.hpp"
#include "libtorrent/file_storage.hpp"
#include "libtorrent/utf8.hpp"
namespace libtorrent
@ -58,12 +59,27 @@ namespace libtorrent
return piece_length();
}
void file_storage::set_name(std::wstring const& n)
{
std::string utf8;
wchar_utf8(n, utf8);
m_name = utf8;
}
void file_storage::rename_file(int index, std::string const& new_filename)
{
TORRENT_ASSERT(index >= 0 && index < int(m_files.size()));
m_files[index].path = new_filename;
}
void file_storage::rename_file(int index, std::wstring const& new_filename)
{
TORRENT_ASSERT(index >= 0 && index < int(m_files.size()));
std::string utf8;
wchar_utf8(new_filename, utf8);
m_files[index].path = utf8;
}
file_storage::iterator file_storage::file_at_offset(size_type offset) const
{
// TODO: do a binary search
@ -128,6 +144,13 @@ namespace libtorrent
return ret;
}
void file_storage::add_file(fs::wpath const& file, size_type size, bool pad_file)
{
std::string utf8;
wchar_utf8(file.string(), utf8);
add_file(utf8, size, pad_file);
}
void file_storage::add_file(fs::path const& file, size_type size, bool pad_file)
{
TORRENT_ASSERT(size >= 0);

View File

@ -65,6 +65,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/session.hpp"
#include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/invariant_check.hpp"
#include "libtorrent/utf8.hpp"
#if defined(_MSC_VER) && _MSC_VER < 1300
namespace std
@ -221,12 +222,29 @@ namespace libtorrent
TORRENT_FORWARD(move_storage(save_path));
}
void torrent_handle::move_storage(
fs::wpath const& save_path) const
{
INVARIANT_CHECK;
std::string utf8;
wchar_utf8(save_path.string(), utf8);
TORRENT_FORWARD(move_storage(utf8));
}
void torrent_handle::rename_file(int index, fs::path const& new_name) const
{
INVARIANT_CHECK;
TORRENT_FORWARD(rename_file(index, new_name.string()));
}
void torrent_handle::rename_file(int index, fs::wpath const& new_name) const
{
INVARIANT_CHECK;
std::string utf8;
wchar_utf8(new_name.string(), utf8);
TORRENT_FORWARD(rename_file(index, utf8));
}
void torrent_handle::add_extension(
boost::function<boost::shared_ptr<torrent_plugin>(torrent*, void*)> const& ext
, void* userdata)