Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-12

Filters and PTOP questions

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

cbase1

Is the simple example below the best way to implement a filter in FPC?

The example code blanks column 2 of a text file.  This program works with both "blankCol2 filename" and "blankCol2 <filename".  Compiled with 3.0.2.

The PTOP question is how to configure PTOP to format files like this example.   Specifically the begin-end block on the "IF". Anybody care to share a PTOP configuration file?

Thanks

John

(* example filter program. blank column 2 *)
{$mode objfpc}{$H+}{$J-}
program blankCol2;
Uses getopts, sysutils;
var
    { for getopts }
    fileName : string;
    fin : text;
    pipe : boolean;  { if true then piped input instead of a file }
    function getOptions() : boolean;
    var
       statusGood : boolean;
       c : char ;
    begin
       { setup options }
       statusGood := true; { assume options correct }
       { get options }
       repeat
          c := GetOpt('');
          case c of
             '?',':':
                begin
                writeln ('Error with opt:' , optopt) ;
                statusGood := false;
                end;
             end ; { case }
       until c=endofoptions;
       { check for filename }
       if optind <=paramcount then
          begin
          fileName := paramstr( optind );
          pipe := false
          end
       else
          begin
          pipe := true; { no filename so pipe }
          end;
       getOptions := statusGood
    end;
    procedure processFile();
    var
       line : string;
       done : boolean;
    begin
       repeat
          if (pipe = true) then
             readln(input, line)
          else
             readln(fin, line);
          if (length(line) > 2) then line[2] := ' ';
          writeln(line);
          if (pipe = true) then
             done := eof(input)
          else
             done := eof(fin);
       until (done);
    end;

begin
   { process options }
   if (getOptions() = false) then
      begin {options bad}
      writeln('bad');
      end
   else
      begin {options good}
      if (pipe = true) then { open stdin }
         reset(input)
      else
         begin {open file }
         assign(fin, fileName);
         reset(fin);
         end;

       processFile();
       if (pipe = true) then
          begin
          close(input);
          writeln('pipe: for test');
          end
       else
          begin
          close(fin);
          writeln('file: for test');
          end;
       close(output);
       end;
end.

Reply by email