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
42 static off_t
filesize(int fd
)
50 static int write_cfile(const unsigned char* buf
, off_t len
, const char* cname
)
56 snprintf(filename
,256,"%s.c",cname
);
58 fp
= fopen(filename
,"w+");
60 fprintf(stderr
,"Couldn't open %s\n",filename
);
64 fprintf(fp
,"/* Generated by bin2c */\n\n");
65 fprintf(fp
,"unsigned char %s[] = {",cname
);
72 fprintf(fp
,"0x%02x",buf
[i
]);
74 fprintf(fp
,"0x%02x, ",buf
[i
]);
83 static int write_hfile(off_t len
, const char* cname
)
88 snprintf(filename
,256,"%s.h",cname
);
89 fp
= fopen(filename
,"w+");
91 fprintf(stderr
,"Couldn't open %s\n",filename
);
95 fprintf(fp
,"/* Generated by bin2c */\n\n");
96 fprintf(fp
,"#define LEN_%s %d\n",cname
,(int)len
);
97 fprintf(fp
,"extern unsigned char %s[];\n",cname
);
102 int main (int argc
, char* argv
[])
112 fprintf(stderr
,"Usage: bin2c file cname\n");
119 fd
= open(infile
,O_RDONLY
|O_BINARY
);
121 fprintf(stderr
,"Can not open %s\n",infile
);
128 n
= read(fd
,buf
,len
);
130 fprintf(stderr
,"Short read, aborting\n");
135 if (write_cfile(buf
,len
,cname
) < 0) {
138 if (write_hfile(len
,cname
) < 0) {