1
/* buffio.c -- Treat buffer as an I/O stream.
3
(c) 1998-2005 (W3C) MIT, ERCIM, Keio University
4
See tidy.h for the copyright notice.
9
$Date: 2005/04/08 09:11:13 $
12
Requires buffer to automatically grow as bytes are added.
13
Must keep track of current read and write points.
25
static int TIDY_CALL insrc_getByte( ulong appData )
27
TidyBuffer* buf = (TidyBuffer*) appData;
28
return tidyBufGetByte( buf );
30
static Bool TIDY_CALL insrc_eof( ulong appData )
32
TidyBuffer* buf = (TidyBuffer*) appData;
33
return tidyBufEndOfInput( buf );
35
static void TIDY_CALL insrc_ungetByte( ulong appData, byte bv )
37
TidyBuffer* buf = (TidyBuffer*) appData;
38
tidyBufUngetByte( buf, bv );
41
void TIDY_CALL initInputBuffer( TidyInputSource* inp, TidyBuffer* buf )
43
inp->getByte = insrc_getByte;
45
inp->ungetByte = insrc_ungetByte;
46
inp->sourceData = (ulong) buf;
49
static void TIDY_CALL outsink_putByte( ulong appData, byte bv )
51
TidyBuffer* buf = (TidyBuffer*) appData;
52
tidyBufPutByte( buf, bv );
55
void TIDY_CALL initOutputBuffer( TidyOutputSink* outp, TidyBuffer* buf )
57
outp->putByte = outsink_putByte;
58
outp->sinkData = (ulong) buf;
62
void TIDY_CALL tidyBufInit( TidyBuffer* buf )
64
assert( buf != NULL );
65
ClearMemory( buf, sizeof(TidyBuffer) );
68
void TIDY_CALL tidyBufAlloc( TidyBuffer* buf, uint allocSize )
71
tidyBufCheckAlloc( buf, allocSize, 0 );
74
void TIDY_CALL tidyBufFree( TidyBuffer* buf )
76
assert( buf != NULL );
81
void TIDY_CALL tidyBufClear( TidyBuffer* buf )
83
assert( buf != NULL );
86
ClearMemory( buf->bp, buf->allocated );
92
/* Avoid thrashing memory by doubling buffer size
93
** until larger than requested size.
95
void TIDY_CALL tidyBufCheckAlloc( TidyBuffer* buf, uint allocSize, uint chunkSize )
97
assert( buf != NULL );
100
if ( allocSize > buf->allocated )
103
uint allocAmt = chunkSize;
104
if ( buf->allocated > 0 )
105
allocAmt = buf->allocated;
106
while ( allocAmt < allocSize )
109
bp = (byte*)MemRealloc( buf->bp, allocAmt );
112
ClearMemory( bp + buf->allocated, allocAmt - buf->allocated );
114
buf->allocated = allocAmt;
119
/* Attach buffer to a chunk O' memory w/out allocation */
120
void TIDY_CALL tidyBufAttach( TidyBuffer* buf, byte* bp, uint size )
122
assert( buf != NULL );
124
buf->size = buf->allocated = size;
128
/* Clear pointer to memory w/out deallocation */
129
void TIDY_CALL tidyBufDetach( TidyBuffer* buf )
139
void TIDY_CALL tidyBufAppend( TidyBuffer* buf, void* vp, uint size )
141
assert( buf != NULL );
142
if ( vp != NULL && size > 0 )
144
tidyBufCheckAlloc( buf, buf->size + size, 0 );
145
memcpy( buf->bp + buf->size, vp, size );
150
void TIDY_CALL tidyBufPutByte( TidyBuffer* buf, byte bv )
152
assert( buf != NULL );
153
tidyBufCheckAlloc( buf, buf->size + 1, 0 );
154
buf->bp[ buf->size++ ] = bv;
158
int TIDY_CALL tidyBufPopByte( TidyBuffer* buf )
161
assert( buf != NULL );
163
bv = buf->bp[ --buf->size ];
171
int TIDY_CALL tidyBufGetByte( TidyBuffer* buf )
174
if ( ! tidyBufEndOfInput(buf) )
175
bv = buf->bp[ buf->next++ ];
179
Bool TIDY_CALL tidyBufEndOfInput( TidyBuffer* buf )
181
return ( buf->next >= buf->size );
184
void TIDY_CALL tidyBufUngetByte( TidyBuffer* buf, byte bv )
189
assert( bv == buf->bp[ buf->next ] );