Blame pccts/h/BufFileInput.cpp

Packit Service 3e823c
// FILE:        BufFileInput.cpp
Packit Service 3e823c
// AUTHOR:      Alexey Demakov (AVD) demakov@kazbek.ispras.ru
Packit Service 3e823c
// CREATION:    26-JAN-1998
Packit Service 3e823c
// DESCRIPTION: File Input Stream with lookahead for Scanner.
Packit Service 3e823c
//   See file BufFileInput.h for details
Packit Service 3e823c
Packit Service 3e823c
// Change History:
Packit Service 3e823c
//
Packit Service 3e823c
//   22-Jun-1998    assert.h -> PCCTS_ASSERT_H
Packit Service 3e823c
//                  string.h -> PCCTS_STRING_H
Packit Service 3e823c
//
Packit Service 3e823c
//   28-May-1998    Add virtual destructor to release buffer.
Packit Service 3e823c
//
Packit Service 3e823c
//                  Add dummy definition for ANTLRTokenType
Packit Service 3e823c
//                  to allow compilation without knowing
Packit Service 3e823c
//                  token type codes.
Packit Service 3e823c
//
Packit Service 3e823c
//                  Manfred Kogler (km@cast.uni-linz.ac.at)
Packit Service 3e823c
//                  (1.33MR14)
Packit Service 3e823c
//
Packit Service 3e823c
//   20-Jul-1998    MR14a - Reorder initialization list for ctor.
Packit Service 3e823c
//
Packit Service 3e823c
Packit Service 3e823c
enum ANTLRTokenType {TER_HATES_CPP=0, SO_DO_OTHERS=9999 };
Packit Service 3e823c
Packit Service 3e823c
#include "pcctscfg.h"
Packit Service 3e823c
#include "pccts_assert.h"
Packit Service 3e823c
#include "pccts_string.h"
Packit Service 3e823c
Packit Service 3e823c
PCCTS_NAMESPACE_STD
Packit Service 3e823c
Packit Service 3e823c
#include "BufFileInput.h"
Packit Service 3e823c
Packit Service 3e823c
BufFileInput::BufFileInput( FILE *f, int buf_size )
Packit Service 3e823c
: input( f ),
Packit Service 3e823c
  buf( new int[buf_size] ),
Packit Service 3e823c
  size( buf_size ),
Packit Service 3e823c
  start( 0 ),
Packit Service 3e823c
  len( 0 )
Packit Service 3e823c
{
Packit Service 3e823c
}
Packit Service 3e823c
Packit Service 3e823c
BufFileInput::~BufFileInput()
Packit Service 3e823c
{
Packit Service 3e823c
  delete [] buf;
Packit Service 3e823c
}
Packit Service 3e823c
Packit Service 3e823c
int BufFileInput::nextChar( void )
Packit Service 3e823c
{
Packit Service 3e823c
    if( len > 0 )
Packit Service 3e823c
    {
Packit Service 3e823c
        // get char from buffer
Packit Service 3e823c
        int c = buf[start];
Packit Service 3e823c
Packit Service 3e823c
        if( c != EOF )
Packit Service 3e823c
        {
Packit Service 3e823c
            start++; start %= size;
Packit Service 3e823c
            len--;
Packit Service 3e823c
        }
Packit Service 3e823c
        return c;
Packit Service 3e823c
    } else {
Packit Service 3e823c
        // get char from file
Packit Service 3e823c
        int c = getc( input );
Packit Service 3e823c
Packit Service 3e823c
        if( c == EOF )
Packit Service 3e823c
        {
Packit Service 3e823c
            // if EOF - put it in the buffer as indicator
Packit Service 3e823c
            buf[start] = EOF;
Packit Service 3e823c
            len++;
Packit Service 3e823c
        }
Packit Service 3e823c
        return c;
Packit Service 3e823c
    }
Packit Service 3e823c
}
Packit Service 3e823c
Packit Service 3e823c
int BufFileInput::lookahead( char* s )
Packit Service 3e823c
{
Packit Service 3e823c
    int l = strlen( s );
Packit Service 3e823c
Packit Service 3e823c
    assert( 0 < l && l <= size );
Packit Service 3e823c
Packit Service 3e823c
    while( len < l )
Packit Service 3e823c
    {
Packit Service 3e823c
        int c = getc( input );
Packit Service 3e823c
Packit Service 3e823c
        buf[ (start+len) % size ] = c;
Packit Service 3e823c
Packit Service 3e823c
        len++;
Packit Service 3e823c
Packit Service 3e823c
        if( c == EOF ) return 0;
Packit Service 3e823c
    }
Packit Service 3e823c
Packit Service 3e823c
    for( int i = 0; i < l; i++ )
Packit Service 3e823c
    {
Packit Service 3e823c
        if( s[i] != buf[ (start+i) % size ] ) return 0;
Packit Service 3e823c
    }
Packit Service 3e823c
    return 1;
Packit Service 3e823c
}
Packit Service 3e823c
Packit Service 3e823c
// End of file BufFileInput.cpp
Packit Service 3e823c