aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/tangerine-network/mcl/include/cybozu/stream.hpp
blob: bc110bdb0d352c3e89a21f33274321681359ba95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#pragma once
/**
    @file
    @brief stream and line stream class

    @author MITSUNARI Shigeo(@herumi)
*/
#ifndef CYBOZU_DONT_USE_STRING
#include <string>
#include <iosfwd>
#endif
#include <cybozu/exception.hpp>
#include <memory.h>

namespace cybozu {

namespace stream_local {

template <typename From, typename To>
struct is_convertible {
    typedef char yes;
    typedef int no;

    static no test(...);
    static yes test(const To*);
    static const bool value = sizeof(test(static_cast<const From*>(0))) == sizeof(yes);
};

template <bool b, class T = void>
struct enable_if { typedef T type; };

template <class T>
struct enable_if<false, T> {};

#ifndef CYBOZU_DONT_USE_STRING
/* specialization for istream */
template<class InputStream>
size_t readSome_inner(void *buf, size_t size, InputStream& is, typename enable_if<is_convertible<InputStream, std::istream>::value>::type* = 0)
{
    if (size > 0x7fffffff) size = 0x7fffffff;
    is.read(static_cast<char *>(buf), size);
    const int64_t readSize = is.gcount();
    if (readSize < 0) return 0;
    if (size == 1 && readSize == 0) is.clear();
    return static_cast<size_t>(readSize);
}

/* generic version for size_t readSome(void *, size_t) */
template<class InputStream>
size_t readSome_inner(void *buf, size_t size, InputStream& is, typename enable_if<!is_convertible<InputStream, std::istream>::value>::type* = 0)
{
    return is.readSome(buf, size);
}
#else
template<class InputStream>
size_t readSome_inner(void *buf, size_t size, InputStream& is)
{
    return is.readSome(buf, size);
}
#endif

#ifndef CYBOZU_DONT_USE_EXCEPTION
/* specialization for ostream */
template<class OutputStream>
void writeSub(OutputStream& os, const void *buf, size_t size, typename enable_if<is_convertible<OutputStream, std::ostream>::value>::type* = 0)
{
    if (!os.write(static_cast<const char *>(buf), size)) throw cybozu::Exception("stream:writeSub") << size;
}
#endif

#ifndef CYBOZU_DONT_USE_STRING
/* generic version for void write(const void*, size_t), which writes all data */
template<class OutputStream>
void writeSub(OutputStream& os, const void *buf, size_t size, typename enable_if<!is_convertible<OutputStream, std::ostream>::value>::type* = 0)
{
    os.write(buf, size);
}

template<class OutputStream>
void writeSub(bool *pb, OutputStream& os, const void *buf, size_t size, typename enable_if<is_convertible<OutputStream, std::ostream>::value>::type* = 0)
{
    *pb = !!os.write(static_cast<const char *>(buf), size);
}

/* generic version for void write(const void*, size_t), which writes all data */
template<class OutputStream>
void writeSub(bool *pb, OutputStream& os, const void *buf, size_t size, typename enable_if<!is_convertible<OutputStream, std::ostream>::value>::type* = 0)
{
    os.write(pb, buf, size);
}
#else
template<class OutputStream>
void writeSub(bool *pb, OutputStream& os, const void *buf, size_t size)
{
    os.write(pb, buf, size);
}
#endif

} // stream_local

/*
    make a specializaiton of class to use new InputStream, OutputStream
*/
template<class InputStream>
struct InputStreamTag {
    static size_t readSome(void *buf, size_t size, InputStream& is)
    {
        return stream_local::readSome_inner<InputStream>(buf, size, is);
    }
    static bool readChar(char *c, InputStream& is)
    {
        return readSome(c, 1, is) == 1;
    }
};

template<class OutputStream>
struct OutputStreamTag {
    static void write(OutputStream& os, const void *buf, size_t size)
    {
        stream_local::writeSub<OutputStream>(os, buf, size);
    }
};

class MemoryInputStream {
    const char *p_;
    size_t size_;
    size_t pos;
public:
    MemoryInputStream(const void *p, size_t size) : p_(static_cast<const char *>(p)), size_(size), pos(0) {}
    size_t readSome(void *buf, size_t size)
    {
        if (size > size_  - pos) size = size_ - pos;
        memcpy(buf, p_ + pos, size);
        pos += size;
        return size;
    }
    size_t getPos() const { return pos; }
};

class MemoryOutputStream {
    char *p_;
    size_t size_;
    size_t pos;
public:
    MemoryOutputStream(void *p, size_t size) : p_(static_cast<char *>(p)), size_(size), pos(0) {}
    void write(bool *pb, const void *buf, size_t size)
    {
        if (size > size_ - pos) {
            *pb = false;
            return;
        }
        memcpy(p_ + pos, buf, size);
        pos += size;
        *pb = true;
    }
#ifndef CYBOZU_DONT_USE_EXCEPTION
    void write(const void *buf, size_t size)
    {
        bool b;
        write(&b, buf, size);
        if (!b) throw cybozu::Exception("MemoryOutputStream:write") << size << size_ << pos;
    }
#endif
    size_t getPos() const { return pos; }
};

#ifndef CYBOZU_DONT_USE_STRING
class StringInputStream {
    const std::string& str_;
    size_t pos;
    StringInputStream(const StringInputStream&);
    void operator=(const StringInputStream&);
public:
    explicit StringInputStream(const std::string& str) : str_(str), pos(0) {}
    size_t readSome(void *buf, size_t size)
    {
        const size_t remainSize = str_.size() - pos;
        if (size > remainSize) size = remainSize;
        memcpy(buf, &str_[pos], size);
        pos += size;
        return size;
    }
    size_t getPos() const { return pos; }
};

class StringOutputStream {
    std::string& str_;
    StringOutputStream(const StringOutputStream&);
    void operator=(const StringOutputStream&);
public:
    explicit StringOutputStream(std::string& str) : str_(str) {}
    void write(bool *pb, const void *buf, size_t size)
    {
        str_.append(static_cast<const char *>(buf), size);
        *pb = true;
    }
    void write(const void *buf, size_t size)
    {
        str_.append(static_cast<const char *>(buf), size);
    }
    size_t getPos() const { return str_.size(); }
};
#endif

template<class InputStream>
size_t readSome(void *buf, size_t size, InputStream& is)
{
    return stream_local::readSome_inner(buf, size, is);
}

template<class OutputStream>
void write(OutputStream& os, const void *buf, size_t size)
{
    stream_local::writeSub(os, buf, size);
}

template<class OutputStream>
void write(bool *pb, OutputStream& os, const void *buf, size_t size)
{
    stream_local::writeSub(pb, os, buf, size);
}

template<typename InputStream>
void read(bool *pb, void *buf, size_t size, InputStream& is)
{
    char *p = static_cast<char*>(buf);
    while (size > 0) {
        size_t readSize = cybozu::readSome(p, size, is);
        if (readSize == 0) {
            *pb = false;
            return;
        }
        p += readSize;
        size -= readSize;
    }
    *pb = true;
}

#ifndef CYBOZU_DONT_USE_EXCEPTION
template<typename InputStream>
void read(void *buf, size_t size, InputStream& is)
{
    bool b;
    read(&b, buf, size, is);
    if (!b) throw cybozu::Exception("stream:read");
}
#endif

template<class InputStream>
bool readChar(char *c, InputStream& is)
{
    return readSome(c, 1, is) == 1;
}

template<class OutputStream>
void writeChar(OutputStream& os, char c)
{
    cybozu::write(os, &c, 1);
}

template<class OutputStream>
void writeChar(bool *pb, OutputStream& os, char c)
{
    cybozu::write(pb, os, &c, 1);
}

} // cybozu