blob: ee6208ee18af3acc0f8d7deeff27b4c1599c97c2 (
plain)
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
|
#include "common.hh"
#include "ro_buffer.hh"
#include <algorithm>
size_t RoBuffer::read(RoBuffer* buf, void* data, size_t len) {
assert(buf);
assert(data);
if (len == 0)
return 0;
auto* d = reinterpret_cast<char*>(data);
size_t got = 0;
while (true) {
size_t avail;
auto want = len - got;
auto* ptr = buf->rbuf(want, avail);
if (avail == 0)
return got;
if (avail >= want) {
std::copy_n(ptr, want, d + got);
buf->rcommit(want);
return len;
}
std::copy_n(ptr, avail, d + got);
buf->rcommit(avail);
got += avail;
}
}
|