The threading model should be set from configure, not config.h.
[maemo-rb.git] / rbutil / tools / bin2c.c
blob4600746d5277ef34cacc872b95b82e86e01b76d4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 Dave Chapman
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #if !defined(_MSC_VER)
28 #include <unistd.h>
29 #else
30 #include <io.h>
31 #define snprintf _snprintf
32 #define open _open
33 #define close _close
34 #define read _read
35 #endif
36 #include <libgen.h>
38 #ifndef O_BINARY
39 #define O_BINARY 0
40 #endif
42 static void usage(void)
44 fprintf(stderr, "bin2c [options] infile cfile\n");
45 fprintf(stderr, " -i ipod mode\n");
49 static off_t filesize(int fd)
51 struct stat buf;
53 fstat(fd,&buf);
54 return buf.st_size;
57 static int write_cfile(const unsigned char* buf, off_t len, const char* cname)
59 char filename[256];
60 char filebase[256];
61 char* bn;
62 FILE* fp;
63 int i;
65 snprintf(filename,256,"%s.c",cname);
66 strncpy(filebase, cname, 256);
67 bn = basename(filebase);
69 fp = fopen(filename,"w+");
70 if (fp == NULL) {
71 fprintf(stderr,"Couldn't open %s\n",filename);
72 return -1;
75 fprintf(fp,"/* Generated by bin2c */\n\n");
76 fprintf(fp,"unsigned char %s[] = {",bn);
78 for (i=0;i<len;i++) {
79 if ((i % 16) == 0) {
80 fprintf(fp,"\n ");
82 if (i == (len-1)) {
83 fprintf(fp,"0x%02x",buf[i]);
84 } else {
85 fprintf(fp,"0x%02x, ",buf[i]);
88 fprintf(fp,"\n};\n");
90 fclose(fp);
91 return 0;
94 static int write_hfile(off_t len, const char* cname)
96 char filename[256];
97 char filebase[256];
98 char* bn;
99 FILE* fp;
101 snprintf(filename,256,"%s.h",cname);
102 strncpy(filebase, cname, 256);
103 bn = basename(filebase);
104 fp = fopen(filename,"w+");
105 if (fp == NULL) {
106 fprintf(stderr,"Couldn't open %s\n",filename);
107 return -1;
110 fprintf(fp,"/* Generated by bin2c */\n\n");
111 fprintf(fp,"#define LEN_%s %d\n",bn,(int)len);
112 fprintf(fp,"extern unsigned char %s[];\n",bn);
113 fclose(fp);
114 return 0;
117 int main (int argc, char* argv[])
119 char* infile;
120 char* cname;
121 int fd;
122 unsigned char* buf;
123 int len;
124 int n;
125 int skip = 0;
126 int opts = 0;
129 if(argc < 2) {
130 usage();
131 return 0;
133 if(strcmp(argv[1], "-i") == 0) {
134 skip = 8;
135 opts++;
137 if (argc < opts + 3) {
138 usage();
139 return 0;
142 infile=argv[opts + 1];
143 cname=argv[opts + 2];
145 fd = open(infile,O_RDONLY|O_BINARY);
146 if (fd < 0) {
147 fprintf(stderr,"Can not open %s\n",infile);
148 return 0;
151 len = filesize(fd) - skip;
152 n = lseek(fd, skip, SEEK_SET);
153 if (n != skip) {
154 fprintf(stderr,"Seek failed\n");
155 return 0;
158 buf = malloc(len);
159 n = read(fd,buf,len);
160 if (n < len) {
161 fprintf(stderr,"Short read, aborting\n");
162 return 0;
164 close(fd);
166 if (write_cfile(buf,len,cname) < 0) {
167 return -1;
169 if (write_hfile(len,cname) < 0) {
170 return -1;
173 return 0;