Class TPeekCharStream
Unit
Declaration
type TPeekCharStream = class(TStream)
Description
Abstract class to read another stream, always being able to back one character. This is a purely sequential read-only stream. This means that calling Write, Seek (changing Position) or setting Size
will always cause an exception with appropriate descendant of EStreamNotImplemented.
Getting Size
and Position
is allowed. Getting Size
is simply implemented by getting SourceStream.Size (so it works if the underlying source stream supports getting Size). Getting Position
always works correctly, as it's just the number of characters read from this stream. The fact that we may read ahead in the underlying source stream (for buffering, for peeking) is completely hidden.
We do not assume anything about the underlying source stream, in particular the underlying source stream may also be purely sequential (so we can only read from it, and we cannot predict when it ends).
The main advantage of using this class is that you get PeekChar routine: you can always peek ahead one character in the stream, without reading it (i.e. next time you will call Read or ReadBuffer you will still get that char). And it works for all source streams, including the ones where seeking is not allowed (so Seek(-1, soCurrent) would not work).
Another advantage is the Line and Column information.
Hierarchy
- TObject
- TStream
- TPeekCharStream
Overview
Methods
function GetSize: Int64; override; |
|
procedure SetSize(NewSize: Longint); override; |
|
procedure UpdateLineColumn(const C: AnsiChar); overload; |
|
procedure UpdateLineColumn(const Buffer; const BufferCount: Integer); overload; |
|
constructor Create(ASourceStream: TStream; AOwnsSourceStream: boolean); |
|
destructor Destroy; override; |
|
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
|
function Write(const Buffer; Count: Longint): Longint; override; |
|
function PeekChar: Integer; virtual; abstract; |
|
function ReadChar: Integer; virtual; abstract; |
|
function ReadUpto(const EndingChars: TSetOfChars): AnsiString; virtual; |
Properties
property SourceStream: TStream read FSourceStream; |
|
property OwnsSourceStream: boolean
read FOwnsSourceStream write FOwnsSourceStream; |
|
property Line: Int64 read FLine; |
|
property Column: Int64 read FColumn; |
Description
Methods
function GetSize: Int64; override; |
|
. ReturnsSourceStream.Size |
procedure SetSize(NewSize: Longint); override; |
|
This stream doesn't support setting size. (All other versions of Exceptions raised
|
procedure UpdateLineColumn(const C: AnsiChar); overload; |
|
procedure UpdateLineColumn(const Buffer; const BufferCount: Integer); overload; |
|
constructor Create(ASourceStream: TStream; AOwnsSourceStream: boolean); |
|
destructor Destroy; override; |
|
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
|
This stream doesn't support seeking. (SetPosition and all other versions of Seek also call this.) Exceptions raised
|
function Write(const Buffer; Count: Longint): Longint; override; |
|
This stream doesn't support writing. (WriteBuffer also calls this.) Exceptions raised
|
function ReadChar: Integer; virtual; abstract; |
|
Read next character. A shortcut for Read(c, 1), but it returns -1 if end of stream is reached. So it's consistent with PeekChar. Sometimes it's also more comfortable, and it's a little faster. |
function ReadUpto(const EndingChars: TSetOfChars): AnsiString; virtual; |
|
Read characters, until one of EndingChars (or end of stream) is found. The ending character is not "consumed" from the stream. The Result is guaranteed to not contain any char from EndingChars. |
Properties
property SourceStream: TStream read FSourceStream; |
|
Underlying stream. |
property OwnsSourceStream: boolean
read FOwnsSourceStream write FOwnsSourceStream; |
|
Should we free underlying SourceStream at destruction. |
property Line: Int64 read FLine; |
|
|
property Column: Int64 read FColumn; |
|
|
Generated by PasDoc 0.16.0.