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.6 2004/11/18 02:01:39 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)
109 *-----------------------------------------------------------------------
111 * Add a number of bytes to the buffer.
119 *-----------------------------------------------------------------------
122 Buf_AddBytes (Buffer bp, int numBytes, const Byte *bytesPtr)
125 BufExpand (bp, numBytes);
127 memcpy (bp->inPtr, bytesPtr, numBytes);
128 bp->inPtr += numBytes;
129 bp->left -= numBytes;
138 *-----------------------------------------------------------------------
140 * Place the byte back at the beginning of the buffer.
143 * SUCCESS if the byte was added ok. FAILURE if not.
146 * The byte is stuffed in the buffer and outPtr is decremented.
148 *-----------------------------------------------------------------------
151 Buf_UngetByte (Buffer bp, int byte)
154 if (bp->outPtr != bp->buffer) {
157 } else if (bp->outPtr == bp->inPtr) {
164 * Yech. have to expand the buffer to stuff this thing in.
165 * We use a different expansion constant because people don't
166 * usually push back many bytes when they're doing it a byte at
169 int numBytes = bp->inPtr - bp->outPtr;
172 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
173 memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
174 bp->outPtr = newBuf + BUF_UNGET_INC;
175 bp->inPtr = bp->outPtr + numBytes;
176 free ((char *)bp->buffer);
178 bp->size += BUF_UNGET_INC;
179 bp->left = bp->size - (bp->inPtr - bp->buffer);
186 *-----------------------------------------------------------------------
188 * Push back a series of bytes at the beginning of the buffer.
194 * outPtr is decremented and the bytes copied into the buffer.
196 *-----------------------------------------------------------------------
199 Buf_UngetBytes (Buffer bp, int numBytes, Byte *bytesPtr)
202 if (bp->outPtr - bp->buffer >= numBytes) {
203 bp->outPtr -= numBytes;
204 memcpy (bp->outPtr, bytesPtr, numBytes);
205 } else if (bp->outPtr == bp->inPtr) {
206 Buf_AddBytes (bp, numBytes, bytesPtr);
208 int curNumBytes = bp->inPtr - bp->outPtr;
210 int newBytes = max(numBytes,BUF_UNGET_INC);
212 newBuf = (Byte *)emalloc (bp->size + newBytes);
213 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
214 bp->outPtr = newBuf + newBytes;
215 bp->inPtr = bp->outPtr + curNumBytes;
216 free ((char *)bp->buffer);
218 bp->size += newBytes;
219 bp->left = bp->size - (bp->inPtr - bp->buffer);
220 bp->outPtr -= numBytes;
221 memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
226 *-----------------------------------------------------------------------
228 * Return the next byte from the buffer. Actually returns an integer.
231 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
232 * itself if there is one.
235 * outPtr is incremented and both outPtr and inPtr will be reset if
236 * the buffer is emptied.
238 *-----------------------------------------------------------------------
241 Buf_GetByte (Buffer bp)
245 if (bp->inPtr == bp->outPtr) {
248 res = (int) *bp->outPtr;
250 if (bp->outPtr == bp->inPtr) {
251 bp->outPtr = bp->inPtr = bp->buffer;
260 *-----------------------------------------------------------------------
262 * Extract a number of bytes from the buffer.
265 * The number of bytes gotten.
268 * The passed array is overwritten.
270 *-----------------------------------------------------------------------
273 Buf_GetBytes (Buffer bp, int numBytes, Byte *bytesPtr)
276 if (bp->inPtr - bp->outPtr < numBytes) {
277 numBytes = bp->inPtr - bp->outPtr;
279 memcpy (bytesPtr, bp->outPtr, numBytes);
280 bp->outPtr += numBytes;
282 if (bp->outPtr == bp->inPtr) {
283 bp->outPtr = bp->inPtr = bp->buffer;
291 *-----------------------------------------------------------------------
293 * Get all the available data at once.
296 * A pointer to the data and the number of bytes available.
301 *-----------------------------------------------------------------------
304 Buf_GetAll (Buffer bp, int *numBytesPtr)
307 if (numBytesPtr != (int *)NULL) {
308 *numBytesPtr = bp->inPtr - bp->outPtr;
315 *-----------------------------------------------------------------------
317 * Throw away bytes in a buffer.
323 * The bytes are discarded.
325 *-----------------------------------------------------------------------
328 Buf_Discard (Buffer bp, int numBytes)
331 if (bp->inPtr - bp->outPtr <= numBytes) {
332 bp->inPtr = bp->outPtr = bp->buffer;
336 bp->outPtr += numBytes;
341 *-----------------------------------------------------------------------
343 * Returns the number of bytes in the given buffer. Doesn't include
344 * the null-terminating byte.
347 * The number of bytes.
352 *-----------------------------------------------------------------------
355 Buf_Size (Buffer buf)
357 return (buf->inPtr - buf->outPtr);
361 *-----------------------------------------------------------------------
363 * Initialize a buffer. If no initial size is given, a reasonable
367 * A buffer to be given to other functions in this library.
370 * The buffer is created, the space allocated and pointers
373 *-----------------------------------------------------------------------
378 Buffer bp; /* New Buffer */
380 bp = (Buffer)emalloc(sizeof(*bp));
385 bp->left = bp->size = size;
386 bp->buffer = (Byte *)emalloc(size);
387 bp->inPtr = bp->outPtr = bp->buffer;
394 *-----------------------------------------------------------------------
396 * Destroy a buffer, and optionally free its data, too.
402 * The buffer is freed.
404 *-----------------------------------------------------------------------
407 Buf_Destroy (Buffer buf, Boolean freeData)
411 free ((char *)buf->buffer);
417 *-----------------------------------------------------------------------
418 * Buf_ReplaceLastByte --
419 * Replace the last byte in a buffer.
425 * If the buffer was empty intially, then a new byte will be added.
426 * Otherwise, the last byte is overwritten.
428 *-----------------------------------------------------------------------
431 Buf_ReplaceLastByte (Buffer buf, int byte)
433 if (buf->inPtr == buf->outPtr)
434 Buf_AddByte(buf, byte);
436 *(buf->inPtr - 1) = byte;