1 #ifndef _BUF_H 2 #define _BUF_H 3 4 /* $OpenPackages$ */ 5 /* $OpenBSD: buf.h,v 1.15 2001/05/23 12:34:40 espie Exp $ */ 6 /* $NetBSD: buf.h,v 1.7 1996/12/31 17:53:22 christos Exp $ */ 7 8 /* 9 * Copyright (c) 1999 Marc Espie. 10 * 11 * Extensive code changes for the OpenBSD project. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD 26 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 37 * Copyright (c) 1988, 1989 by Adam de Boor 38 * Copyright (c) 1989 by Berkeley Softworks 39 * All rights reserved. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * Adam de Boor. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * from: @(#)buf.h 8.1 (Berkeley) 6/6/93 73 */ 74 75 /* 76 * buf 77 * Support for extensible char buffers. 78 * One adds chars to a buffer, then retrieves the contents using 79 * Buf_Retrieve (no copy involved), or releases the memory using 80 * Buf_Destroy. 81 */ 82 83 84 /* Internal data structures and functions. BUFFER is visible so 85 * that users can allocate the memory themselves. */ 86 typedef struct Buffer_ { 87 char *buffer; /* The buffer itself. */ 88 char *inPtr; /* Place to write to. */ 89 char *endPtr; /* End of allocated space. */ 90 } BUFFER; 91 92 /* Internal support for Buf_AddChar. */ 93 extern void BufOverflow(Buffer); 94 95 96 /* User interface */ 97 98 /* Buf_AddChars(buf, n, str); 99 * Adds n chars to buffer buf starting from str. */ 100 extern void Buf_AddChars(Buffer, size_t, const char *); 101 /* Buf_Reset(buf); 102 * Empties buffer. */ 103 #define Buf_Reset(bp) ((void)((bp)->inPtr = (bp)->buffer)) 104 /* n = Buf_Size(buf); 105 * Returns number of chars currently in buf. 106 * Doesn't include the null-terminating char. */ 107 #define Buf_Size(bp) ((size_t)((bp)->inPtr - (bp)->buffer)) 108 /* Buf_Init(buf, init); 109 * Initializes a buffer, to hold approximately init chars. 110 * Set init to 0 if you have no idea. */ 111 extern void Buf_Init(Buffer, size_t); 112 /* Buf_Destroy(buf); 113 * Nukes a buffer and all its resources. */ 114 #define Buf_Destroy(bp) ((void)free((bp)->buffer)) 115 /* str = Buf_Retrieve(buf); 116 * Retrieves data from a buffer, as a NULL terminated string. */ 117 #define Buf_Retrieve(bp) (*(bp)->inPtr = '\0', (bp)->buffer) 118 /* Buf_AddChar(buf, c); 119 * Adds a single char to buffer. */ 120 #define Buf_AddChar(bp, byte) \ 121 do { \ 122 if ((bp)->endPtr - (bp)->inPtr <= 1) \ 123 BufOverflow(bp); \ 124 *(bp)->inPtr++ = (byte); \ 125 } while (0) 126 127 /* Buf_AddSpace(buf); 128 * Adds a space to buffer. */ 129 #define Buf_AddSpace(b) Buf_AddChar((b), ' ') 130 /* Buf_AddString(buf, str); 131 * Adds the contents of a NULL terminated string to buffer. */ 132 #define Buf_AddString(b, s) Buf_AddChars((b), strlen(s), (s)) 133 /* Buf_Addi(buf, s, e); 134 * Adds characters between s and e to buffer. */ 135 #define Buf_Addi(b, s, e) Buf_AddChars((b), (e) - (s), (s)) 136 137 /* Buf_KillTrailingSpaces(buf); 138 * Removes non-backslashed spaces at the end of a buffer. */ 139 extern void Buf_KillTrailingSpaces(Buffer); 140 141 #endif /* _BUF_H */ 142