xref: /freebsd/contrib/bmake/buf.h (revision 06b9b3e0ad0dc3f0166b3e8f26ced68c271cf527)
1*06b9b3e0SSimon J. Gerraty /*	$NetBSD: buf.h,v 1.38 2020/12/28 15:42:53 rillig Exp $	*/
23955d011SMarcel Moolenaar 
33955d011SMarcel Moolenaar /*
43955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
53955d011SMarcel Moolenaar  *
63955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
73955d011SMarcel Moolenaar  * Adam de Boor.
83955d011SMarcel Moolenaar  *
93955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
103955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
113955d011SMarcel Moolenaar  * are met:
123955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
133955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
143955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
153955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
163955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
173955d011SMarcel Moolenaar  * 3. Neither the name of the University nor the names of its contributors
183955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
193955d011SMarcel Moolenaar  *    without specific prior written permission.
203955d011SMarcel Moolenaar  *
213955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
223955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
233955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
243955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
253955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
263955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
273955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
283955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
293955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
303955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313955d011SMarcel Moolenaar  * SUCH DAMAGE.
323955d011SMarcel Moolenaar  *
333955d011SMarcel Moolenaar  *	from: @(#)buf.h	8.1 (Berkeley) 6/6/93
343955d011SMarcel Moolenaar  */
353955d011SMarcel Moolenaar 
363955d011SMarcel Moolenaar /*
373955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989 by Adam de Boor
383955d011SMarcel Moolenaar  * Copyright (c) 1989 by Berkeley Softworks
393955d011SMarcel Moolenaar  * All rights reserved.
403955d011SMarcel Moolenaar  *
413955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
423955d011SMarcel Moolenaar  * Adam de Boor.
433955d011SMarcel Moolenaar  *
443955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
453955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
463955d011SMarcel Moolenaar  * are met:
473955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
483955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
493955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
503955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
513955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
523955d011SMarcel Moolenaar  * 3. All advertising materials mentioning features or use of this software
533955d011SMarcel Moolenaar  *    must display the following acknowledgement:
543955d011SMarcel Moolenaar  *	This product includes software developed by the University of
553955d011SMarcel Moolenaar  *	California, Berkeley and its contributors.
563955d011SMarcel Moolenaar  * 4. Neither the name of the University nor the names of its contributors
573955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
583955d011SMarcel Moolenaar  *    without specific prior written permission.
593955d011SMarcel Moolenaar  *
603955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
613955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
623955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
633955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
643955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
653955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
663955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
673955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
683955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
693955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
703955d011SMarcel Moolenaar  * SUCH DAMAGE.
713955d011SMarcel Moolenaar  *
723955d011SMarcel Moolenaar  *	from: @(#)buf.h	8.1 (Berkeley) 6/6/93
733955d011SMarcel Moolenaar  */
743955d011SMarcel Moolenaar 
752c3632d1SSimon J. Gerraty /* Automatically growing null-terminated buffers of characters. */
763955d011SMarcel Moolenaar 
778695518cSSimon J. Gerraty #ifndef MAKE_BUF_H
788695518cSSimon J. Gerraty #define MAKE_BUF_H
793955d011SMarcel Moolenaar 
802c3632d1SSimon J. Gerraty #include <stddef.h>
813955d011SMarcel Moolenaar 
822c3632d1SSimon J. Gerraty /* An automatically growing null-terminated buffer of characters. */
833955d011SMarcel Moolenaar typedef struct Buffer {
84956e45f6SSimon J. Gerraty 	size_t cap;	/* Allocated size of the buffer, including the null */
85956e45f6SSimon J. Gerraty 	size_t len;	/* Number of bytes in buffer, excluding the null */
86956e45f6SSimon J. Gerraty 	char *data;	/* The buffer itself (always null-terminated) */
873955d011SMarcel Moolenaar } Buffer;
883955d011SMarcel Moolenaar 
892c3632d1SSimon J. Gerraty /* If we aren't on NetBSD, __predict_false() might not be defined. */
903955d011SMarcel Moolenaar #ifndef __predict_false
913955d011SMarcel Moolenaar #define __predict_false(x) (x)
923955d011SMarcel Moolenaar #endif
933955d011SMarcel Moolenaar 
94*06b9b3e0SSimon J. Gerraty void Buf_Expand(Buffer *);
952c3632d1SSimon J. Gerraty 
962c3632d1SSimon J. Gerraty /* Buf_AddByte adds a single byte to a buffer. */
97e2eeea75SSimon J. Gerraty MAKE_INLINE void
98956e45f6SSimon J. Gerraty Buf_AddByte(Buffer *buf, char byte)
992c3632d1SSimon J. Gerraty {
100956e45f6SSimon J. Gerraty 	size_t old_len = buf->len++;
101956e45f6SSimon J. Gerraty 	char *end;
102956e45f6SSimon J. Gerraty 	if (__predict_false(old_len + 1 >= buf->cap))
103*06b9b3e0SSimon J. Gerraty 		Buf_Expand(buf);
104956e45f6SSimon J. Gerraty 	end = buf->data + old_len;
105956e45f6SSimon J. Gerraty 	end[0] = byte;
106956e45f6SSimon J. Gerraty 	end[1] = '\0';
1072c3632d1SSimon J. Gerraty }
1082c3632d1SSimon J. Gerraty 
109e2eeea75SSimon J. Gerraty MAKE_INLINE size_t
110956e45f6SSimon J. Gerraty Buf_Len(const Buffer *buf)
1112c3632d1SSimon J. Gerraty {
112956e45f6SSimon J. Gerraty 	return buf->len;
113956e45f6SSimon J. Gerraty }
114956e45f6SSimon J. Gerraty 
115e2eeea75SSimon J. Gerraty MAKE_INLINE Boolean
116956e45f6SSimon J. Gerraty Buf_EndsWith(const Buffer *buf, char ch)
117956e45f6SSimon J. Gerraty {
118956e45f6SSimon J. Gerraty 	return buf->len > 0 && buf->data[buf->len - 1] == ch;
1192c3632d1SSimon J. Gerraty }
1202c3632d1SSimon J. Gerraty 
1212c3632d1SSimon J. Gerraty void Buf_AddBytes(Buffer *, const char *, size_t);
1222c3632d1SSimon J. Gerraty void Buf_AddBytesBetween(Buffer *, const char *, const char *);
1232c3632d1SSimon J. Gerraty void Buf_AddStr(Buffer *, const char *);
1242c3632d1SSimon J. Gerraty void Buf_AddInt(Buffer *, int);
1252c3632d1SSimon J. Gerraty char *Buf_GetAll(Buffer *, size_t *);
1263955d011SMarcel Moolenaar void Buf_Empty(Buffer *);
127e2eeea75SSimon J. Gerraty void Buf_Init(Buffer *);
128e2eeea75SSimon J. Gerraty void Buf_InitSize(Buffer *, size_t);
1292c3632d1SSimon J. Gerraty char *Buf_Destroy(Buffer *, Boolean);
1302c3632d1SSimon J. Gerraty char *Buf_DestroyCompact(Buffer *);
1313955d011SMarcel Moolenaar 
1328695518cSSimon J. Gerraty #endif /* MAKE_BUF_H */
133