1 // FILE: BufFileInput.cpp 2 // AUTHOR: Alexey Demakov (AVD) demakov@kazbek.ispras.ru 3 // CREATION: 26-JAN-1998 4 // DESCRIPTION: File Input Stream with lookahead for Scanner. 5 // See file BufFileInput.h for details 6 7 // Change History: 8 // 9 // 22-Jun-1998 assert.h -> PCCTS_ASSERT_H 10 // string.h -> PCCTS_STRING_H 11 // 12 // 28-May-1998 Add virtual destructor to release buffer. 13 // 14 // Add dummy definition for ANTLRTokenType 15 // to allow compilation without knowing 16 // token type codes. 17 // 18 // Manfred Kogler (km@cast.uni-linz.ac.at) 19 // (1.33MR14) 20 // 21 // 20-Jul-1998 MR14a - Reorder initialization list for ctor. 22 // 23 24 enum ANTLRTokenType {TER_HATES_CPP=0, SO_DO_OTHERS=9999 }; 25 26 #include "pcctscfg.h" 27 #include "pccts_assert.h" 28 #include "pccts_string.h" 29 30 PCCTS_NAMESPACE_STD 31 32 #include "BufFileInput.h" 33 BufFileInput(FILE * f,int buf_size)34BufFileInput::BufFileInput( FILE *f, int buf_size ) 35 : input( f ), 36 buf( new int[buf_size] ), 37 size( buf_size ), 38 start( 0 ), 39 len( 0 ) 40 { 41 } 42 ~BufFileInput()43BufFileInput::~BufFileInput() 44 { 45 delete [] buf; 46 } 47 nextChar(void)48int BufFileInput::nextChar( void ) 49 { 50 if( len > 0 ) 51 { 52 // get char from buffer 53 int c = buf[start]; 54 55 if( c != EOF ) 56 { 57 start++; start %= size; 58 len--; 59 } 60 return c; 61 } else { 62 // get char from file 63 int c = getc( input ); 64 65 if( c == EOF ) 66 { 67 // if EOF - put it in the buffer as indicator 68 buf[start] = EOF; 69 len++; 70 } 71 return c; 72 } 73 } 74 lookahead(char * s)75int BufFileInput::lookahead( char* s ) 76 { 77 int l = strlen( s ); 78 79 assert( 0 < l && l <= size ); 80 81 while( len < l ) 82 { 83 int c = getc( input ); 84 85 buf[ (start+len) % size ] = c; 86 87 len++; 88 89 if( c == EOF ) return 0; 90 } 91 92 for( int i = 0; i < l; i++ ) 93 { 94 if( s[i] != buf[ (start+i) % size ] ) return 0; 95 } 96 return 1; 97 } 98 99 // End of file BufFileInput.cpp 100 101