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

« back to all changes in this revision

Viewing changes to include/libtorrent/io.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (c) 2003, Arvid Norberg
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions
 
8
are met:
 
9
 
 
10
    * Redistributions of source code must retain the above copyright
 
11
      notice, this list of conditions and the following disclaimer.
 
12
    * Redistributions in binary form must reproduce the above copyright
 
13
      notice, this list of conditions and the following disclaimer in
 
14
      the documentation and/or other materials provided with the distribution.
 
15
    * Neither the name of the author nor the names of its
 
16
      contributors may be used to endorse or promote products derived
 
17
      from this software without specific prior written permission.
 
18
 
 
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
29
POSSIBILITY OF SUCH DAMAGE.
 
30
 
 
31
*/
 
32
 
 
33
#ifndef TORRENT_IO_HPP_INCLUDED
 
34
#define TORRENT_IO_HPP_INCLUDED
 
35
 
 
36
#include <boost/cstdint.hpp>
 
37
#include <string>
 
38
 
 
39
namespace libtorrent
 
40
{
 
41
        namespace detail
 
42
        {
 
43
                template <class T> struct type {};
 
44
 
 
45
                // reads an integer from a byte stream
 
46
                // in big endian byte order and converts
 
47
                // it to native endianess
 
48
                template <class T, class InIt>
 
49
                inline T read_impl(InIt& start, type<T>)
 
50
                {
 
51
                        T ret = 0;
 
52
                        for (int i = 0; i < (int)sizeof(T); ++i)
 
53
                        {
 
54
                                ret <<= 8;
 
55
                                ret |= static_cast<unsigned char>(*start);
 
56
                                ++start;
 
57
                        }
 
58
                        return ret;
 
59
                }
 
60
 
 
61
                template <class T, class OutIt>
 
62
                inline void write_impl(T val, OutIt& start)
 
63
                {
 
64
                        for (int i = (int)sizeof(T)-1; i >= 0; --i)
 
65
                        {
 
66
                                *start = static_cast<unsigned char>((val >> (i * 8)) & 0xff);
 
67
                                ++start;
 
68
                        }
 
69
                }
 
70
 
 
71
                // -- adaptors
 
72
 
 
73
                template <class InIt>
 
74
                boost::int64_t read_int64(InIt& start)
 
75
                { return read_impl(start, type<boost::int64_t>()); }
 
76
 
 
77
                template <class InIt>
 
78
                boost::uint64_t read_uint64(InIt& start)
 
79
                { return read_impl(start, type<boost::uint64_t>()); }
 
80
 
 
81
                template <class InIt>
 
82
                boost::uint32_t read_uint32(InIt& start)
 
83
                { return read_impl(start, type<boost::uint32_t>()); }
 
84
 
 
85
                template <class InIt>
 
86
                boost::int32_t read_int32(InIt& start)
 
87
                { return read_impl(start, type<boost::int32_t>()); }
 
88
 
 
89
                template <class InIt>
 
90
                boost::int16_t read_int16(InIt& start)
 
91
                { return read_impl(start, type<boost::int16_t>()); }
 
92
 
 
93
                template <class InIt>
 
94
                boost::uint16_t read_uint16(InIt& start)
 
95
                { return read_impl(start, type<boost::uint16_t>()); }
 
96
 
 
97
                template <class InIt>
 
98
                boost::int8_t read_int8(InIt& start)
 
99
                { return read_impl(start, type<boost::int8_t>()); }
 
100
 
 
101
                template <class InIt>
 
102
                boost::uint8_t read_uint8(InIt& start)
 
103
                { return read_impl(start, type<boost::uint8_t>()); }
 
104
 
 
105
 
 
106
                template <class OutIt>
 
107
                void write_uint64(boost::uint64_t val, OutIt& start)
 
108
                { write_impl(val, start); }
 
109
 
 
110
                template <class OutIt>
 
111
                void write_int64(boost::int64_t val, OutIt& start)
 
112
                { write_impl(val, start); }
 
113
 
 
114
                template <class OutIt>
 
115
                void write_uint32(boost::uint32_t val, OutIt& start)
 
116
                { write_impl(val, start); }
 
117
 
 
118
                template <class OutIt>
 
119
                void write_int32(boost::int32_t val, OutIt& start)
 
120
                { write_impl(val, start); }
 
121
 
 
122
                template <class OutIt>
 
123
                void write_uint16(boost::uint16_t val, OutIt& start)
 
124
                { write_impl(val, start); }
 
125
 
 
126
                template <class OutIt>
 
127
                void write_int16(boost::int16_t val, OutIt& start)
 
128
                { write_impl(val, start); }
 
129
 
 
130
                template <class OutIt>
 
131
                void write_uint8(boost::uint8_t val, OutIt& start)
 
132
                { write_impl(val, start); }
 
133
 
 
134
                template <class OutIt>
 
135
                void write_int8(boost::int8_t val, OutIt& start)
 
136
                { write_impl(val, start); }
 
137
 
 
138
                inline void write_string(std::string const& str, char*& start)
 
139
                {
 
140
                        std::copy(str.begin(), str.end(), start);
 
141
                        start += str.size();
 
142
                }
 
143
 
 
144
                template <class OutIt>
 
145
                void write_string(std::string const& str, OutIt& start)
 
146
                {
 
147
                        std::copy(str.begin(), str.end(), start);
 
148
                }
 
149
 
 
150
        }
 
151
}
 
152
 
 
153
#endif // TORRENT_IO_HPP_INCLUDED