home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / WSX.ZIP / WSX.PAS
Encoding:
Pascal/Delphi Source File  |  1985-12-28  |  8.7 KB  |  372 lines

  1. program wsutil;
  2.  
  3. {WordStar file utility  --  3-27-85 Ver B5   }
  4. {Copyright 1985 by David W. Carroll          }
  5. {                                            }
  6. {Converts WordStar files to ASCII text files }
  7. {with options.                               }
  8.  
  9. {ModifieΣ b∙ Beε Ettelsoε 11-05-8╡ t∩ turε off
  10. cursor, use gotoxy's in update window, 
  11. provide exit for null file name, and put ^Z at
  12. end of file.}
  13.  
  14. const
  15.    nummax      = 200;   {Maximum input line length}
  16.  
  17.    version     = 'B5';
  18.    date        = 'March 27, 1985';
  19.  
  20.    null        = 00;
  21.    bell        = 07;
  22.    lf          = 10;
  23.    ff          = 12;
  24.    cr          = 13;
  25.    nobrksp     = 15;   {no break space ^O}
  26.    softhyph1   = 30;   {mid-text soft hyphen}
  27.    softhyph2   = 31;   {eol soft hyphen}
  28.    space       = 32;
  29.    hyph        = 45;
  30.    period      = 46;
  31.    softlf      = 138;  {soft line feed}
  32.    softcr      = 141;  {soft carriage return}
  33.    softsp      = 160;  {soft space}
  34.  
  35.    ctlb        = 02;
  36.    ctld        = 04;
  37.    ctls        = 19;
  38.    ctlt        = 20;
  39.    ctlv        = 22;
  40.  
  41. { arrays pcon and pcoff contain the strings that are  }
  42. { substituted for WordStar print control characters   }
  43. { ^b, ^d, ^s, ^t, and ^v. Constant pc is the total    }
  44. { number of WS print control characters supported.    }
  45.  
  46.    pc          = 05;
  47.    spc:   array[1..pc] of byte = (ctlb, ctld, ctls, ctlt, ctlv);
  48.    pcon:  array[1..pc] of string[10] =
  49.             ('[bf]','[ds]','[us]','[sup]','[sub]');
  50.    pcoff: array[1..pc] of string[10] =
  51.             ('[ebf]','[eds]','[eus]','[esup]','[esub]');
  52. type
  53.     regpack =
  54.       record
  55.         case boolean of
  56.           true:
  57.             (ax, bx, cx, dx, bp, si, ds, es, flags: integer);
  58.           false:
  59.             (al, ah, bl, bh, cl, ch, dl, dh: byte);
  60.       end;
  61.  
  62. var
  63.    infile       :  text;
  64.    outfile      :  text;
  65.    numlist      :  array[1..nummax] of byte;
  66.    flagpc       :  array[1..pc] of boolean;
  67.    totchrin     :  real;
  68.    totchrout    :  real;
  69.    cnt          :  integer;
  70.    quit         :  boolean;
  71.    stripsp      :  boolean;
  72.    striplf      :  boolean;
  73.    chgpc        :  boolean;
  74.    stripdc      :  boolean;
  75.    strippc      :  boolean;
  76.    translt      :  boolean;
  77.  
  78.   procedure cursor(on, block: boolean);
  79.    {turn cursor on or off and set size}
  80.  
  81.     var
  82.       regs: regpack;
  83.  
  84.     begin
  85.       with regs do
  86.         begin
  87.           begin
  88.             ah := $0F; {get current video state}
  89.             intr($10, regs);
  90.             if al = 7 then
  91.               begin {80 x 25 using bw card}
  92.                 if block then
  93.                   begin
  94.                     ch := 0;
  95.                     cl := 13;
  96.                   end
  97.                 else
  98.                   begin
  99.                     ch := 11;
  100.                     cl := 12;
  101.                   end;
  102.               end
  103.             else
  104.               begin {80 x 25 using color card}
  105.                 if block then
  106.                   begin
  107.                     ch := 0;
  108.                     cl := 7;
  109.                   end
  110.                 else
  111.                   begin
  112.                     ch := 6;
  113.                     cl := 7;
  114.                   end;
  115.               end;
  116.           end; {if}
  117.           if not on then
  118.             ch := ch or $20; {cursor off}
  119.           ah := 1; {set cursor type}
  120.           intr($10, regs);
  121.         end; {with}
  122.     end; {cursor}
  123.  
  124. procedure open_files;
  125. var
  126.    infname      :  string[20];
  127.    outfname     :  string[20];
  128.    ans          :  string[10];
  129.    goodfile     :  boolean;
  130.  
  131. begin {open_files}
  132.      window (1,5,80,25);
  133.      repeat
  134.         ClrScr;
  135.         write ('Input filename  -->  ');
  136.         readln (infname);
  137.         if infname = '' then
  138.           begin
  139.             window(1,1,80,25);
  140.             clrscr;
  141.             halt;
  142.           end;
  143.         assign(infile,infname);
  144.         {$I-} reset(infile) {$I+};
  145.         goodfile := (IOresult = 0);
  146.         if not goodfile then
  147.         begin
  148.           write (chr(bell));
  149.           writeln ('FILE ',infname,' NOT FOUND');
  150.           delay(3000)
  151.         end;
  152.      until goodfile;
  153.      window (1,6,80,25);
  154.      repeat
  155.         ClrScr;
  156.         write ('Output filename -->  ');
  157.         readln (outfname);
  158.         assign (outfile,outfname);
  159.         {$I-} reset(outfile) {$I+};
  160.         goodfile := (IOresult <> 0);
  161.         if not goodfile then
  162.         begin
  163.           write (chr(bell));
  164.           write ('FILE ',outfname,' EXISTS, OVERWRITE? (y/n) ');
  165.           readln (ans);
  166.           goodfile := (UpCase(ans[1])='Y')
  167.         end;
  168.      until goodfile;
  169.      rewrite(outfile)
  170. end; {open_files}
  171.  
  172. procedure get_line;
  173. var
  174.    ch    : char;
  175.    num   : byte;
  176.    lonum : byte;
  177.  
  178. begin
  179.   ch:=chr(0);
  180.   lonum:=0;
  181.   num:=0;
  182.   cnt:=0;
  183.  
  184.   while not eof(infile) and (lonum<>lf) do
  185.   begin
  186.     cnt:=cnt+1;
  187.     read(infile,ch);
  188.     totchrin := totchrin + 1;
  189.     num:=ord(ch);
  190.     lonum:=(num and 127);
  191.     numlist[cnt]:=num;
  192.   end
  193. end;
  194.  
  195. procedure test_line;
  196. begin
  197.      translt := true;
  198.      if stripdc then
  199.        if numlist[1]=period then translt := false;
  200. end;
  201.  
  202. procedure translate_line;
  203. var
  204.    spstr  :  string[10];
  205.    indx1  :  integer;
  206.    indx2  :  integer;
  207.    indx3  :  integer;
  208.    num    :  byte;
  209.    chnum  :  byte;
  210.    lonum  :  byte;
  211.    exch   :  boolean;
  212.  
  213. begin
  214.   for indx1:=1 to cnt do
  215.   begin
  216.     exch := false;
  217.     num:=numlist[indx1];
  218.     chnum := num and 127;
  219.     lonum :=chnum;
  220.  
  221.     if (num=softhyph2) then
  222.        chnum := hyph
  223.     else if (num=softhyph1) then
  224.        chnum := null;
  225.  
  226.     if num=nobrksp then chnum := space;
  227.  
  228.     if chgpc then
  229.     begin
  230.       for indx2:=1 to pc do
  231.       begin
  232.         if lonum = spc[indx2] then
  233.         begin
  234.           chnum := null;
  235.           exch := true;
  236.           if flagpc[indx2] then
  237.             spstr := pcoff[indx2]
  238.           else
  239.             spstr := pcon[indx2];
  240.           flagpc[indx2] := not flagpc[indx2]
  241.         end
  242.       end
  243.     end;
  244.  
  245.     if stripsp and (num=softsp) then chnum := null;
  246.     if striplf and (lonum=lf) then chnum := null;
  247.  
  248.     if strippc then
  249.       for indx3 := 1 to pc do
  250.         if lonum = spc[indx3] then chnum := null;
  251.  
  252.     if chnum <> null then
  253.     begin
  254.       write (outfile, chr(chnum));
  255.       totchrout := totchrout+1
  256.     end;
  257.     if exch then
  258.     begin
  259.       write(outfile,spstr);
  260.       totchrout := totchrout + length(spstr)
  261.     end
  262.   end
  263. end;
  264.  
  265. function inyn : boolean;
  266. var
  267.   ans  : string[10];
  268.  
  269. begin
  270.   write('[y/n] ');
  271.   readln(ans);
  272.   inyn := (Upcase(ans[1]) = 'Y')
  273. end;
  274.  
  275. procedure menu;
  276. begin
  277.       writeln;
  278.       writeln('Wordstar to ASCII Conversion');
  279.       writeln;
  280.       writeln;
  281.       write(' 1.  Strip soft-spaces (un-justify)?   ');
  282.       stripsp := inyn;
  283.       write(' 2.  Strip line feeds?                 ');
  284.       striplf := inyn;
  285.       write(' 3.  Change control (print) commands?  ');
  286.       chgpc   := inyn;
  287.       write(' 4.  Strip dot commands?               ');
  288.       stripdc := inyn;
  289.       if chgpc = false then
  290.       begin
  291.         write(' 5.  Strip print commands?             ');
  292.         strippc := inyn;
  293.       end
  294.       else
  295.         strippc := false;
  296.       writeln;
  297.       write(' Quit?                                 ');
  298.       quit := inyn;
  299. end;
  300.  
  301. procedure process;
  302. var
  303.    line  :  integer;
  304.    indx  :  integer;
  305.  
  306. begin {process}
  307.      window(1,7,80,25);
  308.      ClrScr;
  309.      line:=0;
  310.      totchrin:=0;
  311.      totchrout:=0;
  312.  
  313.      for indx :=1 to pc do
  314.        flagpc[indx] := false;
  315.  
  316.      cursor(false,false);
  317.      while not eof(infile) do
  318.      begin
  319.         line:=line+1;
  320.         get_line;
  321.         test_line;
  322.         if translt then
  323.           begin
  324.             translate_line;
  325.             window(1,12,80,16);
  326.             gotoxy(1,1);
  327.             write('Line # ',line:5);
  328.             gotoxy(1,2);
  329.             write('Total characters input:    ',totchrin:6:0);
  330.             gotoxy(1,3);
  331.             write('Total characters output:   ',totchrout:6:0);
  332.             gotoxy(1,4);
  333.             write('Total filtered(+)/added(-):',(totchrin-totchrout):6:0);
  334.           end
  335.      end
  336. end;
  337.  
  338. procedure exit;
  339. begin
  340.      window(1,23,80,25);
  341.      ClrScr;
  342.      cursor(true, false);
  343.      writeln('Translation completed!');
  344. (*
  345.      writeln(outfile);
  346. *)
  347.      close(infile);
  348.      write(outfile, ^Z); {BLE 11-05-85}
  349.      close(outfile)
  350. end;
  351.  
  352. begin
  353.      ClrScr;
  354.      writeln;
  355.      writeln('WordStar File Conversion Program');
  356.      writeln('Copyright 1985 by David W. Carroll');
  357.      writeln('Version #',version,' of ',date,'.');
  358.      writeln;
  359.      window(1,5,80,25);
  360.      ClrScr;
  361.      menu;
  362.      ClrScr;
  363.      if not quit then
  364.      begin
  365.        open_files;
  366.        process;
  367.        exit
  368.      end
  369.      else
  370.        writeln('Translation cancelled.');
  371. end.
  372.