Program TextFileAnalyser;

Uses Crt,DOS;

{$I flength.inc}
{$I style.inc}

Const
  VNM='1.00';
  VDT='23-May-95';

Var
  FLN:String;

Procedure ShowInformation;
Begin
  Style(0,1);
  Style(1,1);
  Style(3,1);
  WriteLn('TFA (Text File Analyser) v',VNM,' by Zebedee ©1995 Carnage (',VDT,')');
  Style(0,1);
  WriteLn(#$a,'Usage: ',ParamStr(0),' <textfile>',#$a);
  WriteLn('Where <textfile> is the name of the ASCII file you want to analyse.  TFA');
  WriteLn('will read the file and display some information about it.');
  Halt(20);
  Write('$VER: TFA Text File Analyser v1.00 (23-May-95) ');
End;

Procedure GetParam;
Begin
  If ParamStr(1)='?' Then ShowInformation;
  If ParamStr(1)='wHy' Then
  Begin
    WriteLn('Written because I hate having to load Protext every time I want to analyse');
    WriteLn('a file!  -zeb/crn');
    Halt(20);
  End;
  If ParamCount=0 Then
  Begin
    WriteLn('Usage: ',ParamStr(0),' <textfile>');
    Halt(20);
  End;
  If ParamCount>1 Then
  Begin
    WriteLn(ParamStr(0),': Too many arguments');
    Halt(20);
  End;
  FLN:=ParamStr(1);
End;

Procedure AnalyseFile;
Var
  IFS:Text;
  RCH:Char;
  TXT:String;
  CTR:Integer;
  FLCTR,ELCTR,WCTR,LOF,SPC:LongInt;
Begin
  ReSet(IFS,FLN);
  If IOResult<>0 Then
  Begin
    WriteLn(ParamStr(0),': Can''t locate the file');
    Halt(20);
  End;
  FLCTR:=0;
  ELCTR:=0;
  WCTR:=0;
  SPC:=0;
  While not Eof(IFS) Do
  Begin
    ReadLn(IFS,TXT);
    If Length(TXT)=0 Then Inc(ELCTR) Else
    Begin
      Inc(FLCTR);
      For CTR:=2 To Length(TXT) Do
      Begin
        If (TXT[CTR+1]<>' ') and (TXT[CTR]=' ') Then Inc(WCTR);
        If TXT[CTR]=' ' Then Inc(SPC);
      End;
      Inc(WCTR);
      If KeyPressed Then
      Begin
        RCH:=ReadKey;
        If RCH=#3 Then
        Begin
          WriteLn('*** Break');
          Halt(20);
        End;
      End;
    End;
  End;
  Close(IFS);
  LOF:=FLength(FLN);
  Style(0,1);
  Style(2,1);
  TextColor(2);
  WriteLn('Results for the file "',FLN,'"...');
  TextColor(1);
  Style(0,1);
  WriteLn('File length ................... ',LOF,' bytes');
  WriteLn('Number of words ............... ',WCTR);
  WriteLn('Number of spaces .............. ',SPC);
  WriteLn('Number of empty lines ......... ',ELCTR);
  WriteLn('Number of lines not empty ..... ',FLCTR);
  WriteLn('Total number of lines ......... ',ELCTR+FLCTR);
End;

Begin
  GetParam;
  AnalyseFile;
End.