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

Protected function GetSize: Int64; override;
Protected procedure SetSize(NewSize: Longint); override;
Protected procedure UpdateLineColumn(const C: AnsiChar); overload;
Protected procedure UpdateLineColumn(const Buffer; const BufferCount: Integer); overload;
Public constructor Create(ASourceStream: TStream; AOwnsSourceStream: boolean);
Public destructor Destroy; override;
Public function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
Public function Write(const Buffer; Count: Longint): Longint; override;
Public function PeekChar: Integer; virtual; abstract;
Public function ReadChar: Integer; virtual; abstract;
Public function ReadUpto(const EndingChars: TSetOfChars): AnsiString; virtual;

Properties

Public property SourceStream: TStream read FSourceStream;
Public property OwnsSourceStream: boolean read FOwnsSourceStream write FOwnsSourceStream;
Public property Line: Int64 read FLine;
Public property Column: Int64 read FColumn;

Description

Methods

Protected function GetSize: Int64; override;

.

Returns

SourceStream.Size

Protected procedure SetSize(NewSize: Longint); override;

This stream doesn't support setting size. (All other versions of SetSize also call this.)

Exceptions raised
EStreamNotImplementedSetSize
Always.
Protected procedure UpdateLineColumn(const C: AnsiChar); overload;

This item has no description.

Protected procedure UpdateLineColumn(const Buffer; const BufferCount: Integer); overload;

This item has no description.

Public constructor Create(ASourceStream: TStream; AOwnsSourceStream: boolean);

This item has no description.

Public destructor Destroy; override;

This item has no description.

Public 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
EStreamNotImplementedSeek
Always.
Public function Write(const Buffer; Count: Longint): Longint; override;

This stream doesn't support writing. (WriteBuffer also calls this.)

Exceptions raised
EStreamNotImplementedWrite
Always.
Public function PeekChar: Integer; virtual; abstract;

Peek next character. Returns the next character without making it "already read", so the next call to Read or ReadBuffer will return this char. Subsequent calls to PeekChar (without any Read/ReadBuffer between) will also return the same character.

Returns -1 if stream ended, otherwise returns Ord or given char.

This may call SourceStream.Read, and any exceptions that may be raised in SourceStream.Read are propagated higher from this method.

Public 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.

Public 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

Public property SourceStream: TStream read FSourceStream;

Underlying stream.

Public property OwnsSourceStream: boolean read FOwnsSourceStream write FOwnsSourceStream;

Should we free underlying SourceStream at destruction.

Public property Line: Int64 read FLine;

Line number in the file (counting from 1).

Public property Column: Int64 read FColumn;

Column number in the file (counting from 1).


Generated by PasDoc 0.16.0-snapshot.