Standard Input to stream lazarus / 8-Other ---------------------------------------------------------------------- Kunstbanause · 2008-10-30 08:21 Hi, I currently use the following code to get the standard input at the start of a console program: Code: [Select] var   c: char;   s: string;   t: text; begin   s := '';   AssignFile(t, '');   Reset(t);   while not eof(t) do   begin     Read(t, c);     s := s + c;   end; end. Is there a way to access standard input as a stream? ---------------------------------------------------------------------- Zaher · 2008-10-30 13:54 Search for PIPE in FPC source. ---------------------------------------------------------------------- Leledumbo · 2008-10-31 03:03 Can't you use TIOStream? ---------------------------------------------------------------------- Kunstbanause · 2008-10-31 10:57 Quote from: "Leledumbo"Can't you use TIOStream? I tried TIOStream. But there's a problem. I use the following code: Code: [Select]program project1; {$mode objfpc}{$H+} uses   Classes, SysUtils, iostream; var   mem: TMemoryStream;   ios: TIOStream; begin   ios := TIOStream.Create(iosInput);   mem := TMemoryStream.Create;   mem.CopyFrom(ios, 0);   ios.Free;   WriteLn(Format('Size of mem: %d', [mem.Size]));   mem.Free; end. The output is: Code: [Select]$ ls | ./project1 Size of mem: 0 $ ls | wc -c 223 ---------------------------------------------------------------------- Chronos · 2011-12-25 22:39 Now I want to do same thing and problem is that standard input is rather queue/pipe than seekable stream and property InputStream.Size which use Seek to get size will fail. It is necessary to fetch available data using Read method which return actual read count. Stream CopyFrom method with Count = 0 will determine size using Size property. Code for copy input to stream using buffer: Code: [Select]uses   iostream, ...; var   Buffer: string;   Count: Integer;   MyStream: TMemoryStream;   InputStream: TIOStream; begin   MyStream := TMemoryStream.Create;   try     InputStream := TIOStream.Create(iosInput);     SetLength(Buffer, 10000);     repeat       Count := InputStream.Read(Buffer[1], Length(Buffer));       if Count > 0 then MyStream.Write(Buffer[1], Count);     until Count = 0;   finally     InputStream.Free;   end;   ... ---------------------------------------------------------------------- felipemdc · 2011-12-26 01:26 Quote from: Kunstbanause on October 30, 2008, 09:21:58 amIs there a way to access standard input as a stream? There is one section in the Lazarus book in one of my chapters dedicated to that. But in short, you just need to do this: Code: [Select]MyStream := THandleStream.Create(StdInputHandle); ---------------------------------------------------------------------- Chronos · 2011-12-27 00:45 TIOStream is descendant of THandleStream and problem is how to get size of stream because TStream.Size method determine size using seek from end of stream which means change position in stream and standard input doesn't have start and end. All data readed from input stream is lost. Seeking from current position to end of stream means that all data are lost and this is probably reason why Kunstbanause example doesn't work. CopyFrom with zero count means "get count from size". Standard input should be rather queue than seekable stream. Stream is good for positionable media as tape, disk or file. And stream may not have limited size and than it would be unlimited stream. Another situation is with buffered stream where one would want to read all currently buffered data at once. Not limited to size but somehow determine size of internal buffered data and read them all. And avoid repeated reading in small chunks or maybe one byte by one. I am solving this problem for handling of CGI application where I need to read available data from input stream passed by apache web server. Another problem would be persistent connection with HTTP 1.1 and server more requests per connection.