-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirp2xml.c
105 lines (86 loc) · 1.7 KB
/
irp2xml.c
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
//#include "item_names.h"
static int read_int(FILE *fp)
{
int i = 0;
fread(&i, 4, 1, fp);
return i;
}
static void indent(FILE *fp, int ind)
{
for (int i = 0; i < ind; i++)
{
fputc('\t', fp);
}
}
static int print_int(FILE *ifp, FILE *ofp, int ind, const char *name)
{
int i = read_int(ifp);
indent(ofp, ind);
fprintf(ofp, "<i32 name=\"%s\" value=\"%d\"/>\n", name, i);
return i;
}
static int irp2xml(FILE *ifp, char *file)
{
if (read_int(ifp) != 7369321)
{
printf("Bad magic in file %s\n", file);
return 1;
}
char path[PATH_MAX];
snprintf(path, PATH_MAX, "%s.xml", file);
FILE *ofp = fopen(path, "w");
if (!ofp)
{
perror(path);
return 1;
}
fprintf(ofp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
fprintf(ofp, "<irp magic=\"0x707269\">\n");
print_int(ifp, ofp, 1, "version");
int n = read_int(ifp);
indent(ofp, 1);
fprintf(ofp, "<array count=\"%d\">\n", n);
for (int i = 0; i < n; i++)
{
indent(ofp, 2);
fprintf(ofp, "<class index=\"%d\">\n", i);
print_int(ifp, ofp, 3, "type");
print_int(ifp, ofp, 3, "min1");
print_int(ifp, ofp, 3, "max1");
print_int(ifp, ofp, 3, "min2");
print_int(ifp, ofp, 3, "max2");
print_int(ifp, ofp, 3, "min3");
print_int(ifp, ofp, 3, "max3");
indent(ofp, 2);
fprintf(ofp, "</class>\n");
}
indent(ofp, 1);
fprintf(ofp, "</array>\n");
fprintf(ofp, "</irp>\n");
fclose(ofp);
return 0;
}
int main(int argc, char **argv)
{
int ret = 0;
for (int i = 1; i < argc; i++)
{
FILE *fp = fopen(argv[i], "rb");
if (!fp)
{
perror(argv[i]);
ret = 1;
continue;
}
if (irp2xml(fp, argv[i]))
{
ret = 1;
}
fclose(fp);
}
return ret;
}