/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ /* From input file "rb.p" */ #include /* remove blanks from ends of lines, and trailing blank lines from a file Dr. Thomas D. Schneider National Cancer Institute Laboratory of Experimental and Computational Biology Frederick, Maryland 21702-1201 toms@ncifcrf.gov permanent email: toms@alum.mit.edu http://www.lecb.ncifcrf.gov/~toms/ module libraries: delman, prgmods */ /* begin module version */ #define version 1.03 #define tab '\t' /* the tab character */ /* of rb.p 2002 Nov 19 2002 Nov 19, 1.03: remove tabs too! 2000 Dec 4, 1.02: use stdin 2000 Mar 31, 1.01: fix synopsis 2000 Jan 10, 1.00: origin from rembla */ /* end module version */ /* begin module describe.rb */ /* name rb: remove blanks from ends of lines in a file synopsis rb(input: stdin, output: out) files input: a text file output: a copy of fin with trailing blanks removed from all lines, any blank lines at the end of the file will also be removed. Tabs are counted as blanks. 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. The original rembla program uses fin and fout files. This rb version uses input and output to allow it to be used in Unix pipes: rb < myinputfile > my outputfile or ... | rb | sort | ... see also {Description of what stdin means: } shell.p {file version:} rembla.p author Thomas D. Schneider bugs none known */ /* end module describe.rb */ /* (* 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 *) */ Static Void themain(fin, fout) _TEXT *fin, *fout; { /* rb */ /* the main procedure of the program */ long blankchars; /* the number of blank characters currently counted */ long tabchars; /* the number of tab characters currently counted */ long blanklines = 0; /* the number of blank lines currently counted */ long totalblankchars = 0; /* the actual number of blanks removed */ long index; /* a counter for blanks and lines */ boolean wasblank; /* was a line blank? */ /* writeln(output,' rb ',version:4:2); */ /* 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 (!BUFEOF(fin->f)) { blankchars = 0; tabchars = 0; wasblank = true; /* copy a line */ while (!P_eoln(fin->f)) { if (P_peek(fin->f) == ' ') blankchars++; else if (P_peek(fin->f) == tab) tabchars++; else { if (blanklines != 0) { for (index = 1; index <= blanklines; index++) putc('\n', fout->f); blanklines = 0; /* reset */ } if (blankchars != 0) { for (index = 1; index <= blankchars; index++) putc(' ', fout->f); blankchars = 0; /* reset */ } if (tabchars != 0) { for (index = 1; index <= tabchars; index++) putc(' ', fout->f); tabchars = 0; /* reset */ } putc(P_peek(fin->f), fout->f); wasblank = false; } getc(fin->f); } /* count the ones left over */ totalblankchars += blankchars; if (wasblank) blanklines++; else putc('\n', fout->f); /* the actual end of that line */ fscanf(fin->f, "%*[^\n]"); getc(fin->f); } /* 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') */ } #undef tab main(argc, argv) int argc; Char *argv[]; { _TEXT TEMP, TEMP1; PASCAL_MAIN(argc, argv); TEMP.f = stdin; *TEMP.name = '\0'; TEMP1.f = stdout; *TEMP1.name = '\0'; themain(&TEMP, &TEMP1); exit(EXIT_SUCCESS); } /* rb */ /* End. */