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