summaryrefslogtreecommitdiff
path: root/pkg/mpv/file2string.c
blob: 84f31ce118d232b1ad153fdd85da9fcd59722907 (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
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

static const char safe[] =
	"][ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#%&'()*+,./:;<=>^_{|}~ -";

int main(int argc, char *argv[]) {
	FILE *f;
	char *line = NULL, *s;
	size_t size = 0;
	ssize_t n;

	f = fopen(argv[1], "r");
	if (!f) {
		fprintf(stderr, "fopen %s: %s\n", argv[1], strerror(errno));
		return 1;
	}

	printf("/* Generated from %s */\n", argv[1]);
	while ((n = getline(&line, &size, f)) >= 0) {
		printf("\"");
		for (s = line; s < line + n; ++s) {
			if (memchr(safe, *s, sizeof(safe) - 1))
				fputc(*s, stdout);
			else
				printf("\\%03hho", *s);
		}
		printf("\"\n");
	}

	return 0;
}