program rembla(fin, fout, output); (* remove blanks from ends of lines, and trailing blank lines from a file by thomas schneider copyright (c) 1986 module libraries: delman, prgmods *) const (* begin module version *) version = 2.10; (* of rembla.p 2000 Jan 10 2000 Jan 10: clone rb from rembla 1995 dec 21: modularize the plural procedure, remove unlimitln 1986 dec 12: next change (unknown) origin 1982 sep 5 *) (* end module version *) (* begin module describe.rembla *) (* name rembla: remove blanks from ends of lines in a file synopsis rembla(fin: in, fout: out, output: out) files fin: a text file fout: a copy of fin with trailing blanks removed from all lines, any blank lines at the end of the file will also be removed. output: messages to the user description blanks can creep onto the end of lines in a file without one knowing it, either by the computer system, from transportation or an editor. this program removes those blanks, so that less storage is needed for the file. some programs require that there be no blank lines in the file, yet transportation can generate blank lines at the end of the file. this program will remove such lines. see also {Clone of this program using input and output:} rb.p author thomas d. schneider bugs none known *) (* end module describe.rembla *) var fin, fout: text; (* input and output files *) blankchars, (* the number of blank characters currently counted *) blanklines, (* the number of blank lines currently counted *) totalblankchars, (* the actual number of blanks removed *) index: (* a counter for blanks and lines *) integer; wasblank: boolean; (* was a line blank? *) (* begin module plural *) procedure plural(var thefile: text; number: integer); (* if the number is not 1, return an s *) begin (* plural *) if number <> 1 then write(thefile,'s') end; (* plural *) (* end module plural *) begin (* rembla *) writeln(output,' rembla ',version:4:2); reset(fin); rewrite(fout); blanklines := 0; totalblankchars := 0; (* the mechanism of the program is very simple: copy fin to fout. when blanks appear, do not write them to fout, but keep track of how many there are. if another character appears on the line, then first write any blanks out before the character. if no other characters appear on the line, we forget to write out the blanks... the same principle is applied to lines, so that extra blank lines at the end of the file will not be written out. *) while not eof(fin) do begin blankchars := 0; wasblank := true; (* copy a line *) while not eoln(fin) do begin if fin^ = ' ' then blankchars := succ(blankchars) else begin if blanklines <> 0 then begin for index := 1 to blanklines do writeln(fout); blanklines := 0 (* reset *) end; if blankchars <> 0 then begin for index := 1 to blankchars do write(fout,' '); blankchars := 0 (* reset *) end; write(fout, fin^); wasblank := false end; get(fin) end; (* count the ones left over *) totalblankchars := totalblankchars + blankchars; if wasblank then blanklines := succ(blanklines) else writeln(fout); (* the actual end of that line *) readln(fin) end; write(output,' ',totalblankchars:1,' blank'); plural(output, totalblankchars); writeln(output,' removed'); write(output,' ',blanklines:1,' blank line'); plural(output, blanklines); writeln(output,' at the end removed') end. (* rembla *)