                  {28 Apr '85}
program Copy2ws;
        {Program is designed to take a file with c/r on each line
and remove those not at the end of paragraphs,and to remove
excess spaces.}
     { Written in Turbo Pascal 3.0
       by Herb Builteman
       408-867-3917
       All rights reserved}


type Buffer= String[80];

var
   Fil1Var,Fil2Var: Text;
   Buffer1,Buffer2: Buffer;
   fileName: string[14];

procedure WriteNewLine(line1:Buffer);
var
   II: integer;
   Buffer3: Buffer;
   Prev,Last,Current: char;
const
   space = ' ';

begin{WriteNewLine}
   Prev := space; Last := space; Buffer3 := '';
   for II := 1 to length(line1) do
      begin
      Current := line1[II];
         If ((Prev = '.') and (Last = Space) and (Current = Space)) or
            {leaves 2 spaces at end of sentence if originally there}
            ((Prev = Space) and (Last = Space) and (Current in ['0'..'9']))
            then Buffer3 := Buffer3 + Current
            {leaves 2 spaces before numbers}
         Else
            If not((Last = Space) and (Current = Space)) then
            Buffer3 := Buffer3 + Current;
            {omits writing two spaces together unless foregoing
            conditions pertain}
      Prev := Last; Last := Current;
      end; {for II = 1 to length(1)}
      If (length(Buffer3) < 40) and not (Current  in ['.','?','!',':',';'])
         then Writeln(Fil2Var, Buffer1)
         {Short line implies a list or table if not terminated. Hence,
          print unchanged from original}
      Else Write(Fil2Var, Buffer3 + space);
   {When end of line is reached, a space is inserted to avoid joining
   the next line first word to end of last}
end; {procedure WriteNewLine}

begin {program}
     write('Enter name of non-WordStar file: ');
     Readln(FileName);
     Assign(Fil1Var,FileName);
     Reset(Fil1Var);
     Write('Enter name of WordStar file: ');
     Readln(FileName);
     Assign (Fil2Var,FileName);
     Rewrite(Fil2Var);
     writeln(Fil2Var);

     ClrScr;
     GotoXY(30,10);
     Writeln('Program Running');

     readln(Fil1Var,Buffer1);
     While not eof(fil1Var) do
        begin
              readln(Fil1Var,Buffer2);
            
            If length(Buffer2)>0 then
            {next line is NOT blank}
               begin
               WriteNewLine(Buffer1);
               Buffer1 := Buffer2;
               end {if}
            Else
            {next line IS blank}
               begin
               WriteNewLine(Buffer1);
               Writeln(Fil2Var);
               Writeln(Fil2Var);
               Buffer1 := Buffer2;
               end{else}
       end;{while not eof fil1var}
Close(fil2Var);
{I found that nothing comes through if Fil2Var is not closed!!}

     ClrScr;
     GotoXY(30,20);
     Writeln('Program Complete');

end. {program}