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 void write_cfile(const unsigned char* buf
, off_t len
, FILE* fp
, const char *name
)
46 fprintf(fp
,"unsigned char %s[%ld] = {",name
,len
);
53 fprintf(fp
,"0x%02x",buf
[i
]);
54 } else if ((i
% 16) == 15) {
55 fprintf(fp
,"0x%02x,",buf
[i
]);
57 fprintf(fp
,"0x%02x, ",buf
[i
]);
63 int main (int argc
, char* argv
[])
68 char cfilename
[256], hfilename
[256];
71 fprintf(stderr
,"Usage: bin2c cname file1 [file2 [file3 ...]]\n");
77 snprintf(cfilename
,256,"%s.c",cname
);
78 cfile
= fopen(cfilename
,"w+");
80 fprintf(stderr
,"Couldn't open %s\n",cfilename
);
84 snprintf(hfilename
,256,"%s.h",cname
);
85 hfile
= fopen(hfilename
,"w+");
87 fprintf(stderr
,"Couldn't open %s\n",hfilename
);
92 fprintf(cfile
,"/* Generated by bin2c */\n\n");
93 fprintf(cfile
,"#include \"%s\"\n\n", hfilename
);
94 fprintf(hfile
,"/* Generated by bin2c */\n\n");
96 for(i
=0; i
< argc
- 2; i
++) {
100 char *array
= argv
[2+i
];
102 int fd
= open(array
,O_RDONLY
|O_BINARY
);
104 fprintf(stderr
,"Can not open %s\n",argv
[2+i
]);
113 if (read(fd
,buf
,len
) < len
) {
114 fprintf(stderr
,"Short read, aborting\n");
118 /* remove file extension */
119 ext
= strchr (array
, '.');
122 write_cfile (buf
, len
, cfile
, array
);
123 fprintf(hfile
,"extern unsigned char %s[%ld];\n",array
,len
);