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