1 #ifndef _BUF_H 2 #define _BUF_H 3 4 /* $OpenPackages$ */ 5 /* $OpenBSD: buf.h,v 1.17 2007/09/17 09:28:36 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. Neither the name of the University nor the names of its contributors 53 * may be used to endorse or promote products derived from this software 54 * without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 66 * SUCH DAMAGE. 67 * 68 * from: @(#)buf.h 8.1 (Berkeley) 6/6/93 69 */ 70 71 /* 72 * buf 73 * Support for extensible char buffers. 74 * One adds chars to a buffer, then retrieves the contents using 75 * Buf_Retrieve (no copy involved), or releases the memory using 76 * Buf_Destroy. 77 */ 78 79 80 /* Internal data structures and functions. BUFFER is visible so 81 * that users can allocate the memory themselves. */ 82 typedef struct Buffer_ { 83 char *buffer; /* The buffer itself. */ 84 char *inPtr; /* Place to write to. */ 85 char *endPtr; /* End of allocated space. */ 86 } BUFFER; 87 88 /* Internal support for Buf_AddChar. */ 89 extern void BufOverflow(Buffer); 90 91 92 /* User interface */ 93 94 /* Buf_AddChars(buf, n, str); 95 * Adds n chars to buffer buf starting from str. */ 96 extern void Buf_AddChars(Buffer, size_t, const char *); 97 /* Buf_Reset(buf); 98 * Empties buffer. */ 99 #define Buf_Reset(bp) ((void)((bp)->inPtr = (bp)->buffer)) 100 /* n = Buf_Size(buf); 101 * Returns number of chars currently in buf. 102 * Doesn't include the null-terminating char. */ 103 #define Buf_Size(bp) ((size_t)((bp)->inPtr - (bp)->buffer)) 104 /* Buf_Init(buf, init); 105 * Initializes a buffer, to hold approximately init chars. 106 * Set init to 0 if you have no idea. */ 107 extern void Buf_Init(Buffer, size_t); 108 /* Buf_Destroy(buf); 109 * Nukes a buffer and all its resources. */ 110 #define Buf_Destroy(bp) ((void)free((bp)->buffer)) 111 /* str = Buf_Retrieve(buf); 112 * Retrieves data from a buffer, as a NULL terminated string. */ 113 #define Buf_Retrieve(bp) (*(bp)->inPtr = '\0', (bp)->buffer) 114 /* Buf_AddChar(buf, c); 115 * Adds a single char to buffer. */ 116 #define Buf_AddChar(bp, byte) \ 117 do { \ 118 if ((bp)->endPtr - (bp)->inPtr <= 1) \ 119 BufOverflow(bp); \ 120 *(bp)->inPtr++ = (byte); \ 121 } while (0) 122 123 /* Buf_AddSpace(buf); 124 * Adds a space to buffer. */ 125 #define Buf_AddSpace(b) Buf_AddChar((b), ' ') 126 /* Buf_AddString(buf, str); 127 * Adds the contents of a NULL terminated string to buffer. */ 128 #define Buf_AddString(b, s) Buf_AddChars((b), strlen(s), (s)) 129 /* Buf_Addi(buf, s, e); 130 * Adds characters between s and e to buffer. */ 131 #define Buf_Addi(b, s, e) Buf_AddChars((b), (e) - (s), (s)) 132 133 /* Buf_KillTrailingSpaces(buf); 134 * Removes non-backslashed spaces at the end of a buffer. */ 135 extern void Buf_KillTrailingSpaces(Buffer); 136 137 #endif /* _BUF_H */ 138