home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / bix / linefeed.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-08-04  |  4.3 KB  |  141 lines

  1. {TITLE: GROWING OLD WAITING WHILE YOU STRIP LINEFEEDS OUT OF A FILE?
  2.     Well, have I got the right tool for the job!  The version of 'striplf'
  3. I was using took 2 minutes 21 seconds to strip the linefeeds out of a 13K
  4. file.  The program below takes 9 seconds flat.  That makes it more than 15
  5. times as fast.
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. }
  8. PROGRAM FaStrip;  {Strip Linefeeds from Text Files}
  9. {$P256,G256}
  10.  
  11. {July 20, 1986  --  Bob Brown
  12.  
  13.  This is a program to strip linefeeds from text files; it is functionally
  14.  equivalent to other programs posted on BIX, but it runs substantially
  15.  faster because it uses block I-O.  The 12.5K of buffer will let you grab
  16.  most files suitable for posting on BIX in one gulp.   Use the time you
  17.  save to drink beer!
  18.  All the usual dire warnings about nothing being guaranteed or waranted
  19.  apply here.
  20. }
  21.  
  22. CONST
  23.   NumberBuffers  =  100;   {Number of 128-byte buffers}
  24.   LineFeed       =  10;
  25.  
  26. VAR
  27.   AByte:       BYTE;
  28.   ByteFile:    FILE OF BYTE;
  29.   InFile:      FILE;
  30.   InFileName:  STRING[66];
  31.   OutFile:     FILE;
  32.   OutFileName: STRING[66];
  33.   InBuffer:    ARRAY[1..NumberBuffers,1..128] OF BYTE;
  34.   OutBuffer:   ARRAY[1..NumberBuffers,1..128] OF BYTE;
  35.   OutPtr:      Integer;
  36.   OutBlkCount: INTEGER;
  37.   BlksRead:    Integer;
  38.   BlkCount:    Integer;
  39.   I:           Integer;
  40.   BlksProcessed: INTEGER;
  41.   OutFileSize: REAL;
  42.   InFileSize:  REAL;
  43.   LoWord:      INTEGER;       {For fake 'long' arithmetic}
  44.   HiWord:      INTEGER;
  45.  
  46. PROCEDURE PutByte;
  47. BEGIN {PutByte}
  48.   OutPtr := OutPtr+1;
  49.   OutBuffer[OutBlkCount,OutPtr] := AByte;
  50.   IF (OutPtr = 128) THEN
  51.     BEGIN
  52.       OutBlkCount := OutBlkCount+1;
  53.       OutPtr := 0;
  54.       IF (OutBlkCount > NumberBuffers) THEN
  55.         BEGIN
  56.           BlockWrite(OutFile,OutBuffer,OutBlkCount-1);
  57.           OutBlkCount := 1;
  58.         END;
  59.     END;
  60. END; {PutByte}
  61.  
  62. PROCEDURE Sub1;  {Subtract one from a 'double integer'... we go to this
  63.                   extreme, rather than using REALS, for speed reasons.}
  64. BEGIN
  65.   LoWord := LoWord - 1;
  66.   IF (LoWord < 0) THEN
  67.     BEGIN
  68.       LoWord := 32767;
  69.       HiWord := HiWord - 1;
  70.     END;
  71. END; {Sub1}
  72.  
  73. BEGIN {Main}
  74. InFileName := ParamStr(1);
  75. OutFileName := ParamStr(2);
  76. {$I-}
  77. ASSIGN(ByteFile,InFileName);         {Discover size of input file}
  78. RESET(ByteFile);
  79. IF (IOResult <> 0) THEN
  80.   BEGIN
  81.     WRITELN('Unable to open ',InFileName);
  82.     HALT(1);
  83.   END;
  84. InFileSize := LongFileSize(ByteFile);
  85. CLOSE(ByteFile);
  86. WRITELN('Input file size:  ',InFileSize:8:0,' bytes.');
  87. HiWord := Trunc(InFileSize / 32768.0); {Convert to fake long integer}
  88. LoWord := Trunc(InFileSize - HiWord * 32768.0);
  89. ASSIGN(Infile,InFileName);             {We know we can open it; we just did}
  90. RESET(InFile);
  91. ASSIGN(Outfile,OutFileName);
  92. REWRITE(OutFile);
  93. IF (IOResult <> 0) THEN
  94.   BEGIN
  95.     WRITELN('Unable to open ',InFileName);
  96.     HALT(1);
  97.   END;
  98. {$I+}
  99. OutPtr := 0;
  100. OutBlkCount := 1;
  101. BlksProcessed := 0;
  102. WHILE NOT EOF(InFile) DO
  103.   BEGIN
  104.     BlockRead(InFile,InBuffer,NumberBuffers,BlksRead);
  105.     BlksProcessed := BlksProcessed + BlksRead;
  106.     FOR BlkCount := 1 to BlksRead DO
  107.       BEGIN
  108.         FOR I := 1 to 128 DO
  109.           BEGIN
  110.             Sub1;                          {Decrement infile byte counter}
  111.             AByte := InBuffer[BlkCount,I];
  112.             IF (AByte <> LineFeed) THEN
  113.               BEGIN
  114.                 IF (HiWord >= 0) THEN
  115.                   PutByte;
  116.               END;
  117.           END;
  118.       END;
  119.   END;
  120. CLOSE(InFile);
  121. {This gets a little sneaky.  We want to write out exactly the number of
  122.  bytes needed for the output file because some programs use this number
  123.  rather than looking for an end-of-file marker.  Soooo, in most cases we
  124.  have to cheat.  We write n-1 blocks, close the file, and open it for
  125.  appending as a FILE OF BYTE.  Then we write from one to OutPtr the list
  126.  block.  The only time we don't have to do this is if the output file
  127.  was already an exact multiple of 128 bytes.}
  128.  
  129. IF (OutBlkCount > 1) THEN
  130.    BlockWrite(OutFile,OutBuffer,(OutBlkCount-1));
  131. CLOSE(OutFile);
  132. ASSIGN(ByteFile,OutFileName);
  133. RESET(ByteFile);
  134. LongSeek(ByteFile,LongFileSize(ByteFile));
  135. IF (OutPtr > 0) THEN
  136.   FOR I := 1 to OutPtr DO
  137.     WRITE(ByteFile,OutBuffer[OutBlkCount,I]);
  138. WRITELN('Output file size: ',(LongFileSize(ByteFile)):8:0,' bytes.');
  139. CLOSE(ByteFile);
  140. END.  {Main}
  141.