/* $NetBSD: buf.h,v 1.50 2024/04/28 15:10:19 rillig Exp $ */12/*3* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.4*5* This code is derived from software contributed to Berkeley by6* Adam de Boor.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*32* from: @(#)buf.h 8.1 (Berkeley) 6/6/9333*/3435/*36* Copyright (c) 1988, 1989 by Adam de Boor37* Copyright (c) 1989 by Berkeley Softworks38* All rights reserved.39*40* This code is derived from software contributed to Berkeley by41* Adam de Boor.42*43* Redistribution and use in source and binary forms, with or without44* modification, are permitted provided that the following conditions45* are met:46* 1. Redistributions of source code must retain the above copyright47* notice, this list of conditions and the following disclaimer.48* 2. Redistributions in binary form must reproduce the above copyright49* notice, this list of conditions and the following disclaimer in the50* documentation and/or other materials provided with the distribution.51* 3. All advertising materials mentioning features or use of this software52* must display the following acknowledgement:53* This product includes software developed by the University of54* California, Berkeley and its contributors.55* 4. Neither the name of the University nor the names of its contributors56* may be used to endorse or promote products derived from this software57* without specific prior written permission.58*59* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND60* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE61* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE62* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE63* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL64* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS65* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)66* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT67* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY68* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF69* SUCH DAMAGE.70*71* from: @(#)buf.h 8.1 (Berkeley) 6/6/9372*/7374/* Automatically growing null-terminated buffers of characters. */7576#ifndef MAKE_BUF_H77#define MAKE_BUF_H7879#include <stddef.h>8081/* An automatically growing null-terminated buffer of characters. */82typedef struct Buffer {83size_t cap; /* Allocated size of the buffer, including the '\0' */84size_t len; /* Number of bytes in buffer, excluding the '\0' */85char *data; /* The buffer itself (always null-terminated) */86} Buffer;8788void Buf_Expand(Buffer *);8990/* Mark the buffer as empty, so it can be filled with data again. */91MAKE_INLINE void92Buf_Clear(Buffer *buf)93{94buf->len = 0;95buf->data[0] = '\0';96}9798/* Adds a single byte to a buffer. */99MAKE_INLINE void100Buf_AddByte(Buffer *buf, char byte)101{102size_t old_len = buf->len++;103char *end;104if (old_len + 1 >= buf->cap)105Buf_Expand(buf);106end = buf->data + old_len;107end[0] = byte;108end[1] = '\0';109}110111MAKE_INLINE bool MAKE_ATTR_USE112Buf_EndsWith(const Buffer *buf, char ch)113{114return buf->len > 0 && buf->data[buf->len - 1] == ch;115}116117void Buf_AddBytes(Buffer *, const char *, size_t);118void Buf_AddRange(Buffer *, const char *, const char *);119void Buf_AddStr(Buffer *, const char *);120void Buf_AddInt(Buffer *, int);121void Buf_AddFlag(Buffer *, bool, const char *);122void Buf_Init(Buffer *);123void Buf_InitSize(Buffer *, size_t);124void Buf_Done(Buffer *);125char *Buf_DoneData(Buffer *) MAKE_ATTR_USE;126127#endif128129130