master hovercats/oakiss / pkg / mpv / file2string.c
 1#include <errno.h>
 2#include <stdint.h>
 3#include <stdio.h>
 4#include <string.h>
 5
 6static const char safe[] =
 7	"][ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#%&'()*+,./:;<=>^_{|}~ -";
 8
 9int main(int argc, char *argv[]) {
10	FILE *f;
11	char *line = NULL, *s;
12	size_t size = 0;
13	ssize_t n;
14
15	f = fopen(argv[1], "r");
16	if (!f) {
17		fprintf(stderr, "fopen %s: %s\n", argv[1], strerror(errno));
18		return 1;
19	}
20
21	printf("/* Generated from %s */\n", argv[1]);
22	while ((n = getline(&line, &size, f)) >= 0) {
23		printf("\"");
24		for (s = line; s < line + n; ++s) {
25			if (memchr(safe, *s, sizeof(safe) - 1))
26				fputc(*s, stdout);
27			else
28				printf("\\%03hho", *s);
29		}
30		printf("\"\n");
31	}
32	if (ferror(f)) {
33		fprintf(stderr, "ferror: %s\n", strerror(errno));
34		return 1;
35	}
36
37	return 0;
38}