/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ /* From input file "chacha.p" */ #include /* chacha: character changing program by patrick roche module libraries: delman */ /* end of chacha */ /* begin module version */ #define version 3.12 /* of chacha 1996 July 29 origin 1981 november 8 */ /* end module version */ /* begin module describe.chacha */ /* name chacha: changes characters in a file synopsis chacha(fin: in, fout: out, chachap: in, output: out) files fin: any file in which one wants to translate one set of characters into another set. fout: the file to which the translated copy of fin is written. chachap: the chacha parameter file which contains the translation sets. chachap must only contain 2 lines. the first line contains the characters used in fin, typed one right after the next with no blanks at the beginning. the second line contains the characters that the characters in the first line are to be translated into, typed in the same way and in corresponding order. if you want to change a character to blanks, or vice versa, then you must have the blank character in between other characters in chachap. output: where error messages will appear. description chacha translates characters in a file to a new set of characters. also, more than one character can be translated in one run of the program. examples to convert between double and single quotes, use: '" "' to convert blanks to periods, use: j j j.j in the chachap file. each character on the first line on chachap will be translated into the character directly beneath it on the second line in the output file. documentation delman.assembly.intro and delman.assembly.chacha see also worcha.p author patrick r. roche bugs none known technical notes the maximum number of characters that can be translated is constant top. caution: top is also the maximum line length. */ /* end module describe.chacha */ #define top 100 /* max length of line */ typedef struct buffer { /* holds the characters from chachap */ Char place[top]; /* length of place */ long length; } buffer; Static _TEXT fin; /* the input file to be translated */ Static _TEXT fout; /* the file to which translated fin will be written */ Static _TEXT chachap; /* the parameter file containing the translation definition */ Static buffer chafrom; /* the first line of chachap, characters to "change from" */ Static buffer chato; /* the second line of chachap, characters to "change to" */ Static buffer b; /* line buffer, holds a line from the input file */ 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 = 'delmod 6.51 85 apr 17 tds/gds' */ Static Void readintobuffer(fin, b) _TEXT *fin; buffer *b; { /* read a line into a buffer */ long i = 0; /* index for b.place[i] and counter */ /* starting at begining of the line */ b->length = 0; /* keeping track of the length of the buffer */ while ((i <= top) & (!(P_eoln(fin->f) | BUFEOF(fin->f)))) { /* keeping line length below max and going to end of line */ i++; /* next character */ b->place[i-1] = getc(fin->f); /* read in next character to buffer */ if (b->place[i-1] == '\n') b->place[i-1] = ' '; b->length = i; /* keeping track of length of the buffer */ } if (!BUFEOF(fin->f)) { /* go to beginning of next line */ fscanf(fin->f, "%*[^\n]"); getc(fin->f); } } Static Void removeblanks(b) buffer *b; { /* remove blanks from the end of a buffer */ if (b->length == 0) { printf("\n no blank lines allowed in chachap.\n\n"); /* stop program and notify user */ halt(); } if (b->place[b->length - 1] == ' ') /* if the last place is blank */ { /* remove the blank and check next place etc. */ do { b->length--; } while (b->place[b->length - 1] == ' '); } } Static Void checkchachap() { /* check chachap---the parameter file containing the transdef */ if (chafrom.length <= 0 || chato.length <= 0) { printf("\n no blank lines allowed in chachap.\n\n"); /* stop program and notify user */ halt(); } /* if chachap contains blank lines */ if (chafrom.length == chato.length) /* if lines do not match up */ return; printf("\n the 2 lines in chachap do not contain"); printf(" the same number of characters.\n\n"); /* stop program and notify user */ halt(); } Static Void translate(fin, fout) _TEXT *fin, *fout; { long i; /* index of buffer.places */ Char inptchar; /* inputcharacter */ Char tranchar; /* translated character */ /* read the input char, look for input char in the transdef, if input char is not found copy input char to output, in input char is found write translated char to output. */ while (!BUFEOF(fin->f)) { while (!P_eoln(fin->f)) { inptchar = getc(fin->f); if (inptchar == '\n') inptchar = ' '; i = 0; do { i++; } while (chafrom.place[i-1] != inptchar && chafrom.length != i); if (chafrom.place[i-1] != inptchar) putc(inptchar, fout->f); else { tranchar = chato.place[i-1]; putc(tranchar, fout->f); } } putc('\n', fout->f); /* go to next output line */ /* go to next input line */ fscanf(fin->f, "%*[^\n]"); getc(fin->f); } } main(argc, argv) int argc; Char *argv[]; { PASCAL_MAIN(argc, argv); if (setjmp(_JL1)) goto _L1; chachap.f = NULL; strcpy(chachap.name, "chachap"); fout.f = NULL; strcpy(fout.name, "fout"); fin.f = NULL; strcpy(fin.name, "fin"); printf(" chacha %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 (*chachap.name != '\0') { if (chachap.f != NULL) chachap.f = freopen(chachap.name, "r", chachap.f); else chachap.f = fopen(chachap.name, "r"); } else rewind(chachap.f); if (chachap.f == NULL) _EscIO2(FileNotFound, chachap.name); RESETBUF(chachap.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); readintobuffer(&chachap, &chafrom); removeblanks(&chafrom); readintobuffer(&chachap, &chato); removeblanks(&chato); checkchachap(); translate(&fin, &fout); _L1: if (fin.f != NULL) fclose(fin.f); if (fout.f != NULL) fclose(fout.f); if (chachap.f != NULL) fclose(chachap.f); exit(EXIT_SUCCESS); } /* End. */