home *** CD-ROM | disk | FTP | other *** search
- {TITLE: GROWING OLD WAITING WHILE YOU STRIP LINEFEEDS OUT OF A FILE?
- Well, have I got the right tool for the job! The version of 'striplf'
- I was using took 2 minutes 21 seconds to strip the linefeeds out of a 13K
- file. The program below takes 9 seconds flat. That makes it more than 15
- times as fast.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- }
- PROGRAM FaStrip; {Strip Linefeeds from Text Files}
- {$P256,G256}
-
- {July 20, 1986 -- Bob Brown
-
- This is a program to strip linefeeds from text files; it is functionally
- equivalent to other programs posted on BIX, but it runs substantially
- faster because it uses block I-O. The 12.5K of buffer will let you grab
- most files suitable for posting on BIX in one gulp. Use the time you
- save to drink beer!
- All the usual dire warnings about nothing being guaranteed or waranted
- apply here.
- }
-
- CONST
- NumberBuffers = 100; {Number of 128-byte buffers}
- LineFeed = 10;
-
- VAR
- AByte: BYTE;
- ByteFile: FILE OF BYTE;
- InFile: FILE;
- InFileName: STRING[66];
- OutFile: FILE;
- OutFileName: STRING[66];
- InBuffer: ARRAY[1..NumberBuffers,1..128] OF BYTE;
- OutBuffer: ARRAY[1..NumberBuffers,1..128] OF BYTE;
- OutPtr: Integer;
- OutBlkCount: INTEGER;
- BlksRead: Integer;
- BlkCount: Integer;
- I: Integer;
- BlksProcessed: INTEGER;
- OutFileSize: REAL;
- InFileSize: REAL;
- LoWord: INTEGER; {For fake 'long' arithmetic}
- HiWord: INTEGER;
-
- PROCEDURE PutByte;
- BEGIN {PutByte}
- OutPtr := OutPtr+1;
- OutBuffer[OutBlkCount,OutPtr] := AByte;
- IF (OutPtr = 128) THEN
- BEGIN
- OutBlkCount := OutBlkCount+1;
- OutPtr := 0;
- IF (OutBlkCount > NumberBuffers) THEN
- BEGIN
- BlockWrite(OutFile,OutBuffer,OutBlkCount-1);
- OutBlkCount := 1;
- END;
- END;
- END; {PutByte}
-
- PROCEDURE Sub1; {Subtract one from a 'double integer'... we go to this
- extreme, rather than using REALS, for speed reasons.}
- BEGIN
- LoWord := LoWord - 1;
- IF (LoWord < 0) THEN
- BEGIN
- LoWord := 32767;
- HiWord := HiWord - 1;
- END;
- END; {Sub1}
-
- BEGIN {Main}
- InFileName := ParamStr(1);
- OutFileName := ParamStr(2);
- {$I-}
- ASSIGN(ByteFile,InFileName); {Discover size of input file}
- RESET(ByteFile);
- IF (IOResult <> 0) THEN
- BEGIN
- WRITELN('Unable to open ',InFileName);
- HALT(1);
- END;
- InFileSize := LongFileSize(ByteFile);
- CLOSE(ByteFile);
- WRITELN('Input file size: ',InFileSize:8:0,' bytes.');
- HiWord := Trunc(InFileSize / 32768.0); {Convert to fake long integer}
- LoWord := Trunc(InFileSize - HiWord * 32768.0);
- ASSIGN(Infile,InFileName); {We know we can open it; we just did}
- RESET(InFile);
- ASSIGN(Outfile,OutFileName);
- REWRITE(OutFile);
- IF (IOResult <> 0) THEN
- BEGIN
- WRITELN('Unable to open ',InFileName);
- HALT(1);
- END;
- {$I+}
- OutPtr := 0;
- OutBlkCount := 1;
- BlksProcessed := 0;
- WHILE NOT EOF(InFile) DO
- BEGIN
- BlockRead(InFile,InBuffer,NumberBuffers,BlksRead);
- BlksProcessed := BlksProcessed + BlksRead;
- FOR BlkCount := 1 to BlksRead DO
- BEGIN
- FOR I := 1 to 128 DO
- BEGIN
- Sub1; {Decrement infile byte counter}
- AByte := InBuffer[BlkCount,I];
- IF (AByte <> LineFeed) THEN
- BEGIN
- IF (HiWord >= 0) THEN
- PutByte;
- END;
- END;
- END;
- END;
- CLOSE(InFile);
- {This gets a little sneaky. We want to write out exactly the number of
- bytes needed for the output file because some programs use this number
- rather than looking for an end-of-file marker. Soooo, in most cases we
- have to cheat. We write n-1 blocks, close the file, and open it for
- appending as a FILE OF BYTE. Then we write from one to OutPtr the list
- block. The only time we don't have to do this is if the output file
- was already an exact multiple of 128 bytes.}
-
- IF (OutBlkCount > 1) THEN
- BlockWrite(OutFile,OutBuffer,(OutBlkCount-1));
- CLOSE(OutFile);
- ASSIGN(ByteFile,OutFileName);
- RESET(ByteFile);
- LongSeek(ByteFile,LongFileSize(ByteFile));
- IF (OutPtr > 0) THEN
- FOR I := 1 to OutPtr DO
- WRITE(ByteFile,OutBuffer[OutBlkCount,I]);
- WRITELN('Output file size: ',(LongFileSize(ByteFile)):8:0,' bytes.');
- CLOSE(ByteFile);
- END. {Main}
-