diff options
author | Adrian Thurston <thurston@complang.org> | 2011-12-30 22:21:57 +0000 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2011-12-30 22:21:57 +0000 |
commit | 981862c0b828c64f1445792ebc2acf243c26390c (patch) | |
tree | 7a1eb400b18435137ba5b1ccafc2813694213ffe | |
parent | fbe3a86373bf7e6f10fbeeaef7222a6fc3163ace (diff) | |
download | colm-981862c0b828c64f1445792ebc2acf243c26390c.tar.gz |
Bit more input interface build up. refs #341.
-rw-r--r-- | colm/input.c | 98 | ||||
-rw-r--r-- | colm/pdarun.c | 5 |
2 files changed, 61 insertions, 42 deletions
diff --git a/colm/input.c b/colm/input.c index 54924aa4..fbf35f06 100644 --- a/colm/input.c +++ b/colm/input.c @@ -228,12 +228,38 @@ int inputStreamDynamicConsumeData( SourceStream *is, int length ) { debug( REALM_INPUT, "source consuming %ld bytes\n", length ); - while ( is->queue->offset == is->queue->length ) { - RunBuf *runBuf = inputStreamPopHead( is ); - free( runBuf ); + int consumed = 0; + + /* Move over skip bytes. */ + while ( true ) { + RunBuf *buf = is->queue; + + if ( buf == 0 ) + break; + + if ( buf->type == RunBufTokenType ) + break; + else if ( buf->type == RunBufIgnoreType ) + break; + else { + /* Anything available in the current buffer. */ + int avail = buf->length - buf->offset; + if ( avail > 0 ) { + /* The source data from the current buffer. */ + int slen = avail <= length ? avail : length; + consumed += slen; + length -= slen; + buf->offset += slen; + } + } + + if ( length == 0 ) + break; + + free( inputStreamPopHead( is ) ); } - is->queue->offset += length; + return consumed; return length; } @@ -476,26 +502,6 @@ static RunBuf *inputStreamPopTail2( InputStream *is ) return ret; } -static int inputStreamDynamicGetDataRev2( InputStream *is, char *dest, int length ) -{ - /* If there is any data in the rubuf queue then read that first. */ - if ( is->queueTail != 0 ) { - long avail = is->queueTail->length - is->queueTail->offset; - if ( length >= avail ) { - memcpy( dest, &is->queueTail->data[is->queue->offset], avail ); - RunBuf *del = inputStreamPopTail2(is); - free(del); - return avail; - } - else { - memcpy( dest, &is->queueTail->data[is->queueTail->offset], length ); - is->queueTail->length -= length; - return length; - } - } - return 0; -} - static int isSourceStream( InputStream *is ) { if ( is->queue != 0 && is->queue->type == RunBufSourceType ) @@ -804,22 +810,38 @@ void appendData( InputStream *is, const char *data, long len ) Tree *undoAppendData( InputStream *is, int length ) { - if ( is->queueTail->type == RunBufDataType ) { - char tmp[length]; - int have = 0; - while ( have < length ) { - int res = inputStreamDynamicGetDataRev2( is, tmp, length-have ); - have += res; + int consumed = 0; + + /* Move over skip bytes. */ + while ( true ) { + RunBuf *buf = is->queueTail; + + if ( buf == 0 ) + break; + + if ( buf->type == RunBufTokenType ) + break; + else if ( buf->type == RunBufIgnoreType ) + break; + else { + /* Anything available in the current buffer. */ + int avail = buf->length - buf->offset; + if ( avail > 0 ) { + /* The source data from the current buffer. */ + int slen = avail <= length ? avail : length; + consumed += slen; + length -= slen; + buf->length -= slen; + } } - return 0; - } - else { - /* FIXME: leak here. */ - RunBuf *rb = inputStreamPopTail2( is ); - Tree *tree = rb->tree; - free(rb); - return tree; + + if ( length == 0 ) + break; + + free( inputStreamPopTail2( is ) ); } + + return 0; } void appendTree( InputStream *is, Tree *tree ) diff --git a/colm/pdarun.c b/colm/pdarun.c index 0de4a7e9..b96217ba 100644 --- a/colm/pdarun.c +++ b/colm/pdarun.c @@ -230,10 +230,7 @@ void undoStreamAppend( Program *prg, Tree **sp, FsmRun *fsmRun, InputStream *inp undoAppendStream( inputStream ); } else { - // sendBackBuffered( fsmRun, inputStream ); - Tree *tree = undoAppendData( inputStream, length ); - if ( tree != 0 ) - treeDownref( prg, sp, tree ); + undoAppendData( inputStream, length ); } } |