1*e2eeea75SSimon J. Gerraty /* $NetBSD: buf.c,v 1.44 2020/11/07 14:11:58 rillig Exp $ */ 23955d011SMarcel Moolenaar 33955d011SMarcel Moolenaar /* 43955d011SMarcel Moolenaar * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 53955d011SMarcel Moolenaar * All rights reserved. 63955d011SMarcel Moolenaar * 73955d011SMarcel Moolenaar * This code is derived from software contributed to Berkeley by 83955d011SMarcel Moolenaar * Adam de Boor. 93955d011SMarcel Moolenaar * 103955d011SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 113955d011SMarcel Moolenaar * modification, are permitted provided that the following conditions 123955d011SMarcel Moolenaar * are met: 133955d011SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 143955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 153955d011SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 163955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 173955d011SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 183955d011SMarcel Moolenaar * 3. Neither the name of the University nor the names of its contributors 193955d011SMarcel Moolenaar * may be used to endorse or promote products derived from this software 203955d011SMarcel Moolenaar * without specific prior written permission. 213955d011SMarcel Moolenaar * 223955d011SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 233955d011SMarcel Moolenaar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 243955d011SMarcel Moolenaar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 253955d011SMarcel Moolenaar * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 263955d011SMarcel Moolenaar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 273955d011SMarcel Moolenaar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 283955d011SMarcel Moolenaar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 293955d011SMarcel Moolenaar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 303955d011SMarcel Moolenaar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 313955d011SMarcel Moolenaar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 323955d011SMarcel Moolenaar * SUCH DAMAGE. 333955d011SMarcel Moolenaar */ 343955d011SMarcel Moolenaar 353955d011SMarcel Moolenaar /* 363955d011SMarcel Moolenaar * Copyright (c) 1988, 1989 by Adam de Boor 373955d011SMarcel Moolenaar * Copyright (c) 1989 by Berkeley Softworks 383955d011SMarcel Moolenaar * All rights reserved. 393955d011SMarcel Moolenaar * 403955d011SMarcel Moolenaar * This code is derived from software contributed to Berkeley by 413955d011SMarcel Moolenaar * Adam de Boor. 423955d011SMarcel Moolenaar * 433955d011SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 443955d011SMarcel Moolenaar * modification, are permitted provided that the following conditions 453955d011SMarcel Moolenaar * are met: 463955d011SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 473955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 483955d011SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 493955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 503955d011SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 513955d011SMarcel Moolenaar * 3. All advertising materials mentioning features or use of this software 523955d011SMarcel Moolenaar * must display the following acknowledgement: 533955d011SMarcel Moolenaar * This product includes software developed by the University of 543955d011SMarcel Moolenaar * California, Berkeley and its contributors. 553955d011SMarcel Moolenaar * 4. Neither the name of the University nor the names of its contributors 563955d011SMarcel Moolenaar * may be used to endorse or promote products derived from this software 573955d011SMarcel Moolenaar * without specific prior written permission. 583955d011SMarcel Moolenaar * 593955d011SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 603955d011SMarcel Moolenaar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 613955d011SMarcel Moolenaar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 623955d011SMarcel Moolenaar * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 633955d011SMarcel Moolenaar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 643955d011SMarcel Moolenaar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 653955d011SMarcel Moolenaar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 663955d011SMarcel Moolenaar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 673955d011SMarcel Moolenaar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 683955d011SMarcel Moolenaar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 693955d011SMarcel Moolenaar * SUCH DAMAGE. 703955d011SMarcel Moolenaar */ 713955d011SMarcel Moolenaar 72956e45f6SSimon J. Gerraty /* Automatically-expanding null-terminated buffers. */ 733955d011SMarcel Moolenaar 742c3632d1SSimon J. Gerraty #include <limits.h> 753955d011SMarcel Moolenaar #include "make.h" 763955d011SMarcel Moolenaar 77956e45f6SSimon J. Gerraty /* "@(#)buf.c 8.1 (Berkeley) 6/6/93" */ 78*e2eeea75SSimon J. Gerraty MAKE_RCSID("$NetBSD: buf.c,v 1.44 2020/11/07 14:11:58 rillig Exp $"); 79956e45f6SSimon J. Gerraty 80956e45f6SSimon J. Gerraty /* Make space in the buffer for adding a single byte. */ 813955d011SMarcel Moolenaar void 82956e45f6SSimon J. Gerraty Buf_Expand_1(Buffer *buf) 833955d011SMarcel Moolenaar { 84956e45f6SSimon J. Gerraty buf->cap += buf->cap > 16 ? buf->cap : 16; 85956e45f6SSimon J. Gerraty buf->data = bmake_realloc(buf->data, buf->cap); 863955d011SMarcel Moolenaar } 873955d011SMarcel Moolenaar 88956e45f6SSimon J. Gerraty /* Add the bytes to the buffer. */ 893955d011SMarcel Moolenaar void 90956e45f6SSimon J. Gerraty Buf_AddBytes(Buffer *buf, const char *bytes, size_t bytes_len) 913955d011SMarcel Moolenaar { 92956e45f6SSimon J. Gerraty size_t old_len = buf->len; 93956e45f6SSimon J. Gerraty char *end; 943955d011SMarcel Moolenaar 95956e45f6SSimon J. Gerraty if (__predict_false(old_len + bytes_len >= buf->cap)) { 96956e45f6SSimon J. Gerraty buf->cap += buf->cap > bytes_len + 16 ? buf->cap : bytes_len + 16; 97956e45f6SSimon J. Gerraty buf->data = bmake_realloc(buf->data, buf->cap); 983955d011SMarcel Moolenaar } 993955d011SMarcel Moolenaar 100956e45f6SSimon J. Gerraty end = buf->data + old_len; 101956e45f6SSimon J. Gerraty buf->len = old_len + bytes_len; 102956e45f6SSimon J. Gerraty memcpy(end, bytes, bytes_len); 103956e45f6SSimon J. Gerraty end[bytes_len] = '\0'; 1043955d011SMarcel Moolenaar } 1053955d011SMarcel Moolenaar 1062c3632d1SSimon J. Gerraty /* Add the bytes between start and end to the buffer. */ 1072c3632d1SSimon J. Gerraty void 108956e45f6SSimon J. Gerraty Buf_AddBytesBetween(Buffer *buf, const char *start, const char *end) 1093955d011SMarcel Moolenaar { 110956e45f6SSimon J. Gerraty Buf_AddBytes(buf, start, (size_t)(end - start)); 1112c3632d1SSimon J. Gerraty } 1123955d011SMarcel Moolenaar 113956e45f6SSimon J. Gerraty /* Add the string to the buffer. */ 1142c3632d1SSimon J. Gerraty void 115956e45f6SSimon J. Gerraty Buf_AddStr(Buffer *buf, const char *str) 1162c3632d1SSimon J. Gerraty { 117956e45f6SSimon J. Gerraty Buf_AddBytes(buf, str, strlen(str)); 1182c3632d1SSimon J. Gerraty } 1192c3632d1SSimon J. Gerraty 120956e45f6SSimon J. Gerraty /* Add the number to the buffer. */ 1212c3632d1SSimon J. Gerraty void 122956e45f6SSimon J. Gerraty Buf_AddInt(Buffer *buf, int n) 1232c3632d1SSimon J. Gerraty { 1242c3632d1SSimon J. Gerraty enum { 1252c3632d1SSimon J. Gerraty bits = sizeof(int) * CHAR_BIT, 1262c3632d1SSimon J. Gerraty max_octal_digits = (bits + 2) / 3, 1272c3632d1SSimon J. Gerraty max_decimal_digits = /* at most */ max_octal_digits, 1282c3632d1SSimon J. Gerraty max_sign_chars = 1, 129956e45f6SSimon J. Gerraty str_size = max_sign_chars + max_decimal_digits + 1 1302c3632d1SSimon J. Gerraty }; 131956e45f6SSimon J. Gerraty char str[str_size]; 1322c3632d1SSimon J. Gerraty 133956e45f6SSimon J. Gerraty size_t len = (size_t)snprintf(str, sizeof str, "%d", n); 134956e45f6SSimon J. Gerraty Buf_AddBytes(buf, str, len); 1352c3632d1SSimon J. Gerraty } 1362c3632d1SSimon J. Gerraty 1372c3632d1SSimon J. Gerraty /* Get the data (usually a string) from the buffer. 1382c3632d1SSimon J. Gerraty * The returned data is valid until the next modifying operation 1392c3632d1SSimon J. Gerraty * on the buffer. 1402c3632d1SSimon J. Gerraty * 141956e45f6SSimon J. Gerraty * Returns the data and optionally the length of the data. */ 1422c3632d1SSimon J. Gerraty char * 143956e45f6SSimon J. Gerraty Buf_GetAll(Buffer *buf, size_t *out_len) 1442c3632d1SSimon J. Gerraty { 145956e45f6SSimon J. Gerraty if (out_len != NULL) 146956e45f6SSimon J. Gerraty *out_len = buf->len; 147956e45f6SSimon J. Gerraty return buf->data; 1483955d011SMarcel Moolenaar } 1493955d011SMarcel Moolenaar 1502c3632d1SSimon J. Gerraty /* Mark the buffer as empty, so it can be filled with data again. */ 1513955d011SMarcel Moolenaar void 152956e45f6SSimon J. Gerraty Buf_Empty(Buffer *buf) 1533955d011SMarcel Moolenaar { 154956e45f6SSimon J. Gerraty buf->len = 0; 155956e45f6SSimon J. Gerraty buf->data[0] = '\0'; 1563955d011SMarcel Moolenaar } 1573955d011SMarcel Moolenaar 158*e2eeea75SSimon J. Gerraty /* Initialize a buffer. */ 1593955d011SMarcel Moolenaar void 160*e2eeea75SSimon J. Gerraty Buf_InitSize(Buffer *buf, size_t cap) 1613955d011SMarcel Moolenaar { 162956e45f6SSimon J. Gerraty buf->cap = cap; 163956e45f6SSimon J. Gerraty buf->len = 0; 164956e45f6SSimon J. Gerraty buf->data = bmake_malloc(cap); 165956e45f6SSimon J. Gerraty buf->data[0] = '\0'; 1663955d011SMarcel Moolenaar } 1673955d011SMarcel Moolenaar 168*e2eeea75SSimon J. Gerraty void 169*e2eeea75SSimon J. Gerraty Buf_Init(Buffer *buf) 170*e2eeea75SSimon J. Gerraty { 171*e2eeea75SSimon J. Gerraty Buf_InitSize(buf, 256); 172*e2eeea75SSimon J. Gerraty } 173*e2eeea75SSimon J. Gerraty 1742c3632d1SSimon J. Gerraty /* Reset the buffer. 1752c3632d1SSimon J. Gerraty * If freeData is TRUE, the data from the buffer is freed as well. 1762c3632d1SSimon J. Gerraty * Otherwise it is kept and returned. */ 1772c3632d1SSimon J. Gerraty char * 1783955d011SMarcel Moolenaar Buf_Destroy(Buffer *buf, Boolean freeData) 1793955d011SMarcel Moolenaar { 180956e45f6SSimon J. Gerraty char *data = buf->data; 1813955d011SMarcel Moolenaar if (freeData) { 1823955d011SMarcel Moolenaar free(data); 1833955d011SMarcel Moolenaar data = NULL; 1843955d011SMarcel Moolenaar } 1853955d011SMarcel Moolenaar 186956e45f6SSimon J. Gerraty buf->cap = 0; 187956e45f6SSimon J. Gerraty buf->len = 0; 188956e45f6SSimon J. Gerraty buf->data = NULL; 1893955d011SMarcel Moolenaar 1903955d011SMarcel Moolenaar return data; 1913955d011SMarcel Moolenaar } 1923955d011SMarcel Moolenaar 1933955d011SMarcel Moolenaar #ifndef BUF_COMPACT_LIMIT 1943955d011SMarcel Moolenaar # define BUF_COMPACT_LIMIT 128 /* worthwhile saving */ 1953955d011SMarcel Moolenaar #endif 1963955d011SMarcel Moolenaar 1972c3632d1SSimon J. Gerraty /* Reset the buffer and return its data. 1982c3632d1SSimon J. Gerraty * 1992c3632d1SSimon J. Gerraty * If the buffer size is much greater than its content, 2002c3632d1SSimon J. Gerraty * a new buffer will be allocated and the old one freed. */ 2012c3632d1SSimon J. Gerraty char * 2023955d011SMarcel Moolenaar Buf_DestroyCompact(Buffer *buf) 2033955d011SMarcel Moolenaar { 2043955d011SMarcel Moolenaar #if BUF_COMPACT_LIMIT > 0 205956e45f6SSimon J. Gerraty if (buf->cap - buf->len >= BUF_COMPACT_LIMIT) { 2063955d011SMarcel Moolenaar /* We trust realloc to be smart */ 207956e45f6SSimon J. Gerraty char *data = bmake_realloc(buf->data, buf->len + 1); 208956e45f6SSimon J. Gerraty data[buf->len] = '\0'; /* XXX: unnecessary */ 2093955d011SMarcel Moolenaar Buf_Destroy(buf, FALSE); 2103955d011SMarcel Moolenaar return data; 2113955d011SMarcel Moolenaar } 2123955d011SMarcel Moolenaar #endif 2133955d011SMarcel Moolenaar return Buf_Destroy(buf, FALSE); 2143955d011SMarcel Moolenaar } 215