BufFileInput.cpp
资源名称:pccts133.zip [点击查看]
上传用户:itx_2006
上传日期:2007-01-06
资源大小:493k
文件大小:2k
源码类别:
编译器/解释器
开发平台:
Others
- // FILE: BufFileInput.cpp
- // AUTHOR: Alexey Demakov (AVD) demakov@kazbek.ispras.ru
- // CREATION: 26-JAN-1998
- // DESCRIPTION: File Input Stream with lookahead for Scanner.
- // See file BufFileInput.h for details
- // Change History:
- //
- // 22-Jun-1998 assert.h -> PCCTS_ASSERT_H
- // string.h -> PCCTS_STRING_H
- //
- // 28-May-1998 Add virtual destructor to release buffer.
- //
- // Add dummy definition for ANTLRTokenType
- // to allow compilation without knowing
- // token type codes.
- //
- // Manfred Kogler (km@cast.uni-linz.ac.at)
- // (1.33MR14)
- enum ANTLRTokenType {TER_HATES_CPP=0, SO_DO_OTHERS=9999 };
- #include "pcctscfg.h"
- #include PCCTS_ASSERT_H
- #include PCCTS_STRING_H
- PCCTS_NAMESPACE_STD
- #include "BufFileInput.h"
- BufFileInput::BufFileInput( FILE *f, int buf_size )
- : input( f ),
- start( 0 ),
- len( 0 ),
- buf( new int[buf_size] ),
- size( buf_size )
- {
- }
- BufFileInput::~BufFileInput()
- {
- delete [] buf;
- }
- int BufFileInput::nextChar( void )
- {
- if( len > 0 )
- {
- // get char from buffer
- int c = buf[start];
- if( c != EOF )
- {
- start++; start %= size;
- len--;
- }
- return c;
- } else {
- // get char from file
- int c = getc( input );
- if( c == EOF )
- {
- // if EOF - put it in the buffer as indicator
- buf[start] = EOF;
- len++;
- }
- return c;
- }
- }
- int BufFileInput::lookahead( char* s )
- {
- int l = strlen( s );
- assert( 0 < l && l <= size );
- while( len < l )
- {
- int c = getc( input );
- buf[ (start+len) % size ] = c;
- len++;
- if( c == EOF ) return 0;
- }
- for( int i = 0; i < l; i++ )
- {
- if( s[i] != buf[ (start+i) % size ] ) return 0;
- }
- return 1;
- }
- // End of file BufFileInput.cpp