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>
34 static off_t
filesize(int fd
)
42 static int write_cfile(const unsigned char* buf
, off_t len
, const char* cname
)
48 snprintf(filename
,256,"%s.c",cname
);
50 fp
= fopen(filename
,"w+");
52 fprintf(stderr
,"Couldn't open %s\n",filename
);
56 fprintf(fp
,"/* Generated by bin2c */\n\n");
57 fprintf(fp
,"unsigned char %s[] = {",cname
);
64 fprintf(fp
,"0x%02x",buf
[i
]);
66 fprintf(fp
,"0x%02x, ",buf
[i
]);
75 static int write_hfile(off_t len
, const char* cname
)
80 snprintf(filename
,256,"%s.h",cname
);
81 fp
= fopen(filename
,"w+");
83 fprintf(stderr
,"Couldn't open %s\n",filename
);
87 fprintf(fp
,"/* Generated by bin2c */\n\n");
88 fprintf(fp
,"#define LEN_%s %d\n",cname
,(int)len
);
89 fprintf(fp
,"extern unsigned char %s[];\n",cname
);
94 int main (int argc
, char* argv
[])
104 fprintf(stderr
,"Usage: bin2c file cname\n");
111 fd
= open(infile
,O_RDONLY
|O_BINARY
);
113 fprintf(stderr
,"Can not open %s\n",infile
);
120 n
= read(fd
,buf
,len
);
122 fprintf(stderr
,"Short read, aborting\n");
127 if (write_cfile(buf
,len
,cname
) < 0) {
130 if (write_hfile(len
,cname
) < 0) {