1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
24 #include <sys/types.h>
28 #if !defined(_MSC_VER)
32 #define snprintf _snprintf
43 static off_t
filesize(int fd
)
51 static int write_cfile(const unsigned char* buf
, off_t len
, const char* cname
)
59 snprintf(filename
,256,"%s.c",cname
);
60 strncpy(filebase
, cname
, 256);
61 bn
= basename(filebase
);
63 fp
= fopen(filename
,"w+");
65 fprintf(stderr
,"Couldn't open %s\n",filename
);
69 fprintf(fp
,"/* Generated by bin2c */\n\n");
70 fprintf(fp
,"unsigned char %s[] = {",bn
);
77 fprintf(fp
,"0x%02x",buf
[i
]);
79 fprintf(fp
,"0x%02x, ",buf
[i
]);
88 static int write_hfile(off_t len
, const char* cname
)
95 snprintf(filename
,256,"%s.h",cname
);
96 strncpy(filebase
, cname
, 256);
97 bn
= basename(filebase
);
98 fp
= fopen(filename
,"w+");
100 fprintf(stderr
,"Couldn't open %s\n",filename
);
104 fprintf(fp
,"/* Generated by bin2c */\n\n");
105 fprintf(fp
,"#define LEN_%s %d\n",bn
,(int)len
);
106 fprintf(fp
,"extern unsigned char %s[];\n",bn
);
111 int main (int argc
, char* argv
[])
121 fprintf(stderr
,"Usage: bin2c file cname\n");
128 fd
= open(infile
,O_RDONLY
|O_BINARY
);
130 fprintf(stderr
,"Can not open %s\n",infile
);
137 n
= read(fd
,buf
,len
);
139 fprintf(stderr
,"Short read, aborting\n");
144 if (write_cfile(buf
,len
,cname
) < 0) {
147 if (write_hfile(len
,cname
) < 0) {