added out stream operator to lazy_entry
This commit is contained in:
parent
ca872a0e82
commit
a3e7124614
|
@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
#include <vector>
|
||||
#include "libtorrent/assert.hpp"
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <iosfwd>
|
||||
|
||||
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<lazy_entry*>(this)->dict_find(name); }
|
||||
std::pair<char const*, lazy_entry const*> 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<lazy_entry*>(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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -31,6 +31,8 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
*/
|
||||
|
||||
#include "libtorrent/lazy_entry.hpp"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
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<char const*, lazy_entry const*> 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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "libtorrent/bencode.hpp"
|
||||
#include "libtorrent/lazy_entry.hpp"
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#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);
|
||||
|
|
Loading…
Reference in New Issue