version 1.8, 1999/12/06 22:24:31 |
version 1.9, 1999/12/16 16:27:12 |
|
|
* Makes sure there's room for an extra NULL char 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) { \ |
do { \ |
char *newBuf; \ |
char *newBuf; \ |
size_t 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 = 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; \ |
(bp)->size = newSize; \ |
(bp)->size = newSize; \ |
} |
} while (0) |
|
|
#define BUF_DEF_SIZE 256 /* Default buffer size */ |
#define BUF_DEF_SIZE 256 /* Default buffer size */ |
#define BUF_MARGIN 256 /* Make sure we are comfortable */ |
#define BUF_MARGIN 256 /* Make sure we are comfortable */ |
|
|
/*- |
/* Buf_AddChar hard case: buffer must be expanded to accommodate |
*----------------------------------------------------------------------- |
* one more char. */ |
* Buf_OvAddChar -- |
|
* Add a single char to the buffer. |
|
* |
|
* Side Effects: |
|
* The buffer may be expanded. |
|
* |
|
*----------------------------------------------------------------------- |
|
*/ |
|
void |
void |
Buf_OvAddChar(bp, byte) |
BufOverflow(bp) |
Buffer bp; |
Buffer bp; |
char byte; |
|
{ |
{ |
BufExpand(bp, 1); |
BufExpand(bp, 1); |
|
|
*bp->inPtr++ = byte; |
|
bp->left--; |
|
|
|
/* |
|
* Null-terminate |
|
*/ |
|
*bp->inPtr = 0; |
|
} |
} |
|
|
/*- |
/*- |
|
|
const char *bytesPtr; |
const char *bytesPtr; |
{ |
{ |
|
|
BufExpand(bp, numBytes); |
if (bp->left < numBytes+1) |
|
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; |
|
|
/* |
|
* Null-terminate |
|
*/ |
|
*bp->inPtr = 0; |
|
} |
} |
|
|
/*- |
/*- |
|
|
*numBytesPtr = bp->inPtr - bp->outPtr; |
*numBytesPtr = bp->inPtr - bp->outPtr; |
} |
} |
|
|
|
*bp->inPtr = 0; |
return (bp->outPtr); |
return (bp->outPtr); |
} |
} |
|
|
/*- |
/*- |
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
* Buf_Discard -- |
* Buf_Reset - |
* Throw away chars in a buffer. |
* Throw away all chars in a buffer. |
* |
* |
* Side Effects: |
* Side Effects: |
* The chars are discarded. |
* The chars are discarded. |
|
|
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
void |
void |
Buf_Discard(bp, numBytes) |
Buf_Reset(bp) |
Buffer bp; |
Buffer bp; |
size_t numBytes; |
|
{ |
{ |
|
|
if (bp->inPtr - bp->outPtr <= numBytes) { |
bp->inPtr = bp->outPtr = bp->buffer; |
bp->inPtr = bp->outPtr = bp->buffer; |
bp->left = bp->size; |
bp->left = bp->size; |
|
*bp->inPtr = 0; |
|
} else { |
|
bp->outPtr += numBytes; |
|
} |
|
} |
} |
|
|
/*- |
/*- |
|
|
*----------------------------------------------------------------------- |
*----------------------------------------------------------------------- |
*/ |
*/ |
int |
int |
Buf_Size (buf) |
Buf_Size(buf) |
Buffer buf; |
Buffer buf; |
{ |
{ |
return (buf->inPtr - buf->outPtr); |
return (buf->inPtr - buf->outPtr); |
|
|
bp->left = bp->size = size; |
bp->left = bp->size = size; |
bp->buffer = emalloc(size); |
bp->buffer = emalloc(size); |
bp->inPtr = bp->outPtr = bp->buffer; |
bp->inPtr = bp->outPtr = bp->buffer; |
*bp->inPtr = 0; |
|
|
|
return (bp); |
return (bp); |
} |
} |