2 * Copyright (c) 2005 Max Okumoto
3 * Copyright (c) 1988, 1989, 1990, 1993
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1988, 1989 by Adam de Boor
6 * Copyright (c) 1989 by Berkeley Softworks
9 * This code is derived from software contributed to Berkeley by
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)buf.c 8.1 (Berkeley) 6/6/93
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
48 * Functions for automatically-expanded buffers.
58 * Returns the number of bytes in the buffer. Doesn't include the
59 * null-terminating byte.
62 Buf_Size(const Buffer *buf)
65 return (buf->end - buf->buf);
69 * Returns a reference to the data contained in the buffer.
71 * @note Adding data to the Buffer object may invalidate the reference.
74 Buf_Data(const Buffer *bp)
81 * Expand the buffer to hold the number of additional bytes, plus
82 * space to store a terminating NULL byte.
85 BufExpand(Buffer *bp, size_t nb)
87 size_t len = Buf_Size(bp);
90 if (bp->size < len + nb + 1) {
91 size = bp->size + MAX(nb + 1, BUF_ADD_INC);
93 bp->buf = erealloc(bp->buf, size);
94 bp->end = bp->buf + len;
99 * Add a single byte to the buffer.
102 Buf_AddByte(Buffer *bp, Byte byte)
113 * Add bytes to the buffer.
116 Buf_AddBytes(Buffer *bp, size_t len, const Byte *bytes)
121 memcpy(bp->end, bytes, len);
127 * Get a reference to the internal buffer.
130 * Pointer to where we return the number of bytes in the internal buffer.
133 * return A pointer to the data.
136 Buf_GetAll(Buffer *bp, size_t *len)
146 * Get the contents of a buffer and destroy the buffer. If the buffer
147 * is NULL, return NULL.
150 * the pointer to the data.
165 * Initialize a buffer. If no initial size is given, a reasonable
169 * A buffer object to be given to other functions in this library.
172 * Space is allocated for the Buffer object and a internal buffer.
175 Buf_Init(size_t size)
177 Buffer *bp; /* New Buffer */
182 bp = emalloc(sizeof(*bp));
184 bp->buf = emalloc(size);
192 * Destroy a buffer, and optionally free its data, too.
195 * Space for the Buffer object and possibly the internal buffer
199 Buf_Destroy(Buffer *buf, Boolean freeData)
208 * Replace the last byte in a buffer. If the buffer was empty
209 * intially, then a new byte will be added.
212 Buf_ReplaceLastByte(Buffer *bp, Byte byte)
215 if (bp->end == bp->buf) {
216 Buf_AddByte(bp, byte);
218 *(bp->end - 1) = byte;
223 * Append characters in str to Buffer object
226 Buf_Append(Buffer *bp, const char str[])
229 Buf_AddBytes(bp, strlen(str), str);
233 * Append characters in buf to Buffer object
236 Buf_AppendBuf(Buffer *bp, const Buffer *buf)
239 Buf_AddBytes(bp, Buf_Size(buf), buf->buf);
243 * Append characters between str and end to Buffer object.
246 Buf_AppendRange(Buffer *bp, const char str[], const char *end)
249 Buf_AddBytes(bp, end - str, str);
253 * Convert newlines in buffer to spaces. The trailing newline is
257 Buf_StripNewlines(Buffer *bp)
262 * If there is anything in the buffer, remove the last
265 if (ptr != bp->buf) {
266 if (*(ptr - 1) == '\n') {
274 /* Convert newline characters to a space characters. */
275 while (ptr != bp->buf) {
283 * Clear the contents of the buffer.
286 Buf_Clear(Buffer *bp)