-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer.cpp
177 lines (156 loc) · 4.3 KB
/
buffer.cpp
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
/*
* mp3chop
*
* (C) 2000-2020 Mike Crowe <mac@mcrowe.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <string.h>
#include "buffer.h"
#include <stdio.h>
#include <ctype.h>
#include "assert.h"
InputStreamBuffer::InputStreamBuffer(size_t size, size_t lookbehind)
: source(0), input_size(size), input_min(lookbehind),
input_writep(0), input_readp(0), buffer_start_offset(0)
{
input_buffer = reinterpret_cast<uint8_t *>(operator new(size));
}
InputStreamBuffer::~InputStreamBuffer()
{
delete input_buffer;
}
void InputStreamBuffer::ShoveUp()
{
// if it's worth doing push everything up to the start of the
// buffer + minimum input value leaving space at the end for new
// data.
if (input_readp > input_min)
{
size_t begin = input_readp - input_min;
if (begin < input_size)
{
int bytes_kept = input_writep - begin;
if (bytes_kept)
memmove(input_buffer, input_buffer + begin, bytes_kept);
}
else
begin = input_size;
input_readp -= begin;
input_writep -= begin;
buffer_start_offset += begin;
}
}
uint64_t InputStreamBuffer::GetOffset() const
{
return buffer_start_offset + input_readp;
}
size_t InputStreamBuffer::Space() const
{
assert(input_size > input_writep);
return input_size - input_writep;
}
size_t InputStreamBuffer::GetAvailable() const
{
if (input_writep < input_readp)
return 0;
return input_writep - input_readp;
}
void InputStreamBuffer::EnsureAvailable(size_t count)
{
// We can't be asked for more than the input size.
assert(count <= input_size);
if (GetAvailable() < count)
{
do
{
ShoveUp();
input_writep += source->ReadInto(input_buffer + input_writep, Space());
}
while (input_readp > input_size);
if (GetAvailable() < count)
throw InsufficientDataException();
}
}
void InputStreamBuffer::Advance(uint64_t count)
{
input_readp += count;
}
void InputStreamBuffer::Rewind(size_t count)
{
if (input_readp >= count)
input_readp -= count;
else
throw InsufficientDataException();
}
OutputStreamBuffer::OutputStreamBuffer()
: sink(NULL),
bookmark_active(false),
append_mode(true),
offset(0)
{
}
OutputStreamBuffer::~OutputStreamBuffer()
{
}
void OutputStreamBuffer::Flush()
{
if (!bookmark_active)
{
size_t n = sink->WriteOut(output_buffer.data(), output_buffer.size());
assert(n <= output_buffer.size());
output_buffer.erase(output_buffer.begin(), output_buffer.begin() + n);
}
}
void OutputStreamBuffer::Append(const uint8_t *begin, size_t length)
{
if (append_mode)
{
output_buffer.insert(output_buffer.end(), begin, begin + length);
if (output_buffer.size() > 8192)
Flush();
offset += length;
}
else
{
// If we're not in append mode then just write it out
// directly and then throw enough away from the buffer to
// cover it.
const uint8_t *end = begin + length;
while (begin < end)
{
size_t n = sink->WriteOut(begin, length);
const size_t discard = std::min(n, output_buffer.size());
output_buffer.erase(output_buffer.begin(), output_buffer.begin() + discard);
begin += n;
}
}
}
void OutputStreamBuffer::SetBookmark()
{
Flush();
bookmark_active = true;
}
void OutputStreamBuffer::ClearBookmark()
{
bookmark_active = false;
append_mode = false;
Flush();
}
void OutputStreamBuffer::GoToBookmark()
{
append_mode = false;
}