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
|
/***************************************************************************
file2str.c
Simple file to string converter.
Copyright Nicola Salmoria and the MAME Team.
Visit http://mamedev.org for licensing and usage restrictions.
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
/*-------------------------------------------------
main - primary entry point
-------------------------------------------------*/
int main(int argc, char *argv[])
{
const char *srcfile, *dstfile, *varname, *type;
FILE *src, *dst;
unsigned char *buffer;
int bytes, offs;
int terminate = 1;
/* needs at least three arguments */
if (argc < 4)
{
fprintf(stderr,
"Usage:\n"
" laytostr <source.lay> <output.h> <varname> [<type>]\n"
"\n"
"The default <type> is char, with an assumed NULL terminator\n"
);
return 0;
}
/* extract arguments */
srcfile = argv[1];
dstfile = argv[2];
varname = argv[3];
type = (argc >= 5) ? argv[4] : "char";
if (argc >= 5)
terminate = 0;
/* open source file */
src = fopen(srcfile, "rb");
if (src == NULL)
{
fprintf(stderr, "Unable to open source file '%s'\n", srcfile);
return 1;
}
/* determine file size */
fseek(src, 0, SEEK_END);
bytes = ftell(src);
fseek(src, 0, SEEK_SET);
/* allocate memory */
buffer = (unsigned char *)malloc(bytes + 1);
if (buffer == NULL)
{
fprintf(stderr, "Out of memory allocating %d byte buffer\n", bytes);
return 1;
}
/* read the source file */
fread(buffer, 1, bytes, src);
buffer[bytes] = 0;
fclose(src);
/* open dest file */
dst = fopen(dstfile, "w");
if (dst == NULL)
{
fprintf(stderr, "Unable to open output file '%s'\n", dstfile);
return 1;
}
/* write the initial header */
fprintf(dst, "extern const %s %s[];\n", type, varname);
fprintf(dst, "const %s %s[] =\n{\n\t", type, varname);
/* write out the data */
for (offs = 0; offs < bytes + terminate; offs++)
{
fprintf(dst, "0x%02x%s", buffer[offs], (offs != bytes + terminate - 1) ? "," : "");
if (offs % 16 == 15)
fprintf(dst, "\n\t");
}
fprintf(dst, "\n};\n");
/* close the files */
free(buffer);
fclose(dst);
return 0;
}
|