home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / FILEUT20.ZIP / FILEUTIL.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1985-05-31  |  10.5 KB  |  393 lines

  1. program fileutil(input, output);
  2.  
  3. {  This program will add linefeeds, convert to all upper-case, convert
  4.    to all-lowercase or convert all non-quoted characters (those in either
  5.    " or ').  A quote is terminated by either the same as the start or an
  6.    end of line.
  7.    The file is invoked this way:
  8.  
  9.         fileutil [params] filename [outfile]
  10.  
  11.           available params are:
  12.              ? - List parameters
  13.              A - add linefeeds
  14.              L - convert lower
  15.              U - convert upper
  16.              W - convert from WordStar to ASCII
  17.              R - remove extraneous carriage return
  18.              Q - don't modify inside quotes
  19.  
  20.            any combination except L and U is allowed                     }
  21.  
  22. type
  23.   name = string[80];
  24.   line = string[255];
  25.   regpack = record
  26.               ax,bx,cx,dx,bp,si,di,ds,es,flags : integer;
  27.             end;
  28.  
  29. var
  30.   i: integer;
  31.   linefeeds, wstar, lower, upper, crs, quotes, ok: boolean;
  32.   filen: text;
  33.   filename, outfile, backfile: name;
  34.   qu, sq : boolean;
  35.  
  36. procedure outchar(ch: char);
  37. var
  38.   regs : regpack;
  39. begin
  40.   with regs do
  41.     begin
  42.       ax := $0600;
  43.       dx := ord(ch);
  44.       msdos(regs);
  45.     end;
  46. end;
  47.  
  48. procedure outstring(ln: line);
  49. var
  50.   i : integer;
  51. begin
  52.   for i := 1 to length(ln) do
  53.     outchar(ln[i]);
  54. end;
  55.  
  56. procedure help;
  57. begin
  58.   outstring('The format for FILEUTIL is:'+#13#10);
  59.   outstring(#13#10);
  60.   outstring('FILEUTIL param [param..] [dr:][\path\]filename [[dr:][\path\]outfile]'+#13#10);
  61.   outstring('  where'+#13#10);
  62.   outstring('    param is'+#13#10);
  63.   outstring('      A - add linefeeds'+#13#10);
  64.   outstring('      L - convert lower'+#13#10);
  65.   outstring('      U - convert upper'+#13#10);
  66.   outstring('      W - convert from WordStar to ASCII'+#13#10);
  67.   outstring('      R - remove extraneous carriage returns'+#13#10);
  68.   outstring('      Q - don''t modify inside quotes'+#13#10);
  69.   outstring('    [param..] are subsequent parameters seperated'+#13#10);
  70.   outstring('      by spaces.  Any combination of parameters'+#13#10);
  71.   outstring('      except L and U or A and W are allowed'+#13#10);
  72.   outstring('    [dr:] is the optional drive with colon'+#13#10);
  73.   outstring('    [\path\] is the optional path'+#13#10);
  74.   outstring('    filename is the filename to make the changes to'+#13#10);
  75.   outstring('      (the program with tell you the options you'+#13#10);
  76.   outstring('      requested).'+#13#10);
  77.   outstring('    [outfile] is the file to output the results to'+#13#10);
  78.   outstring('      (the program will rename filename to filename.BAK'+#13#10);
  79.   outstring('      if no outfile is specified).'+#13#10);
  80. end;
  81.  
  82. function lowcase(ch: char): char;
  83. begin
  84.   if (ord(ch) > 64) and (ord(ch) < 91)
  85.     then
  86.       lowcase := chr(ord(ch)+32)
  87.     else
  88.       lowcase := ch;
  89. end;
  90.  
  91. function exist(filename: name): boolean;
  92. begin
  93.   {$I-}
  94.   assign(filen, filename);
  95.   reset(filen);
  96.   {$I+}
  97.   exist := (IOresult = 0);
  98. end;
  99.  
  100. procedure switch_it(var st: char; w1, w2: boolean);
  101. begin
  102.   if w1
  103.     then
  104.       begin
  105.         if (st = chr(34)) and (not qu) and (not sq)
  106.           then
  107.             qu := true
  108.            else
  109.              if (st = chr(34)) and (qu) and (not sq)
  110.                then
  111.                  qu := false
  112.                 else
  113.                   if (st = chr(39)) and (sq) and (not qu)
  114.                     then
  115.                       sq := false
  116.                     else
  117.                       if (st = chr(39)) and (not sq) and (not qu)
  118.                         then
  119.                           sq := true;
  120.           if not (qu or sq)
  121.             then
  122.               if w2
  123.                 then
  124.                   st := upcase(st)
  125.                 else
  126.                   st := lowcase(st);
  127.       end
  128.     else
  129.       if w2
  130.         then
  131.           st := upcase(st)
  132.         else
  133.           st := lowcase(st);
  134. end;
  135.  
  136. procedure parameters(var ok: boolean);
  137. var
  138.   i,j : integer;
  139. begin
  140.   ok := true;
  141.   if outfile=''
  142.     then
  143.       j := paramcount-1
  144.     else
  145.       j := paramcount-2;
  146.   for i:=1 to j do
  147.     case upcase(paramstr(i)) of
  148.       'A': linefeeds := true;
  149.       'L': lower     := true;
  150.       'U': upper     := true;
  151.       'W': wstar     := true;
  152.       'R': crs       := true;
  153.       'Q': quotes    := true;
  154.     end;
  155.   if (lower and upper)
  156.     then
  157.       ok := false;
  158.   if (linefeeds and wstar)
  159.     then
  160.       ok := false;
  161. end;
  162.  
  163. procedure backup(var fn: name);
  164. var
  165.   f1 : text;
  166.   filename : name;
  167. begin
  168.   filename := fn;
  169.   outstring('  Backing up...');
  170.   if pos('.',filename)>0
  171.     then
  172.       delete(filename,pos('.',filename),4);
  173.   filename := filename+'.bak';
  174.   if exist(filename)
  175.     then
  176.       begin
  177.         assign(f1,filename);
  178.         erase(f1);
  179.         close(f1);
  180.       end;
  181.   assign(f1,fn);
  182.   rename(f1,filename);
  183.   close(f1);
  184.   fn := filename;
  185. end;
  186.  
  187. procedure remove_extraneous(var f1, f2: text);
  188. var
  189.   t : char;
  190. begin
  191.   write(f2,' ');
  192.   readln(f1);
  193.   read(f1,t);
  194.   if (eoln(f1)) or (t=#9)
  195.     then
  196.       writeln(f2);
  197.   if (eoln(f1))
  198.     then
  199.       readln(f1)
  200.     else
  201.       write(f2,t);
  202. end;
  203.  
  204. procedure strip(oldn, newn: name);
  205. var
  206.   f1, f2 : text;
  207.   t1 : char;
  208.   t2 : byte;
  209. begin
  210.   outstring(' reading...');
  211.   assign(f1,oldn);
  212.   reset(f1);
  213.   assign(f2,newn);
  214.   rewrite(f2);
  215.   outstring(' modifying...');
  216.   while not eof(f1)
  217.     begin
  218.       while not eoln(f1)
  219.         begin
  220.           read(f1,t1);
  221.           t2 := ord(t1);
  222.           if t2 > 127
  223.             then
  224.               t2 := t2-128;
  225.           t1 := chr(t2);
  226.           if not (upper or lower)
  227.             then
  228.               write(f2,t1)
  229.             else
  230.               begin
  231.                 if (t1=chr(34)) and (not qu) and (not sq)
  232.                   then
  233.                     qu := true
  234.                   else
  235.                     if (t1=chr(34)) and (qu) and (not sq)
  236.                       then
  237.                         qu := false
  238.                       else
  239.                         if (t1=chr(39)) and (sq) and (not qu)
  240.                           then
  241.                             sq := false
  242.                           else
  243.                             if (t1=chr(39)) and (not sq) and (not qu)
  244.                               then
  245.                                 sq := true
  246.                               else
  247.                                 if (t1=chr(13))
  248.                                   then
  249.                                     begin
  250.                                       qu := false;
  251.                                       sq := false;
  252.                                     end;
  253.                 if not (qu or sq)
  254.                   then
  255.                     if upper
  256.                       then
  257.                         t1:=upcase(t1)
  258.                       else
  259.                         if lower
  260.                           then
  261.                             t1:=lowcase(t1);
  262.                write(f2,t1);
  263.              end;
  264.            if crs
  265.              then
  266.                remove_extraneous(f1,f2)
  267.              else
  268.                begin
  269.                  readln(f1);
  270.                  writeln(f2);
  271.                end;
  272.            end;
  273.     end;
  274.   close(f1);
  275.   close(f2);
  276.   outstring(' done.'+#13#10);
  277. end;
  278.  
  279. procedure modify(oldn, newn: name);
  280. var
  281.   f1, f2 : text;
  282.   temp : char;
  283. begin
  284.   outstring(' reading...');
  285.   assign(f1,oldn);
  286.   reset(f1);
  287.   assign(f2,newn);
  288.   rewrite(f2);
  289.   outstring(' modifying...');
  290.   while not eof(f1)
  291.     begin
  292.       while not eoln(f1)
  293.         begin
  294.           read(f1,temp);
  295.           qu := false;
  296.           sq := false;
  297.           if upper
  298.             then
  299.               if quotes
  300.                 then
  301.                   switch_it(temp,true,true)
  302.                 else
  303.                   switch_it(temp,false,true);
  304.           if lower
  305.             then
  306.               if quotes
  307.                 then
  308.                   switch_it(temp,true,false)
  309.                 else
  310.                   switch_it(temp,false,false);
  311.           write(f2,temp);
  312.         end;
  313.       if crs
  314.         then
  315.           remove_extraneous(f1,f2)
  316.         else
  317.           begin
  318.             readln(f1);
  319.             writeln(f2);
  320.           end;
  321.     end;
  322.   close(f1);
  323.   close(f2);
  324.   outstring(' done.'+#13#10);
  325. end;
  326.  
  327. begin
  328.   linefeeds := false;
  329.   wstar     := false;
  330.   upper     := false;
  331.   lower     := false;
  332.   crs       := false;
  333.   quotes    := false;
  334.   qu        := false;
  335.   sq        := false;
  336.   if (paramcount < 2)
  337.     then
  338.       help
  339.     else
  340.       begin
  341.         filename := paramstr(paramcount-1);
  342.         outfile  := paramstr(paramcount);
  343.         if length(filename)=1
  344.           then
  345.             begin
  346.               filename := outfile;
  347.               outfile  := '';
  348.             end;
  349.           outstring('FILEUTIL ver 2.0 (C)1985 Sarcastic Software  (Use ? for help)'+#13#10);
  350.           outstring(#13#10);
  351.           if exist(filename)
  352.             then
  353.               begin
  354.                 parameters(ok);
  355.                 if not ok
  356.                   then
  357.                     outstring('Invalid parameters.  Use UTIL ? for help'+#13#10)
  358.                   else
  359.                     begin
  360.                       if linefeeds then outstring('Adding linefeeds.'+#13#10);
  361.                       if wstar then outstring('Converting from WordStar to ASCII.'+#13#10);
  362.                       if crs then outstring('Removing extraneous carriage returns.'+#13#10);
  363.                       if upper then outstring('Converting to UPPER case');
  364.                       if lower then outstring('Converying to lower case');
  365.                       if quotes and (upper or lower) then outstring(' except within quotes.'+#13#10);
  366.                       if (upper or lower) and not (quotes) then outstring('.'+#13#10);
  367.                       if outfile=''
  368.                         then
  369.                           begin
  370.                             backfile := filename;
  371.                             backup(backfile);
  372.                           end;
  373.                       if outfile>''
  374.                         then
  375.                           begin
  376.                             outstring('  '+filename+' --> '+outfile+' ... ');
  377.                             backfile := filename;
  378.                             filename := outfile;
  379.                           end;
  380.                       if wstar
  381.                         then
  382.                           strip(backfile, filename)
  383.                         else
  384.                           modify(backfile, filename);
  385.                     end;
  386.               end
  387.             else
  388.               outstring(filename+' not found!'+#13#10);
  389.       end;
  390. end.
  391.  
  392.  
  393.