[fpc-pascal] How to initialize a "Text" variable?

Michael Van Canneyt michael at freepascal.org
Sun Mar 16 12:37:58 CET 2008



On Sun, 16 Mar 2008, Felipe Monteiro de Carvalho wrote:

> On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt
> <michael at freepascal.org> wrote:
> >  Assign yyoutput to a dummy stream, which simply skips output.
> 
> Could you explain better how can this be implemented?
> 
> The only think I know about streams is TStream.

Exactly:

-----------------------------------------------------------------------

uses lexlib, Classes, StreamIO;

Type
  TNullStream = Class(TStream)
    function Read(var Buffer; Count : LongInt) : Longint; override;
    function Write(const Buffer; Count : LongInt) : LongInt; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;
  end;

Function TNullStream.Read(var Buffer; Count : LongInt) : Longint;
begin
  Result:=Count;
end;

Function TNullStream.Write(const Buffer; Count : LongInt) : LongInt;

begin
  Result:=Count;
end;

function TNullStream.Seek(Offset: Longint; Origin: Word): Longint; 

begin
  Result:=0;
end;


Var
  Null : TNullStream;

begin
  Null:=TNullstream.Create;
  assignstream(yyoutput,Null);
  // blah blah
  FreeAndNil(Null);
end.
-----------------------------------------------------------------------

That's it. Come to think of it,I may add TNullStream to the streamex unit or so.

Michael.



More information about the fpc-pascal mailing list