version 1.7, 1999/10/05 21:59:00 |
version 1.8, 1999/12/06 22:24:31 |
|
|
/* $NetBSD: buf.c,v 1.9 1996/12/31 17:53:21 christos Exp $ */ |
/* $NetBSD: buf.c,v 1.9 1996/12/31 17:53:21 christos Exp $ */ |
|
|
/* |
/* |
|
* Copyright (c) 1999 Marc Espie. |
|
* |
|
* Extensive code modifications for the OpenBSD project. |
|
* |
|
* Redistribution and use in source and binary forms, with or without |
|
* modification, are permitted provided that the following conditions |
|
* are met: |
|
* 1. Redistributions of source code must retain the above copyright |
|
* notice, this list of conditions and the following disclaimer. |
|
* 2. Redistributions in binary form must reproduce the above copyright |
|
* notice, this list of conditions and the following disclaimer in the |
|
* documentation and/or other materials provided with the distribution. |
|
* |
|
* THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS |
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD |
|
* PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
*/ |
|
|
|
/* |
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California. |
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California. |
* Copyright (c) 1988, 1989 by Adam de Boor |
* Copyright (c) 1988, 1989 by Adam de Boor |
* Copyright (c) 1989 by Berkeley Softworks |
* Copyright (c) 1989 by Berkeley Softworks |
|
|
/* |
/* |
* BufExpand -- |
* BufExpand -- |
* Expand the given buffer to hold the given number of additional |
* Expand the given buffer to hold the given number of additional |
* bytes. |
* chars. |
* Makes sure there's room for an extra NULL byte at the end of the |
* Makes sure there's room for an extra NULL char at the end of the |
* buffer in case it holds a string. |
* buffer in case it holds a string. |
*/ |
*/ |
#define BufExpand(bp,nb) \ |
#define BufExpand(bp,nb) \ |
if (bp->left < (nb)+1) { \ |
if (bp->left < (nb)+1) { \ |
Byte *newBuf; \ |
char *newBuf; \ |
int newSize = (bp)->size; \ |
size_t newSize = (bp)->size; \ |
\ |
\ |
do { \ |
do { \ |
newSize *= 2 ; \ |
newSize *= 2 ; \ |
(bp)->left = newSize - ((bp)->inPtr - (bp)->buffer); \ |
(bp)->left = newSize - ((bp)->inPtr - (bp)->buffer); \ |
} while ((bp)->left < (nb)+1+BUF_MARGIN); \ |
} while ((bp)->left < (nb)+1+BUF_MARGIN); \ |
newBuf = (Byte *) erealloc((bp)->buffer, newSize); \ |
newBuf = erealloc((bp)->buffer, newSize); \ |
(bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \ |
(bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \ |
(bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer); \ |
(bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer); \ |
(bp)->buffer = newBuf; \ |
(bp)->buffer = newBuf; \ |
|
|
|
|
/*- |
/*- |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
* Buf_OvAddByte -- |
* Buf_OvAddChar -- |
* Add a single byte to the buffer. left is zero or negative. |
* Add a single char to the buffer. |
* |
* |
* Results: |
|
* None. |
|
* |
|
* Side Effects: |
* Side Effects: |
* The buffer may be expanded. |
* The buffer may be expanded. |
* |
* |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
void |
void |
Buf_OvAddByte (bp, byte) |
Buf_OvAddChar(bp, byte) |
register Buffer bp; |
Buffer bp; |
int byte; |
char byte; |
{ |
{ |
int nbytes = 1; |
BufExpand(bp, 1); |
bp->left = 0; |
|
BufExpand (bp, nbytes); |
|
|
|
*bp->inPtr++ = byte; |
*bp->inPtr++ = byte; |
bp->left--; |
bp->left--; |
|
|
|
|
/*- |
/*- |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
* Buf_AddBytes -- |
* Buf_AddChars -- |
* Add a number of bytes to the buffer. |
* Add a number of chars to the buffer. |
* |
* |
* Results: |
|
* None. |
|
* |
|
* Side Effects: |
* Side Effects: |
* Guess what? |
* Guess what? |
* |
* |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
void |
void |
Buf_AddBytes (bp, numBytes, bytesPtr) |
Buf_AddChars(bp, numBytes, bytesPtr) |
register Buffer bp; |
Buffer bp; |
int numBytes; |
size_t numBytes; |
const Byte *bytesPtr; |
const char *bytesPtr; |
{ |
{ |
|
|
BufExpand (bp, numBytes); |
BufExpand(bp, numBytes); |
|
|
memcpy (bp->inPtr, bytesPtr, numBytes); |
memcpy(bp->inPtr, bytesPtr, numBytes); |
bp->inPtr += numBytes; |
bp->inPtr += numBytes; |
bp->left -= numBytes; |
bp->left -= numBytes; |
|
|
|
|
* Get all the available data at once. |
* Get all the available data at once. |
* |
* |
* Results: |
* Results: |
* A pointer to the data and the number of bytes available. |
* A pointer to the data and the number of chars available. |
* |
* |
* Side Effects: |
|
* None. |
|
* |
|
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
Byte * |
char * |
Buf_GetAll (bp, numBytesPtr) |
Buf_GetAll(bp, numBytesPtr) |
register Buffer bp; |
Buffer bp; |
int *numBytesPtr; |
size_t *numBytesPtr; |
{ |
{ |
|
|
if (numBytesPtr != (int *)NULL) { |
if (numBytesPtr != NULL) { |
*numBytesPtr = bp->inPtr - bp->outPtr; |
*numBytesPtr = bp->inPtr - bp->outPtr; |
} |
} |
|
|
|
|
/*- |
/*- |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
* Buf_Discard -- |
* Buf_Discard -- |
* Throw away bytes in a buffer. |
* Throw away chars in a buffer. |
* |
* |
* Results: |
|
* None. |
|
* |
|
* Side Effects: |
* Side Effects: |
* The bytes are discarded. |
* The chars are discarded. |
* |
* |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
void |
void |
Buf_Discard (bp, numBytes) |
Buf_Discard(bp, numBytes) |
register Buffer bp; |
Buffer bp; |
int numBytes; |
size_t numBytes; |
{ |
{ |
|
|
if (bp->inPtr - bp->outPtr <= numBytes) { |
if (bp->inPtr - bp->outPtr <= numBytes) { |
|
|
/*- |
/*- |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
* Buf_Size -- |
* Buf_Size -- |
* Returns the number of bytes in the given buffer. Doesn't include |
* Returns the number of chars in the given buffer. Doesn't include |
* the null-terminating byte. |
* the null-terminating char. |
* |
* |
* Results: |
* Results: |
* The number of bytes. |
* The number of chars. |
* |
* |
* Side Effects: |
|
* None. |
|
* |
|
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
int |
int |
|
|
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
Buffer |
Buffer |
Buf_Init (size) |
Buf_Init(size) |
int size; /* Initial size for the buffer */ |
size_t size; /* Initial size for the buffer */ |
{ |
{ |
Buffer bp; /* New Buffer */ |
Buffer bp; /* New Buffer */ |
|
|
bp = (Buffer)emalloc(sizeof(*bp)); |
bp = (Buffer)emalloc(sizeof(*bp)); |
|
|
if (size <= 0) { |
if (size == 0) { |
size = BUF_DEF_SIZE; |
size = BUF_DEF_SIZE; |
} |
} |
bp->left = bp->size = size; |
bp->left = bp->size = size; |
bp->buffer = (Byte *)emalloc(size); |
bp->buffer = emalloc(size); |
bp->inPtr = bp->outPtr = bp->buffer; |
bp->inPtr = bp->outPtr = bp->buffer; |
*bp->inPtr = 0; |
*bp->inPtr = 0; |
|
|
|
|
* Buf_Destroy -- |
* Buf_Destroy -- |
* Nuke a buffer and all its resources. |
* Nuke a buffer and all its resources. |
* |
* |
* Results: |
|
* None. |
|
* |
|
* Side Effects: |
* Side Effects: |
* The buffer is freed. |
* The buffer is freed. |
* |
* |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
void |
void |
Buf_Destroy (buf, freeData) |
Buf_Destroy(buf, freeData) |
Buffer buf; /* Buffer to destroy */ |
Buffer buf; /* Buffer to destroy */ |
Boolean freeData; /* TRUE if the data should be destroyed as well */ |
Boolean freeData; /* TRUE if the data should be destroyed as well */ |
{ |
{ |
|
|
if (freeData) { |
if (freeData) { |
free ((char *)buf->buffer); |
free(buf->buffer); |
} |
} |
free ((char *)buf); |
free ((char *)buf); |
} |
} |
|
|
/*- |
/*- |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
* Buf_ReplaceLastByte -- |
* Buf_ReplaceLastChar -- |
* Replace the last byte in a buffer. |
* Replace the last char in a buffer. |
* |
* |
* Results: |
|
* None. |
|
* |
|
* Side Effects: |
* Side Effects: |
* If the buffer was empty intially, then a new byte will be added. |
* If the buffer was empty intially, then a new byte will be added. |
* Otherwise, the last byte is overwritten. |
* Otherwise, the last byte is overwritten. |
|
|
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
void |
void |
Buf_ReplaceLastByte (buf, byte) |
Buf_ReplaceLastChar(buf, byte) |
Buffer buf; /* buffer to augment */ |
Buffer buf; /* buffer to augment */ |
int byte; /* byte to be written */ |
char byte; /* byte to be written */ |
{ |
{ |
if (buf->inPtr == buf->outPtr) |
if (buf->inPtr == buf->outPtr) |
Buf_AddByte(buf, byte); |
Buf_AddChar(buf, byte); |
else |
else |
*(buf->inPtr - 1) = byte; |
*(buf->inPtr - 1) = byte; |
} |
} |