xref: /openbsd/usr.bin/make/buf.h (revision 583c2e1d)
1 #ifndef _BUF_H
2 #define _BUF_H
3 
4 /*	$OpenBSD: buf.h,v 1.24 2020/01/13 13:54:44 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 BufExpand(Buffer, size_t);
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_Truncate(buffer, length) */
97 #define Buf_Truncate(bp, len)	((void)((bp)->inPtr = (bp)->buffer + (len)))
98 /* Buf_Reset(buf);
99  *	Empties buffer.  */
100 #define Buf_Reset(bp)	Buf_Truncate(bp, 0)
101 /* n = Buf_Size(buf);
102  *	Returns number of chars currently in buf.
103  *	Doesn't include the null-terminating char.  */
104 #define Buf_Size(bp)	((size_t)((bp)->inPtr - (bp)->buffer))
105 /* Buf_Init(buf, init);
106  *	Initializes a buffer, to hold approximately init chars.
107  *	Set init to 0 if you have no idea.  */
108 extern void Buf_Init(Buffer, size_t);
109 /* Buf_Reinit(buf, init);
110  *	Initializes/reset a static buffer */
111 extern void Buf_Reinit(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 	    BufExpand(bp, 1);			\
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 extern void Buf_printf(Buffer, const char *, ...);
138 #define Buf_puts(b, s)	Buf_AddChars((b), strlen(s), (s))
139 
140 #endif /* _BUF_H */
141