~sircmpwn/koio

ref: ad89ac71c705ec0d7a6879b332d3013a150a0e66 koio/tool/main.c -rw-r--r-- 2.7 KiB
ad89ac71Seth Hall Support zero length files 4 years ago
                                                                                
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#define _POSIX_C_SOURCE 200112L
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>

static void print_usage(const char *name) {
	fprintf(stdout, "Usage: %s -o output.c [flags...] [assets...]\n", name);
}

int main(int argc, char **argv) {
	const char *deinit = "koio_unload_assets";
	const char *init = "koio_load_assets";
	const char *output = NULL;

	int c;
	while ((c = getopt(argc, argv, "hd:i:o:")) != -1) {
		switch (c) {
		case 'h':
			print_usage(argv[0]);
			return 0;
		case 'd':
			deinit = optarg;
			break;
		case 'i':
			init = optarg;
			break;
		case 'o':
			output = optarg;
			break;
		default:
			print_usage(argv[0]);
			return 1;
		}
	}

	if (!output) {
		fprintf(stderr, "Must specify an output file.\n");
		return 1;
	}

	FILE *out;
	if (strcmp(output, "-") == 0) {
		out = stdout;
	} else {
		out = fopen(output, "w");
		if (!out) {
			fprintf(stderr, "Unable to open '%s' for writing: %s\n",
					output, strerror(errno));
			return 1;
		}
	}

	fprintf(out, "#include <koio.h>\n\n");
	fprintf(out,
			"static struct {\n"
			"	const char *path;\n"
			"	size_t len;\n"
			"	char *data;\n"
			"} files[] = {\n");

	for (int i = optind; i < argc; ++i) {
		char *real_path = argv[i],
			*virt_path = argv[i];
		char *colon = strchr(argv[i], ':');
		if (colon) {
			*colon = '\0';
			virt_path = colon + 1;
		}
		FILE *in = fopen(real_path, "r");
		if (!in) {
			fprintf(stderr, "Unable to open '%s' for reading: %s\n",
					real_path, strerror(errno));
		}
		fseek(in, 0, SEEK_END);
		size_t len = ftell(in);
		fseek(in, 0, SEEK_SET);
		fprintf(out,
				"	{\n"
				"		.path = \"%s\",\n"
				"		.len = %zu,\n"
				"		.data = \"\"", virt_path, len);
		int cols = 1;
		int c;
		while ((c = fgetc(in)) != EOF) {
			if (cols == 1) {
				fprintf(out, "\n\"");
			}
			switch (c) {
			case '\\':
				cols += fprintf(out, "\\\\");
				break;
			case '\"':
				cols += fprintf(out, "\\\"");
				break;
			case '\n':
				cols += fprintf(out, "\\n");
				break;
			case '\t':
				cols += fprintf(out, "\\t");
				break;
			default:
				if (isprint(c)) {
					cols += fprintf(out, "%c", c);
				} else {
					cols += fprintf(out, "\\x%02X", c);
				}
				break;
			}
			if (cols >= 79) {
				cols = 1;
				fprintf(out, "\"");
			}
		}
		if (cols != 1) {
			fprintf(out, "\"");
		}
		fprintf(out,
				",\n"
				"	},\n");
	}
	fprintf(out, "};\n\n");

	fprintf(out, "void %s(void) {\n", init);
	for (int i = 0; i < argc - optind; ++i) {
		fprintf(out, "	ko_add_file(files[%d].path, "
				"files[%d].data, files[%d].len);\n", i, i, i);
	}
	fprintf(out, "}\n\n");

	fprintf(out, "void %s(void) {\n", deinit);
	for (int i = 0; i < argc - optind; ++i) {
		fprintf(out, "	ko_del_file(files[%d].path);\n", i);
	}
	fprintf(out, "}\n");

	fclose(out);
	return 0;
}