home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / turbo55 / install / demos.arc / LISTER.PAS < prev    next >
Pascal/Delphi Source File  |  1989-05-02  |  6KB  |  212 lines

  1.  
  2. { Turbo List }
  3. { Copyright (c) 1985, 1989 by Borland International, Inc. }
  4.  
  5. program SourceLister;
  6. {
  7.           SOURCE LISTER DEMONSTRATION PROGRAM
  8.  
  9.    This is a simple program to list your TURBO PASCAL source programs.
  10.  
  11.    PSEUDO CODE
  12.    1.  Find Pascal source file to be listed
  13.    2.  Initialize program variables
  14.    3.  Open main source file
  15.    4.  Process the file
  16.        a.  Read a character into line buffer until linebuffer full or eoln;
  17.        b.  Search line buffer for include file.
  18.        c.  If line contains include file command:
  19.              Then process include file and extract command from line buffer
  20.              Else print out the line buffer.
  21.        d.  Repeat step 4.a thru 4.c until eof(main file);
  22.  
  23.    INSTRUCTIONS
  24.    1. Compile and run the program:
  25.        a. In the Development Environment load LISTER.PAS and
  26.           press ALT-R.
  27.        b. From the command line type TPC LISTER.PAS /R
  28.    2. Specify the file to print.
  29. }
  30.  
  31. uses
  32.   Printer;
  33.  
  34. const
  35.   PageWidth = 80;
  36.   PrintLength = 55;
  37.   PathLength  = 65;
  38.   FormFeed = #12;
  39.   VerticalTabLength = 3;
  40.  
  41. type
  42.   WorkString = string[126];
  43.   FileName  = string[PathLength];
  44.  
  45. var
  46.   CurRow : integer;
  47.   MainFileName: FileName;
  48.   MainFile: text;
  49.   search1,
  50.   search2,
  51.   search3,
  52.   search4: string[5];
  53.  
  54.   procedure Initialize;
  55.   begin
  56.     CurRow := 0;
  57.     search1 := '{$'+'I';    { different forms that the include compiler }
  58.     search2 := '{$'+'i';    { directive can take. }
  59.     search3 := '(*$'+'I';
  60.     search4 := '(*$'+'i';
  61.   end {initialize};
  62.  
  63.   function Open(var fp:text; name: Filename): boolean;
  64.   begin
  65.     Assign(fp,Name);
  66.     {$I-}
  67.     Reset(fp);
  68.     {$I+}
  69.     Open := IOResult = 0;
  70.   end { Open };
  71.  
  72.   procedure OpenMain;
  73.   begin
  74.     if ParamCount = 0 then
  75.     begin
  76.       Write('Enter filename: ');
  77.       Readln(MainFileName);
  78.     end
  79.     else
  80.       MainFileName := ParamStr(1);
  81.  
  82.     if (MainFileName = '') or not Open(MainFile,MainFileName) then
  83.     begin
  84.       Writeln('ERROR:  file not found (', MainFileName, ')');
  85.       Halt(1);
  86.     end;
  87.   end {Open Main};
  88.  
  89.   procedure VerticalTab;
  90.   var i: integer;
  91.   begin
  92.     for i := 1 to VerticalTabLength do Writeln(LST);
  93.   end {vertical tab};
  94.  
  95.   procedure ProcessLine(PrintStr: WorkString);
  96.   begin
  97.     CurRow := Succ(CurRow);
  98.     if Length(PrintStr) > PageWidth then Inc(CurRow);
  99.     if CurRow > PrintLength then
  100.     begin
  101.       Write(LST,FormFeed);
  102.       VerticalTab;
  103.       CurRow := 1;
  104.     end;
  105.     Writeln(LST,PrintStr);
  106.   end {Process line};
  107.  
  108.   procedure ProcessFile;
  109.   { This procedure displays the contents of the Turbo Pascal program on the }
  110.   { printer. It recursively processes include files if they are nested.     }
  111.  
  112.   var
  113.     LineBuffer: WorkString;
  114.  
  115.      function IncludeIn(var CurStr: WorkString): boolean;
  116.      var
  117.        ChkChar: char;
  118.        column: integer;
  119.      begin
  120.        ChkChar := '-';
  121.        column := Pos(search1,CurStr);
  122.        if column <> 0 then
  123.          chkchar := CurStr[column+3]
  124.        else
  125.        begin
  126.          column := Pos(search3,CurStr);
  127.          if column <> 0 then
  128.            chkchar := CurStr[column+4]
  129.          else
  130.          begin
  131.            column := Pos(search2,CurStr);
  132.            if column <> 0 then
  133.              chkchar := CurStr[column+3]
  134.            else
  135.            begin
  136.              column := Pos(search4,CurStr);
  137.              if column <> 0 then
  138.                chkchar := CurStr[column+4]
  139.            end;
  140.          end;
  141.        end;
  142.        if ChkChar in ['+','-'] then IncludeIn := False
  143.        else IncludeIn := True;
  144.      end { IncludeIn };
  145.  
  146.      procedure ProcessIncludeFile(var IncStr: WorkString);
  147.  
  148.      var NameStart, NameEnd: integer;
  149.          IncludeFile: text;
  150.          IncludeFileName: Filename;
  151.  
  152.        Function Parse(IncStr: WorkString): WorkString;
  153.        begin
  154.          NameStart := Pos('$I',IncStr)+2;
  155.          while IncStr[NameStart] = ' ' do
  156.            NameStart := Succ(NameStart);
  157.          NameEnd := NameStart;
  158.          while (not (IncStr[NameEnd] in [' ','}','*']))
  159.               and ((NameEnd - NameStart) <= PathLength) do
  160.            Inc(NameEnd);
  161.          Dec(NameEnd);
  162.          Parse := Copy(IncStr,NameStart,(NameEnd-NameStart+1));
  163.        end {Parse};
  164.  
  165.      begin  {Process include file}
  166.        IncludeFileName := Parse(IncStr);
  167.  
  168.        if not Open(IncludeFile,IncludeFileName) then
  169.        begin
  170.          LineBuffer := 'ERROR:  include file not found (' +
  171.                        IncludeFileName + ')';
  172.          ProcessLine(LineBuffer);
  173.        end
  174.        else
  175.        begin
  176.          while not EOF(IncludeFile) do
  177.          begin
  178.            Readln(IncludeFile,LineBuffer);
  179.            { Turbo Pascal 5.5 allows nested include files so we must
  180.              check for them and do a recursive call if necessary }
  181.            if IncludeIn(LineBuffer) then
  182.              ProcessIncludeFile(LineBuffer)
  183.            else
  184.              ProcessLine(LineBuffer);
  185.          end;
  186.          Close(IncludeFile);
  187.        end;
  188.      end {Process include file};
  189.  
  190.   begin  {Process File}
  191.     VerticalTab;
  192.     Writeln('Printing . . . ');
  193.     while not EOF(mainfile) do
  194.     begin
  195.       Readln(MainFile,LineBuffer);
  196.       if IncludeIn(LineBuffer) then
  197.          ProcessIncludeFile(LineBuffer)
  198.       else
  199.          ProcessLine(LineBuffer);
  200.     end;
  201.     Close(MainFile);
  202.     Write(LST,FormFeed); { move the printer to the beginning of the next }
  203.                          { page }
  204.   end {Process File};
  205.  
  206.  
  207. begin
  208.   Initialize;      { initialize some global variables }
  209.   OpenMain;        { open the file to print }
  210.   ProcessFile;     { print the program }
  211. end.
  212.