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.11 2004/12/16 22:23:59 okumoto 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) do { \
65 if ((bp)->left < (nb) + 1) { \
66 int newSize = (bp)->size + max((nb) + 1, BUF_ADD_INC); \
67 Byte *newBuf = 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); \
77 #define BUF_DEF_SIZE 256 /* Default buffer size */
78 #define BUF_ADD_INC 256 /* Expansion increment when Adding */
79 #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
82 *-----------------------------------------------------------------------
84 * Add a single byte to the buffer. left is zero or negative.
90 * The buffer may be expanded.
92 *-----------------------------------------------------------------------
95 Buf_OvAddByte(Buffer bp, Byte byte)
111 *-----------------------------------------------------------------------
113 * Add a number of bytes to the buffer.
121 *-----------------------------------------------------------------------
124 Buf_AddBytes(Buffer bp, size_t numBytes, const Byte *bytesPtr)
127 BufExpand(bp, numBytes);
129 memcpy(bp->inPtr, bytesPtr, numBytes);
130 bp->inPtr += numBytes;
131 bp->left -= numBytes;
140 *-----------------------------------------------------------------------
142 * Place the byte back at the beginning of the buffer.
145 * SUCCESS if the byte was added ok. FAILURE if not.
148 * The byte is stuffed in the buffer and outPtr is decremented.
150 *-----------------------------------------------------------------------
153 Buf_UngetByte(Buffer bp, Byte byte)
156 if (bp->outPtr != bp->buffer) {
160 } else if (bp->outPtr == bp->inPtr) {
168 * Yech. have to expand the buffer to stuff this thing in.
169 * We use a different expansion constant because people don't
170 * usually push back many bytes when they're doing it a byte at
173 size_t numBytes = bp->inPtr - bp->outPtr;
176 newBuf = emalloc(bp->size + BUF_UNGET_INC);
177 memcpy(newBuf + BUF_UNGET_INC, bp->outPtr, numBytes + 1);
178 bp->outPtr = newBuf + BUF_UNGET_INC;
179 bp->inPtr = bp->outPtr + numBytes;
182 bp->size += BUF_UNGET_INC;
183 bp->left = bp->size - (bp->inPtr - bp->buffer);
190 *-----------------------------------------------------------------------
192 * Push back a series of bytes at the beginning of the buffer.
198 * outPtr is decremented and the bytes copied into the buffer.
200 *-----------------------------------------------------------------------
203 Buf_UngetBytes(Buffer bp, size_t numBytes, Byte *bytesPtr)
206 if ((size_t)(bp->outPtr - bp->buffer) >= numBytes) {
207 bp->outPtr -= numBytes;
208 memcpy(bp->outPtr, bytesPtr, numBytes);
209 } else if (bp->outPtr == bp->inPtr) {
210 Buf_AddBytes(bp, numBytes, bytesPtr);
212 size_t curNumBytes = bp->inPtr - bp->outPtr;
214 size_t newBytes = max(numBytes, BUF_UNGET_INC);
216 newBuf = emalloc(bp->size + newBytes);
217 memcpy(newBuf + newBytes, bp->outPtr, curNumBytes + 1);
218 bp->outPtr = newBuf + newBytes;
219 bp->inPtr = bp->outPtr + curNumBytes;
222 bp->size += newBytes;
223 bp->left = bp->size - (bp->inPtr - bp->buffer);
224 bp->outPtr -= numBytes;
225 memcpy(bp->outPtr, bytesPtr, numBytes);
230 *-----------------------------------------------------------------------
232 * Return the next byte from the buffer. Actually returns an integer.
235 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
236 * itself if there is one.
239 * outPtr is incremented and both outPtr and inPtr will be reset if
240 * the buffer is emptied.
242 *-----------------------------------------------------------------------
245 Buf_GetByte(Buffer bp)
249 if (bp->inPtr == bp->outPtr)
252 res = (int)*bp->outPtr;
254 if (bp->outPtr == bp->inPtr) {
255 bp->outPtr = bp->inPtr = bp->buffer;
263 *-----------------------------------------------------------------------
265 * Extract a number of bytes from the buffer.
268 * The number of bytes gotten.
271 * The passed array is overwritten.
273 *-----------------------------------------------------------------------
276 Buf_GetBytes(Buffer bp, size_t numBytes, Byte *bytesPtr)
279 if ((size_t)(bp->inPtr - bp->outPtr) < numBytes)
280 numBytes = bp->inPtr - bp->outPtr;
282 memcpy(bytesPtr, bp->outPtr, numBytes);
283 bp->outPtr += numBytes;
285 if (bp->outPtr == bp->inPtr) {
286 bp->outPtr = bp->inPtr = bp->buffer;
294 *-----------------------------------------------------------------------
296 * Get all the available data at once.
299 * A pointer to the data and the number of bytes available.
304 *-----------------------------------------------------------------------
307 Buf_GetAll(Buffer bp, size_t *numBytesPtr)
310 if (numBytesPtr != NULL)
311 *numBytesPtr = bp->inPtr - bp->outPtr;
317 *-----------------------------------------------------------------------
319 * Throw away bytes in a buffer.
325 * The bytes are discarded.
327 *-----------------------------------------------------------------------
330 Buf_Discard(Buffer bp, size_t numBytes)
333 if ((size_t)(bp->inPtr - bp->outPtr) <= numBytes) {
334 bp->inPtr = bp->outPtr = bp->buffer;
338 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 *-----------------------------------------------------------------------
359 return (buf->inPtr - buf->outPtr);
363 *-----------------------------------------------------------------------
365 * Initialize a buffer. If no initial size is given, a reasonable
369 * A buffer to be given to other functions in this library.
372 * The buffer is created, the space allocated and pointers
375 *-----------------------------------------------------------------------
378 Buf_Init(size_t size)
380 Buffer bp; /* New Buffer */
382 bp = emalloc(sizeof(*bp));
387 bp->left = bp->size = size;
388 bp->buffer = emalloc(size);
389 bp->inPtr = bp->outPtr = bp->buffer;
396 *-----------------------------------------------------------------------
398 * Destroy a buffer, and optionally free its data, too.
404 * The buffer is freed.
406 *-----------------------------------------------------------------------
409 Buf_Destroy(Buffer buf, Boolean freeData)
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, Byte byte)
434 if (buf->inPtr == buf->outPtr)
435 Buf_AddByte(buf, byte);
437 *(buf->inPtr - 1) = byte;