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;
41 LPBYTE p_in_file = NULL;
43 static char* errorOpenFile = "Unable to open file.\n";
44 static char* errorRCFormat = "Unexpexted syntax in rc file line %i\n";
48 printf("Usage: bin2res [-d bin] [input file]\n");
49 printf(" -d bin convert a *.res back to a binary\n");
50 printf(" -f force overwriting newer files\n");
54 void parse_options(int argc, char **argv)
61 g_lpstrInputFile = argv[1];
67 for( i = 1; i < argc - 1; i++ )
69 if( argv[i][0] != '-' ||
70 strlen(argv[i]) != 2 ) break;
72 if( argv[i][1] == 'd')
74 if (strcmp ("bin", argv[i+1])==0)
85 else if ( argv[i][1] == 'f')
87 b_force_overwrite = 1;
96 g_lpstrInputFile = argv[i];
103 int insert_hex (char * infile, FILE * outfile)
109 if( (fd = open( infile, O_RDONLY))==-1 )
111 fprintf(stderr, errorOpenFile );
114 if ((fstat(fd, &st) == -1) || (p_in_file = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
116 fprintf(stderr, errorOpenFile );
121 fprintf (outfile, "{\n '");
125 fprintf(outfile, "%02X", p_in_file[i]);
126 if (++i >= st.st_size) break;
127 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
129 fprintf (outfile, "'\n}");
130 munmap(p_in_file, st.st_size);
135 int convert_to_res ()
140 char tmpfile[L_tmpnam];
146 long startpos, endpos;
148 strcpy( tmpfile, g_lpstrInputFile );
149 strcat( tmpfile, "-tmp" );
150 /* FIXME: should use better tmp name and create with O_EXCL */
151 if( (ftemp = fopen( tmpfile, "w")) == NULL ) goto error_open_file;
153 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
154 tinput = st.st_ctime;
156 while ( NULL != fgets(buffer, 255, fin))
158 fputs(buffer, ftemp);
160 if ( (pos = strstr(buffer, "BINRES")) != NULL)
162 /* get the out-file name */
163 len = 0; pos += 6; startpos=0; endpos=0;
164 while ( *pos == ' ') pos++;
165 while ( pos[len] != ' ') len++;
166 strncpy(infile, pos, len);
169 if ( (!stat(infile, &st) && st.st_ctime > tinput) || b_force_overwrite)
171 /* write a output file */
172 printf("[%s:c]", infile);
173 while((c = fgetc(fin))!='{' && c != EOF) fputc(c, ftemp);
174 if (c == EOF ) goto error_rc_format;
175 while((c = fgetc(fin))!='}' && c != EOF);
176 if (c == EOF ) goto error_rc_format;
178 insert_hex(infile, ftemp);
182 printf("[%s:s]", infile);
189 if (rename(tmpfile, g_lpstrInputFile) == -1)
198 fprintf(stderr, errorOpenFile );
202 fprintf(stderr, errorRCFormat, line);
212 int len, index, in_resource;
218 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
219 tinput = st.st_ctime;
221 while ( NULL != fgets(buffer, 255, fin))
224 if ( (pos = strstr(buffer, "BINRES")) != NULL)
226 /* get the out-file name */
228 while ( *pos == ' ') pos++;
229 while ( pos[len] != ' ') len++;
230 strncpy(outfile, pos, len);
233 if ( stat(outfile, &st) || st.st_ctime < tinput || b_force_overwrite)
235 /* write a output file */
236 printf("[%s:c]", outfile);
237 if ( (fout = fopen( outfile, "w")) == NULL) goto error_open_file;
242 if ( NULL == fgets(buffer, 255, fin)) goto error_rc_format;
246 for ( index = 0; buffer[index] != 0; index++ )
250 if ( buffer[index] == '{' ) in_resource = 1;
254 if ( buffer[index] == ' ' || buffer[index] == '\''|| buffer[index] == '\n' ) continue;
255 if ( buffer[index] == '}' ) goto end_of_resource;
256 if ( ! isxdigit(buffer[index])) goto error_rc_format;
257 index += sscanf(&buffer[index], "%02x", &byte);
265 printf("[%s:s]", outfile);
275 fprintf(stderr, errorOpenFile );
279 fprintf(stderr, errorRCFormat, line);
283 int main(int argc, char **argv)
285 parse_options( argc, argv);
287 if (b_to_binary == 0)