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>
29 extern char* g_lpstrFileName;
33 char* g_lpstrFileName = NULL;
34 char* g_lpstrInputFile = NULL;
36 int b_force_overwrite = 0;
37 LPBYTE p_in_file = NULL;
39 static char* errorOpenFile = "Unable to open file.\n";
40 static char* errorRCFormat = "Unexpexted syntax in rc file line %i\n";
44 printf("Usage: bin2res [-d bin] [input file]\n");
45 printf(" -d bin convert a *.res back to a binary\n");
46 printf(" -f force overwriting newer files\n");
50 void parse_options(int argc, char **argv)
57 g_lpstrInputFile = argv[1];
63 for( i = 1; i < argc - 1; i++ )
65 if( argv[i][0] != '-' ||
66 strlen(argv[i]) != 2 ) break;
68 if( argv[i][1] == 'd')
70 if (strcmp ("bin", argv[i+1])==0)
81 else if ( argv[i][1] == 'f')
83 b_force_overwrite = 1;
92 g_lpstrInputFile = argv[i];
99 int insert_hex (char * infile, FILE * outfile)
105 if( (fd = open( infile, O_RDONLY))==-1 )
107 fprintf(stderr, errorOpenFile );
110 if ((fstat(fd, &st) == -1) || (p_in_file = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
112 fprintf(stderr, errorOpenFile );
117 fprintf (outfile, "{\n '");
121 fprintf(outfile, "%02X", p_in_file[i]);
122 if (++i >= st.st_size) break;
123 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
125 fprintf (outfile, "'\n}");
126 munmap(p_in_file, st.st_size);
131 int convert_to_res ()
136 char tmpfile[L_tmpnam];
142 long startpos, endpos;
144 strcpy( tmpfile, g_lpstrInputFile );
145 strcat( tmpfile, "-tmp" );
146 /* FIXME: should use better tmp name and create with O_EXCL */
147 if( (ftemp = fopen( tmpfile, "w")) == NULL ) goto error_open_file;
149 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
150 tinput = st.st_ctime;
152 while ( NULL != fgets(buffer, 255, fin))
154 fputs(buffer, ftemp);
156 if ( (pos = strstr(buffer, "BINRES")) != NULL)
158 /* get the out-file name */
159 len = 0; pos += 6; startpos=0; endpos=0;
160 while ( *pos == ' ') pos++;
161 while ( pos[len] != ' ') len++;
162 strncpy(infile, pos, len);
165 if ( (!stat(infile, &st) && st.st_ctime > tinput) || b_force_overwrite)
167 /* write a output file */
168 printf("[%s:c]", infile);
169 while((c = fgetc(fin))!='{' && c != EOF) fputc(c, ftemp);
170 if (c == EOF ) goto error_rc_format;
171 while((c = fgetc(fin))!='}' && c != EOF);
172 if (c == EOF ) goto error_rc_format;
174 insert_hex(infile, ftemp);
178 printf("[%s:s]", infile);
185 if (rename(tmpfile, g_lpstrInputFile) == -1)
194 fprintf(stderr, errorOpenFile );
198 fprintf(stderr, errorRCFormat, line);
208 int len, index, in_resource;
214 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
215 tinput = st.st_ctime;
217 while ( NULL != fgets(buffer, 255, fin))
220 if ( (pos = strstr(buffer, "BINRES")) != NULL)
222 /* get the out-file name */
224 while ( *pos == ' ') pos++;
225 while ( pos[len] != ' ') len++;
226 strncpy(outfile, pos, len);
229 if ( stat(outfile, &st) || st.st_ctime < tinput || b_force_overwrite)
231 /* write a output file */
232 printf("[%s:c]", outfile);
233 if ( (fout = fopen( outfile, "w")) == NULL) goto error_open_file;
238 if ( NULL == fgets(buffer, 255, fin)) goto error_rc_format;
242 for ( index = 0; buffer[index] != 0; index++ )
246 if ( buffer[index] == '{' ) in_resource = 1;
250 if ( buffer[index] == ' ' || buffer[index] == '\''|| buffer[index] == '\n' ) continue;
251 if ( buffer[index] == '}' ) goto end_of_resource;
252 if ( ! isxdigit(buffer[index])) goto error_rc_format;
253 index += sscanf(&buffer[index], "%02x", &byte);
261 printf("[%s:s]", outfile);
271 fprintf(stderr, errorOpenFile );
275 fprintf(stderr, errorRCFormat, line);
279 int main(int argc, char **argv)
281 parse_options( argc, argv);
283 if (b_to_binary == 0)