diff options
Diffstat (limited to 'ste/ste.c')
-rw-r--r-- | ste/ste.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/ste/ste.c b/ste/ste.c new file mode 100644 index 0000000..43252b9 --- /dev/null +++ b/ste/ste.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <string.h> + +void process_files(FILE* in, FILE* out, const char *macro) { + enum {MODESTR = 0, MODESRC = 1} a = MODESTR; + fprintf(out, "%s(\"", macro); + for (;;) { + int c = fgetc(in); + if (feof(in)) break; + if (c == '%') { + a = !a; + if (a == MODESTR) fprintf(out, " %s(\"", macro); + else fputs("\");", out); + continue; + } + if (a == MODESTR) fprintf(out, "\\x%02X", c); + else (fputc(c, out)); + } + if (a == MODESTR) fputs("\");", out); +} + +int main(int argc, char **argv) { + const char *macro = "OUT"; + char *filename = NULL; + FILE *in, *out; + + if (argc < 2) { + usage: + fprintf(stderr, "Usage: %s [-m name] <file.ste>\n", *argv); + return 1; + } + + for (int i = 0; i < argc; ++i) { + if (!strcmp("-m", argv[i])) macro = argv[++i]; + else filename = argv[i]; + } + if (!filename || strlen(filename) <= 4) goto usage; + + in = fopen(filename, "r"); + for (char *fn = filename; fn[3]; ++fn) { + if (!strncmp(".ste", fn, 4)) { + *fn = '\0'; + break; + } + } + out = fopen(filename, "w"); + if (!in || !out) { + perror("fopen"); + goto usage; + } + + process_files(in, out, macro); + + fclose(in); + fclose(out); + return 0; +} |