program WSMOD2;
(* (C) COPYWRITE 1986, DRAGON DATASYSTEMS, Inc. *)

LABEL       GETCHAR;
LABEL       QUIT;

Var
   infile:  Text;
   outfile: Text;

   sfile:   string[14];
   ofile:   string[14];

   inchar:  Char;
   outchar: Char;
   c:       Char;
   Class:   Char;

   numchar:  Integer;
   numchar1: Integer;
   numchar2: Integer;

{V-}
type
    Name = string[14];
function Exist(Var FileName: Name): boolean;
Var
   Fil:   file;
begin
    Assign(Fil, FileName);
    {$I-}
    Reset(Fil);
    {$I+}
    Exist := (IOresult = 0)
end;

begin
     ClrScr;
     Writeln('                                  ENSOFT3.COM');
     Writeln(' ');
     Writeln('                 (C) Copywrite 1986, DRAGON DATASYSTEMS, Inc.');
     Writeln(' ');
     Writeln('This program reads a text file character by character and checks for control');
     Writeln('characters, all control characters are deleted except the carriage return');
     Writeln('(13 decimal, 0D hex) and line feed (10 decimal, 0A hex), all cr-lf pairs are');
     Writeln('replaced by WordStar (TM) style "soft" returns, the high bit of the last  ');
     Writeln('character of each word is also set to make the file compatible with WordStar.');
     Writeln('The text is then written to a designated output file.');
     Writeln(' ');
     Write('Enter the file to be read: ');
     Readln(sfile);
     if Exist(sfile) = False then
     repeat
           Writeln('That file does not exist: check the drive and name');
           Write('and reenter the file to be read: ');
           Readln(sfile);
     until Exist(sfile);
     Write('Enter the file to be used for output: ');
     Readln(ofile);
     c := 'Y';
     if Exist(ofile) = True then
     begin
       Write('WARNING: That file exists, any  contents of ');
       Write(ofile);
       Write(' will be destroyed.');
       Writeln(' ');
       Write('Do you wish to proceed? (Y or N) ');
       Read(c);
       Writeln(' ');
     end;
     if c <> 'N' then
         Assign(infile,sfile);
         Reset(infile);
         Assign(outfile,ofile);
         Rewrite(outfile);
         Writeln('Please wait this takes a few minutes.  Thank you.');
         numchar2 := 0;
         REPEAT;
GETCHAR:   Read(infile,inchar);
           numchar := Ord(inchar);
           if (numchar >= 128) then
              numchar := numchar - 128;
           if (numchar < 32) then Class := 'W';
           if (numchar = 10) then Class := 'L';
           if (numchar = 13) then Class := 'R';
           if Class = 'W' then goto GETCHAR;
           numchar1 := numchar2;
           numchar2 := numchar;
           if numchar2 = 32 then
             numchar1 := numchar1 + 128;
           if (numchar1 = 13) AND (numchar2 = 10) then
             numchar1 := 141;
           outchar := Chr(numchar1);
           Write(outfile,outchar);
         UNTIL EOF(infile);
QUIT:    close(outfile);
         close(infile);
         Writeln('Finished');
END.