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>
35 extern char* g_lpstrFileName;
39 char* g_lpstrFileName = NULL;
40 char* g_lpstrInputFile = NULL;
42 int b_force_overwrite = 0;
44 static char* errorOpenFile = "Unable to open file.\n";
45 static char* errorRCFormat = "Unexpexted syntax in rc file line %i\n";
49 printf("Usage: bin2res [-d bin] [input file]\n");
50 printf(" -d bin convert a *.res back to a binary\n");
51 printf(" -f force overwriting newer files\n");
55 void parse_options(int argc, char **argv)
62 g_lpstrInputFile = argv[1];
68 for( i = 1; i < argc - 1; i++ )
70 if( argv[i][0] != '-' ||
71 strlen(argv[i]) != 2 ) break;
73 if( argv[i][1] == 'd')
75 if (strcmp ("bin", argv[i+1])==0)
86 else if ( argv[i][1] == 'f')
88 b_force_overwrite = 1;
97 g_lpstrInputFile = argv[i];
104 int insert_hex (char * infile, FILE * outfile)
110 LPBYTE p_in_file = NULL;
112 if( (fd = open( infile, O_RDONLY))==-1 )
114 fprintf(stderr, errorOpenFile );
117 if ((fstat(fd, &st) == -1) || (p_in_file = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
119 fprintf(stderr, errorOpenFile );
124 fprintf (outfile, "{\n '");
128 fprintf(outfile, "%02X", p_in_file[i]);
129 if (++i >= st.st_size) break;
130 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
132 fprintf (outfile, "'\n}");
133 munmap(p_in_file, st.st_size);
136 #else /* HAVE_MMAP */
142 fp = fopen( infile, "r" );
145 fprintf(stderr, errorOpenFile );
148 if (fstat(fileno(fp), &st) == -1)
150 fprintf(stderr, errorOpenFile );
155 fprintf (outfile, "{\n '");
162 fprintf(stderr, errorOpenFile );
166 fprintf(outfile, "%02X", c);
167 if (++i >= st.st_size) break;
168 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
170 fprintf (outfile, "'\n}");
174 #endif /* HAVE_MMAP */
177 int convert_to_res ()
182 char tmpfile[L_tmpnam];
188 long startpos, endpos;
190 strcpy( tmpfile, g_lpstrInputFile );
191 strcat( tmpfile, "-tmp" );
192 /* FIXME: should use better tmp name and create with O_EXCL */
193 if( (ftemp = fopen( tmpfile, "w")) == NULL ) goto error_open_file;
195 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
196 tinput = st.st_ctime;
198 while ( NULL != fgets(buffer, 255, fin))
200 fputs(buffer, ftemp);
202 if ( (pos = strstr(buffer, "BINRES")) != NULL)
204 /* get the out-file name */
205 len = 0; pos += 6; startpos=0; endpos=0;
206 while ( *pos == ' ') pos++;
207 while ( pos[len] != ' ') len++;
208 strncpy(infile, pos, len);
211 if ( (!stat(infile, &st) && st.st_ctime > tinput) || b_force_overwrite)
213 /* write a output file */
214 printf("[%s:c]", infile);
215 while((c = fgetc(fin))!='{' && c != EOF) fputc(c, ftemp);
216 if (c == EOF ) goto error_rc_format;
217 while((c = fgetc(fin))!='}' && c != EOF);
218 if (c == EOF ) goto error_rc_format;
220 insert_hex(infile, ftemp);
224 printf("[%s:s]", infile);
231 if (rename(tmpfile, g_lpstrInputFile) == -1)
240 fprintf(stderr, errorOpenFile );
244 fprintf(stderr, errorRCFormat, line);
254 int len, index, in_resource;
260 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
261 tinput = st.st_ctime;
263 while ( NULL != fgets(buffer, 255, fin))
266 if ( (pos = strstr(buffer, "BINRES")) != NULL)
268 /* get the out-file name */
270 while ( *pos == ' ') pos++;
271 while ( pos[len] != ' ') len++;
272 strncpy(outfile, pos, len);
275 if ( stat(outfile, &st) || st.st_ctime < tinput || b_force_overwrite)
277 /* write a output file */
278 printf("[%s:c]", outfile);
279 if ( (fout = fopen( outfile, "w")) == NULL) goto error_open_file;
284 if ( NULL == fgets(buffer, 255, fin)) goto error_rc_format;
288 for ( index = 0; buffer[index] != 0; index++ )
292 if ( buffer[index] == '{' ) in_resource = 1;
296 if ( buffer[index] == ' ' || buffer[index] == '\''|| buffer[index] == '\n' ) continue;
297 if ( buffer[index] == '}' ) goto end_of_resource;
298 if ( ! isxdigit(buffer[index])) goto error_rc_format;
299 index += sscanf(&buffer[index], "%02x", &byte);
307 printf("[%s:s]", outfile);
317 fprintf(stderr, errorOpenFile );
321 fprintf(stderr, errorRCFormat, line);
325 int main(int argc, char **argv)
327 parse_options( argc, argv);
329 if (b_to_binary == 0)