diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index dfd256d18..473eb1ca8 100755 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -167,6 +167,8 @@ namespace libtorrent #endif entry* find_key(char const* key); entry const* find_key(char const* key) const; + entry* find_key(std::string const& key); + entry const* find_key(std::string const& key) const; void print(std::ostream& os, int indent = 0) const; diff --git a/include/libtorrent/peer_id.hpp b/include/libtorrent/peer_id.hpp index b91717c0a..0ec096de3 100755 --- a/include/libtorrent/peer_id.hpp +++ b/include/libtorrent/peer_id.hpp @@ -142,6 +142,9 @@ namespace libtorrent iterator begin() { return m_number; } iterator end() { return m_number+number_size; } + std::string to_string() const + { return std::string((char const*)&m_number[0], number_size); } + private: unsigned char m_number[number_size]; diff --git a/src/entry.cpp b/src/entry.cpp index 2b8410115..0dd9a43fc 100755 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -54,19 +54,6 @@ namespace TORRENT_ASSERT(o); o->~T(); } - - struct compare_string - { - compare_string(char const* s): m_str(s) {} - - bool operator()( - std::pair const& e) const - { - return m_str && e.first == m_str; - } - char const* m_str; - }; } namespace libtorrent @@ -94,6 +81,16 @@ namespace libtorrent } entry& entry::operator[](char const* key) + { + dictionary_type::iterator i = dict().find(key); + if (i != dict().end()) return i->second; + dictionary_type::iterator ret = dict().insert( + dict().begin() + , std::make_pair(key, entry())); + return ret->second; + } + + entry& entry::operator[](std::string const& key) { dictionary_type::iterator i = dict().find(key); if (i != dict().end()) return i->second; @@ -103,21 +100,11 @@ namespace libtorrent return ret->second; } - - entry& entry::operator[](std::string const& key) - { - return (*this)[key.c_str()]; - } - entry* entry::find_key(char const* key) { - dictionary_type::iterator i = std::find_if( - dict().begin() - , dict().end() - , compare_string(key)); + dictionary_type::iterator i = dict().find(key); if (i == dict().end()) return 0; return &i->second; - } entry const* entry::find_key(char const* key) const @@ -127,6 +114,20 @@ namespace libtorrent return &i->second; } + entry* entry::find_key(std::string const& key) + { + dictionary_type::iterator i = dict().find(key); + if (i == dict().end()) return 0; + return &i->second; + } + + entry const* entry::find_key(std::string const& key) const + { + dictionary_type::const_iterator i = dict().find(key); + if (i == dict().end()) return 0; + return &i->second; + } + #ifndef BOOST_NO_EXCEPTIONS const entry& entry::operator[](char const* key) const { diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index 3ec3970d4..288ee7267 100755 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -313,9 +313,7 @@ namespace libtorrent if (tracker_req().kind == tracker_request::scrape_request) { - std::string ih; - std::copy(tracker_req().info_hash.begin(), tracker_req().info_hash.end() - , std::back_inserter(ih)); + std::string ih = tracker_req().info_hash.to_string(); entry const* files = e.find_key("files"); if (files == 0 || files->type() != entry::dictionary_t) @@ -324,7 +322,7 @@ namespace libtorrent return; } - entry const* scrape_data = e.find_key(ih.c_str()); + entry const* scrape_data = files->find_key(ih); if (scrape_data == 0 || scrape_data->type() != entry::dictionary_t) { fail(-1, "missing or invalid info-hash entry in scrape response");