/* $NetBSD: buf.c,v 1.58 2024/04/28 15:10:19 rillig Exp $ */12/*3* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.4* All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Adam de Boor.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334/*35* Copyright (c) 1988, 1989 by Adam de Boor36* Copyright (c) 1989 by Berkeley Softworks37* All rights reserved.38*39* This code is derived from software contributed to Berkeley by40* Adam de Boor.41*42* Redistribution and use in source and binary forms, with or without43* modification, are permitted provided that the following conditions44* are met:45* 1. Redistributions of source code must retain the above copyright46* notice, this list of conditions and the following disclaimer.47* 2. Redistributions in binary form must reproduce the above copyright48* notice, this list of conditions and the following disclaimer in the49* documentation and/or other materials provided with the distribution.50* 3. All advertising materials mentioning features or use of this software51* must display the following acknowledgement:52* This product includes software developed by the University of53* California, Berkeley and its contributors.54* 4. Neither the name of the University nor the names of its contributors55* may be used to endorse or promote products derived from this software56* without specific prior written permission.57*58* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND59* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE60* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE61* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE62* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL63* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS64* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)65* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT66* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY67* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF68* SUCH DAMAGE.69*/7071/* Automatically growing null-terminated buffers of characters. */7273#include <limits.h>74#include "make.h"7576/* "@(#)buf.c 8.1 (Berkeley) 6/6/93" */77MAKE_RCSID("$NetBSD: buf.c,v 1.58 2024/04/28 15:10:19 rillig Exp $");7879/* Make space in the buffer for adding at least 16 more bytes. */80void81Buf_Expand(Buffer *buf)82{83buf->cap += buf->cap > 16 ? buf->cap : 16;84buf->data = bmake_realloc(buf->data, buf->cap);85}8687/* Add the bytes to the buffer. */88void89Buf_AddBytes(Buffer *buf, const char *bytes, size_t bytes_len)90{91size_t old_len = buf->len;92char *end;9394if (old_len + bytes_len >= buf->cap) {95size_t minIncr = bytes_len + 16;96buf->cap += buf->cap > minIncr ? buf->cap : minIncr;97buf->data = bmake_realloc(buf->data, buf->cap);98}99100end = buf->data + old_len;101buf->len = old_len + bytes_len;102memcpy(end, bytes, bytes_len);103end[bytes_len] = '\0';104}105106/* Add the bytes between start and end to the buffer. */107void108Buf_AddRange(Buffer *buf, const char *start, const char *end)109{110Buf_AddBytes(buf, start, (size_t)(end - start));111}112113/* Add the string to the buffer. */114void115Buf_AddStr(Buffer *buf, const char *str)116{117Buf_AddBytes(buf, str, strlen(str));118}119120/* Add the number to the buffer. */121void122Buf_AddInt(Buffer *buf, int n)123{124char str[sizeof(int) * CHAR_BIT + 1];125126size_t len = (size_t)snprintf(str, sizeof str, "%d", n);127Buf_AddBytes(buf, str, len);128}129130void131Buf_AddFlag(Buffer *buf, bool flag, const char *name)132{133if (flag) {134if (buf->len > 0)135Buf_AddByte(buf, '|');136Buf_AddBytes(buf, name, strlen(name));137}138}139140/* Initialize a buffer. */141void142Buf_InitSize(Buffer *buf, size_t cap)143{144buf->cap = cap;145buf->len = 0;146buf->data = bmake_malloc(cap);147buf->data[0] = '\0';148}149150void151Buf_Init(Buffer *buf)152{153Buf_InitSize(buf, 256);154}155156/*157* Free the data from the buffer.158* Leave the buffer itself in an indeterminate state.159*/160void161Buf_Done(Buffer *buf)162{163free(buf->data);164165#ifdef CLEANUP166buf->cap = 0;167buf->len = 0;168buf->data = NULL;169#endif170}171172/*173* Return the data from the buffer.174* Leave the buffer itself in an indeterminate state.175*/176char *177Buf_DoneData(Buffer *buf)178{179char *data = buf->data;180181#ifdef CLEANUP182buf->cap = 0;183buf->len = 0;184buf->data = NULL;185#endif186187return data;188}189190191