summary refs log tree commit diff
path: root/misc/vita/gxp_to_c.c
blob: 2fe842222f0e5d32691dd2c243288b2989590171 (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
#include <dirent.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Inspired by the public domain bin2c https://github.com/gwilymk/bin2c
// TODO: log errors
void convert_gxp(const char* src) {
	char* copy = strdup(src);
	char* name = strtok(copy, ".");
	char dst[256];
	sprintf(dst, "%s.h", name);	
	printf("  %s --> %s\n", src, dst);

	FILE* file_in  = fopen(src, "r");
	if (!file_in) return;

	FILE* file_out = fopen(dst, "w");
	if (!file_out) return;
	
	fseek(file_in, 0, SEEK_END);
	int file_size = ftell(file_in);
	fseek(file_in, 0, SEEK_SET);

	char* data = malloc(file_size);
	fread(data, file_size, 1, file_in);
	fclose(file_in);

	int comma = 0;
	fprintf(file_out, "const char %s[%i] = {", name, file_size);
	
	for (int i = 0; i < file_size; i++)
	{
		if (comma) fprintf(file_out, ", ");
		if ((i % 16) == 0) fprintf(file_out, "\n\t");
			
		fprintf(file_out, "0x%.2x", data[i] & 0xFF);
		comma = 1;
	}
	
	fprintf(file_out, "\n};");
	fclose(file_out);
}

int main(void) {
	struct dirent* e;
	DIR* d = opendir(".");
	if (!d) return 0;

	while ((e = readdir(d))) {
		printf("checking %s\n", e->d_name);
		
		if (strstr(e->d_name, ".gxp")) {
			convert_gxp(e->d_name);
		}
	}
	
	closedir(d);
	return 0;
}