1 /* $NetBSD: buf.c,v 1.53 2021/11/28 22:48:06 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1988, 1989 by Adam de Boor 37 * Copyright (c) 1989 by Berkeley Softworks 38 * All rights reserved. 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Adam de Boor. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 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. 58 * 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 69 * SUCH DAMAGE. 70 */ 71 72 /* Automatically-expanding null-terminated character buffers. */ 73 74 #include <limits.h> 75 #include "make.h" 76 77 /* "@(#)buf.c 8.1 (Berkeley) 6/6/93" */ 78 MAKE_RCSID("$NetBSD: buf.c,v 1.53 2021/11/28 22:48:06 rillig Exp $"); 79 80 /* Make space in the buffer for adding at least 16 more bytes. */ 81 void 82 Buf_Expand(Buffer *buf) 83 { 84 buf->cap += buf->cap > 16 ? buf->cap : 16; 85 buf->data = bmake_realloc(buf->data, buf->cap); 86 } 87 88 /* Add the bytes to the buffer. */ 89 void 90 Buf_AddBytes(Buffer *buf, const char *bytes, size_t bytes_len) 91 { 92 size_t old_len = buf->len; 93 char *end; 94 95 if (old_len + bytes_len >= buf->cap) { 96 size_t minIncr = bytes_len + 16; 97 buf->cap += buf->cap > minIncr ? buf->cap : minIncr; 98 buf->data = bmake_realloc(buf->data, buf->cap); 99 } 100 101 end = buf->data + old_len; 102 buf->len = old_len + bytes_len; 103 memcpy(end, bytes, bytes_len); 104 end[bytes_len] = '\0'; 105 } 106 107 /* Add the bytes between start and end to the buffer. */ 108 void 109 Buf_AddBytesBetween(Buffer *buf, const char *start, const char *end) 110 { 111 Buf_AddBytes(buf, start, (size_t)(end - start)); 112 } 113 114 /* Add the string to the buffer. */ 115 void 116 Buf_AddStr(Buffer *buf, const char *str) 117 { 118 Buf_AddBytes(buf, str, strlen(str)); 119 } 120 121 /* Add the number to the buffer. */ 122 void 123 Buf_AddInt(Buffer *buf, int n) 124 { 125 char str[sizeof(int) * CHAR_BIT + 1]; 126 127 size_t len = (size_t)snprintf(str, sizeof str, "%d", n); 128 Buf_AddBytes(buf, str, len); 129 } 130 131 void 132 Buf_AddFlag(Buffer *buf, bool flag, const char *name) 133 { 134 if (flag) { 135 if (buf->len > 0) 136 Buf_AddByte(buf, '|'); 137 Buf_AddBytes(buf, name, strlen(name)); 138 } 139 } 140 141 /* Mark the buffer as empty, so it can be filled with data again. */ 142 void 143 Buf_Empty(Buffer *buf) 144 { 145 buf->len = 0; 146 buf->data[0] = '\0'; 147 } 148 149 /* Initialize a buffer. */ 150 void 151 Buf_InitSize(Buffer *buf, size_t cap) 152 { 153 buf->cap = cap; 154 buf->len = 0; 155 buf->data = bmake_malloc(cap); 156 buf->data[0] = '\0'; 157 } 158 159 void 160 Buf_Init(Buffer *buf) 161 { 162 Buf_InitSize(buf, 256); 163 } 164 165 /* 166 * Free the data from the buffer. 167 * Leave the buffer itself in an indeterminate state. 168 */ 169 void 170 Buf_Done(Buffer *buf) 171 { 172 free(buf->data); 173 174 #ifdef CLEANUP 175 buf->cap = 0; 176 buf->len = 0; 177 buf->data = NULL; 178 #endif 179 } 180 181 /* 182 * Return the data from the buffer. 183 * Leave the buffer itself in an indeterminate state. 184 */ 185 char * 186 Buf_DoneData(Buffer *buf) 187 { 188 char *data = buf->data; 189 190 #ifdef CLEANUP 191 buf->cap = 0; 192 buf->len = 0; 193 buf->data = NULL; 194 #endif 195 196 return data; 197 } 198 199 #ifndef BUF_COMPACT_LIMIT 200 # define BUF_COMPACT_LIMIT 128 /* worthwhile saving */ 201 #endif 202 203 /* 204 * Return the data from the buffer. 205 * Leave the buffer itself in an indeterminate state. 206 * 207 * If the buffer size is much greater than its content, 208 * a new buffer will be allocated and the old one freed. 209 */ 210 char * 211 Buf_DoneDataCompact(Buffer *buf) 212 { 213 #if BUF_COMPACT_LIMIT > 0 214 if (buf->cap - buf->len >= BUF_COMPACT_LIMIT) { 215 /* We trust realloc to be smart */ 216 char *data = bmake_realloc(buf->data, buf->len + 1); 217 data[buf->len] = '\0'; /* XXX: unnecessary */ 218 Buf_DoneData(buf); 219 return data; 220 } 221 #endif 222 return Buf_DoneData(buf); 223 } 224