/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ /* From input file "center.p" */ #include /* center: center text lines Tom Schneider NCI/FCRDC Bldg 469. Room 144 P.O. Box B Frederick, MD 21702-1201 (301) 846-5581 (-5532 for messages) network address: toms@ncifcrf.gov National Cancer Institute Laboratory of Mathematical Biology 1994 */ /* end of program */ /* begin module version */ #define version 1.08 /* of center.p 1994 April 15 origin 1994 April 15 */ /* end module version */ /* begin module describe.center */ /* name center: center text lines synopsis center(input: in, centerp: in, output: out) files input: A text file to be centered centerp: one integer: location of the center in characters output: The centered text file description The program reads lines of text and outputs them centered around whatever point requested. If the text is too long, it is left alone. examples with centering at 10, these lines of text will become: these lines of text will become: documentation see also author Thomas Dana Schneider bugs The longest possible line is defined by constant maxstring. technical notes */ /* end module describe.center */ /* begin module interact.const */ #define maxstring 150 /* the maximum string */ /* end module interact.const version = 4.12; (@ of prgmod.p 1993 Mar 26 */ /* begin module interact.type */ typedef struct string { /* a string of characters */ Char letters[maxstring]; /* the letters in the string */ long length; /* the number of characters in the string */ long current; /* the letter we are working on */ } string; /* end module interact.type version = 4.12; (@ of prgmod.p 1993 Mar 26 */ Static _TEXT centerp; /* a file used by this program */ Static jmp_buf _JL1; /* begin module halt */ Static Void halt() { /* stop the program. the procedure performs a goto to the end of the program. you must have a label: label 1; declared, and also the end of the program must have this label: 1: end. examples are in the module libraries. this is the only goto in the delila system. */ printf(" program halt.\n"); longjmp(_JL1, 1); } /* end module halt version = 4.12; (@ of prgmod.p 1993 Mar 26 */ /* begin module interact.clearstring */ Static Void clearstring(ribbon) string *ribbon; { /* empty the string */ long index; /* to the ribbon */ for (index = 0; index < maxstring; index++) ribbon->letters[index] = ' '; ribbon->length = 0; ribbon->current = 0; } /* clearstring */ /* end module interact.clearstring version = 4.12; (@ of prgmod.p 1993 Mar 26 */ /* begin module interact.getstring */ Static Void getstring(afile, buffer, gotten) _TEXT *afile; string *buffer; boolean *gotten; { /* get a string from a file not using string calls. this lets one obtain lines from a file without interactive prompts */ long index = 0; /* of buffer */ clearstring(buffer); if (BUFEOF(afile->f)) { *gotten = false; return; } while (!P_eoln(afile->f) && index < maxstring) { index++; buffer->letters[index-1] = getc(afile->f); if (buffer->letters[index-1] == '\n') buffer->letters[index-1] = ' '; } if (!P_eoln(afile->f)) { printf(" getstring: a line exceeds maximum string size (%ld)\n", (long)maxstring); halt(); } buffer->length = index; buffer->current = 1; fscanf(afile->f, "%*[^\n]"); getc(afile->f); *gotten = true; } /* getstring */ /* end module interact.getstring version = 4.12; (@ of prgmod.p 1993 Mar 26 */ /* begin module interact.writestring */ Static Void writestring(tofile, s) _TEXT *tofile; string *s; { /* write the string s to file tofile, no writeln */ long i; /* index to s */ long FORLIM; FORLIM = s->length; for (i = 0; i < FORLIM; i++) putc(s->letters[i], tofile->f); } /* writestring */ #define debug false /* whether to debug the program */ /* end module interact.writestring version = 4.12; (@ of prgmod.p 1993 Mar 26 */ /* begin module center.themain */ Static Void themain(centerp) _TEXT *centerp; { /* the main procedure of the program */ long b; /* index to buffer */ string buffer; /* a line of text */ boolean done; /* done operating on the line */ long first; /* first non blank character on the line */ boolean gotten; /* whether we got the line of text */ long last; /* last non blank character on the line */ long length; /* length of the buffer that is surrounded by blanks */ long thecenter; /* the requested center of the text */ _TEXT TEMP; long FORLIM; if (debug) printf("center %4.2f\n", version); if (*centerp->name != '\0') { if (centerp->f != NULL) centerp->f = freopen(centerp->name, "r", centerp->f); else centerp->f = fopen(centerp->name, "r"); } else rewind(centerp->f); if (centerp->f == NULL) _EscIO2(FileNotFound, centerp->name); RESETBUF(centerp->f, Char); fscanf(centerp->f, "%ld%*[^\n]", &thecenter); getc(centerp->f); if (debug) printf("centering at %ld\n", thecenter); while (!P_eof(stdin)) { TEMP.f = stdin; *TEMP.name = '\0'; getstring(&TEMP, &buffer, &gotten); if (!gotten) continue; if (buffer.length == 0) putchar('\n'); else { if (debug) putchar('"'); if (debug) { TEMP.f = stdout; *TEMP.name = '\0'; writestring(&TEMP, &buffer); } if (debug) printf("\"\n"); /* find the first non blank characters in the string */ first = 1; done = false; while (!done) { if (buffer.letters[first-1] == ' ') first++; else done = true; if (first >= buffer.length) done = true; } /* find the last non blank characters in the string */ last = buffer.length; done = false; while (!done) { if (buffer.letters[last-1] == ' ') last--; else done = true; if (last <= 1) done = true; } if (debug) printf("first: %ld\n", first); if (debug) printf("last: %ld\n", last); if (debug) putchar(' '); if (debug) { for (b = 1; b <= buffer.length; b++) { if (b == first) putchar('F'); else if (b == last) putchar('L'); else putchar(' '); } } if (debug) printf(" \n"); if (debug) putchar('"'); if (debug) { TEMP.f = stdout; *TEMP.name = '\0'; writestring(&TEMP, &buffer); } if (debug) printf("\"\n"); length = last - first + 1; if (debug) printf("length = %ld\n", length); if (length > 0) { FORLIM = thecenter - (long)floor(length / 2.0 + 0.5); /* put leading blanks as needed */ for (b = 1; b <= FORLIM; b++) putchar(' '); /* reduce buffer to remove final blanks: */ buffer.length = last; if (debug) putchar('"'); for (b = first - 1; b < last; b++) putchar(buffer.letters[b]); if (debug) putchar('"'); } putchar('\n'); } if (debug) printf("--------------------------------------\n"); } } #undef debug /* end module center.themain */ main(argc, argv) int argc; Char *argv[]; { PASCAL_MAIN(argc, argv); if (setjmp(_JL1)) goto _L1; centerp.f = NULL; strcpy(centerp.name, "centerp"); themain(¢erp); _L1: if (centerp.f != NULL) fclose(centerp.f); exit(EXIT_SUCCESS); } /* End. */