2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * Copyright (c) 1988, 1989 by Adam de Boor
4 * Copyright (c) 1989 by Berkeley Softworks
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)buf.c 8.1 (Berkeley) 6/6/93
42 #include <sys/cdefs.h>
48 * Functions for automatically-expanded buffers.
56 #define max(a,b) ((a) > (b) ? (a) : (b))
61 * Expand the given buffer to hold the given number of additional
63 * Makes sure there's room for an extra NULL byte at the end of the
64 * buffer in case it holds a string.
66 #define BufExpand(bp,nb) \
67 if (bp->left < (nb)+1) {\
68 int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
69 Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
71 (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
72 (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
73 (bp)->buffer = newBuf;\
74 (bp)->size = newSize;\
75 (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
78 #define BUF_DEF_SIZE 256 /* Default buffer size */
79 #define BUF_ADD_INC 256 /* Expansion increment when Adding */
80 #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
83 *-----------------------------------------------------------------------
85 * Add a single byte to the buffer. left is zero or negative.
91 * The buffer may be expanded.
93 *-----------------------------------------------------------------------
96 Buf_OvAddByte (bp, byte)
102 BufExpand (bp, nbytes);
114 *-----------------------------------------------------------------------
116 * Add a number of bytes to the buffer.
124 *-----------------------------------------------------------------------
127 Buf_AddBytes (bp, numBytes, bytesPtr)
130 const Byte *bytesPtr;
133 BufExpand (bp, numBytes);
135 memcpy (bp->inPtr, bytesPtr, numBytes);
136 bp->inPtr += numBytes;
137 bp->left -= numBytes;
146 *-----------------------------------------------------------------------
148 * Place the byte back at the beginning of the buffer.
151 * SUCCESS if the byte was added ok. FAILURE if not.
154 * The byte is stuffed in the buffer and outPtr is decremented.
156 *-----------------------------------------------------------------------
159 Buf_UngetByte (bp, byte)
164 if (bp->outPtr != bp->buffer) {
167 } else if (bp->outPtr == bp->inPtr) {
174 * Yech. have to expand the buffer to stuff this thing in.
175 * We use a different expansion constant because people don't
176 * usually push back many bytes when they're doing it a byte at
179 int numBytes = bp->inPtr - bp->outPtr;
182 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
183 memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
184 bp->outPtr = newBuf + BUF_UNGET_INC;
185 bp->inPtr = bp->outPtr + numBytes;
186 free ((char *)bp->buffer);
188 bp->size += BUF_UNGET_INC;
189 bp->left = bp->size - (bp->inPtr - bp->buffer);
196 *-----------------------------------------------------------------------
198 * Push back a series of bytes at the beginning of the buffer.
204 * outPtr is decremented and the bytes copied into the buffer.
206 *-----------------------------------------------------------------------
209 Buf_UngetBytes (bp, numBytes, bytesPtr)
215 if (bp->outPtr - bp->buffer >= numBytes) {
216 bp->outPtr -= numBytes;
217 memcpy (bp->outPtr, bytesPtr, numBytes);
218 } else if (bp->outPtr == bp->inPtr) {
219 Buf_AddBytes (bp, numBytes, bytesPtr);
221 int curNumBytes = bp->inPtr - bp->outPtr;
223 int newBytes = max(numBytes,BUF_UNGET_INC);
225 newBuf = (Byte *)emalloc (bp->size + newBytes);
226 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
227 bp->outPtr = newBuf + newBytes;
228 bp->inPtr = bp->outPtr + curNumBytes;
229 free ((char *)bp->buffer);
231 bp->size += newBytes;
232 bp->left = bp->size - (bp->inPtr - bp->buffer);
233 bp->outPtr -= numBytes;
234 memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
239 *-----------------------------------------------------------------------
241 * Return the next byte from the buffer. Actually returns an integer.
244 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
245 * itself if there is one.
248 * outPtr is incremented and both outPtr and inPtr will be reset if
249 * the buffer is emptied.
251 *-----------------------------------------------------------------------
259 if (bp->inPtr == bp->outPtr) {
262 res = (int) *bp->outPtr;
264 if (bp->outPtr == bp->inPtr) {
265 bp->outPtr = bp->inPtr = bp->buffer;
274 *-----------------------------------------------------------------------
276 * Extract a number of bytes from the buffer.
279 * The number of bytes gotten.
282 * The passed array is overwritten.
284 *-----------------------------------------------------------------------
287 Buf_GetBytes (bp, numBytes, bytesPtr)
293 if (bp->inPtr - bp->outPtr < numBytes) {
294 numBytes = bp->inPtr - bp->outPtr;
296 memcpy (bytesPtr, bp->outPtr, numBytes);
297 bp->outPtr += numBytes;
299 if (bp->outPtr == bp->inPtr) {
300 bp->outPtr = bp->inPtr = bp->buffer;
308 *-----------------------------------------------------------------------
310 * Get all the available data at once.
313 * A pointer to the data and the number of bytes available.
318 *-----------------------------------------------------------------------
321 Buf_GetAll (bp, numBytesPtr)
326 if (numBytesPtr != (int *)NULL) {
327 *numBytesPtr = bp->inPtr - bp->outPtr;
334 *-----------------------------------------------------------------------
336 * Throw away bytes in a buffer.
342 * The bytes are discarded.
344 *-----------------------------------------------------------------------
347 Buf_Discard (bp, numBytes)
352 if (bp->inPtr - bp->outPtr <= numBytes) {
353 bp->inPtr = bp->outPtr = bp->buffer;
357 bp->outPtr += numBytes;
362 *-----------------------------------------------------------------------
364 * Returns the number of bytes in the given buffer. Doesn't include
365 * the null-terminating byte.
368 * The number of bytes.
373 *-----------------------------------------------------------------------
379 return (buf->inPtr - buf->outPtr);
383 *-----------------------------------------------------------------------
385 * Initialize a buffer. If no initial size is given, a reasonable
389 * A buffer to be given to other functions in this library.
392 * The buffer is created, the space allocated and pointers
395 *-----------------------------------------------------------------------
399 int size; /* Initial size for the buffer */
401 Buffer bp; /* New Buffer */
403 bp = (Buffer)emalloc(sizeof(*bp));
408 bp->left = bp->size = size;
409 bp->buffer = (Byte *)emalloc(size);
410 bp->inPtr = bp->outPtr = bp->buffer;
417 *-----------------------------------------------------------------------
419 * Nuke a buffer and all its resources.
425 * The buffer is freed.
427 *-----------------------------------------------------------------------
430 Buf_Destroy (buf, freeData)
431 Buffer buf; /* Buffer to destroy */
432 Boolean freeData; /* TRUE if the data should be destroyed as well */
436 free ((char *)buf->buffer);
442 *-----------------------------------------------------------------------
443 * Buf_ReplaceLastByte --
444 * Replace the last byte in a buffer.
450 * If the buffer was empty intially, then a new byte will be added.
451 * Otherwise, the last byte is overwritten.
453 *-----------------------------------------------------------------------
456 Buf_ReplaceLastByte (buf, byte)
457 Buffer buf; /* buffer to augment */
458 int byte; /* byte to be written */
460 if (buf->inPtr == buf->outPtr)
461 Buf_AddByte(buf, byte);
463 *(buf->inPtr - 1) = byte;