1 /************************************************
3 * Converting binary resources from/to *.rc files
5 * Copyright 1999 Juergen Schmied
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #ifdef HAVE_SYS_PARAM_H
27 # include <sys/param.h>
29 #include <sys/types.h>
40 #ifdef HAVE_SYS_MMAN_H
41 # include <sys/mman.h>
49 extern char* g_lpstrFileName;
53 char* g_lpstrFileName = NULL;
54 char* g_lpstrInputFile = NULL;
56 int b_force_overwrite = 0;
58 static char* errorOpenFile = "Unable to open file.\n";
59 static char* errorRCFormat = "Unexpexted syntax in rc file line %i\n";
63 printf("Usage: bin2res [-d bin] [input file]\n");
64 printf(" -d bin convert a *.res back to a binary\n");
65 printf(" -f force overwriting newer files\n");
69 void parse_options(int argc, char **argv)
76 g_lpstrInputFile = argv[1];
82 for( i = 1; i < argc - 1; i++ )
84 if( argv[i][0] != '-' ||
85 strlen(argv[i]) != 2 ) break;
87 if( argv[i][1] == 'd')
89 if (strcmp ("bin", argv[i+1])==0)
100 else if ( argv[i][1] == 'f')
102 b_force_overwrite = 1;
111 g_lpstrInputFile = argv[i];
118 int insert_hex (char * infile, FILE * outfile)
124 LPBYTE p_in_file = NULL;
126 if( (fd = open( infile, O_RDONLY))==-1 )
128 fprintf(stderr, errorOpenFile );
131 if ((fstat(fd, &st) == -1) || (p_in_file = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
133 fprintf(stderr, errorOpenFile );
138 fprintf (outfile, "{\n '");
142 fprintf(outfile, "%02X", p_in_file[i]);
143 if (++i >= st.st_size) break;
144 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
146 fprintf (outfile, "'\n}");
147 munmap(p_in_file, st.st_size);
150 #else /* HAVE_MMAP */
156 fp = fopen( infile, "r" );
159 fprintf(stderr, errorOpenFile );
162 if (fstat(fileno(fp), &st) == -1)
164 fprintf(stderr, errorOpenFile );
169 fprintf (outfile, "{\n '");
176 fprintf(stderr, errorOpenFile );
180 fprintf(outfile, "%02X", c);
181 if (++i >= st.st_size) break;
182 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
184 fprintf (outfile, "'\n}");
188 #endif /* HAVE_MMAP */
191 int convert_to_res ()
202 long startpos, endpos;
204 strcpy( tmpfile, g_lpstrInputFile );
205 strcat( tmpfile, "-tmp" );
206 /* FIXME: should use better tmp name and create with O_EXCL */
207 if( (ftemp = fopen( tmpfile, "w")) == NULL ) goto error_open_file;
209 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
210 tinput = st.st_ctime;
212 while ( NULL != fgets(buffer, 255, fin))
214 fputs(buffer, ftemp);
216 if ( (pos = strstr(buffer, "BINRES")) != NULL)
218 /* get the out-file name */
219 len = 0; pos += 6; startpos=0; endpos=0;
220 while ( *pos == ' ') pos++;
221 while ( pos[len] != ' ') len++;
222 strncpy(infile, pos, len);
225 if ( (!stat(infile, &st) && st.st_ctime > tinput) || b_force_overwrite)
227 /* write a output file */
228 printf("[%s:c]", infile);
229 while((c = fgetc(fin))!='{' && c != EOF) fputc(c, ftemp);
230 if (c == EOF ) goto error_rc_format;
231 while((c = fgetc(fin))!='}' && c != EOF);
232 if (c == EOF ) goto error_rc_format;
234 insert_hex(infile, ftemp);
238 printf("[%s:s]", infile);
246 if (unlink(g_lpstrInputFile) == -1)
252 if (rename(tmpfile, g_lpstrInputFile) == -1)
261 fprintf(stderr, errorOpenFile );
265 fprintf(stderr, errorRCFormat, line);
275 int len, index, in_resource;
281 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
282 tinput = st.st_ctime;
284 while ( NULL != fgets(buffer, 255, fin))
287 if ( (pos = strstr(buffer, "BINRES")) != NULL)
289 /* get the out-file name */
291 while ( *pos == ' ') pos++;
292 while ( pos[len] != ' ') len++;
293 strncpy(outfile, pos, len);
296 if ( stat(outfile, &st) || st.st_ctime < tinput || b_force_overwrite)
298 /* write a output file */
299 printf("[%s:c]", outfile);
300 if ( (fout = fopen( outfile, "w")) == NULL) goto error_open_file;
305 if ( NULL == fgets(buffer, 255, fin)) goto error_rc_format;
309 for ( index = 0; buffer[index] != 0; index++ )
313 if ( buffer[index] == '{' ) in_resource = 1;
317 if ( buffer[index] == ' ' || buffer[index] == '\''|| buffer[index] == '\n' ) continue;
318 if ( buffer[index] == '}' ) goto end_of_resource;
319 if ( ! isxdigit(buffer[index])) goto error_rc_format;
320 index += sscanf(&buffer[index], "%02x", &byte);
328 printf("[%s:s]", outfile);
338 fprintf(stderr, errorOpenFile );
342 fprintf(stderr, errorRCFormat, line);
346 int main(int argc, char **argv)
348 parse_options( argc, argv);
350 if (b_to_binary == 0)