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.15 2005/01/09 23:03:28 okumoto Exp $
46 * Functions for automatically-expanded buffers.
57 #define max(a,b) ((a) > (b) ? (a) : (b))
62 * Expand the given buffer to hold the given number of additional
64 * Makes sure there's room for an extra NULL byte at the end of the
65 * buffer in case it holds a string.
67 #define BufExpand(bp, nb) do { \
68 if ((bp)->left < (nb) + 1) { \
69 int newSize = (bp)->size + max((nb) + 1, BUF_ADD_INC); \
70 Byte *newBuf = erealloc((bp)->buffer, newSize); \
72 (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
73 (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer); \
74 (bp)->buffer = newBuf; \
75 (bp)->size = newSize; \
76 (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer); \
80 #define BUF_DEF_SIZE 256 /* Default buffer size */
81 #define BUF_ADD_INC 256 /* Expansion increment when Adding */
82 #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
85 *-----------------------------------------------------------------------
87 * Add a single byte to the buffer. left is zero or negative.
93 * The buffer may be expanded.
95 *-----------------------------------------------------------------------
98 Buf_OvAddByte(Buffer *bp, Byte byte)
113 *-----------------------------------------------------------------------
115 * Add a number of bytes to the buffer.
123 *-----------------------------------------------------------------------
126 Buf_AddBytes(Buffer *bp, size_t numBytes, const Byte *bytesPtr)
128 BufExpand(bp, numBytes);
130 memcpy(bp->inPtr, bytesPtr, numBytes);
131 bp->inPtr += numBytes;
132 bp->left -= numBytes;
141 *-----------------------------------------------------------------------
143 * Get all the available data at once.
146 * A pointer to the data and the number of bytes available.
151 *-----------------------------------------------------------------------
154 Buf_GetAll(Buffer *bp, size_t *numBytesPtr)
156 if (numBytesPtr != NULL)
157 *numBytesPtr = bp->inPtr - bp->outPtr;
163 *-----------------------------------------------------------------------
165 * Throw away bytes in a buffer.
171 * The bytes are discarded.
173 *-----------------------------------------------------------------------
176 Buf_Discard(Buffer *bp, size_t numBytes)
178 if ((size_t)(bp->inPtr - bp->outPtr) <= numBytes) {
179 bp->inPtr = bp->outPtr = bp->buffer;
183 bp->outPtr += numBytes;
187 *-----------------------------------------------------------------------
189 * Returns the number of bytes in the given buffer. Doesn't include
190 * the null-terminating byte.
193 * The number of bytes.
198 *-----------------------------------------------------------------------
201 Buf_Size(Buffer *buf)
203 return (buf->inPtr - buf->outPtr);
207 *-----------------------------------------------------------------------
209 * Initialize a buffer. If no initial size is given, a reasonable
213 * A buffer to be given to other functions in this library.
216 * The buffer is created, the space allocated and pointers
219 *-----------------------------------------------------------------------
222 Buf_Init(size_t size)
224 Buffer *bp; /* New Buffer */
226 bp = emalloc(sizeof(*bp));
231 bp->left = bp->size = size;
232 bp->buffer = emalloc(size);
233 bp->inPtr = bp->outPtr = bp->buffer;
240 *-----------------------------------------------------------------------
242 * Destroy a buffer, and optionally free its data, too.
248 * The buffer is freed.
250 *-----------------------------------------------------------------------
253 Buf_Destroy(Buffer *buf, Boolean freeData)
261 *-----------------------------------------------------------------------
262 * Buf_ReplaceLastByte --
263 * Replace the last byte in a buffer.
269 * If the buffer was empty intially, then a new byte will be added.
270 * Otherwise, the last byte is overwritten.
272 *-----------------------------------------------------------------------
275 Buf_ReplaceLastByte(Buffer *buf, Byte byte)
277 if (buf->inPtr == buf->outPtr)
278 Buf_AddByte(buf, byte);
280 *(buf->inPtr - 1) = byte;