Recent

Author Topic: Create a TextFile from a File Descriptor  (Read 634 times)

MMarie

  • New Member
  • *
  • Posts: 37
  • Right, lets bodge this pisspot
    • Homepage
Create a TextFile from a File Descriptor
« on: July 20, 2024, 08:51:11 pm »
Hi,

I am working on a program which processes some text, this text can come from either STDIN or a file which's path was provided as a parameter. For the past 30 minutes ive been trying to find out how I could easily fallback to STDIN unsuccessfully. I desperatly want to avoid having to wrap every call to read in an if condition and so on, and would much prefer if i could just do something basically equivalent to this:
Code: Pascal  [Select][+][-]
  1. procedure foo(filename: string);
  2. var
  3.   file: TextFile;
  4. begin
  5.   if Length(filename) = 0 then
  6.     Assign(file, StdInputHandle)
  7.   else
  8.     Assign(file, filename);
  9.  
  10.   { go on and do something ... }
  11. end;
  12.  

Thank you in advance for your help :)
« Last Edit: July 20, 2024, 09:05:22 pm by MMarie »
i use arch btw

MMarie

  • New Member
  • *
  • Posts: 37
  • Right, lets bodge this pisspot
    • Homepage
Re: Create a TextFile from a File Descriptor
« Reply #1 on: July 20, 2024, 09:07:46 pm »
Nevermind, I found it on "accident" whilst googling for something else, I was looking for "Input" from the System unit...
i use arch btw

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: Create a TextFile from a File Descriptor
« Reply #2 on: July 20, 2024, 10:39:35 pm »
I know that this is after the event, but as background: originally, Pascal's initial "program" statement read like

Code: Pascal  [Select][+][-]
  1. program(input, output);
  2.  

or sometimes

Code: Pascal  [Select][+][-]
  1. program(input, output, error);
  2.  

where the two or three program parameters where bound in a way specific to the operating system to external sources or sinks of line-by-line data (i.e. Pascal's "text" file type). The mapping was- in the early days- set up by control cards when the student (etc.) submitted his program to the mainframe, in unix terms they correspond to the < > and 2> redirections.

It actually makes a lot of sense: the program parameters as viewed from inside the program look like function parameters viewed from inside a function.

If you're feeling brave- very brave- the example at https://forum.lazarus.freepascal.org/index.php/topic,60666.msg454852.html#msg454852 applies: the first few lines are control cards, note the FILE OUTPUT directive.

MarkMLl
« Last Edit: July 20, 2024, 10:54:13 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

silvercoder70

  • Jr. Member
  • **
  • Posts: 96
    • Tim Coates
Re: Create a TextFile from a File Descriptor
« Reply #3 on: July 20, 2024, 10:57:41 pm »
Another way around that would be to create an abstract class (?) like TInputStreamBase and implement 2 classes from that like - TInputStream and TInputFileStream (to avoid conflict) with however you want to do read/write classes. Then ...

Code: Pascal  [Select][+][-]
  1. procedure Fpo(...)
  2. var
  3.   stream: TInputStreamBase;
  4. begin
  5.   if filename = '' then
  6.     stream := TInputStream.Create(...)  
  7.   else
  8.     stream := TInputFileStream.Create(...)
  9.  

and just use stream after this.
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

 

TinyPortal © 2005-2018