Google

TOP --> libjdl

class CJdlBufferedFileReader

Defines a buffered file reader.

Objects of this form read data from files using fopen(), fread() and fclose(). It is efficient because it reads data in large chunks and then manipulates the internal memory buffer.

One nice feature is that you can put up to 256 characters back into the buffer to handle lookahead.

A simple usage is shown below:

    CJdlBufferedFileReader rd("my.cpp");
    if(rd.Open()) {
      int ch;
      while((ch = rd.GetChar())) {
        if('*' == ch) {
          rd.PutChar('$'); // The next GetChar() will return '$'.
        }
      }
      rd.Close();
    }
 

Author:
Joe Linoff

Version:
$Id: jdlbuffilerd.h,v 1.4 1999/06/12 18:26:00 jdl Exp $

Source:
../../libjdl/src/jdlbuffilerd.h:55

See Also:
CJdlBufferedFileWriter

Constructors Index

CJdlBufferedFileReader
[public] Default Constructor. This allows the class to be used in a has-a relationsip.
CJdlBufferedFileReader
[public] Constructor.
CJdlBufferedFileReader
[public] Constructor.
~CJdlBufferedFileReader
[public] Destructor.


Methods Index

Close
[public] Close the file.
Debug
[public] Turn debugging on or off.
Eof
[public] End of file?
GetChar
[public] Returns the next character in the file.
GetCharOffset
[public] Returns the offset from the beginning of the line for the current character.
GetFileName
[public]
GetLineNumber
[public]
GetNumChars
[public] The total number of characters read.
Ok
[public] Ok to read?
Open
[public] Open the file.
Open
[public] Open the file.
PutChar
[public] Put a character in the lookahead buffer.


Constructors

CJdlBufferedFileReader

public CJdlBufferedFileReader ( ) ;

Default Constructor. This allows the class to be used in a has-a relationsip.

Use Open(fn) to set the file name.

CJdlBufferedFileReader

public CJdlBufferedFileReader ( const char * fn ) ;

Constructor.

If the file does not exist, Ok() will return false. The default buffer size is 64K.

Parameters:
fn Name of the file to read.

CJdlBufferedFileReader

public CJdlBufferedFileReader ( const char * fn ,
                                uint bufsize ) ;

Constructor.

If the file does not exist, Ok() will return false.

Parameters:
fn Name of the file to read.
bufsize The size of the read buffer. If the buffer size specified is less than 64, then 64 is assumed as the minimum.

CJdlBufferedFileReader

public ~ CJdlBufferedFileReader ( ) ;

Destructor.


Methods

Open

public bool Open ( ) ;

Open the file.

The member function was provided so that the same input file could be opened and closed multiple times.

Return:
The status of the open (same as Ok()).

Open

public bool Open ( const char * fn ,
                   uint bufsz = 65535) ) ;

Open the file.

The member function was provided so that the same input file could be opened and closed multiple times.

Parameters:
fn The file name. This replaces the file name that was specified in the constructor.
bufsz The internal buffer size.

Return:
The status of the open (same as Ok()).

Ok

public bool Ok ( ) const ;

Ok to read?

Return:
true if the file was opened successfully and the end of file has not been reached or false otherwise.

Eof

public bool Eof ( ) const ;

End of file?

Return:
true if the file was opened successfully and the end of file has been reached.

GetChar

public char GetChar ( ) ;

Returns the next character in the file.

If the file has been closed, GetChar() will return 0.
If Eof() is true, GetChar() will return 0.
If Ok() is false, GetChar() will return 0.

If pending characters were put using PutChar(), this routine will return those characters until the put buffer is exhausted.

Return:
The next character or 0 if the EOF is reached.

PutChar

public bool PutChar ( char ch ) ;

Put a character in the lookahead buffer.

A maximum of 256 characters are allowed. If more than 256 characters are added, they are ignored.

Parameters:
ch The character to queue.

Return:
true if the character was added or false if an overflow occurred.

Close

public void Close ( ) ;

Close the file.

After the file is closed, Ok() will return true and Eof() will return true.

GetFileName

public const char * GetFileName ( ) const ;

Return:
The file name.

GetLineNumber

public long GetLineNumber ( ) const ;

Return:
The current line number.

GetCharOffset

public uint GetCharOffset ( ) const ;

Returns the offset from the beginning of the line for the current character.

The value returned is as follows:

    "This is a sample line.\n"
     ^    ^                ^
     |    |                +--- CharOffset() == 0
     |    +-------------------- CharOffset() == 6
     +------------------------- CharOffset() == 1
 

Return:
The current character offset from the beginning of the line.

GetNumChars

public long GetNumChars ( ) const ;

The total number of characters read.

This number persists after the file is Closed().

Return:
The total number of characters parsed.

Debug

public void Debug ( bool flag = true ) ;

Turn debugging on or off.

This generates a lot of output to stderr so use it sparingly.

Parameters:
flag If true, turn on debugging.

This documentation was generated automatically by the ccdoc tool (version 0.7a).
Click here to submit a bug report or feature request.

Click here to return to the top of the page.