-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.h
More file actions
217 lines (196 loc) · 4.83 KB
/
buffer.h
File metadata and controls
217 lines (196 loc) · 4.83 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
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
/* dvbcut
Copyright (c) 2005 Sven Over <svenover@svenover.de>
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
*/
/* $Id$ */
#ifndef _DVBCUT_BUFFER_H_
#define _DVBCUT_BUFFER_H_
#include "port.h"
class buffer
{
protected:
void *d;
unsigned int size, readpos, writepos;
unsigned long long wrtot;
void relax();
public:
buffer(unsigned int _size);
~buffer();
void writeposition(int v)
{
if (v<-(signed)writepos)
writepos=0;
else {
writepos+=v;
if (writepos>size)
writepos=size;
}
}
unsigned int inbytes() const
{
return writepos-readpos;
}
unsigned int freebytes() const
{
return size-writepos+readpos;
}
bool empty() const
{
return writepos==readpos;
}
bool full() const
{
return (readpos==0)&&(writepos==size);
}
unsigned long long written() const
{
return wrtot;
}
void *data() const
{
return (void*)((unsigned char*)d+readpos);
}
void *writeptr() const
{
return (void*)((unsigned char*)d+writepos);
}
int discard(unsigned int len)
{
if (len>inbytes())
len=inbytes();
readpos+=len;
return len;
}
int discardback(unsigned int len)
{
if (len>inbytes())
len=inbytes();
writepos-=len;
return len;
}
void resize(unsigned int newsize);
void clear()
{
readpos=writepos=0;
wrtot=0;
}
unsigned int getsize() const
{
return size;
}
unsigned int putdata(const void *data, unsigned int len, bool autoresize=false);
unsigned int getdata(void *data, unsigned int len);
int readdata(int fd);
int writedata(int fd);
int writedata(int fd, unsigned int minspace)
{
int wrn=0;
if (minspace>size) {
while (readpos<writepos) {
int w=writedata(fd);
if (w<0)
return w;
wrn+=w;
}
readpos=writepos=0;
resize(minspace);
} else {
while (freebytes()<minspace) {
int w=writedata(fd);
if (w<0)
return w;
wrn+=w;
}
}
return wrn;
}
int flush(int fd)
{
int wrn=0;
while (inbytes()>0) {
int w=writedata(fd);
if (w<0)
return w;
wrn+=w;
}
return wrn;
}
};
class inbuffer {
protected:
void *d;
unsigned int size, mmapsize, readpos, writepos;
struct infile {
dvbcut_off_t off;
dvbcut_off_t end;
bool closeme;
int fd;
std::string name;
};
std::vector<infile> files;
bool eof;
dvbcut_off_t pos;
dvbcut_off_t filesize;
bool mmapped;
static long pagesize;
bool sequential;
bool pipe_mode;
void close();
int pipedata(unsigned int amount, long long position);
public:
inbuffer(unsigned int _size, unsigned int mmapsize = 0);
~inbuffer();
bool open(int fd, std::string *errmsg = 0, bool closeme = false, std::string filename="");
bool open(std::string filename, std::string *errmsg = 0);
void reset();
const void *data() const { return (void*)((char*)d + readpos); }
unsigned int getsize() const { return size; }
unsigned int inbytes() const { return writepos - readpos; }
bool iseof() const { return eof; }
int providedata(unsigned int amount, long long position);
int providedata(unsigned int amount) {
if (amount <= inbytes())
return inbytes();
return providedata(amount, pos + readpos);
}
void discarddata(unsigned int amount) {
readpos += amount;
if (readpos >= writepos) {
pos += writepos;
readpos = 0;
writepos = 0;
}
}
dvbcut_off_t getfilesize() const { return filesize; }
dvbcut_off_t getfilepos() const { return pos + readpos; }
int getfilenum(dvbcut_off_t offset, dvbcut_off_t &fileoff);
std::string getfilename(int filenum);
void setsequential(bool flag) { sequential = flag; }
};
class outbuffer : protected buffer
{
protected:
int fd;
bool close;
public:
outbuffer(unsigned int _size, int _fd=-1, bool tobeclosed=true) :
buffer(_size), fd(_fd), close(tobeclosed)
{}
~outbuffer();
int open(const char *filename);
int putdata(const void *data, unsigned int len, bool autoresize=false);
using buffer::written;
using buffer::getsize;
using buffer::resize;
};
#endif