home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l044 / 4.ddi / DEMOS.ZIP / LISTER.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-23  |  5.6 KB  |  212 lines

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