[BACK]Return to buf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/buf.c, Revision 1.7

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