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.25 2005/01/24 09:38:01 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 * This function is also used to make sure there's room for
65 * an extra NULL byte at the end of the buffer in case it holds
69 BufExpand(Buffer *bp, size_t nb)
71 if (bp->left < nb + 1) {
72 int newSize = bp->size + max(nb + 1, BUF_ADD_INC);
73 Byte *newBuf = erealloc(bp->buffer, newSize);
75 bp->inPtr = newBuf + (bp->inPtr - bp->buffer);
78 bp->left = newSize - (bp->inPtr - bp->buffer);
82 /* Buf_AddByte adds a single byte to a buffer. */
84 Buf_AddByte(Buffer *bp, Byte byte)
98 *bp->inPtr = 0; /* Null-terminate */
102 *-----------------------------------------------------------------------
104 * Add a number of bytes to the buffer.
112 *-----------------------------------------------------------------------
115 Buf_AddBytes(Buffer *bp, size_t numBytes, const Byte *bytesPtr)
117 BufExpand(bp, numBytes);
119 memcpy(bp->inPtr, bytesPtr, numBytes);
120 bp->inPtr += numBytes;
121 bp->left -= numBytes;
130 *-----------------------------------------------------------------------
132 * Get all the available data at once.
135 * A pointer to the data and the number of bytes available.
140 *-----------------------------------------------------------------------
143 Buf_GetAll(Buffer *bp, size_t *numBytesPtr)
145 if (numBytesPtr != NULL)
146 *numBytesPtr = bp->inPtr - bp->buffer;
152 *-----------------------------------------------------------------------
154 * Returns the number of bytes in the given buffer. Doesn't include
155 * the null-terminating byte.
158 * The number of bytes.
163 *-----------------------------------------------------------------------
166 Buf_Size(Buffer *buf)
168 return (buf->inPtr - buf->buffer);
172 *-----------------------------------------------------------------------
174 * Initialize a buffer. If no initial size is given, a reasonable
178 * A buffer to be given to other functions in this library.
181 * The buffer is created, the space allocated and pointers
184 *-----------------------------------------------------------------------
187 Buf_Init(size_t size)
189 Buffer *bp; /* New Buffer */
191 bp = emalloc(sizeof(*bp));
196 bp->left = bp->size = size;
197 bp->buffer = emalloc(size);
198 bp->inPtr = bp->buffer;
205 *-----------------------------------------------------------------------
207 * Destroy a buffer, and optionally free its data, too.
213 * The buffer is freed.
215 *-----------------------------------------------------------------------
218 Buf_Destroy(Buffer *buf, Boolean freeData)
226 *-----------------------------------------------------------------------
227 * Buf_ReplaceLastByte --
228 * Replace the last byte in a buffer.
234 * If the buffer was empty intially, then a new byte will be added.
235 * Otherwise, the last byte is overwritten.
237 *-----------------------------------------------------------------------
240 Buf_ReplaceLastByte(Buffer *buf, Byte byte)
242 if (buf->inPtr == buf->buffer)
243 Buf_AddByte(buf, byte);
245 *(buf->inPtr - 1) = byte;
249 Buf_Clear(Buffer *bp)
251 bp->inPtr = bp->buffer;