2 Copyright 2005 Mark Calderbank
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 void Stream::shutdown()
41 int Stream::init(int bufsize)
43 outbuf = (UCHAR*) malloc(bufsize);
44 if (!outbuf) return 0;
60 int Stream::put(UCHAR* inbuf, int len)
63 int tail = bufferTail;
64 int head = bufferHead;
65 if (tail == 0) tail = bufferSize;
69 // The free space is in one continuous chunk.
70 if (len < tail - head)
72 memcpy(outbuf + head, inbuf, len);
77 else if (len <= bufferSize - head)
79 // There is enough space above the Head.
80 memcpy(outbuf + head, inbuf, len);
81 if (head + len == bufferSize)
90 memcpy(outbuf, inbuf, len);
97 int Stream::drain(int fd)
100 int head = bufferHead;
101 int tail = bufferTail;
102 int mark = bufferMark;
105 if (mark == -1 && tail > head) mark = bufferSize;
109 // Drain up to the marker.
110 written = write(fd, outbuf + tail, (mark - tail));
111 if (written < 0) return ret;
113 if (written == (mark - tail))
116 bufferTail = tail = 0;
120 bufferTail += written;
125 if (tail == head) return ret; // Empty
127 written = write(fd, outbuf + tail, (head - tail));
128 if (written < 0) return ret;
130 bufferTail = tail + written;