xref: /minix3/usr.bin/make/buf.h (revision 2bc7c627ace37c52ea76fc2f2c700c563df69735)
1*2bc7c627SLionel Sambuc /*	$NetBSD: buf.h,v 1.17 2012/04/24 20:26:58 sjg Exp $	*/
22e2caf59SThomas Veerman 
32e2caf59SThomas Veerman /*
42e2caf59SThomas Veerman  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
52e2caf59SThomas Veerman  *
62e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
72e2caf59SThomas Veerman  * Adam de Boor.
82e2caf59SThomas Veerman  *
92e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
102e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
112e2caf59SThomas Veerman  * are met:
122e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
132e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
142e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
152e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
162e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
172e2caf59SThomas Veerman  * 3. Neither the name of the University nor the names of its contributors
182e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
192e2caf59SThomas Veerman  *    without specific prior written permission.
202e2caf59SThomas Veerman  *
212e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
222e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
232e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
242e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
252e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
262e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
272e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
282e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
292e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
302e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
312e2caf59SThomas Veerman  * SUCH DAMAGE.
322e2caf59SThomas Veerman  *
332e2caf59SThomas Veerman  *	from: @(#)buf.h	8.1 (Berkeley) 6/6/93
342e2caf59SThomas Veerman  */
352e2caf59SThomas Veerman 
362e2caf59SThomas Veerman /*
372e2caf59SThomas Veerman  * Copyright (c) 1988, 1989 by Adam de Boor
382e2caf59SThomas Veerman  * Copyright (c) 1989 by Berkeley Softworks
392e2caf59SThomas Veerman  * All rights reserved.
402e2caf59SThomas Veerman  *
412e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
422e2caf59SThomas Veerman  * Adam de Boor.
432e2caf59SThomas Veerman  *
442e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
452e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
462e2caf59SThomas Veerman  * are met:
472e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
482e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
492e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
502e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
512e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
522e2caf59SThomas Veerman  * 3. All advertising materials mentioning features or use of this software
532e2caf59SThomas Veerman  *    must display the following acknowledgement:
542e2caf59SThomas Veerman  *	This product includes software developed by the University of
552e2caf59SThomas Veerman  *	California, Berkeley and its contributors.
562e2caf59SThomas Veerman  * 4. Neither the name of the University nor the names of its contributors
572e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
582e2caf59SThomas Veerman  *    without specific prior written permission.
592e2caf59SThomas Veerman  *
602e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
612e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
622e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
632e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
642e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
652e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
662e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
672e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
682e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
692e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
702e2caf59SThomas Veerman  * SUCH DAMAGE.
712e2caf59SThomas Veerman  *
722e2caf59SThomas Veerman  *	from: @(#)buf.h	8.1 (Berkeley) 6/6/93
732e2caf59SThomas Veerman  */
742e2caf59SThomas Veerman 
752e2caf59SThomas Veerman /*-
762e2caf59SThomas Veerman  * buf.h --
772e2caf59SThomas Veerman  *	Header for users of the buf library.
782e2caf59SThomas Veerman  */
792e2caf59SThomas Veerman 
802e2caf59SThomas Veerman #ifndef _BUF_H
812e2caf59SThomas Veerman #define _BUF_H
822e2caf59SThomas Veerman 
832e2caf59SThomas Veerman typedef char Byte;
842e2caf59SThomas Veerman 
852e2caf59SThomas Veerman typedef struct Buffer {
862e2caf59SThomas Veerman     int	    size; 	/* Current size of the buffer */
872e2caf59SThomas Veerman     int     count;	/* Number of bytes in buffer */
882e2caf59SThomas Veerman     Byte    *buffer;	/* The buffer itself (zero terminated) */
892e2caf59SThomas Veerman } Buffer;
902e2caf59SThomas Veerman 
912e2caf59SThomas Veerman /* If we aren't on netbsd, __predict_false() might not be defined. */
922e2caf59SThomas Veerman #ifndef __predict_false
932e2caf59SThomas Veerman #define __predict_false(x) (x)
942e2caf59SThomas Veerman #endif
952e2caf59SThomas Veerman 
962e2caf59SThomas Veerman /* Buf_AddByte adds a single byte to a buffer. */
972e2caf59SThomas Veerman #define	Buf_AddByte(bp, byte) do { \
982e2caf59SThomas Veerman 	int _count = ++(bp)->count; \
992e2caf59SThomas Veerman 	char *_ptr; \
1002e2caf59SThomas Veerman 	if (__predict_false(_count >= (bp)->size)) \
1012e2caf59SThomas Veerman 		Buf_Expand_1(bp); \
1022e2caf59SThomas Veerman 	_ptr = (bp)->buffer + _count; \
1032e2caf59SThomas Veerman 	_ptr[-1] = (byte); \
1042e2caf59SThomas Veerman 	_ptr[0] = 0; \
1052e2caf59SThomas Veerman     } while (0)
1062e2caf59SThomas Veerman 
1072e2caf59SThomas Veerman #define BUF_ERROR 256
1082e2caf59SThomas Veerman 
1092e2caf59SThomas Veerman #define Buf_Size(bp) ((bp)->count)
1102e2caf59SThomas Veerman 
1112e2caf59SThomas Veerman void Buf_Expand_1(Buffer *);
1122e2caf59SThomas Veerman void Buf_AddBytes(Buffer *, int, const Byte *);
1132e2caf59SThomas Veerman Byte *Buf_GetAll(Buffer *, int *);
1142e2caf59SThomas Veerman void Buf_Empty(Buffer *);
1152e2caf59SThomas Veerman void Buf_Init(Buffer *, int);
1162e2caf59SThomas Veerman Byte *Buf_Destroy(Buffer *, Boolean);
117*2bc7c627SLionel Sambuc Byte *Buf_DestroyCompact(Buffer *);
1182e2caf59SThomas Veerman 
1192e2caf59SThomas Veerman #endif /* _BUF_H */
120