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 1;
46 bufferHead = bufferTail = 0;
54 bufferHead = bufferTail = 0;
58 int Stream::put(UCHAR* inbuf, int len)
61 int localTail = bufferTail;
62 if (len == 0 || (full)) return ret;
64 if (bufferHead >= localTail)
66 // We have free rein to the end of the buffer
67 if (len < (bufferSize - bufferHead))
69 // We won't reach the end of the buffer.
70 memcpy(outbuf + bufferHead, inbuf, len);
72 ret += len; return ret;
75 // We'll reach the end of the buffer and loop round.
76 memcpy(outbuf + bufferHead, inbuf, (bufferSize - bufferHead));
77 inbuf += (bufferSize - bufferHead);
78 ret += (bufferSize - bufferHead);
79 len -= (bufferSize - bufferHead);
81 if (localTail == 0) full = 1;
82 if (len == 0 || (full)) return ret;
85 // We can advance the head up to the tail
86 // If we reach it, we are full.
87 if (len < (localTail - bufferHead))
89 // We'll fall short of the tail.
90 memcpy(outbuf + bufferHead, inbuf, len);
92 ret += len; return ret;
95 // We'll hit the tail.
96 memcpy(outbuf + bufferHead, inbuf, (localTail - bufferHead));
97 ret += (localTail - bufferHead);
98 bufferHead = localTail; full = 1;
103 int Stream::drain(int fd)
106 int localHead = bufferHead;
108 if (localHead == bufferTail && (!full)) return ret;
110 if (localHead <= bufferTail)
112 // Drain to the end of the buffer
113 written = write(fd, outbuf + bufferTail, (bufferSize - bufferTail));
114 if (written < 0) return ret;
117 if (written == (bufferSize - bufferTail))
120 bufferTail += written;
121 if (localHead == 0) return ret;
123 written = write(fd, outbuf + bufferTail, (localHead - bufferTail));
124 if (written < 0) return ret;
126 bufferTail += written;