~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/peer_id.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#ifndef TORRENT_PEER_ID_HPP_INCLUDED
34
34
#define TORRENT_PEER_ID_HPP_INCLUDED
35
35
 
36
 
#include <iostream>
37
 
#include <iomanip>
38
36
#include <cctype>
39
37
#include <algorithm>
40
38
#include <string>
44
42
#include "libtorrent/assert.hpp"
45
43
#include "libtorrent/escape_string.hpp"
46
44
 
 
45
#if TORRENT_USE_IOSTREAM
 
46
#include <iostream>
 
47
#include <iomanip>
 
48
#endif
 
49
 
 
50
#ifdef max
 
51
#undef max
 
52
#endif
 
53
 
 
54
#ifdef min
 
55
#undef min
 
56
#endif
 
57
 
47
58
namespace libtorrent
48
59
{
49
60
 
76
87
                        std::memcpy(m_number, &s[0], sl);
77
88
                }
78
89
 
 
90
                void assign(char const* str)
 
91
                {
 
92
                        std::memcpy(m_number, str, size);
 
93
                }
 
94
 
79
95
                void clear()
80
96
                {
81
 
                        std::fill(m_number,m_number+number_size,0);
 
97
                        std::fill(m_number,m_number+number_size,(const unsigned char)(0));
82
98
                }
83
99
 
84
100
                bool is_all_zeros() const
85
101
                {
86
 
                        return std::count(m_number,m_number+number_size,0) == number_size;
 
102
                        for (const unsigned char* i = m_number; i < m_number+number_size; ++i)
 
103
                                if (*i != 0) return false;
 
104
                        return true;
87
105
                }
88
106
 
89
107
                bool operator==(big_number const& n) const
162
180
        typedef big_number peer_id;
163
181
        typedef big_number sha1_hash;
164
182
 
 
183
#if TORRENT_USE_IOSTREAM
165
184
        inline std::ostream& operator<<(std::ostream& os, big_number const& peer)
166
185
        {
167
 
                for (big_number::const_iterator i = peer.begin();
168
 
                        i != peer.end(); ++i)
169
 
                {
170
 
                        os << std::hex << std::setw(2) << std::setfill('0')
171
 
                                << static_cast<unsigned int>(*i);
172
 
                }
173
 
                os << std::dec << std::setfill(' ');
174
 
                return os;
 
186
                char out[41];
 
187
                to_hex((char const*)&peer[0], big_number::size, out);
 
188
                return os << out;
175
189
        }
176
190
 
177
191
        inline std::istream& operator>>(std::istream& is, big_number& peer)
178
192
        {
179
 
                for (big_number::iterator i = peer.begin();
180
 
                        i != peer.end(); ++i)
181
 
                {
182
 
                        char c[2];
183
 
                        is >> c[0] >> c[1];
184
 
                        c[0] = tolower(c[0]);
185
 
                        c[1] = tolower(c[1]);
186
 
                        if (
187
 
                                ((c[0] < '0' || c[0] > '9') && (c[0] < 'a' || c[0] > 'f'))
188
 
                                || ((c[1] < '0' || c[1] > '9') && (c[1] < 'a' || c[1] > 'f'))
189
 
                                || is.fail())
190
 
                        {
191
 
                                is.setstate(std::ios_base::failbit);
192
 
                                return is;
193
 
                        }
194
 
                        *i = ((is_digit(c[0])?c[0]-'0':c[0]-'a'+10) << 4)
195
 
                                + (is_digit(c[1])?c[1]-'0':c[1]-'a'+10);
196
 
                }
 
193
                char hex[40];
 
194
                is.read(hex, 40);
 
195
                if (!from_hex(hex, 40, (char*)&peer[0]))
 
196
                        is.setstate(std::ios_base::failbit);
197
197
                return is;
198
198
        }
199
 
 
 
199
#endif // TORRENT_USE_IOSTREAM
200
200
}
201
201
 
202
202
#endif // TORRENT_PEER_ID_HPP_INCLUDED