From a3e7124614bb4077ddcb9f5aafac99976c79d59d Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 13 Apr 2008 05:19:10 +0000 Subject: [PATCH] added out stream operator to lazy_entry --- include/libtorrent/lazy_entry.hpp | 11 ++++++ include/libtorrent/peer_id.hpp | 13 ++++--- src/lazy_bdecode.cpp | 64 +++++++++++++++++++++++++++++++ test/test_bencoding.cpp | 5 +++ 4 files changed, 87 insertions(+), 6 deletions(-) diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index a6b27782b..19294c2c1 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include "libtorrent/assert.hpp" #include +#include namespace libtorrent { @@ -103,6 +104,12 @@ namespace libtorrent lazy_entry* dict_find(char const* name); lazy_entry const* dict_find(char const* name) const { return const_cast(this)->dict_find(name); } + std::pair dict_at(int i) const + { + TORRENT_ASSERT(m_type == dict_t); + TORRENT_ASSERT(i < m_size); + return std::make_pair(m_data.dict[i].first, &m_data.dict[i].second); + } int dict_size() const { @@ -128,6 +135,8 @@ namespace libtorrent TORRENT_ASSERT(i < m_size); return &m_data.list[i]; } + lazy_entry const* list_at(int i) const + { return const_cast(this)->list_at(i); } int list_size() const { @@ -153,6 +162,8 @@ namespace libtorrent int m_capacity; // if list or dictionary, allocated number of items }; + std::ostream& operator<<(std::ostream& os, lazy_entry const& e); + }; diff --git a/include/libtorrent/peer_id.hpp b/include/libtorrent/peer_id.hpp index a546c6e08..b91717c0a 100755 --- a/include/libtorrent/peer_id.hpp +++ b/include/libtorrent/peer_id.hpp @@ -48,8 +48,6 @@ namespace libtorrent class TORRENT_EXPORT big_number { - // private type - struct private_pointer {}; // the number of bytes of the number enum { number_size = 20 }; public: @@ -57,16 +55,19 @@ namespace libtorrent big_number() {} - big_number(std::string const& s) + explicit big_number(char const* s) + { + if (s == 0) clear(); + else std::memcpy(m_number, s, size); + } + + explicit big_number(std::string const& s) { TORRENT_ASSERT(s.size() >= 20); int sl = int(s.size()) < size ? int(s.size()) : size; std::memcpy(m_number, &s[0], sl); } - // when initialized with 0 - big_number(private_pointer*) { clear(); } - void clear() { std::fill(m_number,m_number+number_size,0); diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index b5b6b9db6..32aae2aec 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -31,6 +31,8 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/lazy_entry.hpp" +#include +#include namespace libtorrent { @@ -239,5 +241,67 @@ namespace libtorrent m_capacity = 0; m_type = none_t; } + + std::ostream& operator<<(std::ostream& os, lazy_entry const& e) + { + switch (e.type()) + { + case lazy_entry::none_t: return os << "none"; + case lazy_entry::int_t: return os << e.int_value(); + case lazy_entry::string_t: + { + bool printable = true; + char const* str = e.string_value(); + for (int i = 0; i < e.string_length(); ++i) + { + using namespace std; + if (isprint(str[i])) continue; + printable = false; + break; + } + if (printable) return os << str; + for (int i = 0; i < e.string_length(); ++i) + os << std::hex << (int)str[i]; + } + case lazy_entry::list_t: + { + os << "["; + bool one_liner = e.list_size() == 0 + || e.list_at(0)->type() == lazy_entry::int_t + || (e.list_at(0)->type() == lazy_entry::string_t + && e.list_at(0)->string_length() < 5); + if (!one_liner) os << "\n"; + for (int i = 0; i < e.list_size(); ++i) + { + if (i == 0 && one_liner) os << " "; + os << *e.list_at(i); + if (i < e.list_size() - 1) os << (one_liner?", ":",\n"); + else os << (one_liner?" ":"\n"); + } + return os << "]"; + } + case lazy_entry::dict_t: + { + os << "{"; + bool one_liner = e.dict_size() == 0 + || e.dict_at(0).second->type() == lazy_entry::int_t + || (e.dict_at(0).second->type() == lazy_entry::string_t + && e.dict_at(0).second->string_length() < 4) + || strlen(e.dict_at(0).first) < 10; + if (!one_liner) os << "\n"; + for (int i = 0; i < e.dict_size(); ++i) + { + if (i == 0 && one_liner) os << " "; + std::pair ent = e.dict_at(i); + os << "'" << ent.first << "': " << *ent.second; + if (i < e.dict_size() - 1) os << (one_liner?", ":",\n"); + else os << (one_liner?" ":"\n"); + } + return os << "}"; + } + } + return os; + } + }; diff --git a/test/test_bencoding.cpp b/test/test_bencoding.cpp index 34b83dc92..b15ec92d7 100644 --- a/test/test_bencoding.cpp +++ b/test/test_bencoding.cpp @@ -1,6 +1,7 @@ #include "libtorrent/bencode.hpp" #include "libtorrent/lazy_entry.hpp" #include +#include #include "test.hpp" @@ -75,6 +76,7 @@ int test_main() lazy_entry e; int ret = lazy_bdecode(b, b + sizeof(b)-1, e); TORRENT_ASSERT(ret == 0); + std::cout << e << std::endl; TORRENT_ASSERT(e.type() == lazy_entry::int_t); TORRENT_ASSERT(e.int_value() == 12453); } @@ -84,6 +86,7 @@ int test_main() lazy_entry e; int ret = lazy_bdecode(b, b + sizeof(b)-1, e); TORRENT_ASSERT(ret == 0); + std::cout << e << std::endl; TORRENT_ASSERT(e.type() == lazy_entry::string_t); TORRENT_ASSERT(e.string_value() == std::string("abcdefghijklmnopqrstuvwxyz")); TORRENT_ASSERT(e.string_length() == 26); @@ -94,6 +97,7 @@ int test_main() lazy_entry e; int ret = lazy_bdecode(b, b + sizeof(b)-1, e); TORRENT_ASSERT(ret == 0); + std::cout << e << std::endl; TORRENT_ASSERT(e.type() == lazy_entry::list_t); TORRENT_ASSERT(e.list_size() == 2); TORRENT_ASSERT(e.list_at(0)->type() == lazy_entry::int_t); @@ -108,6 +112,7 @@ int test_main() lazy_entry e; int ret = lazy_bdecode(b, b + sizeof(b)-1, e); TORRENT_ASSERT(ret == 0); + std::cout << e << std::endl; TORRENT_ASSERT(e.type() == lazy_entry::dict_t); TORRENT_ASSERT(e.dict_size() == 3); TORRENT_ASSERT(e.dict_find("a")->type() == lazy_entry::int_t);