2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)buf.c 8.1 (Berkeley) 6/6/93
40 * $FreeBSD: src/usr.bin/make/buf.c,v 1.11 1999/09/11 13:08:01 hoek Exp $
41 * $DragonFly: src/usr.bin/make/buf.c,v 1.5 2004/11/12 22:57:04 dillon Exp $
46 * Functions for automatically-expanded buffers.
54 #define max(a,b) ((a) > (b) ? (a) : (b))
59 * Expand the given buffer to hold the given number of additional
61 * Makes sure there's room for an extra NULL byte at the end of the
62 * buffer in case it holds a string.
64 #define BufExpand(bp,nb) \
65 if (bp->left < (nb)+1) {\
66 int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
67 Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
69 (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
70 (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
71 (bp)->buffer = newBuf;\
72 (bp)->size = newSize;\
73 (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
76 #define BUF_DEF_SIZE 256 /* Default buffer size */
77 #define BUF_ADD_INC 256 /* Expansion increment when Adding */
78 #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
81 *-----------------------------------------------------------------------
83 * Add a single byte to the buffer. left is zero or negative.
89 * The buffer may be expanded.
91 *-----------------------------------------------------------------------
94 Buf_OvAddByte (Buffer bp, int byte)
98 BufExpand (bp, nbytes);
110 *-----------------------------------------------------------------------
112 * Add a number of bytes to the buffer.
120 *-----------------------------------------------------------------------
123 Buf_AddBytes (Buffer bp, int numBytes, const Byte *bytesPtr)
126 BufExpand (bp, numBytes);
128 memcpy (bp->inPtr, bytesPtr, numBytes);
129 bp->inPtr += numBytes;
130 bp->left -= numBytes;
139 *-----------------------------------------------------------------------
141 * Place the byte back at the beginning of the buffer.
144 * SUCCESS if the byte was added ok. FAILURE if not.
147 * The byte is stuffed in the buffer and outPtr is decremented.
149 *-----------------------------------------------------------------------
152 Buf_UngetByte (Buffer bp, int byte)
155 if (bp->outPtr != bp->buffer) {
158 } else if (bp->outPtr == bp->inPtr) {
165 * Yech. have to expand the buffer to stuff this thing in.
166 * We use a different expansion constant because people don't
167 * usually push back many bytes when they're doing it a byte at
170 int numBytes = bp->inPtr - bp->outPtr;
173 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
174 memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
175 bp->outPtr = newBuf + BUF_UNGET_INC;
176 bp->inPtr = bp->outPtr + numBytes;
177 free ((char *)bp->buffer);
179 bp->size += BUF_UNGET_INC;
180 bp->left = bp->size - (bp->inPtr - bp->buffer);
187 *-----------------------------------------------------------------------
189 * Push back a series of bytes at the beginning of the buffer.
195 * outPtr is decremented and the bytes copied into the buffer.
197 *-----------------------------------------------------------------------
200 Buf_UngetBytes (Buffer bp, int numBytes, Byte *bytesPtr)
203 if (bp->outPtr - bp->buffer >= numBytes) {
204 bp->outPtr -= numBytes;
205 memcpy (bp->outPtr, bytesPtr, numBytes);
206 } else if (bp->outPtr == bp->inPtr) {
207 Buf_AddBytes (bp, numBytes, bytesPtr);
209 int curNumBytes = bp->inPtr - bp->outPtr;
211 int newBytes = max(numBytes,BUF_UNGET_INC);
213 newBuf = (Byte *)emalloc (bp->size + newBytes);
214 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
215 bp->outPtr = newBuf + newBytes;
216 bp->inPtr = bp->outPtr + curNumBytes;
217 free ((char *)bp->buffer);
219 bp->size += newBytes;
220 bp->left = bp->size - (bp->inPtr - bp->buffer);
221 bp->outPtr -= numBytes;
222 memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
227 *-----------------------------------------------------------------------
229 * Return the next byte from the buffer. Actually returns an integer.
232 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
233 * itself if there is one.
236 * outPtr is incremented and both outPtr and inPtr will be reset if
237 * the buffer is emptied.
239 *-----------------------------------------------------------------------
242 Buf_GetByte (Buffer bp)
246 if (bp->inPtr == bp->outPtr) {
249 res = (int) *bp->outPtr;
251 if (bp->outPtr == bp->inPtr) {
252 bp->outPtr = bp->inPtr = bp->buffer;
261 *-----------------------------------------------------------------------
263 * Extract a number of bytes from the buffer.
266 * The number of bytes gotten.
269 * The passed array is overwritten.
271 *-----------------------------------------------------------------------
274 Buf_GetBytes (Buffer bp, int numBytes, Byte *bytesPtr)
277 if (bp->inPtr - bp->outPtr < numBytes) {
278 numBytes = bp->inPtr - bp->outPtr;
280 memcpy (bytesPtr, bp->outPtr, numBytes);
281 bp->outPtr += numBytes;
283 if (bp->outPtr == bp->inPtr) {
284 bp->outPtr = bp->inPtr = bp->buffer;
292 *-----------------------------------------------------------------------
294 * Get all the available data at once.
297 * A pointer to the data and the number of bytes available.
302 *-----------------------------------------------------------------------
305 Buf_GetAll (Buffer bp, int *numBytesPtr)
308 if (numBytesPtr != (int *)NULL) {
309 *numBytesPtr = bp->inPtr - bp->outPtr;
316 *-----------------------------------------------------------------------
318 * Throw away bytes in a buffer.
324 * The bytes are discarded.
326 *-----------------------------------------------------------------------
329 Buf_Discard (Buffer bp, int numBytes)
332 if (bp->inPtr - bp->outPtr <= numBytes) {
333 bp->inPtr = bp->outPtr = bp->buffer;
337 bp->outPtr += numBytes;
342 *-----------------------------------------------------------------------
344 * Returns the number of bytes in the given buffer. Doesn't include
345 * the null-terminating byte.
348 * The number of bytes.
353 *-----------------------------------------------------------------------
356 Buf_Size (Buffer buf)
358 return (buf->inPtr - buf->outPtr);
362 *-----------------------------------------------------------------------
364 * Initialize a buffer. If no initial size is given, a reasonable
368 * A buffer to be given to other functions in this library.
371 * The buffer is created, the space allocated and pointers
374 *-----------------------------------------------------------------------
379 Buffer bp; /* New Buffer */
381 bp = (Buffer)emalloc(sizeof(*bp));
386 bp->left = bp->size = size;
387 bp->buffer = (Byte *)emalloc(size);
388 bp->inPtr = bp->outPtr = bp->buffer;
395 *-----------------------------------------------------------------------
397 * Destroy a buffer, and optionally free its data, too.
403 * The buffer is freed.
405 *-----------------------------------------------------------------------
408 Buf_Destroy (Buffer buf, Boolean freeData)
412 free ((char *)buf->buffer);
418 *-----------------------------------------------------------------------
419 * Buf_ReplaceLastByte --
420 * Replace the last byte in a buffer.
426 * If the buffer was empty intially, then a new byte will be added.
427 * Otherwise, the last byte is overwritten.
429 *-----------------------------------------------------------------------
432 Buf_ReplaceLastByte (Buffer buf, int byte)
434 if (buf->inPtr == buf->outPtr)
435 Buf_AddByte(buf, byte);
437 *(buf->inPtr - 1) = byte;