Home | History | Annotate | Line # | Download | only in make
buf.c revision 1.15
      1 /*	$NetBSD: buf.c,v 1.15 2004/05/07 00:04:38 ross Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     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. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  */
     71 
     72 #ifndef MAKE_NATIVE
     73 static char rcsid[] = "$NetBSD: buf.c,v 1.15 2004/05/07 00:04:38 ross Exp $";
     74 #else
     75 #include <sys/cdefs.h>
     76 #ifndef lint
     77 #if 0
     78 static char sccsid[] = "@(#)buf.c	8.1 (Berkeley) 6/6/93";
     79 #else
     80 __RCSID("$NetBSD: buf.c,v 1.15 2004/05/07 00:04:38 ross Exp $");
     81 #endif
     82 #endif /* not lint */
     83 #endif
     84 
     85 /*-
     86  * buf.c --
     87  *	Functions for automatically-expanded buffers.
     88  */
     89 
     90 #include    "sprite.h"
     91 #include    "make.h"
     92 #include    "buf.h"
     93 
     94 #ifndef max
     95 #define max(a,b)  ((a) > (b) ? (a) : (b))
     96 #endif
     97 
     98 /*
     99  * BufExpand --
    100  * 	Expand the given buffer to hold the given number of additional
    101  *	bytes.
    102  *	Makes sure there's room for an extra NULL byte at the end of the
    103  *	buffer in case it holds a string.
    104  */
    105 #define BufExpand(bp,nb) \
    106  	while (bp->left < (nb)+1) {\
    107 	    int newSize = (bp)->size * 2; \
    108 	    Byte  *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
    109 	    \
    110 	    (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
    111 	    (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
    112 	    (bp)->buffer = newBuf;\
    113 	    (bp)->size = newSize;\
    114 	    (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
    115 	}
    116 
    117 #define BUF_DEF_SIZE	256 	/* Default buffer size */
    118 
    119 /*-
    120  *-----------------------------------------------------------------------
    121  * Buf_OvAddByte --
    122  *	Add a single byte to the buffer.  left is zero or negative.
    123  *
    124  * Results:
    125  *	None.
    126  *
    127  * Side Effects:
    128  *	The buffer may be expanded.
    129  *
    130  *-----------------------------------------------------------------------
    131  */
    132 void
    133 Buf_OvAddByte(Buffer bp, int byte)
    134 {
    135     int nbytes = 1;
    136     bp->left = 0;
    137     BufExpand (bp, nbytes);
    138 
    139     *bp->inPtr++ = byte;
    140     bp->left--;
    141 
    142     /*
    143      * Null-terminate
    144      */
    145     *bp->inPtr = 0;
    146 }
    147 
    148 /*-
    150  *-----------------------------------------------------------------------
    151  * Buf_AddBytes --
    152  *	Add a number of bytes to the buffer.
    153  *
    154  * Results:
    155  *	None.
    156  *
    157  * Side Effects:
    158  *	Guess what?
    159  *
    160  *-----------------------------------------------------------------------
    161  */
    162 void
    163 Buf_AddBytes(Buffer bp, int numBytes, const Byte *bytesPtr)
    164 {
    165 
    166     BufExpand (bp, numBytes);
    167 
    168     memcpy (bp->inPtr, bytesPtr, numBytes);
    169     bp->inPtr += numBytes;
    170     bp->left -= numBytes;
    171 
    172     /*
    173      * Null-terminate
    174      */
    175     *bp->inPtr = 0;
    176 }
    177 
    178 /*-
    180  *-----------------------------------------------------------------------
    181  * Buf_GetAll --
    182  *	Get all the available data at once.
    183  *
    184  * Results:
    185  *	A pointer to the data and the number of bytes available.
    186  *
    187  * Side Effects:
    188  *	None.
    189  *
    190  *-----------------------------------------------------------------------
    191  */
    192 Byte *
    193 Buf_GetAll(Buffer bp, int *numBytesPtr)
    194 {
    195 
    196     if (numBytesPtr != (int *)NULL) {
    197 	*numBytesPtr = bp->inPtr - bp->outPtr;
    198     }
    199 
    200     return (bp->outPtr);
    201 }
    202 
    203 /*-
    205  *-----------------------------------------------------------------------
    206  * Buf_Discard --
    207  *	Throw away bytes in a buffer.
    208  *
    209  * Results:
    210  *	None.
    211  *
    212  * Side Effects:
    213  *	The bytes are discarded.
    214  *
    215  *-----------------------------------------------------------------------
    216  */
    217 void
    218 Buf_Discard(Buffer bp, int numBytes)
    219 {
    220 
    221     if (bp->inPtr - bp->outPtr <= numBytes) {
    222 	bp->inPtr = bp->outPtr = bp->buffer;
    223 	bp->left = bp->size;
    224 	*bp->inPtr = 0;
    225     } else {
    226 	bp->outPtr += numBytes;
    227     }
    228 }
    229 
    230 /*-
    232  *-----------------------------------------------------------------------
    233  * Buf_Size --
    234  *	Returns the number of bytes in the given buffer. Doesn't include
    235  *	the null-terminating byte.
    236  *
    237  * Results:
    238  *	The number of bytes.
    239  *
    240  * Side Effects:
    241  *	None.
    242  *
    243  *-----------------------------------------------------------------------
    244  */
    245 int
    246 Buf_Size(Buffer buf)
    247 {
    248     return (buf->inPtr - buf->outPtr);
    249 }
    250 
    251 /*-
    253  *-----------------------------------------------------------------------
    254  * Buf_Init --
    255  *	Initialize a buffer. If no initial size is given, a reasonable
    256  *	default is used.
    257  *
    258  * Input:
    259  *	size		Initial size for the buffer
    260  *
    261  * Results:
    262  *	A buffer to be given to other functions in this library.
    263  *
    264  * Side Effects:
    265  *	The buffer is created, the space allocated and pointers
    266  *	initialized.
    267  *
    268  *-----------------------------------------------------------------------
    269  */
    270 Buffer
    271 Buf_Init(int size)
    272 {
    273     Buffer bp;	  	/* New Buffer */
    274 
    275     bp = (Buffer)emalloc(sizeof(*bp));
    276 
    277     if (size <= 0) {
    278 	size = BUF_DEF_SIZE;
    279     }
    280     bp->left = bp->size = size;
    281     bp->buffer = (Byte *)emalloc(size);
    282     bp->inPtr = bp->outPtr = bp->buffer;
    283     *bp->inPtr = 0;
    284 
    285     return (bp);
    286 }
    287 
    288 /*-
    290  *-----------------------------------------------------------------------
    291  * Buf_Destroy --
    292  *	Nuke a buffer and all its resources.
    293  *
    294  * Input:
    295  *	buf		Buffer to destroy
    296  *	freeData	TRUE if the data should be destroyed
    297  *
    298  * Results:
    299  *	None.
    300  *
    301  * Side Effects:
    302  *	The buffer is freed.
    303  *
    304  *-----------------------------------------------------------------------
    305  */
    306 void
    307 Buf_Destroy(Buffer buf, Boolean freeData)
    308 {
    309 
    310     if (freeData) {
    311 	free ((char *)buf->buffer);
    312     }
    313     free ((char *)buf);
    314 }
    315 
    316 /*-
    318  *-----------------------------------------------------------------------
    319  * Buf_ReplaceLastByte --
    320  *     Replace the last byte in a buffer.
    321  *
    322  * Input:
    323  *	buf		buffer to augment
    324  *	byte		byte to be written
    325  *
    326  * Results:
    327  *     None.
    328  *
    329  * Side Effects:
    330  *     If the buffer was empty intially, then a new byte will be added.
    331  *     Otherwise, the last byte is overwritten.
    332  *
    333  *-----------------------------------------------------------------------
    334  */
    335 void
    336 Buf_ReplaceLastByte(Buffer buf, int byte)
    337 {
    338     if (buf->inPtr == buf->outPtr)
    339         Buf_AddByte(buf, byte);
    340     else
    341         *(buf->inPtr - 1) = byte;
    342 }
    343