xref: /dragonfly/contrib/bmake/buf.h (revision 6eef5f0c)
1*6eef5f0cSAntonio Huete Jimenez /*	$NetBSD: buf.h,v 1.47 2022/01/08 17:25:19 rillig Exp $	*/
201e196c8SJohn Marino 
301e196c8SJohn Marino /*
401e196c8SJohn Marino  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
501e196c8SJohn Marino  *
601e196c8SJohn Marino  * This code is derived from software contributed to Berkeley by
701e196c8SJohn Marino  * Adam de Boor.
801e196c8SJohn Marino  *
901e196c8SJohn Marino  * Redistribution and use in source and binary forms, with or without
1001e196c8SJohn Marino  * modification, are permitted provided that the following conditions
1101e196c8SJohn Marino  * are met:
1201e196c8SJohn Marino  * 1. Redistributions of source code must retain the above copyright
1301e196c8SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1401e196c8SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1501e196c8SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1601e196c8SJohn Marino  *    documentation and/or other materials provided with the distribution.
1701e196c8SJohn Marino  * 3. Neither the name of the University nor the names of its contributors
1801e196c8SJohn Marino  *    may be used to endorse or promote products derived from this software
1901e196c8SJohn Marino  *    without specific prior written permission.
2001e196c8SJohn Marino  *
2101e196c8SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2201e196c8SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2301e196c8SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2401e196c8SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2501e196c8SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2601e196c8SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2701e196c8SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2801e196c8SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2901e196c8SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3001e196c8SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3101e196c8SJohn Marino  * SUCH DAMAGE.
3201e196c8SJohn Marino  *
3301e196c8SJohn Marino  *	from: @(#)buf.h	8.1 (Berkeley) 6/6/93
3401e196c8SJohn Marino  */
3501e196c8SJohn Marino 
3601e196c8SJohn Marino /*
3701e196c8SJohn Marino  * Copyright (c) 1988, 1989 by Adam de Boor
3801e196c8SJohn Marino  * Copyright (c) 1989 by Berkeley Softworks
3901e196c8SJohn Marino  * All rights reserved.
4001e196c8SJohn Marino  *
4101e196c8SJohn Marino  * This code is derived from software contributed to Berkeley by
4201e196c8SJohn Marino  * Adam de Boor.
4301e196c8SJohn Marino  *
4401e196c8SJohn Marino  * Redistribution and use in source and binary forms, with or without
4501e196c8SJohn Marino  * modification, are permitted provided that the following conditions
4601e196c8SJohn Marino  * are met:
4701e196c8SJohn Marino  * 1. Redistributions of source code must retain the above copyright
4801e196c8SJohn Marino  *    notice, this list of conditions and the following disclaimer.
4901e196c8SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
5001e196c8SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
5101e196c8SJohn Marino  *    documentation and/or other materials provided with the distribution.
5201e196c8SJohn Marino  * 3. All advertising materials mentioning features or use of this software
5301e196c8SJohn Marino  *    must display the following acknowledgement:
5401e196c8SJohn Marino  *	This product includes software developed by the University of
5501e196c8SJohn Marino  *	California, Berkeley and its contributors.
5601e196c8SJohn Marino  * 4. Neither the name of the University nor the names of its contributors
5701e196c8SJohn Marino  *    may be used to endorse or promote products derived from this software
5801e196c8SJohn Marino  *    without specific prior written permission.
5901e196c8SJohn Marino  *
6001e196c8SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6101e196c8SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6201e196c8SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6301e196c8SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6401e196c8SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6501e196c8SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6601e196c8SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6701e196c8SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6801e196c8SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6901e196c8SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7001e196c8SJohn Marino  * SUCH DAMAGE.
7101e196c8SJohn Marino  *
7201e196c8SJohn Marino  *	from: @(#)buf.h	8.1 (Berkeley) 6/6/93
7301e196c8SJohn Marino  */
7401e196c8SJohn Marino 
75a34d5fb1SAntonio Huete Jimenez /* Automatically growing null-terminated buffers of characters. */
7601e196c8SJohn Marino 
77ca58f742SDaniel Fojt #ifndef MAKE_BUF_H
78ca58f742SDaniel Fojt #define MAKE_BUF_H
7901e196c8SJohn Marino 
80a34d5fb1SAntonio Huete Jimenez #include <stddef.h>
8101e196c8SJohn Marino 
82a34d5fb1SAntonio Huete Jimenez /* An automatically growing null-terminated buffer of characters. */
8301e196c8SJohn Marino typedef struct Buffer {
84a34d5fb1SAntonio Huete Jimenez 	size_t cap;	/* Allocated size of the buffer, including the '\0' */
85a34d5fb1SAntonio Huete Jimenez 	size_t len;	/* Number of bytes in buffer, excluding the '\0' */
86a34d5fb1SAntonio Huete Jimenez 	char *data;	/* The buffer itself (always null-terminated) */
8701e196c8SJohn Marino } Buffer;
8801e196c8SJohn Marino 
89a34d5fb1SAntonio Huete Jimenez void Buf_Expand(Buffer *);
9001e196c8SJohn Marino 
91*6eef5f0cSAntonio Huete Jimenez /* Mark the buffer as empty, so it can be filled with data again. */
92*6eef5f0cSAntonio Huete Jimenez MAKE_INLINE void
Buf_Clear(Buffer * buf)93*6eef5f0cSAntonio Huete Jimenez Buf_Clear(Buffer *buf)
94*6eef5f0cSAntonio Huete Jimenez {
95*6eef5f0cSAntonio Huete Jimenez 	buf->len = 0;
96*6eef5f0cSAntonio Huete Jimenez 	buf->data[0] = '\0';
97*6eef5f0cSAntonio Huete Jimenez }
98*6eef5f0cSAntonio Huete Jimenez 
9901e196c8SJohn Marino /* Buf_AddByte adds a single byte to a buffer. */
100a34d5fb1SAntonio Huete Jimenez MAKE_INLINE void
Buf_AddByte(Buffer * buf,char byte)101a34d5fb1SAntonio Huete Jimenez Buf_AddByte(Buffer *buf, char byte)
102a34d5fb1SAntonio Huete Jimenez {
103a34d5fb1SAntonio Huete Jimenez 	size_t old_len = buf->len++;
104a34d5fb1SAntonio Huete Jimenez 	char *end;
105a34d5fb1SAntonio Huete Jimenez 	if (old_len + 1 >= buf->cap)
106a34d5fb1SAntonio Huete Jimenez 		Buf_Expand(buf);
107a34d5fb1SAntonio Huete Jimenez 	end = buf->data + old_len;
108a34d5fb1SAntonio Huete Jimenez 	end[0] = byte;
109a34d5fb1SAntonio Huete Jimenez 	end[1] = '\0';
110a34d5fb1SAntonio Huete Jimenez }
11101e196c8SJohn Marino 
112*6eef5f0cSAntonio Huete Jimenez MAKE_INLINE bool MAKE_ATTR_USE
Buf_EndsWith(const Buffer * buf,char ch)113a34d5fb1SAntonio Huete Jimenez Buf_EndsWith(const Buffer *buf, char ch)
114a34d5fb1SAntonio Huete Jimenez {
115a34d5fb1SAntonio Huete Jimenez 	return buf->len > 0 && buf->data[buf->len - 1] == ch;
116a34d5fb1SAntonio Huete Jimenez }
11701e196c8SJohn Marino 
118a34d5fb1SAntonio Huete Jimenez void Buf_AddBytes(Buffer *, const char *, size_t);
119a34d5fb1SAntonio Huete Jimenez void Buf_AddBytesBetween(Buffer *, const char *, const char *);
120a34d5fb1SAntonio Huete Jimenez void Buf_AddStr(Buffer *, const char *);
121a34d5fb1SAntonio Huete Jimenez void Buf_AddInt(Buffer *, int);
122*6eef5f0cSAntonio Huete Jimenez void Buf_AddFlag(Buffer *, bool, const char *);
123a34d5fb1SAntonio Huete Jimenez void Buf_Init(Buffer *);
124a34d5fb1SAntonio Huete Jimenez void Buf_InitSize(Buffer *, size_t);
125a34d5fb1SAntonio Huete Jimenez void Buf_Done(Buffer *);
126*6eef5f0cSAntonio Huete Jimenez char *Buf_DoneData(Buffer *) MAKE_ATTR_USE;
127*6eef5f0cSAntonio Huete Jimenez char *Buf_DoneDataCompact(Buffer *) MAKE_ATTR_USE;
12801e196c8SJohn Marino 
129*6eef5f0cSAntonio Huete Jimenez #endif
130