trim whitespace from TTS error string before displaying it.
[Rockbox.git] / rbutil / sansapatcher / bin2c.c
blob765c9a247c965be31cb6440dd08f8271dcf27adf
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
28 #ifndef O_BINARY
29 #define O_BINARY 0
30 #endif
32 static off_t filesize(int fd)
34 struct stat buf;
36 fstat(fd,&buf);
37 return buf.st_size;
40 static int write_cfile(unsigned char* buf, off_t len, char* cname)
42 char filename[256];
43 FILE* fp;
44 int i;
46 snprintf(filename,256,"%s.c",cname);
48 fp = fopen(filename,"w+");
49 if (fp == NULL) {
50 fprintf(stderr,"Couldn't open %s\n",filename);
51 return -1;
54 fprintf(fp,"/* Generated by bin2c */\n\n");
55 fprintf(fp,"unsigned char %s[] = {",cname);
57 for (i=0;i<len;i++) {
58 if ((i % 16) == 0) {
59 fprintf(fp,"\n ");
61 if (i == (len-1)) {
62 fprintf(fp,"0x%02x",buf[i]);
63 } else {
64 fprintf(fp,"0x%02x, ",buf[i]);
67 fprintf(fp,"\n};\n");
69 fclose(fp);
70 return 0;
73 static int write_hfile(off_t len, char* cname)
75 char filename[256];
76 FILE* fp;
78 snprintf(filename,256,"%s.h",cname);
79 fp = fopen(filename,"w+");
80 if (fp == NULL) {
81 fprintf(stderr,"Couldn't open %s\n",filename);
82 return -1;
85 fprintf(fp,"/* Generated by bin2c */\n\n");
86 fprintf(fp,"#define LEN_%s %d\n",cname,(int)len);
87 fprintf(fp,"extern unsigned char %s[];\n",cname);
88 fclose(fp);
89 return 0;
92 int main (int argc, char* argv[])
94 char* infile;
95 char* cname;
96 int fd;
97 unsigned char* buf;
98 int len;
99 int n;
101 if (argc != 3) {
102 fprintf(stderr,"Usage: bin2c file cname\n");
103 return 0;
106 infile=argv[1];
107 cname=argv[2];
109 fd = open(infile,O_RDONLY|O_BINARY);
110 if (fd < 0) {
111 fprintf(stderr,"Can not open %s\n",infile);
112 return 0;
115 len = filesize(fd);
117 buf = malloc(len);
118 n = read(fd,buf,len);
119 if (n < len) {
120 fprintf(stderr,"Short read, aborting\n");
121 return 0;
123 close(fd);
125 if (write_cfile(buf,len,cname) < 0) {
126 return -1;
128 if (write_hfile(len,cname) < 0) {
129 return -1;
132 return 0;