Changeset 51 for trunk/src/kmk/buf.c


Ignore:
Timestamp:
Apr 7, 2003, 3:30:32 AM (22 years ago)
Author:
bird
Message:

kMk and porting to kLib.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/buf.c

    r35 r51  
    1818 * 3. All advertising materials mentioning features or use of this software
    1919 *    must display the following acknowledgement:
    20  *      This product includes software developed by the University of
    21  *      California, Berkeley and its contributors.
     20 *      This product includes software developed by the University of
     21 *      California, Berkeley and its contributors.
    2222 * 4. Neither the name of the University nor the names of its contributors
    2323 *    may be used to endorse or promote products derived from this software
     
    3939#ifndef lint
    4040#if 0
    41 static char sccsid[] = "@(#)buf.c       8.1 (Berkeley) 6/6/93";
     41static char sccsid[] = "@(#)buf.c       8.1 (Berkeley) 6/6/93";
    4242#else
    4343static const char rcsid[] =
    4444  "$FreeBSD: src/usr.bin/make/buf.c,v 1.11 1999/09/11 13:08:01 hoek Exp $";
    4545#endif
     46#define KLIBFILEDEF rcsid
    4647#endif /* not lint */
    4748
    4849/*-
    4950 * buf.c --
    50  *      Functions for automatically-expanded buffers.
     51 *      Functions for automatically-expanded buffers.
    5152 */
    5253
     
    6162/*
    6263 * BufExpand --
    63  *      Expand the given buffer to hold the given number of additional
    64  *      bytes.
    65  *      Makes sure there's room for an extra NULL byte at the end of the
    66  *      buffer in case it holds a string.
     64 *      Expand the given buffer to hold the given number of additional
     65 *      bytes.
     66 *      Makes sure there's room for an extra NULL byte at the end of the
     67 *      buffer in case it holds a string.
    6768 */
    6869#define BufExpand(bp,nb) \
    69         if (bp->left < (nb)+1) {\
    70             int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
    71             Byte  *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
    72             \
    73             (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
    74             (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
    75             (bp)->buffer = newBuf;\
    76             (bp)->size = newSize;\
    77             (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
    78         }
    79 
    80 #define BUF_DEF_SIZE    256     /* Default buffer size */
    81 #define BUF_ADD_INC     256     /* Expansion increment when Adding */
    82 #define BUF_UNGET_INC   16      /* Expansion increment when Ungetting */
     70        if (bp->left < (nb)+1) {\
     71            int newSize = (bp)->size + max((nb)+1,BUF_ADD_INC); \
     72            Byte  *newBuf = (Byte *) erealloc((bp)->buffer, newSize); \
     73            \
     74            (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
     75            (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
     76            (bp)->buffer = newBuf;\
     77            (bp)->size = newSize;\
     78            (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
     79        }
     80
     81#define BUF_DEF_SIZE    256     /* Default buffer size */
     82#define BUF_ADD_INC     256     /* Expansion increment when Adding */
     83#define BUF_UNGET_INC   16      /* Expansion increment when Ungetting */
    8384
    8485/*-
    8586 *-----------------------------------------------------------------------
    8687 * Buf_OvAddByte --
    87  *      Add a single byte to the buffer.  left is zero or negative.
    88  *
    89  * Results:
    90  *      None.
    91  *
    92  * Side Effects:
    93  *      The buffer may be expanded.
     88 *      Add a single byte to the buffer.  left is zero or negative.
     89 *
     90 * Results:
     91 *      None.
     92 *
     93 * Side Effects:
     94 *      The buffer may be expanded.
    9495 *
    9596 *-----------------------------------------------------------------------
     
    117118 *-----------------------------------------------------------------------
    118119 * Buf_AddBytes --
    119  *      Add a number of bytes to the buffer.
    120  *
    121  * Results:
    122  *      None.
    123  *
    124  * Side Effects:
    125  *      Guess what?
     120 *      Add a number of bytes to the buffer.
     121 *
     122 * Results:
     123 *      None.
     124 *
     125 * Side Effects:
     126 *      Guess what?
    126127 *
    127128 *-----------------------------------------------------------------------
     
    130131Buf_AddBytes (bp, numBytes, bytesPtr)
    131132    register Buffer bp;
    132     int     numBytes;
     133    int     numBytes;
    133134    const Byte *bytesPtr;
    134135{
     
    150151 *-----------------------------------------------------------------------
    151152 * Buf_UngetByte --
    152  *      Place the byte back at the beginning of the buffer.
    153  *
    154  * Results:
    155  *      SUCCESS if the byte was added ok. FAILURE if not.
    156  *
    157  * Side Effects:
    158  *      The byte is stuffed in the buffer and outPtr is decremented.
     153 *      Place the byte back at the beginning of the buffer.
     154 *
     155 * Results:
     156 *      SUCCESS if the byte was added ok. FAILURE if not.
     157 *
     158 * Side Effects:
     159 *      The byte is stuffed in the buffer and outPtr is decremented.
    159160 *
    160161 *-----------------------------------------------------------------------
     
    167168
    168169    if (bp->outPtr != bp->buffer) {
    169         bp->outPtr--;
    170         *bp->outPtr = byte;
     170        bp->outPtr--;
     171        *bp->outPtr = byte;
    171172    } else if (bp->outPtr == bp->inPtr) {
    172         *bp->inPtr = byte;
    173         bp->inPtr++;
    174         bp->left--;
    175         *bp->inPtr = 0;
     173        *bp->inPtr = byte;
     174        bp->inPtr++;
     175        bp->left--;
     176        *bp->inPtr = 0;
    176177    } else {
    177         /*
    178         * Yech. have to expand the buffer to stuff this thing in.
    179         * We use a different expansion constant because people don't
    180         * usually push back many bytes when they're doing it a byte at
    181         * a time...
    182         */
    183         int       numBytes = bp->inPtr - bp->outPtr;
    184         Byte      *newBuf;
    185 
    186         newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
    187         memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
    188         bp->outPtr = newBuf + BUF_UNGET_INC;
    189         bp->inPtr = bp->outPtr + numBytes;
    190         efree ((char *)bp->buffer);
    191         bp->buffer = newBuf;
    192         bp->size += BUF_UNGET_INC;
    193         bp->left = bp->size - (bp->inPtr - bp->buffer);
    194         bp->outPtr -= 1;
    195         *bp->outPtr = byte;
     178        /*
     179        * Yech. have to expand the buffer to stuff this thing in.
     180        * We use a different expansion constant because people don't
     181        * usually push back many bytes when they're doing it a byte at
     182        * a time...
     183        */
     184        int       numBytes = bp->inPtr - bp->outPtr;
     185        Byte      *newBuf;
     186
     187        newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
     188        memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
     189        bp->outPtr = newBuf + BUF_UNGET_INC;
     190        bp->inPtr = bp->outPtr + numBytes;
     191        efree ((char *)bp->buffer);
     192        bp->buffer = newBuf;
     193        bp->size += BUF_UNGET_INC;
     194        bp->left = bp->size - (bp->inPtr - bp->buffer);
     195        bp->outPtr -= 1;
     196        *bp->outPtr = byte;
    196197    }
    197198}
     
    201202 *-----------------------------------------------------------------------
    202203 * Buf_UngetBytes --
    203  *      Push back a series of bytes at the beginning of the buffer.
    204  *
    205  * Results:
    206  *      None.
    207  *
    208  * Side Effects:
    209  *      outPtr is decremented and the bytes copied into the buffer.
     204 *      Push back a series of bytes at the beginning of the buffer.
     205 *
     206 * Results:
     207 *      None.
     208 *
     209 * Side Effects:
     210 *      outPtr is decremented and the bytes copied into the buffer.
    210211 *
    211212 *-----------------------------------------------------------------------
     
    214215Buf_UngetBytes (bp, numBytes, bytesPtr)
    215216    register Buffer bp;
    216     int     numBytes;
     217    int     numBytes;
    217218    Byte    *bytesPtr;
    218219{
    219220
    220221    if (bp->outPtr - bp->buffer >= numBytes) {
    221         bp->outPtr -= numBytes;
    222         memcpy (bp->outPtr, bytesPtr, numBytes);
     222        bp->outPtr -= numBytes;
     223        memcpy (bp->outPtr, bytesPtr, numBytes);
    223224    } else if (bp->outPtr == bp->inPtr) {
    224         Buf_AddBytes (bp, numBytes, bytesPtr);
     225        Buf_AddBytes (bp, numBytes, bytesPtr);
    225226    } else {
    226         int       curNumBytes = bp->inPtr - bp->outPtr;
    227         Byte      *newBuf;
    228         int       newBytes = max(numBytes,BUF_UNGET_INC);
    229 
    230         newBuf = (Byte *)emalloc (bp->size + newBytes);
    231         memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
    232         bp->outPtr = newBuf + newBytes;
    233         bp->inPtr = bp->outPtr + curNumBytes;
    234         efree ((char *)bp->buffer);
    235         bp->buffer = newBuf;
    236         bp->size += newBytes;
    237         bp->left = bp->size - (bp->inPtr - bp->buffer);
    238         bp->outPtr -= numBytes;
    239         memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
     227        int       curNumBytes = bp->inPtr - bp->outPtr;
     228        Byte      *newBuf;
     229        int       newBytes = max(numBytes,BUF_UNGET_INC);
     230
     231        newBuf = (Byte *)emalloc (bp->size + newBytes);
     232        memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
     233        bp->outPtr = newBuf + newBytes;
     234        bp->inPtr = bp->outPtr + curNumBytes;
     235        efree ((char *)bp->buffer);
     236        bp->buffer = newBuf;
     237        bp->size += newBytes;
     238        bp->left = bp->size - (bp->inPtr - bp->buffer);
     239        bp->outPtr -= numBytes;
     240        memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
    240241    }
    241242}
     
    245246 *-----------------------------------------------------------------------
    246247 * Buf_GetByte --
    247  *      Return the next byte from the buffer. Actually returns an integer.
    248  *
    249  * Results:
    250  *      Returns BUF_ERROR if there's no byte in the buffer, or the byte
    251  *      itself if there is one.
    252  *
    253  * Side Effects:
    254  *      outPtr is incremented and both outPtr and inPtr will be reset if
    255  *      the buffer is emptied.
     248 *      Return the next byte from the buffer. Actually returns an integer.
     249 *
     250 * Results:
     251 *      Returns BUF_ERROR if there's no byte in the buffer, or the byte
     252 *      itself if there is one.
     253 *
     254 * Side Effects:
     255 *      outPtr is incremented and both outPtr and inPtr will be reset if
     256 *      the buffer is emptied.
    256257 *
    257258 *-----------------------------------------------------------------------
     
    261262    register Buffer bp;
    262263{
    263     int     res;
     264    int     res;
    264265
    265266    if (bp->inPtr == bp->outPtr) {
    266         return (BUF_ERROR);
     267        return (BUF_ERROR);
    267268    } else {
    268         res = (int) *bp->outPtr;
    269         bp->outPtr += 1;
    270         if (bp->outPtr == bp->inPtr) {
    271             bp->outPtr = bp->inPtr = bp->buffer;
    272             bp->left = bp->size;
    273             *bp->inPtr = 0;
    274         }
    275         return (res);
     269        res = (int) *bp->outPtr;
     270        bp->outPtr += 1;
     271        if (bp->outPtr == bp->inPtr) {
     272            bp->outPtr = bp->inPtr = bp->buffer;
     273            bp->left = bp->size;
     274            *bp->inPtr = 0;
     275        }
     276        return (res);
    276277    }
    277278}
     
    281282 *-----------------------------------------------------------------------
    282283 * Buf_GetBytes --
    283  *      Extract a number of bytes from the buffer.
    284  *
    285  * Results:
    286  *      The number of bytes gotten.
    287  *
    288  * Side Effects:
    289  *      The passed array is overwritten.
     284 *      Extract a number of bytes from the buffer.
     285 *
     286 * Results:
     287 *      The number of bytes gotten.
     288 *
     289 * Side Effects:
     290 *      The passed array is overwritten.
    290291 *
    291292 *-----------------------------------------------------------------------
     
    294295Buf_GetBytes (bp, numBytes, bytesPtr)
    295296    register Buffer bp;
    296     int     numBytes;
     297    int     numBytes;
    297298    Byte    *bytesPtr;
    298299{
    299300
    300301    if (bp->inPtr - bp->outPtr < numBytes) {
    301         numBytes = bp->inPtr - bp->outPtr;
     302        numBytes = bp->inPtr - bp->outPtr;
    302303    }
    303304    memcpy (bytesPtr, bp->outPtr, numBytes);
     
    305306
    306307    if (bp->outPtr == bp->inPtr) {
    307         bp->outPtr = bp->inPtr = bp->buffer;
    308         bp->left = bp->size;
    309         *bp->inPtr = 0;
     308        bp->outPtr = bp->inPtr = bp->buffer;
     309        bp->left = bp->size;
     310        *bp->inPtr = 0;
    310311    }
    311312    return (numBytes);
     
    316317 *-----------------------------------------------------------------------
    317318 * Buf_GetAll --
    318  *      Get all the available data at once.
    319  *
    320  * Results:
    321  *      A pointer to the data and the number of bytes available.
    322  *
    323  * Side Effects:
    324  *      None.
     319 *      Get all the available data at once.
     320 *
     321 * Results:
     322 *      A pointer to the data and the number of bytes available.
     323 *
     324 * Side Effects:
     325 *      None.
    325326 *
    326327 *-----------------------------------------------------------------------
     
    329330Buf_GetAll (bp, numBytesPtr)
    330331    register Buffer bp;
    331     int     *numBytesPtr;
     332    int     *numBytesPtr;
    332333{
    333334
    334335    if (numBytesPtr != (int *)NULL) {
    335         *numBytesPtr = bp->inPtr - bp->outPtr;
     336        *numBytesPtr = bp->inPtr - bp->outPtr;
    336337    }
    337338
     
    343344 *-----------------------------------------------------------------------
    344345 * Buf_Discard --
    345  *      Throw away bytes in a buffer.
    346  *
    347  * Results:
    348  *      None.
    349  *
    350  * Side Effects:
    351  *      The bytes are discarded.
     346 *      Throw away bytes in a buffer.
     347 *
     348 * Results:
     349 *      None.
     350 *
     351 * Side Effects:
     352 *      The bytes are discarded.
    352353 *
    353354 *-----------------------------------------------------------------------
     
    356357Buf_Discard (bp, numBytes)
    357358    register Buffer bp;
    358     int     numBytes;
     359    int     numBytes;
    359360{
    360361
    361362    if (bp->inPtr - bp->outPtr <= numBytes) {
    362         bp->inPtr = bp->outPtr = bp->buffer;
    363         bp->left = bp->size;
    364         *bp->inPtr = 0;
     363        bp->inPtr = bp->outPtr = bp->buffer;
     364        bp->left = bp->size;
     365        *bp->inPtr = 0;
    365366    } else {
    366         bp->outPtr += numBytes;
     367        bp->outPtr += numBytes;
    367368    }
    368369}
     
    372373 *-----------------------------------------------------------------------
    373374 * Buf_Size --
    374  *      Returns the number of bytes in the given buffer. Doesn't include
    375  *      the null-terminating byte.
    376  *
    377  * Results:
    378  *      The number of bytes.
    379  *
    380  * Side Effects:
    381  *      None.
     375 *      Returns the number of bytes in the given buffer. Doesn't include
     376 *      the null-terminating byte.
     377 *
     378 * Results:
     379 *      The number of bytes.
     380 *
     381 * Side Effects:
     382 *      None.
    382383 *
    383384 *-----------------------------------------------------------------------
     
    394395 *-----------------------------------------------------------------------
    395396 * Buf_Init --
    396  *      Initialize a buffer. If no initial size is given, a reasonable
    397  *      default is used.
    398  *
    399  * Results:
    400  *      A buffer to be given to other functions in this library.
    401  *
    402  * Side Effects:
    403  *      The buffer is created, the space allocated and pointers
    404  *      initialized.
     397 *      Initialize a buffer. If no initial size is given, a reasonable
     398 *      default is used.
     399 *
     400 * Results:
     401 *      A buffer to be given to other functions in this library.
     402 *
     403 * Side Effects:
     404 *      The buffer is created, the space allocated and pointers
     405 *      initialized.
    405406 *
    406407 *-----------------------------------------------------------------------
     
    408409Buffer
    409410Buf_Init (size)
    410     int     size;       /* Initial size for the buffer */
    411 {
    412     Buffer bp;          /* New Buffer */
     411    int     size;       /* Initial size for the buffer */
     412{
     413    Buffer bp;          /* New Buffer */
    413414
    414415    bp = (Buffer)emalloc(sizeof(*bp));
    415416
    416417    if (size <= 0) {
    417         size = BUF_DEF_SIZE;
     418        size = BUF_DEF_SIZE;
    418419    }
    419420    bp->left = bp->size = size;
     
    429430 *-----------------------------------------------------------------------
    430431 * Buf_Destroy --
    431  *      Nuke a buffer and all its resources.
    432  *
    433  * Results:
    434  *      None.
    435  *
    436  * Side Effects:
    437  *      The buffer is freed.
     432 *      Nuke a buffer and all its resources.
     433 *
     434 * Results:
     435 *      None.
     436 *
     437 * Side Effects:
     438 *      The buffer is freed.
    438439 *
    439440 *-----------------------------------------------------------------------
     
    441442void
    442443Buf_Destroy (buf, freeData)
    443     Buffer  buf;        /* Buffer to destroy */
    444     Boolean freeData;   /* TRUE if the data should be destroyed as well */
     444    Buffer  buf;        /* Buffer to destroy */
     445    Boolean freeData;   /* TRUE if the data should be destroyed as well */
    445446{
    446447
    447448    if (freeData) {
    448         efree ((char *)buf->buffer);
     449        efree ((char *)buf->buffer);
    449450    }
    450451    efree ((char *)buf);
     
    468469void
    469470Buf_ReplaceLastByte (buf, byte)
    470     Buffer buf; /* buffer to augment */
    471     int byte;   /* byte to be written */
     471    Buffer buf; /* buffer to augment */
     472    int byte;   /* byte to be written */
    472473{
    473474    if (buf->inPtr == buf->outPtr)
Note: See TracChangeset for help on using the changeset viewer.