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
39 * $FreeBSD: src/usr.bin/make/buf.c,v 1.11 1999/09/11 13:08:01 hoek Exp $
40 * $DragonFly: src/usr.bin/make/buf.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
45 * Functions for automatically-expanded buffers.
53 #define max(a,b) ((a) > (b) ? (a) : (b))
58 * Expand the given buffer to hold the given number of additional
60 * Makes sure there's room for an extra NULL byte at the end of the
61 * buffer in case it holds a string.
63 #define BufExpand(bp,nb) \
64 if (bp->left < (nb)+1) {\
65 int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
66 Byte *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
68 (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
69 (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
70 (bp)->buffer = newBuf;\
71 (bp)->size = newSize;\
72 (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
75 #define BUF_DEF_SIZE 256 /* Default buffer size */
76 #define BUF_ADD_INC 256 /* Expansion increment when Adding */
77 #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
80 *-----------------------------------------------------------------------
82 * Add a single byte to the buffer. left is zero or negative.
88 * The buffer may be expanded.
90 *-----------------------------------------------------------------------
93 Buf_OvAddByte (bp, byte)
99 BufExpand (bp, nbytes);
111 *-----------------------------------------------------------------------
113 * Add a number of bytes to the buffer.
121 *-----------------------------------------------------------------------
124 Buf_AddBytes (bp, numBytes, bytesPtr)
127 const Byte *bytesPtr;
130 BufExpand (bp, numBytes);
132 memcpy (bp->inPtr, bytesPtr, numBytes);
133 bp->inPtr += numBytes;
134 bp->left -= numBytes;
143 *-----------------------------------------------------------------------
145 * Place the byte back at the beginning of the buffer.
148 * SUCCESS if the byte was added ok. FAILURE if not.
151 * The byte is stuffed in the buffer and outPtr is decremented.
153 *-----------------------------------------------------------------------
156 Buf_UngetByte (bp, byte)
161 if (bp->outPtr != bp->buffer) {
164 } else if (bp->outPtr == bp->inPtr) {
171 * Yech. have to expand the buffer to stuff this thing in.
172 * We use a different expansion constant because people don't
173 * usually push back many bytes when they're doing it a byte at
176 int numBytes = bp->inPtr - bp->outPtr;
179 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
180 memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
181 bp->outPtr = newBuf + BUF_UNGET_INC;
182 bp->inPtr = bp->outPtr + numBytes;
183 free ((char *)bp->buffer);
185 bp->size += BUF_UNGET_INC;
186 bp->left = bp->size - (bp->inPtr - bp->buffer);
193 *-----------------------------------------------------------------------
195 * Push back a series of bytes at the beginning of the buffer.
201 * outPtr is decremented and the bytes copied into the buffer.
203 *-----------------------------------------------------------------------
206 Buf_UngetBytes (bp, numBytes, bytesPtr)
212 if (bp->outPtr - bp->buffer >= numBytes) {
213 bp->outPtr -= numBytes;
214 memcpy (bp->outPtr, bytesPtr, numBytes);
215 } else if (bp->outPtr == bp->inPtr) {
216 Buf_AddBytes (bp, numBytes, bytesPtr);
218 int curNumBytes = bp->inPtr - bp->outPtr;
220 int newBytes = max(numBytes,BUF_UNGET_INC);
222 newBuf = (Byte *)emalloc (bp->size + newBytes);
223 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
224 bp->outPtr = newBuf + newBytes;
225 bp->inPtr = bp->outPtr + curNumBytes;
226 free ((char *)bp->buffer);
228 bp->size += newBytes;
229 bp->left = bp->size - (bp->inPtr - bp->buffer);
230 bp->outPtr -= numBytes;
231 memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
236 *-----------------------------------------------------------------------
238 * Return the next byte from the buffer. Actually returns an integer.
241 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
242 * itself if there is one.
245 * outPtr is incremented and both outPtr and inPtr will be reset if
246 * the buffer is emptied.
248 *-----------------------------------------------------------------------
256 if (bp->inPtr == bp->outPtr) {
259 res = (int) *bp->outPtr;
261 if (bp->outPtr == bp->inPtr) {
262 bp->outPtr = bp->inPtr = bp->buffer;
271 *-----------------------------------------------------------------------
273 * Extract a number of bytes from the buffer.
276 * The number of bytes gotten.
279 * The passed array is overwritten.
281 *-----------------------------------------------------------------------
284 Buf_GetBytes (bp, numBytes, bytesPtr)
290 if (bp->inPtr - bp->outPtr < numBytes) {
291 numBytes = bp->inPtr - bp->outPtr;
293 memcpy (bytesPtr, bp->outPtr, numBytes);
294 bp->outPtr += numBytes;
296 if (bp->outPtr == bp->inPtr) {
297 bp->outPtr = bp->inPtr = bp->buffer;
305 *-----------------------------------------------------------------------
307 * Get all the available data at once.
310 * A pointer to the data and the number of bytes available.
315 *-----------------------------------------------------------------------
318 Buf_GetAll (bp, numBytesPtr)
323 if (numBytesPtr != (int *)NULL) {
324 *numBytesPtr = bp->inPtr - bp->outPtr;
331 *-----------------------------------------------------------------------
333 * Throw away bytes in a buffer.
339 * The bytes are discarded.
341 *-----------------------------------------------------------------------
344 Buf_Discard (bp, numBytes)
349 if (bp->inPtr - bp->outPtr <= numBytes) {
350 bp->inPtr = bp->outPtr = bp->buffer;
354 bp->outPtr += numBytes;
359 *-----------------------------------------------------------------------
361 * Returns the number of bytes in the given buffer. Doesn't include
362 * the null-terminating byte.
365 * The number of bytes.
370 *-----------------------------------------------------------------------
376 return (buf->inPtr - buf->outPtr);
380 *-----------------------------------------------------------------------
382 * Initialize a buffer. If no initial size is given, a reasonable
386 * A buffer to be given to other functions in this library.
389 * The buffer is created, the space allocated and pointers
392 *-----------------------------------------------------------------------
396 int size; /* Initial size for the buffer */
398 Buffer bp; /* New Buffer */
400 bp = (Buffer)emalloc(sizeof(*bp));
405 bp->left = bp->size = size;
406 bp->buffer = (Byte *)emalloc(size);
407 bp->inPtr = bp->outPtr = bp->buffer;
414 *-----------------------------------------------------------------------
416 * Nuke a buffer and all its resources.
422 * The buffer is freed.
424 *-----------------------------------------------------------------------
427 Buf_Destroy (buf, freeData)
428 Buffer buf; /* Buffer to destroy */
429 Boolean freeData; /* TRUE if the data should be destroyed as well */
433 free ((char *)buf->buffer);
439 *-----------------------------------------------------------------------
440 * Buf_ReplaceLastByte --
441 * Replace the last byte in a buffer.
447 * If the buffer was empty intially, then a new byte will be added.
448 * Otherwise, the last byte is overwritten.
450 *-----------------------------------------------------------------------
453 Buf_ReplaceLastByte (buf, byte)
454 Buffer buf; /* buffer to augment */
455 int byte; /* byte to be written */
457 if (buf->inPtr == buf->outPtr)
458 Buf_AddByte(buf, byte);
460 *(buf->inPtr - 1) = byte;