1
2 /*
3 * inliner - This is a small program to make files inline C++ files
4 * Copyright (c) 2005 by Mattias Hultgren <mattias_hultgren@tele2.se>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /*
22 This is a command line program.
23 usage: inliner input_file variable_name output_file
24
25
26
27 */
28
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34
35
main(int argc,char ** argv)36 int main(int argc, char **argv)
37 {
38 char *in_file, *variable_name, *out_file, number[]="\\000\0";
39 unsigned char *in_buf;
40 FILE *fp;
41 long filesize, tmp, i, length;
42
43 if( argc != 4 )
44 {
45 printf(" usage: inliner input_file variable_name output_file\n");
46 return -1;
47 }
48
49 in_file = argv[1];
50 variable_name = argv[2];
51 out_file = argv[3];
52
53 fp = fopen( in_file, "rb" );
54 if(fp == 0)
55 {
56 failed_input:
57 if(fp != 0)
58 fclose(fp);
59
60 printf("inliner: failed to read file\n");
61 return -1;
62 }
63
64 if( fseek( fp, 0L, SEEK_END ) != 0 )
65 goto failed_input;
66 filesize = ftell(fp);
67 if( filesize == -1 )
68 goto failed_input;
69 if( fseek( fp, 0L, SEEK_SET ) != 0 )
70 goto failed_input;
71
72 if( (in_buf = (unsigned char *) malloc( filesize )) == 0 )
73 goto failed_input;
74
75 if( ((long) fread( in_buf, 1, filesize, fp )) != filesize )
76 {
77 free(in_buf);
78 goto failed_input;
79 }
80 if(fclose(fp) != 0)
81 {
82 free(in_buf);
83 goto failed_input;
84 }
85 // in_file is now loaded
86
87 fp = fopen( out_file, "wb" );
88 if(fp == 0)
89 {
90 failed_output:
91 if(fp != 0)
92 fclose(fp);
93
94 free(in_buf);
95 printf("inliner: failed to write file\n");
96 return -1;
97 }
98
99 if( fwrite( "/* This file is generated from ", 1, 31, fp ) != 31 )
100 goto failed_output;
101
102 if( fwrite( in_file, 1, strlen(in_file), fp ) != strlen(in_file) )
103 goto failed_output;
104
105 if( fwrite( " */\n\nchar *", 1, 11, fp ) != 11 )
106 goto failed_output;
107
108 if( fwrite( variable_name, 1, strlen(variable_name), fp ) != strlen(variable_name) )
109 goto failed_output;
110
111 if( fwrite( " =\n\"", 1, 4, fp ) != 4 )
112 goto failed_output;
113
114 for( i=0; i<filesize; i++ )
115 {
116 if( i != 0 && (i % 16) == 0 )
117 if( fwrite( "\"\n\"", 1, 3, fp ) != 3 )
118 goto failed_output;
119 tmp = (long) in_buf[i];
120
121 if( tmp >= 64 )
122 {
123 number[1] = ((char) ( ((long) '0') + tmp / 64 ) );
124 tmp %= 64;
125 number[2] = ((char) ( ((long) '0') + tmp / 8 ) );
126 tmp %= 8;
127 number[3] = ((char) ( ((long) '0') + tmp ) );
128 length = 4;
129 }
130 else if( tmp >= 8 )
131 {
132 number[1] = ((char) ( ((long) '0') + tmp / 8 ) );
133 tmp %= 8;
134 number[2] = ((char) ( ((long) '0') + tmp ) );
135 length = 3;
136 }
137 else
138 {
139 number[2] = ((char) ( ((long) '0') + tmp ) );
140 length = 2;
141 }
142
143 if( fwrite( number, 1, length, fp ) != length )
144 goto failed_output;
145 }
146 if( fwrite( "\";\n", 1, 3, fp ) != 3 )
147 goto failed_output;
148
149 if(fclose(fp) != 0)
150 {
151 free(in_buf);
152 goto failed_output;
153 }
154 return 0;
155 }
156