From afd04635e905ac3c46a16bca12d83a317d14bb5a Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 18 Feb 2009 06:01:24 +0000 Subject: [PATCH] replaced std::isdigit with is_digit, to avoid asserts on windows and locale dependency. Fixes #484 --- include/libtorrent/bencode.hpp | 14 ++------------ include/libtorrent/escape_string.hpp | 4 +++- include/libtorrent/peer_id.hpp | 9 ++++----- src/escape_string.cpp | 10 ++++++++++ src/identify_client.cpp | 11 +---------- src/lazy_bdecode.cpp | 7 +++---- 6 files changed, 23 insertions(+), 32 deletions(-) diff --git a/include/libtorrent/bencode.hpp b/include/libtorrent/bencode.hpp index b5b330d4d..cc4e5a220 100644 --- a/include/libtorrent/bencode.hpp +++ b/include/libtorrent/bencode.hpp @@ -80,17 +80,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/assert.hpp" - -#if defined(_MSC_VER) -namespace std -{ - using ::isdigit; - using ::atoi; -}; - -#define for if (false) {} else for -#endif - +#include "libtorrent/escape_string.hpp" namespace libtorrent { @@ -341,7 +331,7 @@ namespace libtorrent // ---------------------------------------------- // string default: - if (isdigit((unsigned char)*in)) + if (is_digit((unsigned char)*in)) { std::string len_s = read_until(in, end, ':', err); if (err) diff --git a/include/libtorrent/escape_string.hpp b/include/libtorrent/escape_string.hpp index 9f141a923..f76939779 100644 --- a/include/libtorrent/escape_string.hpp +++ b/include/libtorrent/escape_string.hpp @@ -42,7 +42,9 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - boost::array::digits10> to_string(size_type n); + boost::array::digits10> TORRENT_EXPORT to_string(size_type n); + bool TORRENT_EXPORT is_digit(char c); + bool TORRENT_EXPORT isprint(char c); std::string TORRENT_EXPORT unescape_string(std::string const& s); std::string TORRENT_EXPORT escape_string(const char* str, int len); diff --git a/include/libtorrent/peer_id.hpp b/include/libtorrent/peer_id.hpp index 0ec096de3..d737fb4fb 100644 --- a/include/libtorrent/peer_id.hpp +++ b/include/libtorrent/peer_id.hpp @@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/assert.hpp" +#include "libtorrent/escape_string.hpp" namespace libtorrent { @@ -168,8 +169,6 @@ namespace libtorrent inline std::istream& operator>>(std::istream& is, big_number& peer) { - using namespace std; - for (big_number::iterator i = peer.begin(); i != peer.end(); ++i) { @@ -182,11 +181,11 @@ namespace libtorrent || ((c[1] < '0' || c[1] > '9') && (c[1] < 'a' || c[1] > 'f')) || is.fail()) { - is.setstate(ios_base::failbit); + is.setstate(std::ios_base::failbit); return is; } - *i = ((isdigit(c[0])?c[0]-'0':c[0]-'a'+10) << 4) - + (isdigit(c[1])?c[1]-'0':c[1]-'a'+10); + *i = ((is_digit(c[0])?c[0]-'0':c[0]-'a'+10) << 4) + + (is_digit(c[1])?c[1]-'0':c[1]-'a'+10); } return is; } diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 40384e760..39c02a9a6 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -69,6 +69,16 @@ namespace libtorrent return ret; } + bool is_digit(char c) + { + return c >= '0' && c <= '9'; + } + + bool isprint(char c) + { + return c >= 32 && c < 127; + } + std::string unescape_string(std::string const& s) { std::string ret; diff --git a/src/identify_client.cpp b/src/identify_client.cpp index 895792884..3a54029e3 100644 --- a/src/identify_client.cpp +++ b/src/identify_client.cpp @@ -48,28 +48,19 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/identify_client.hpp" #include "libtorrent/fingerprint.hpp" +#include "libtorrent/escape_string.hpp" namespace { using namespace libtorrent; - bool is_digit(char c) - { - return c >= '0' && c <= '9'; - } - int decode_digit(char c) { if (is_digit(c)) return c - '0'; return unsigned(c) - 'A' + 10; } - bool isprint(char c) - { - return c >= 32 && c < 127; - } - // takes a peer id and returns a valid boost::optional // object if the peer id matched the azureus style encoding // the returned fingerprint contains information about the diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index bc0beaab4..983b48a35 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/lazy_entry.hpp" +#include "libtorrent/escape_string.hpp" #include #include #include @@ -58,8 +59,7 @@ namespace libtorrent { while (start < end && *start != delimiter) { - using namespace std; - if (!isdigit(*start)) { return 0; } + if (!is_digit(*start)) { return 0; } val *= 10; val += *start - '0'; ++start; @@ -154,8 +154,7 @@ namespace libtorrent } default: { - using namespace std; - if (!isdigit(t)) return fail_bdecode(ret); + if (!is_digit(t)) return fail_bdecode(ret); boost::int64_t len = t - '0'; start = parse_int(start, end, ':', len);