Lazarus

Free Pascal => General => Topic started by: 440bx on April 12, 2021, 05:57:39 am

Title: can writeln's output be directed to a memory buffer ?
Post by: 440bx on April 12, 2021, 05:57:39 am
Hello,

Writeln will write to a console or a file.

Is there a way to have writeln write its output to a preallocated block of memory ?  something along the lines of:
Code: Pascal  [Select][+][-]
  1. writeln(memoryaddress, 'this is something to output':50, anumber, 'another string', whateverelsehere);  
  2.  
then use the output in memoryaddress as desired ?

Thank you for your help.
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: Martin_fr on April 12, 2021, 06:06:49 am
Afaik you can replace the stdout handle.... But that is from distant memory.

This may be related: http://free-pascal-general.1045716.n5.nabble.com/redirecting-stdout-td2816016.html
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: lucamar on April 12, 2021, 06:18:16 am
Isn't there a WriteBuffer() or WriteStr() or somethign like that exactly for that? I don't remember exactly but if memory isn't failing it produces the same output than either Write() or WriteLn() (don't remember which) only it goes to a buffer or a string.

Let me dig a little to see if I can find it ...

ETA: Found it! It is System.WriteStr (https://freepascal.org/docs-html/current/rtl/system/writestr.html) and it behaves like Write(), not WriteLn(), though that shouldn't be too much of a problem.

There is also the counterpart: ReadStr()
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: skalogryz on April 12, 2021, 06:19:40 am
use memory-mapped files maybe?
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: 440bx on April 12, 2021, 06:48:05 am
Thank you everyone.  This is good stuff.  WriteStr, or using a stream, can do the trick. :)

Title: Re: can writeln's output be directed to a memory buffer ?
Post by: PascalDragon on April 12, 2021, 09:11:41 am
You can implement your own "file" handling. E.g. the unit StreamIO (https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/packages/fcl-base/src/streamio.pp?revision=46268&view=markup) does this for Classes.TStream. Of course you can use this in a less object oriented way. :P
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: 440bx on April 12, 2021, 09:15:07 am
Of course you can use this in a less object oriented way. :P
I was thinking about that.  I remember Turbo Professional had implemented that functionality with a custom TextRec.

I'll be content with just WriteStr, it does the trick. :)  (thank you Lucamar)
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: PascalDragon on April 12, 2021, 09:19:42 am
Of course you can use this in a less object oriented way. :P
I was thinking about that.  I remember Turbo Professional had implemented that functionality with a custom TextRec.

It's essentially the same with FPC/Delphi, though the TextRec itself is the standard one. It's just the function pointers inside it that will be custom (normally setup up using an AssignXXX function, though one can pick any name here).
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: MarkMLl on April 12, 2021, 09:58:05 am
ETA: Found it! It is System.WriteStr (https://freepascal.org/docs-html/current/rtl/system/writestr.html) and it behaves like Write(), not WriteLn(), though that shouldn't be too much of a problem.

There is also the counterpart: ReadStr()

Thanks for that. So WriteStr() and ReadStr() are presumably "magic" functions which rely on the compiler to parse a variable number of parameters without having to put them in (square) brackets.

MarkMLl
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: PascalDragon on April 12, 2021, 01:09:29 pm
ETA: Found it! It is System.WriteStr (https://freepascal.org/docs-html/current/rtl/system/writestr.html) and it behaves like Write(), not WriteLn(), though that shouldn't be too much of a problem.

There is also the counterpart: ReadStr()

Thanks for that. So WriteStr() and ReadStr() are presumably "magic" functions which rely on the compiler to parse a variable number of parameters without having to put them in (square) brackets.

They are intrinsics just like Write is. In fact they use the same infrastructure. Originally they came from ISO Extended Pascal.
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: Leledumbo on April 14, 2021, 12:15:08 am
Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes,
  3.   StreamIO;
  4.  
  5. var
  6.   ms: TStream;
  7.   i: longint;
  8. begin
  9.   // here's the memory buffer
  10.   ms := TMemoryStream.Create;
  11.  
  12.   // standard output is now linked to our memory buffer
  13.   AssignStream(Output,ms);
  14.   Rewrite(Output);
  15.   WriteLn('Yes, you can format it like ',22/7:1:4,' or ',255:8);
  16.   Close(Output);
  17.  
  18.   // restore its original functionality
  19.   Assign(Output,'');
  20.   Rewrite(Output);
  21.   ms.Seek(0, soFromBeginning); // forget to do this and you will read garbage if not exception
  22.   for i := 0 to ms.Size - 1 do
  23.     Write(Char(ms.ReadByte));
  24.   WriteLn;
  25.  
  26.   // cleanup
  27.   ms.Free;
  28. end.
  29.  
talk less, code more.
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: ASBzone on April 14, 2021, 12:27:11 am
Thank you everyone.  This is good stuff.  WriteStr, or using a stream, can do the trick. :)

Hey, @440bx, how would you be able to leverage WriteStr to write to a preallocated block of memory?
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: 440bx on April 14, 2021, 12:30:39 am
Hey, @440bx, how would you be able to leverage WriteStr to write to a preallocated block of memory?
I don't believe WriteStr can be made to write to a preallocated block of memory but, the output of WriteStr can be copied to a preallocated block of memory.  It's a bit pedestrian but, ultimately with a little help, it does the trick.
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: ASBzone on April 14, 2021, 06:06:31 pm
Hey, @440bx, how would you be able to leverage WriteStr to write to a preallocated block of memory?
I don't believe WriteStr can be made to write to a preallocated block of memory but, the output of WriteStr can be copied to a preallocated block of memory.  It's a bit pedestrian but, ultimately with a little help, it does the trick.

Ah, okay.  Thanks for clarifying.  I was seriously wondering how that would work. :)
Title: Re: can writeln's output be directed to a memory buffer ?
Post by: 440bx on April 14, 2021, 11:23:21 pm
Ah, okay.  Thanks for clarifying.  I was seriously wondering how that would work. :)
You're welcome.
TinyPortal © 2005-2018