summaryrefslogtreecommitdiff
path: root/array.c
blob: 6ad54db2f35ba6cdefce90ac12e95ea7ac537d51 (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
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
#define _POSIX_C_SOURCE 2
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define CHARS_PER_LINE 8

int make_array(const char *path, int include_size)
{
	FILE *f = stdin;

	if (path && strcmp(path, "-") != 0) {
		f = fopen(path, "rb");
		if (f == NULL) {
			perror(path);
			return 1;
		}
	} else {
		path = "stdin";
	}

	size_t plen = strlen(path);
	char id[plen + 1];
	for (size_t i = 0; i < plen; i++) {
		id[i] = isalnum(path[i]) ? path[i] : '_';
	}
	id[plen] = '\0';

	printf("unsigned char %s[] = {\n", id);

	int c;
	int i = 0;
	while ((c = fgetc(f)) != EOF) {
		if (i == 0) {
			putchar('\t');
		}
		printf("0x%02hhx, ", c);
		if (++i == CHARS_PER_LINE) {
			putchar('\n');
			i = 0;
		}
	}

	if (i != 0) {
		putchar('\n');
	}

	printf("};\n");

	if (include_size) {
		printf("size_t %s_size = sizeof(%s);\n", id, id);
	}

	if (f != stdin) {
		fclose(f);
	}

	return 0;
}


int main(int argc, char *argv[])
{
	int include_size = 0;
	int c;

	while ((c = getopt(argc, argv, "s")) != -1) {
		switch (c) {
		case 's':
			include_size = 1;
			break;

		default:
			return 1;
		}
	}

	int ret = 0;
	do {
		ret |= make_array(argv[optind++], include_size);
	} while (optind < argc);

	return ret;
}