1 /* $NetBSD: buf.c,v 1.37 2020/08/23 08:21:50 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 static char rcsid[] = "$NetBSD: buf.c,v 1.37 2020/08/23 08:21:50 rillig Exp $";
75 #include <sys/cdefs.h>
78 static char sccsid[] = "@(#)buf.c 8.1 (Berkeley) 6/6/93";
80 __RCSID("$NetBSD: buf.c,v 1.37 2020/08/23 08:21:50 rillig Exp $");
85 /* Functions for automatically-expanded null-terminated buffers. */
90 /* Extend the buffer for adding a single byte. */
92 Buf_Expand_1(Buffer *bp)
94 bp->size += MAX(bp->size, 16);
95 bp->buffer = bmake_realloc(bp->buffer, bp->size);
98 /* Add the given bytes to the buffer. */
100 Buf_AddBytes(Buffer *bp, const char *bytesPtr, size_t numBytes)
102 size_t count = bp->count;
105 if (__predict_false(count + numBytes >= bp->size)) {
106 bp->size += MAX(bp->size, numBytes + 16);
107 bp->buffer = bmake_realloc(bp->buffer, bp->size);
110 ptr = bp->buffer + count;
111 bp->count = count + numBytes;
112 memcpy(ptr, bytesPtr, numBytes);
113 ptr[numBytes] = '\0';
116 /* Add the bytes between start and end to the buffer. */
118 Buf_AddBytesBetween(Buffer *bp, const char *start, const char *end)
120 Buf_AddBytes(bp, start, (size_t)(end - start));
123 /* Add the given string to the buffer. */
125 Buf_AddStr(Buffer *bp, const char *str)
127 Buf_AddBytes(bp, str, strlen(str));
130 /* Add the given number to the buffer. */
132 Buf_AddInt(Buffer *bp, int n)
135 bits = sizeof(int) * CHAR_BIT,
136 max_octal_digits = (bits + 2) / 3,
137 max_decimal_digits = /* at most */ max_octal_digits,
139 buf_size = max_sign_chars + max_decimal_digits + 1
143 size_t len = (size_t)snprintf(buf, sizeof buf, "%d", n);
144 Buf_AddBytes(bp, buf, len);
147 /* Get the data (usually a string) from the buffer.
148 * The returned data is valid until the next modifying operation
151 * Returns the pointer to the data and optionally the length of the
152 * data in the buffer. */
154 Buf_GetAll(Buffer *bp, size_t *numBytesPtr)
156 if (numBytesPtr != NULL)
157 *numBytesPtr = bp->count;
161 /* Mark the buffer as empty, so it can be filled with data again. */
163 Buf_Empty(Buffer *bp)
166 bp->buffer[0] = '\0';
169 /* Initialize a buffer.
170 * If the given initial size is 0, a reasonable default is used. */
172 Buf_Init(Buffer *bp, size_t size)
179 bp->buffer = bmake_malloc(size);
180 bp->buffer[0] = '\0';
184 * If freeData is TRUE, the data from the buffer is freed as well.
185 * Otherwise it is kept and returned. */
187 Buf_Destroy(Buffer *buf, Boolean freeData)
189 char *data = buf->buffer;
202 #ifndef BUF_COMPACT_LIMIT
203 # define BUF_COMPACT_LIMIT 128 /* worthwhile saving */
206 /* Reset the buffer and return its data.
208 * If the buffer size is much greater than its content,
209 * a new buffer will be allocated and the old one freed. */
211 Buf_DestroyCompact(Buffer *buf)
213 #if BUF_COMPACT_LIMIT > 0
214 if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
215 /* We trust realloc to be smart */
216 char *data = bmake_realloc(buf->buffer, buf->count + 1);
217 data[buf->count] = '\0';
218 Buf_Destroy(buf, FALSE);
222 return Buf_Destroy(buf, FALSE);