Recent

Author Topic: can writeln's output be directed to a memory buffer ?  (Read 764 times)

440bx

  • Hero Member
  • *****
  • Posts: 2295
can writeln's output be directed to a memory buffer ?
« 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.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7076
  • Debugger - SynEdit - and more
    • wiki
Re: can writeln's output be directed to a memory buffer ?
« Reply #1 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

lucamar

  • Hero Member
  • *****
  • Posts: 3843
Re: can writeln's output be directed to a memory buffer ?
« Reply #2 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 and it behaves like Write(), not WriteLn(), though that shouldn't be too much of a problem.

There is also the counterpart: ReadStr()
« Last Edit: April 12, 2021, 06:25:24 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: can writeln's output be directed to a memory buffer ?
« Reply #3 on: April 12, 2021, 06:19:40 am »
use memory-mapped files maybe?
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

440bx

  • Hero Member
  • *****
  • Posts: 2295
Re: can writeln's output be directed to a memory buffer ?
« Reply #4 on: April 12, 2021, 06:48:05 am »
Thank you everyone.  This is good stuff.  WriteStr, or using a stream, can do the trick. :)

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2955
  • Compiler Developer
Re: can writeln's output be directed to a memory buffer ?
« Reply #5 on: April 12, 2021, 09:11:41 am »
You can implement your own "file" handling. E.g. the unit StreamIO does this for Classes.TStream. Of course you can use this in a less object oriented way. :P

440bx

  • Hero Member
  • *****
  • Posts: 2295
Re: can writeln's output be directed to a memory buffer ?
« Reply #6 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)
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2955
  • Compiler Developer
Re: can writeln's output be directed to a memory buffer ?
« Reply #7 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).

MarkMLl

  • Hero Member
  • *****
  • Posts: 2473
Re: can writeln's output be directed to a memory buffer ?
« Reply #8 on: April 12, 2021, 09:58:05 am »
ETA: Found it! It is System.WriteStr 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 2955
  • Compiler Developer
Re: can writeln's output be directed to a memory buffer ?
« Reply #9 on: April 12, 2021, 01:09:29 pm »
ETA: Found it! It is System.WriteStr 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.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8324
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: can writeln's output be directed to a memory buffer ?
« Reply #10 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.

ASBzone

  • Hero Member
  • *****
  • Posts: 615
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: can writeln's output be directed to a memory buffer ?
« Reply #11 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?
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055 (via FpcUpDeluxe) -- Windows 64-bit install w/Win32 and Linux/Arm cross-compiles
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042)
Other Systems: Windows 10 Pro x64, Version 2009 (Build 19042) or greater

440bx

  • Hero Member
  • *****
  • Posts: 2295
Re: can writeln's output be directed to a memory buffer ?
« Reply #12 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.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASBzone

  • Hero Member
  • *****
  • Posts: 615
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: can writeln's output be directed to a memory buffer ?
« Reply #13 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. :)
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.0.13 r64843 / FPC v3.2.1-r49055 (via FpcUpDeluxe) -- Windows 64-bit install w/Win32 and Linux/Arm cross-compiles
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042)
Other Systems: Windows 10 Pro x64, Version 2009 (Build 19042) or greater

440bx

  • Hero Member
  • *****
  • Posts: 2295
Re: can writeln's output be directed to a memory buffer ?
« Reply #14 on: April 14, 2021, 11:23:21 pm »
Ah, okay.  Thanks for clarifying.  I was seriously wondering how that would work. :)
You're welcome.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

 

TinyPortal © 2005-2018