1 /************************************************
3 * Converting binary resources from/to *.rc files
5 * Copyright 1999 Juergen Schmied
12 #ifdef HAVE_SYS_PARAM_H
13 # include <sys/param.h>
15 #include <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
27 # include <sys/mman.h>
33 extern char* g_lpstrFileName;
37 char* g_lpstrFileName = NULL;
38 char* g_lpstrInputFile = NULL;
40 int b_force_overwrite = 0;
42 static char* errorOpenFile = "Unable to open file.\n";
43 static char* errorRCFormat = "Unexpexted syntax in rc file line %i\n";
47 printf("Usage: bin2res [-d bin] [input file]\n");
48 printf(" -d bin convert a *.res back to a binary\n");
49 printf(" -f force overwriting newer files\n");
53 void parse_options(int argc, char **argv)
60 g_lpstrInputFile = argv[1];
66 for( i = 1; i < argc - 1; i++ )
68 if( argv[i][0] != '-' ||
69 strlen(argv[i]) != 2 ) break;
71 if( argv[i][1] == 'd')
73 if (strcmp ("bin", argv[i+1])==0)
84 else if ( argv[i][1] == 'f')
86 b_force_overwrite = 1;
95 g_lpstrInputFile = argv[i];
102 int insert_hex (char * infile, FILE * outfile)
108 LPBYTE p_in_file = NULL;
110 if( (fd = open( infile, O_RDONLY))==-1 )
112 fprintf(stderr, errorOpenFile );
115 if ((fstat(fd, &st) == -1) || (p_in_file = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
117 fprintf(stderr, errorOpenFile );
122 fprintf (outfile, "{\n '");
126 fprintf(outfile, "%02X", p_in_file[i]);
127 if (++i >= st.st_size) break;
128 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
130 fprintf (outfile, "'\n}");
131 munmap(p_in_file, st.st_size);
134 #else /* HAVE_MMAP */
140 fp = fopen( infile, "r" );
143 fprintf(stderr, errorOpenFile );
146 if (fstat(fileno(fp), &st) == -1)
148 fprintf(stderr, errorOpenFile );
153 fprintf (outfile, "{\n '");
160 fprintf(stderr, errorOpenFile );
164 fprintf(outfile, "%02X", c);
165 if (++i >= st.st_size) break;
166 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
168 fprintf (outfile, "'\n}");
172 #endif /* HAVE_MMAP */
175 int convert_to_res ()
180 char tmpfile[L_tmpnam];
186 long startpos, endpos;
188 strcpy( tmpfile, g_lpstrInputFile );
189 strcat( tmpfile, "-tmp" );
190 /* FIXME: should use better tmp name and create with O_EXCL */
191 if( (ftemp = fopen( tmpfile, "w")) == NULL ) goto error_open_file;
193 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
194 tinput = st.st_ctime;
196 while ( NULL != fgets(buffer, 255, fin))
198 fputs(buffer, ftemp);
200 if ( (pos = strstr(buffer, "BINRES")) != NULL)
202 /* get the out-file name */
203 len = 0; pos += 6; startpos=0; endpos=0;
204 while ( *pos == ' ') pos++;
205 while ( pos[len] != ' ') len++;
206 strncpy(infile, pos, len);
209 if ( (!stat(infile, &st) && st.st_ctime > tinput) || b_force_overwrite)
211 /* write a output file */
212 printf("[%s:c]", infile);
213 while((c = fgetc(fin))!='{' && c != EOF) fputc(c, ftemp);
214 if (c == EOF ) goto error_rc_format;
215 while((c = fgetc(fin))!='}' && c != EOF);
216 if (c == EOF ) goto error_rc_format;
218 insert_hex(infile, ftemp);
222 printf("[%s:s]", infile);
229 if (rename(tmpfile, g_lpstrInputFile) == -1)
238 fprintf(stderr, errorOpenFile );
242 fprintf(stderr, errorRCFormat, line);
252 int len, index, in_resource;
258 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
259 tinput = st.st_ctime;
261 while ( NULL != fgets(buffer, 255, fin))
264 if ( (pos = strstr(buffer, "BINRES")) != NULL)
266 /* get the out-file name */
268 while ( *pos == ' ') pos++;
269 while ( pos[len] != ' ') len++;
270 strncpy(outfile, pos, len);
273 if ( stat(outfile, &st) || st.st_ctime < tinput || b_force_overwrite)
275 /* write a output file */
276 printf("[%s:c]", outfile);
277 if ( (fout = fopen( outfile, "w")) == NULL) goto error_open_file;
282 if ( NULL == fgets(buffer, 255, fin)) goto error_rc_format;
286 for ( index = 0; buffer[index] != 0; index++ )
290 if ( buffer[index] == '{' ) in_resource = 1;
294 if ( buffer[index] == ' ' || buffer[index] == '\''|| buffer[index] == '\n' ) continue;
295 if ( buffer[index] == '}' ) goto end_of_resource;
296 if ( ! isxdigit(buffer[index])) goto error_rc_format;
297 index += sscanf(&buffer[index], "%02x", &byte);
305 printf("[%s:s]", outfile);
315 fprintf(stderr, errorOpenFile );
319 fprintf(stderr, errorRCFormat, line);
323 int main(int argc, char **argv)
325 parse_options( argc, argv);
327 if (b_to_binary == 0)