/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ /* From input file "rembla.p" */ #include /* remove blanks from ends of lines, and trailing blank lines from a file by thomas schneider copyright (c) 1986 module libraries: delman, prgmods */ /* begin module version */ #define 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 */ Static _TEXT fin, fout; /* input and output files */ Static long blankchars; /* the number of blank characters currently counted */ Static long blanklines; /* the number of blank lines currently counted */ Static long totalblankchars; /* the actual number of blanks removed */ Static long index_; /* a counter for blanks and lines */ Static boolean wasblank; /* was a line blank? */ /* begin module plural */ Static Void plural(thefile, number) _TEXT *thefile; long number; { /* if the number is not 1, return an s */ if (number != 1) putc('s', thefile->f); } /* plural */ /* end module plural */ main(argc, argv) int argc; Char *argv[]; { long FORLIM; _TEXT TEMP; PASCAL_MAIN(argc, argv); fout.f = NULL; strcpy(fout.name, "fout"); fin.f = NULL; strcpy(fin.name, "fin"); printf(" rembla %4.2f\n", version); if (*fin.name != '\0') { if (fin.f != NULL) fin.f = freopen(fin.name, "r", fin.f); else fin.f = fopen(fin.name, "r"); } else rewind(fin.f); if (fin.f == NULL) _EscIO2(FileNotFound, fin.name); RESETBUF(fin.f, Char); if (*fout.name != '\0') { if (fout.f != NULL) fout.f = freopen(fout.name, "w", fout.f); else fout.f = fopen(fout.name, "w"); } else { if (fout.f != NULL) rewind(fout.f); else fout.f = tmpfile(); } if (fout.f == NULL) _EscIO2(FileNotFound, fout.name); SETUPBUF(fout.f, Char); 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 (!BUFEOF(fin.f)) { blankchars = 0; wasblank = true; /* copy a line */ while (!P_eoln(fin.f)) { if (P_peek(fin.f) == ' ') blankchars++; else { if (blanklines != 0) { FORLIM = blanklines; for (index_ = 1; index_ <= FORLIM; index_++) putc('\n', fout.f); blanklines = 0; /* reset */ } if (blankchars != 0) { FORLIM = blankchars; for (index_ = 1; index_ <= FORLIM; index_++) putc(' ', fout.f); blankchars = 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); } printf(" %ld blank", totalblankchars); TEMP.f = stdout; *TEMP.name = '\0'; plural(&TEMP, totalblankchars); printf(" removed\n"); printf(" %ld blank line", blanklines); TEMP.f = stdout; *TEMP.name = '\0'; plural(&TEMP, blanklines); printf(" at the end removed\n"); if (fin.f != NULL) fclose(fin.f); if (fout.f != NULL) fclose(fout.f); exit(EXIT_SUCCESS); } /* rembla */ /* End. */