| 1 |
/* dvbcut |
| 2 |
Copyright (c) 2005 Sven Over <[email protected]> |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* $Id$ */ |
| 20 |
|
| 21 |
#ifndef _DVBCUT_BUFFER_H_ |
| 22 |
#define _DVBCUT_BUFFER_H_ |
| 23 |
|
| 24 |
#include "port.h" |
| 25 |
|
| 26 |
class buffer |
| 27 |
{ |
| 28 |
protected: |
| 29 |
void *d; |
| 30 |
unsigned int size, readpos, writepos; |
| 31 |
unsigned long long wrtot; |
| 32 |
|
| 33 |
void relax(); |
| 34 |
|
| 35 |
public: |
| 36 |
buffer(unsigned int _size); |
| 37 |
~buffer(); |
| 38 |
|
| 39 |
void writeposition(int v) |
| 40 |
{ |
| 41 |
if (v<-(signed)writepos) |
| 42 |
writepos=0; |
| 43 |
else { |
| 44 |
writepos+=v; |
| 45 |
if (writepos>size) |
| 46 |
writepos=size; |
| 47 |
} |
| 48 |
} |
| 49 |
unsigned int inbytes() const |
| 50 |
{ |
| 51 |
return writepos-readpos; |
| 52 |
} |
| 53 |
unsigned int freebytes() const |
| 54 |
{ |
| 55 |
return size-writepos+readpos; |
| 56 |
} |
| 57 |
bool empty() const |
| 58 |
{ |
| 59 |
return writepos==readpos; |
| 60 |
} |
| 61 |
bool full() const |
| 62 |
{ |
| 63 |
return (readpos==0)&&(writepos==size); |
| 64 |
} |
| 65 |
unsigned long long written() const |
| 66 |
{ |
| 67 |
return wrtot; |
| 68 |
} |
| 69 |
void *data() const |
| 70 |
{ |
| 71 |
return (void*)((unsigned char*)d+readpos); |
| 72 |
} |
| 73 |
void *writeptr() const |
| 74 |
{ |
| 75 |
return (void*)((unsigned char*)d+writepos); |
| 76 |
} |
| 77 |
int discard(unsigned int len) |
| 78 |
{ |
| 79 |
if (len>inbytes()) |
| 80 |
len=inbytes(); |
| 81 |
readpos+=len; |
| 82 |
return len; |
| 83 |
} |
| 84 |
int discardback(unsigned int len) |
| 85 |
{ |
| 86 |
if (len>inbytes()) |
| 87 |
len=inbytes(); |
| 88 |
writepos-=len; |
| 89 |
return len; |
| 90 |
} |
| 91 |
void resize(unsigned int newsize); |
| 92 |
void clear() |
| 93 |
{ |
| 94 |
readpos=writepos=0; |
| 95 |
wrtot=0; |
| 96 |
} |
| 97 |
unsigned int getsize() const |
| 98 |
{ |
| 99 |
return size; |
| 100 |
} |
| 101 |
unsigned int putdata(const void *data, unsigned int len, bool autoresize=false); |
| 102 |
unsigned int getdata(void *data, unsigned int len); |
| 103 |
int readdata(int fd); |
| 104 |
int writedata(int fd); |
| 105 |
|
| 106 |
int writedata(int fd, unsigned int minspace) |
| 107 |
{ |
| 108 |
int wrn=0; |
| 109 |
if (minspace>size) { |
| 110 |
while (readpos<writepos) { |
| 111 |
int w=writedata(fd); |
| 112 |
if (w<0) |
| 113 |
return w; |
| 114 |
wrn+=w; |
| 115 |
} |
| 116 |
readpos=writepos=0; |
| 117 |
resize(minspace); |
| 118 |
} else { |
| 119 |
while (freebytes()<minspace) { |
| 120 |
int w=writedata(fd); |
| 121 |
if (w<0) |
| 122 |
return w; |
| 123 |
wrn+=w; |
| 124 |
} |
| 125 |
} |
| 126 |
return wrn; |
| 127 |
} |
| 128 |
|
| 129 |
int flush(int fd) |
| 130 |
{ |
| 131 |
int wrn=0; |
| 132 |
while (inbytes()>0) { |
| 133 |
int w=writedata(fd); |
| 134 |
if (w<0) |
| 135 |
return w; |
| 136 |
wrn+=w; |
| 137 |
} |
| 138 |
return wrn; |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
class inbuffer { |
| 143 |
protected: |
| 144 |
void *d; |
| 145 |
unsigned int size, mmapsize, readpos, writepos; |
| 146 |
|
| 147 |
struct infile { |
| 148 |
dvbcut_off_t off; |
| 149 |
dvbcut_off_t end; |
| 150 |
bool closeme; |
| 151 |
int fd; |
| 152 |
std::string name; |
| 153 |
}; |
| 154 |
std::vector<infile> files; |
| 155 |
bool eof; |
| 156 |
dvbcut_off_t pos; |
| 157 |
dvbcut_off_t filesize; |
| 158 |
bool mmapped; |
| 159 |
static long pagesize; |
| 160 |
bool sequential; |
| 161 |
bool pipe_mode; |
| 162 |
|
| 163 |
void close(); |
| 164 |
int pipedata(unsigned int amount, long long position); |
| 165 |
|
| 166 |
public: |
| 167 |
inbuffer(unsigned int _size, unsigned int mmapsize = 0); |
| 168 |
~inbuffer(); |
| 169 |
bool open(int fd, std::string *errmsg = 0, bool closeme = false, std::string filename=""); |
| 170 |
bool open(std::string filename, std::string *errmsg = 0); |
| 171 |
void reset(); |
| 172 |
|
| 173 |
const void *data() const { return (void*)((char*)d + readpos); } |
| 174 |
unsigned int getsize() const { return size; } |
| 175 |
unsigned int inbytes() const { return writepos - readpos; } |
| 176 |
bool iseof() const { return eof; } |
| 177 |
|
| 178 |
int providedata(unsigned int amount, long long position); |
| 179 |
int providedata(unsigned int amount) { |
| 180 |
if (amount <= inbytes()) |
| 181 |
return inbytes(); |
| 182 |
return providedata(amount, pos + readpos); |
| 183 |
} |
| 184 |
void discarddata(unsigned int amount) { |
| 185 |
readpos += amount; |
| 186 |
if (readpos >= writepos) { |
| 187 |
pos += writepos; |
| 188 |
readpos = 0; |
| 189 |
writepos = 0; |
| 190 |
} |
| 191 |
} |
| 192 |
dvbcut_off_t getfilesize() const { return filesize; } |
| 193 |
dvbcut_off_t getfilepos() const { return pos + readpos; } |
| 194 |
int getfilenum(dvbcut_off_t offset, dvbcut_off_t &fileoff); |
| 195 |
std::string getfilename(int filenum); |
| 196 |
void setsequential(bool flag) { sequential = flag; } |
| 197 |
}; |
| 198 |
|
| 199 |
class outbuffer : protected buffer |
| 200 |
{ |
| 201 |
protected: |
| 202 |
int fd; |
| 203 |
bool close; |
| 204 |
public: |
| 205 |
outbuffer(unsigned int _size, int _fd=-1, bool tobeclosed=true) : |
| 206 |
buffer(_size), fd(_fd), close(tobeclosed) |
| 207 |
{} |
| 208 |
~outbuffer(); |
| 209 |
int open(const char *filename); |
| 210 |
int putdata(const void *data, unsigned int len, bool autoresize=false); |
| 211 |
|
| 212 |
using buffer::written; |
| 213 |
using buffer::getsize; |
| 214 |
using buffer::resize; |
| 215 |
}; |
| 216 |
|
| 217 |
#endif |