From 72176a9256401474c15f4174fbe2b5d2b1424fa8 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 19 Apr 2008 03:00:07 +0000 Subject: [PATCH] lazy bdecoder additions and fixes --- include/libtorrent/lazy_entry.hpp | 13 +++++++-- src/lazy_bdecode.cpp | 44 ++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index 4c47b4a43..5d4eedf06 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -36,7 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include "libtorrent/assert.hpp" -#include +#include "libtorrent/size_type.hpp" #include namespace libtorrent @@ -71,7 +71,7 @@ namespace libtorrent m_end = start + length + 1; // include 'e' } - boost::int64_t int_value() const; + size_type int_value() const; // string functions // ================ @@ -119,6 +119,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::string dict_find_string_value(char const* name) const; + size_type dict_find_int_value(char const* name, size_type default_val = 0) const; + lazy_entry const* dict_find_dict(char const* name) const; + lazy_entry const* dict_find_list(char const* name) const; + std::pair dict_at(int i) const { TORRENT_ASSERT(m_type == dict_t); @@ -154,6 +160,9 @@ namespace libtorrent lazy_entry const* list_at(int i) const { return const_cast(this)->list_at(i); } + std::string list_string_value_at(int i) const; + size_type list_int_value_at(int i, size_type default_val = 0) const; + int list_size() const { TORRENT_ASSERT(m_type == list_t); diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index 2e2662585..b5e2b11e7 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -158,7 +158,7 @@ namespace libtorrent return 0; } - boost::int64_t lazy_entry::int_value() const + size_type lazy_entry::int_value() const { TORRENT_ASSERT(m_type == int_t); boost::int64_t val = 0; @@ -241,6 +241,34 @@ namespace libtorrent } } + std::string lazy_entry::dict_find_string_value(char const* name) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::string_t) return std::string(); + return e->string_value(); + } + + size_type lazy_entry::dict_find_int_value(char const* name, size_type default_val) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::int_t) return default_val; + return e->int_value(); + } + + lazy_entry const* lazy_entry::dict_find_dict(char const* name) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::dict_t) return 0; + return e; + } + + lazy_entry const* lazy_entry::dict_find_list(char const* name) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::list_t) return 0; + return e; + } + lazy_entry* lazy_entry::dict_find(char const* name) { TORRENT_ASSERT(m_type == dict_t); @@ -279,6 +307,20 @@ namespace libtorrent return m_data.list + (m_size++); } + std::string lazy_entry::list_string_value_at(int i) const + { + lazy_entry const* e = list_at(i); + if (e == 0 || e->type() != lazy_entry::string_t) return std::string(); + return e->string_value(); + } + + size_type lazy_entry::list_int_value_at(int i, size_type default_val) const + { + lazy_entry const* e = list_at(i); + if (e == 0 || e->type() != lazy_entry::int_t) return default_val; + return e->int_value(); + } + void lazy_entry::clear() { switch (m_type)