aboutsummaryrefslogtreecommitdiffstats
path: root/ste/ste.c
blob: 43252b9f008fa8a61a3cf9d53ead964d763d15e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}