/* File : tag.c Author : Richard A. O'Keefe Updated: 9/10/99 Purpose: Tag a model file for a programming contest. USAGE pcfptag [-%] RawModel >Model The option (shown as %) is the magic character to use, if it is not $. RawModel is the name of a readable text file. It is copied to Model, - adding $= at the front if the magic character is not $ - replacing by $ - replacing by s - replacing by #! */ #include #include char const prog[] = "pcfptag"; int magic = '$'; void tag(char const *name) { FILE *o = stdout; FILE *f; int m = magic; int c; if (name[0] == '-' && name[1] == '\0') { f = stdin; } else { f = fopen(name, "r"); if (f == 0) { fprintf(stderr, "%s: can't read '%s'\n", prog, name); exit(EXIT_FAILURE); } } while ((c = getc(f)) >= 0) { if (c == m) { putc(m, o); putc('$', o); } else if (c == '\n') { putc(m, o); putc(' ', o); putc(c, o); } else { putc(c, o); } } if (!feof(f)) { fprintf(stderr, "%s: I/O error reading '%s'\n", prog, name); exit(EXIT_FAILURE); } putc(m, o); putc('#', o); putc(m, o); putc('!', o); putc('\n', o); if (f != stdin) fclose(f); } int main(int argc, char **argv) { switch (argc) { case 3: printf("%c", magic); magic = argv[1][0] == '-' && argv[1][1] != '\0' ? argv[1][1] : argv[1][0]; printf("%d=%c! set magic to %c\n", magic, magic, magic); tag(argv[2]); break; case 2: tag(argv[1]); break; case 1: tag("-"); break; default: fprintf(stderr, "Usage: %s [-%%] Raw >Model\n", prog); exit(EXIT_FAILURE); } return EXIT_SUCCESS; }