home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / dos / crlf / crlf.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-06-17  |  4.9 KB  |  202 lines

  1. const 
  2.   UNIX_BREAKS = 1;
  3.   DOS_BREAKS  = 2;
  4.   MAC_BREAKS  = 3;
  5.  
  6.   DEFAULT_BREAKS = DOS_BREAKS;
  7.  
  8.   READ_LENGTH = 4096;
  9.  
  10.   CR = $0D;
  11.   LF = $0A;
  12.   HT = $09;
  13.   FF = $0C;
  14.  
  15. Function LookAhead(var F: File): Integer;
  16. var
  17.   Pos : LongInt;
  18.   Len : Integer;
  19.   Ch  : Char;
  20. begin
  21.   Pos := FilePos(F);
  22.   BlockRead(F, ch, 1, Len);
  23.   Seek(F, Pos);
  24.  
  25.   if (Len = 1) then
  26.     LookAhead := Byte(Ch)
  27.   else
  28.     LookAhead := -1;
  29. end;
  30.  
  31. Procedure usage(progname: String; msg: String);
  32. begin
  33.   if (msg <> '') then
  34.     Writeln ('Error: ', msg);
  35.  
  36.   Writeln('Usage: ', progname, ' [options] infile outfile');
  37.   Writeln('Where: infile = name of input file');
  38.   Writeln('       outfile = name of output file');
  39.   Writeln('Options:');
  40.   Writeln('  -d = output MS-DOS line breaks (CR/LF)');
  41.   Writeln('  -m = output Mac line breaks (CR)');
  42.   Writeln('  -u = output UNIX line breaks (LF)');
  43.   Writeln('  -v = verify that input file is plain text');
  44.   Writeln('  -q = suppress "Converting..." message');
  45.   Writeln('  -h = display this help message');
  46.  
  47.   Halt(1);
  48. end;
  49.  
  50. type
  51.   ByteBuf = Array[1..1] of Byte;
  52.  
  53. const
  54.   brname : Array[1..3] of String[10] = ( 'UNIX', 'DOS', 'Mac' );
  55.   breaks : Byte    = DEFAULT_BREAKS;
  56.   verify : Boolean = False;
  57.   quiet  : Boolean = False;
  58.  
  59. var
  60.   infn, outfn     : String;
  61.   infile, outfile : File;
  62.   progname        : String;
  63.   ch              : Byte;
  64.   inbuf, outbuf   : ^ByteBuf;
  65.   inbuflen        : Word;
  66.   outbuflen       : Word;
  67.   datalen         : Word;
  68.   inplace         : Word;
  69.   outplace        : Word;
  70.   count           : Integer;
  71.   args            : String;
  72.   found           : Boolean;
  73.  
  74. begin
  75.   progname := ParamStr(0);
  76.  
  77.   count := 1;
  78.   while ((Copy(ParamStr(count), 1, 1) = '/') or
  79.        (Copy(ParamStr(count), 1, 1) = '-')) do
  80.     begin
  81.       args := ParamStr(count);
  82.       for inplace := 2 to Length(args) do
  83.         case args[inplace] of
  84.           'u': breaks := UNIX_BREAKS;
  85.           'd': breaks := DOS_BREAKS;
  86.           'm': breaks := MAC_BREAKS;
  87.           'v': verify := True;
  88.           'q': quiet  := True;
  89.           'h', 'H': usage(progname, '');
  90.        else usage(progname, 'unexpected option');
  91.          end;
  92.       count := count + 1;
  93.     end;
  94.  
  95.   if (count+1 <> ParamCount) then
  96.     usage(progname, '');
  97.  
  98.   infn := ParamStr(count);
  99.   outfn := ParamStr(count+1);
  100.  
  101.   assign(infile, infn);
  102.   {$I-} reset(infile, 1); {$I+}
  103.   if (IoResult <> 0) then
  104.     usage(progname, 'error opening input file');
  105.  
  106.   inbuflen := READ_LENGTH;
  107.   outbuflen := 2*inbuflen;
  108.  
  109.   getmem(inbuf, inbuflen);
  110.   getmem(outbuf, outbuflen);
  111.  
  112.   if (verify) then
  113.     begin 
  114.       blockread(infile, inbuf^, inbuflen, datalen);
  115.       found := False;
  116.       inplace := 1;
  117.       while ((inplace <= datalen) and not found) do
  118.         begin
  119.       found := ((inbuf^[inplace] < 32) 
  120.                    and (inbuf^[inplace] <> CR)
  121.              and (inbuf^[inplace] <> LF)
  122.                and (inbuf^[inplace] <> HT)
  123.              and (inbuf^[inplace] <> FF));
  124.           inplace := inplace+1;
  125.         end;
  126.  
  127.       if (found) then
  128.         begin
  129.           Writeln(progname, ': ', infn, 
  130.                   ' does not appear to be a text file (', inplace, ')');
  131.       Halt(2);
  132.         end;
  133.  
  134.       seek(infile, 0);
  135.     end;
  136.  
  137.  
  138.   assign(outfile, outfn);
  139.   {$I-} rewrite(outfile, 1); {$I+}
  140.   if (IoResult <> 0) then
  141.     usage(progname, 'error opening output file');
  142.  
  143.   if (not quiet) then
  144.     Writeln('Converting ', infn, ' to ', outfn, ' with ',
  145.              brname[breaks], ' line breaks.');
  146.  
  147.   blockread(infile, inbuf^, inbuflen, datalen);
  148.   while (datalen > 0) do
  149.     begin
  150.       outplace := 0;
  151.       for inplace := 1 to datalen do
  152.         begin
  153.       found := True;
  154.  
  155.       { If this CR is the start of a CR/LF pair, skip it }
  156.           if (inbuf^[inplace] = CR) then
  157.         if (inplace+1 > datalen) then
  158.           begin 
  159.               if (lookahead(infile) = LF) then
  160.               found := False;
  161.               end
  162.             else 
  163.               if (inbuf^[inplace+1] = LF) then
  164.                 found := False;
  165.  
  166.       if (found) then
  167.             begin
  168.               if ((inbuf^[inplace] = CR) or (inbuf^[inplace] = LF)) then 
  169.                 begin
  170.                   outplace := outplace+1;
  171.               if (breaks = UNIX_BREAKS) then
  172.                 outbuf^[outplace] := LF
  173.                   else
  174.                 outbuf^[outplace] := CR;
  175.  
  176.               if (breaks = DOS_BREAKS) then 
  177.                     begin
  178.               outplace := outplace+1;
  179.                   outbuf^[outplace] := LF;
  180.                     end;
  181.                 end
  182.               else
  183.                 begin
  184.           outplace := outplace+1;
  185.           outbuf^[outplace] := inbuf^[inplace];
  186.                 end;
  187.             end;
  188.         end;
  189.  
  190.       blockwrite(outfile, outbuf^, outplace, datalen);
  191.       if (datalen <> outplace) then
  192.         Halt(1);
  193.  
  194.       blockread(infile, inbuf^, inbuflen, datalen);
  195.     end;
  196.  
  197.   close(infile);
  198.   close(outfile);
  199. end.
  200.  
  201.  
  202.