-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuffer_stream.cc
More file actions
119 lines (86 loc) · 3.14 KB
/
Copy pathbuffer_stream.cc
File metadata and controls
119 lines (86 loc) · 3.14 KB
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
/*
Create a stream from a buffer.
Copyright (C) 2019, Guillaume Gonnet
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "utils/buffer_stream.h"
namespace maconv {
namespace utils {
// "RawDataStreamBuf" constructor.
RawDataStreamBuf::RawDataStreamBuf(uint8_t *data, uint32_t length)
{
SetData(data, length);
}
// Set the data and length for this stream buffer.
void RawDataStreamBuf::SetData(uint8_t *data, uint32_t length)
{
char *start = reinterpret_cast<char *>(data);
char *end = reinterpret_cast<char *>(data + length);
setg(start, start, end);
setp(start, end);
}
// Set the position indicator relative to some other position.
auto RawDataStreamBuf::seekoff(off_type off, std::ios_base::seekdir dir,
std::ios_base::openmode which) -> pos_type
{
// If dir is beginning or end: set an absolute position directly.
switch (dir) {
case std::ios_base::beg:
return seekpos(off, which);
case std::ios_base::end:
return seekpos(off + (egptr() - eback()), which);
}
// Set position from cursor (return twice if both in and out are set).
if (which & std::ios_base::in)
seekpos(off + (gptr() - eback()), std::ios_base::in);
if (which & std::ios_base::out)
seekpos(off + (pptr() - eback()), std::ios_base::out);
return ((which & std::ios_base::in) ? gptr() : pptr()) - eback();
}
// Set the position indicator to an absolute position.
auto RawDataStreamBuf::seekpos(pos_type pos, std::ios_base::openmode which)
-> pos_type
{
if (which & std::ios_base::in)
setg(eback(), eback() + pos, egptr());
if (which & std::ios_base::out)
pbump(pos - static_cast<pos_type>((pptr() - eback())));
return pos;
}
// Get the number of characters available for input.
std::streamsize RawDataStreamBuf::showmanyc()
{
return egptr() - gptr();
}
// Reads count characters from the input sequence.
std::streamsize RawDataStreamBuf::xsgetn(char *p, std::streamsize n)
{
std::streamsize length = std::min(n, egptr() - gptr());
std::copy(gptr(), gptr() + length, p);
gbump(length);
return length;
}
// Write count characters to the output sequence.
std::streamsize RawDataStreamBuf::xsputn(const char *p, std::streamsize n)
{
std::streamsize length = std::min(n, epptr() - pptr());
std::copy(p, p + length, pptr());
pbump(length);
return length;
}
// "BufferStreamBuf" constructor.
BufferStreamBuf::BufferStreamBuf(uint32_t length)
: buffer{new uint8_t[length]}
{
SetData(buffer.get(), length);
}
} // namespace utils
} // namespace maconv