home *** CD-ROM | disk | FTP | other *** search
- const
- UNIX_BREAKS = 1;
- DOS_BREAKS = 2;
- MAC_BREAKS = 3;
-
- DEFAULT_BREAKS = DOS_BREAKS;
-
- READ_LENGTH = 4096;
-
- CR = $0D;
- LF = $0A;
- HT = $09;
- FF = $0C;
-
- Function LookAhead(var F: File): Integer;
- var
- Pos : LongInt;
- Len : Integer;
- Ch : Char;
- begin
- Pos := FilePos(F);
- BlockRead(F, ch, 1, Len);
- Seek(F, Pos);
-
- if (Len = 1) then
- LookAhead := Byte(Ch)
- else
- LookAhead := -1;
- end;
-
- Procedure usage(progname: String; msg: String);
- begin
- if (msg <> '') then
- Writeln ('Error: ', msg);
-
- Writeln('Usage: ', progname, ' [options] infile outfile');
- Writeln('Where: infile = name of input file');
- Writeln(' outfile = name of output file');
- Writeln('Options:');
- Writeln(' -d = output MS-DOS line breaks (CR/LF)');
- Writeln(' -m = output Mac line breaks (CR)');
- Writeln(' -u = output UNIX line breaks (LF)');
- Writeln(' -v = verify that input file is plain text');
- Writeln(' -q = suppress "Converting..." message');
- Writeln(' -h = display this help message');
-
- Halt(1);
- end;
-
- type
- ByteBuf = Array[1..1] of Byte;
-
- const
- brname : Array[1..3] of String[10] = ( 'UNIX', 'DOS', 'Mac' );
- breaks : Byte = DEFAULT_BREAKS;
- verify : Boolean = False;
- quiet : Boolean = False;
-
- var
- infn, outfn : String;
- infile, outfile : File;
- progname : String;
- ch : Byte;
- inbuf, outbuf : ^ByteBuf;
- inbuflen : Word;
- outbuflen : Word;
- datalen : Word;
- inplace : Word;
- outplace : Word;
- count : Integer;
- args : String;
- found : Boolean;
-
- begin
- progname := ParamStr(0);
-
- count := 1;
- while ((Copy(ParamStr(count), 1, 1) = '/') or
- (Copy(ParamStr(count), 1, 1) = '-')) do
- begin
- args := ParamStr(count);
- for inplace := 2 to Length(args) do
- case args[inplace] of
- 'u': breaks := UNIX_BREAKS;
- 'd': breaks := DOS_BREAKS;
- 'm': breaks := MAC_BREAKS;
- 'v': verify := True;
- 'q': quiet := True;
- 'h', 'H': usage(progname, '');
- else usage(progname, 'unexpected option');
- end;
- count := count + 1;
- end;
-
- if (count+1 <> ParamCount) then
- usage(progname, '');
-
- infn := ParamStr(count);
- outfn := ParamStr(count+1);
-
- assign(infile, infn);
- {$I-} reset(infile, 1); {$I+}
- if (IoResult <> 0) then
- usage(progname, 'error opening input file');
-
- inbuflen := READ_LENGTH;
- outbuflen := 2*inbuflen;
-
- getmem(inbuf, inbuflen);
- getmem(outbuf, outbuflen);
-
- if (verify) then
- begin
- blockread(infile, inbuf^, inbuflen, datalen);
- found := False;
- inplace := 1;
- while ((inplace <= datalen) and not found) do
- begin
- found := ((inbuf^[inplace] < 32)
- and (inbuf^[inplace] <> CR)
- and (inbuf^[inplace] <> LF)
- and (inbuf^[inplace] <> HT)
- and (inbuf^[inplace] <> FF));
- inplace := inplace+1;
- end;
-
- if (found) then
- begin
- Writeln(progname, ': ', infn,
- ' does not appear to be a text file (', inplace, ')');
- Halt(2);
- end;
-
- seek(infile, 0);
- end;
-
-
- assign(outfile, outfn);
- {$I-} rewrite(outfile, 1); {$I+}
- if (IoResult <> 0) then
- usage(progname, 'error opening output file');
-
- if (not quiet) then
- Writeln('Converting ', infn, ' to ', outfn, ' with ',
- brname[breaks], ' line breaks.');
-
- blockread(infile, inbuf^, inbuflen, datalen);
- while (datalen > 0) do
- begin
- outplace := 0;
- for inplace := 1 to datalen do
- begin
- found := True;
-
- { If this CR is the start of a CR/LF pair, skip it }
- if (inbuf^[inplace] = CR) then
- if (inplace+1 > datalen) then
- begin
- if (lookahead(infile) = LF) then
- found := False;
- end
- else
- if (inbuf^[inplace+1] = LF) then
- found := False;
-
- if (found) then
- begin
- if ((inbuf^[inplace] = CR) or (inbuf^[inplace] = LF)) then
- begin
- outplace := outplace+1;
- if (breaks = UNIX_BREAKS) then
- outbuf^[outplace] := LF
- else
- outbuf^[outplace] := CR;
-
- if (breaks = DOS_BREAKS) then
- begin
- outplace := outplace+1;
- outbuf^[outplace] := LF;
- end;
- end
- else
- begin
- outplace := outplace+1;
- outbuf^[outplace] := inbuf^[inplace];
- end;
- end;
- end;
-
- blockwrite(outfile, outbuf^, outplace, datalen);
- if (datalen <> outplace) then
- Halt(1);
-
- blockread(infile, inbuf^, inbuflen, datalen);
- end;
-
- close(infile);
- close(outfile);
- end.
-
-
-