1f7923656Sespie #ifndef _BUF_H 2f7923656Sespie #define _BUF_H 3f7923656Sespie 4*583c2e1dSespie /* $OpenBSD: buf.h,v 1.24 2020/01/13 13:54:44 espie Exp $ */ 51dca3691Smillert /* $NetBSD: buf.h,v 1.7 1996/12/31 17:53:22 christos Exp $ */ 6df930be7Sderaadt 7df930be7Sderaadt /* 804ed836eSespie * Copyright (c) 1999 Marc Espie. 904ed836eSespie * 1004ed836eSespie * Extensive code changes for the OpenBSD project. 1104ed836eSespie * 1204ed836eSespie * Redistribution and use in source and binary forms, with or without 1304ed836eSespie * modification, are permitted provided that the following conditions 1404ed836eSespie * are met: 1504ed836eSespie * 1. Redistributions of source code must retain the above copyright 1604ed836eSespie * notice, this list of conditions and the following disclaimer. 1704ed836eSespie * 2. Redistributions in binary form must reproduce the above copyright 1804ed836eSespie * notice, this list of conditions and the following disclaimer in the 1904ed836eSespie * documentation and/or other materials provided with the distribution. 2004ed836eSespie * 2104ed836eSespie * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 2204ed836eSespie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2304ed836eSespie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2404ed836eSespie * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD 2504ed836eSespie * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2604ed836eSespie * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2704ed836eSespie * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2804ed836eSespie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2904ed836eSespie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3004ed836eSespie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3104ed836eSespie * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3204ed836eSespie */ 3304ed836eSespie 3404ed836eSespie /* 35df930be7Sderaadt * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 36df930be7Sderaadt * Copyright (c) 1988, 1989 by Adam de Boor 37df930be7Sderaadt * Copyright (c) 1989 by Berkeley Softworks 38df930be7Sderaadt * All rights reserved. 39df930be7Sderaadt * 40df930be7Sderaadt * This code is derived from software contributed to Berkeley by 41df930be7Sderaadt * Adam de Boor. 42df930be7Sderaadt * 43df930be7Sderaadt * Redistribution and use in source and binary forms, with or without 44df930be7Sderaadt * modification, are permitted provided that the following conditions 45df930be7Sderaadt * are met: 46df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright 47df930be7Sderaadt * notice, this list of conditions and the following disclaimer. 48df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 49df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the 50df930be7Sderaadt * documentation and/or other materials provided with the distribution. 51f75387cbSmillert * 3. Neither the name of the University nor the names of its contributors 52df930be7Sderaadt * may be used to endorse or promote products derived from this software 53df930be7Sderaadt * without specific prior written permission. 54df930be7Sderaadt * 55df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65df930be7Sderaadt * SUCH DAMAGE. 66df930be7Sderaadt * 67789486c7Smillert * from: @(#)buf.h 8.1 (Berkeley) 6/6/93 68df930be7Sderaadt */ 69df930be7Sderaadt 70f7923656Sespie /* 71f7923656Sespie * buf 72f7923656Sespie * Support for extensible char buffers. 73f7923656Sespie * One adds chars to a buffer, then retrieves the contents using 74f7923656Sespie * Buf_Retrieve (no copy involved), or releases the memory using 75f7923656Sespie * Buf_Destroy. 76df930be7Sderaadt */ 77df930be7Sderaadt 78df930be7Sderaadt 796473403bSespie /* Internal data structures and functions. BUFFER is visible so 806473403bSespie * that users can allocate the memory themselves. */ 81f8042c21Sespie typedef struct Buffer_ { 824e623abdSespie char *buffer; /* The buffer itself. */ 834e623abdSespie char *inPtr; /* Place to write to. */ 844e623abdSespie char *endPtr; /* End of allocated space. */ 85f8042c21Sespie } BUFFER; 86f8042c21Sespie 872aa5c534Sespie /* Internal support for Buf_AddChar. */ 887a167ac6Sespie extern void BufExpand(Buffer, size_t); 892aa5c534Sespie 90f7923656Sespie 914e623abdSespie /* User interface */ 924e623abdSespie 93f7923656Sespie /* Buf_AddChars(buf, n, str); 94f7923656Sespie * Adds n chars to buffer buf starting from str. */ 9504ed836eSespie extern void Buf_AddChars(Buffer, size_t, const char *); 961bae8e1fSespie /* Buf_Truncate(buffer, length) */ 971bae8e1fSespie #define Buf_Truncate(bp, len) ((void)((bp)->inPtr = (bp)->buffer + (len))) 98f7923656Sespie /* Buf_Reset(buf); 99f7923656Sespie * Empties buffer. */ 1001bae8e1fSespie #define Buf_Reset(bp) Buf_Truncate(bp, 0) 101f7923656Sespie /* n = Buf_Size(buf); 102f7923656Sespie * Returns number of chars currently in buf. 1036473403bSespie * Doesn't include the null-terminating char. */ 1046473403bSespie #define Buf_Size(bp) ((size_t)((bp)->inPtr - (bp)->buffer)) 105f7923656Sespie /* Buf_Init(buf, init); 106f7923656Sespie * Initializes a buffer, to hold approximately init chars. 107f7923656Sespie * Set init to 0 if you have no idea. */ 10804ed836eSespie extern void Buf_Init(Buffer, size_t); 109*583c2e1dSespie /* Buf_Reinit(buf, init); 110*583c2e1dSespie * Initializes/reset a static buffer */ 111*583c2e1dSespie extern void Buf_Reinit(Buffer, size_t); 112f7923656Sespie /* Buf_Destroy(buf); 113f7923656Sespie * Nukes a buffer and all its resources. */ 11404ed836eSespie #define Buf_Destroy(bp) ((void)free((bp)->buffer)) 115f7923656Sespie /* str = Buf_Retrieve(buf); 116f7923656Sespie * Retrieves data from a buffer, as a NULL terminated string. */ 1176473403bSespie #define Buf_Retrieve(bp) (*(bp)->inPtr = '\0', (bp)->buffer) 118f7923656Sespie /* Buf_AddChar(buf, c); 119f7923656Sespie * Adds a single char to buffer. */ 1207b5807fbSespie #define Buf_AddChar(bp, byte) \ 1212aa5c534Sespie do { \ 1224e623abdSespie if ((bp)->endPtr - (bp)->inPtr <= 1) \ 1237a167ac6Sespie BufExpand(bp, 1); \ 1242aa5c534Sespie *(bp)->inPtr++ = (byte); \ 1252aa5c534Sespie } while (0) 126df930be7Sderaadt 127f7923656Sespie /* Buf_AddSpace(buf); 128f7923656Sespie * Adds a space to buffer. */ 129001ec601Sespie #define Buf_AddSpace(b) Buf_AddChar((b), ' ') 130f7923656Sespie /* Buf_AddString(buf, str); 131f7923656Sespie * Adds the contents of a NULL terminated string to buffer. */ 132001ec601Sespie #define Buf_AddString(b, s) Buf_AddChars((b), strlen(s), (s)) 133f7923656Sespie /* Buf_Addi(buf, s, e); 134f7923656Sespie * Adds characters between s and e to buffer. */ 135f7923656Sespie #define Buf_Addi(b, s, e) Buf_AddChars((b), (e) - (s), (s)) 136001ec601Sespie 137a6b963c8Sespie extern void Buf_printf(Buffer, const char *, ...); 138a6b963c8Sespie #define Buf_puts(b, s) Buf_AddChars((b), strlen(s), (s)) 139a6b963c8Sespie 140df930be7Sderaadt #endif /* _BUF_H */ 141