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