factored out to_hex into the escape_string header file

This commit is contained in:
Arvid Norberg 2008-05-19 07:36:04 +00:00
parent 8613554c2a
commit 07f070868d
4 changed files with 33 additions and 29 deletions

View File

@ -51,6 +51,8 @@ namespace libtorrent
TORRENT_EXPORT boost::optional<std::string> url_has_argument(
std::string const& url, std::string argument);
TORRENT_EXPORT std::string to_hex(std::string const& s);
}
#endif // TORRENT_ESCAPE_STRING_HPP_INCLUDED

View File

@ -35,8 +35,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <boost/bind.hpp>
#include "libtorrent/entry.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/escape_string.hpp"
#if defined(_MSC_VER)
namespace std
@ -371,21 +373,8 @@ namespace libtorrent
break;
}
}
if (binary_string)
{
os.unsetf(std::ios_base::dec);
os.setf(std::ios_base::hex);
for (std::string::const_iterator i = string().begin(); i != string().end(); ++i)
os << std::setfill('0') << std::setw(2)
<< static_cast<unsigned int>((unsigned char)*i);
os.unsetf(std::ios_base::hex);
os.setf(std::ios_base::dec);
os << "\n";
}
else
{
os << string() << "\n";
}
if (binary_string) os << to_hex(string()) << "\n";
else os << string() << "\n";
} break;
case list_t:
{
@ -400,8 +389,21 @@ namespace libtorrent
os << "dictionary\n";
for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i)
{
bool binary_string = false;
for (std::string::const_iterator k = i->first.begin(); k != i->first.end(); ++k)
{
if (!std::isprint(static_cast<unsigned char>(*k)))
{
binary_string = true;
break;
}
}
for (int j = 0; j < indent+1; ++j) os << " ";
os << "[" << i->first << "]";
os << "[";
if (binary_string) os << to_hex(i->first);
else os << i->first;
os << "]";
if (i->second.type() != entry::string_t
&& i->second.type() != entry::int_t)
os << "\n";

View File

@ -352,5 +352,17 @@ namespace libtorrent
return url.substr(pos, url.find('&', pos) - pos);
}
TORRENT_EXPORT std::string to_hex(std::string const& s)
{
std::string ret;
char* digits = "0123456789abcdef";
for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
{
ret += digits[((unsigned char)*i) >> 4];
ret += digits[((unsigned char)*i) & 0xf];
}
return ret;
}
}

View File

@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/bencode.hpp"
#include "libtorrent/io.hpp"
#include "libtorrent/version.hpp"
#include "libtorrent/escape_string.hpp"
using boost::ref;
using boost::lexical_cast;
@ -137,19 +138,6 @@ namespace libtorrent { namespace dht
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_DEFINE_LOG(dht_tracker)
std::string to_hex(std::string const& s)
{
std::string ret;
char* digits = "0123456789abcdef";
for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
{
ret += digits[((unsigned char)*i) >> 4];
ret += digits[((unsigned char)*i) & 0xf];
}
return ret;
}
#endif
// class that puts the networking and the kademlia node in a single