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>
31 extern char* g_lpstrFileName
;
35 char* g_lpstrFileName
= NULL
;
36 char* g_lpstrInputFile
= NULL
;
38 int b_force_overwrite
= 0;
39 LPBYTE p_in_file
= NULL
;
41 static char* errorOpenFile
= "Unable to open file.\n";
42 static char* errorRCFormat
= "Unexpexted syntax in rc file line %i\n";
46 printf("Usage: bin2res [-d bin] [input file]\n");
47 printf(" -d bin convert a *.res back to a binary\n");
48 printf(" -f force overwriting newer files\n");
52 void parse_options(int argc
, char **argv
)
59 g_lpstrInputFile
= argv
[1];
65 for( i
= 1; i
< argc
- 1; i
++ )
67 if( argv
[i
][0] != '-' ||
68 strlen(argv
[i
]) != 2 ) break;
70 if( argv
[i
][1] == 'd')
72 if (strcmp ("bin", argv
[i
+1])==0)
83 else if ( argv
[i
][1] == 'f')
85 b_force_overwrite
= 1;
94 g_lpstrInputFile
= argv
[i
];
101 int insert_hex (char * infile
, FILE * outfile
)
107 if( (fd
= open( infile
, O_RDONLY
))==-1 )
109 fprintf(stderr
, errorOpenFile
);
112 if ((fstat(fd
, &st
) == -1) || (p_in_file
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0)) == (void *)-1)
114 fprintf(stderr
, errorOpenFile
);
119 fprintf (outfile
, "{\n '");
123 fprintf(outfile
, "%02X", p_in_file
[i
]);
124 if (++i
>= st
.st_size
) break;
125 fprintf(outfile
, "%s", (i
== (i
& 0xfffffff0)) ? "'\n '" :" ");
127 fprintf (outfile
, "'\n}");
128 munmap(p_in_file
, st
.st_size
);
133 int convert_to_res ()
138 char tmpfile
[L_tmpnam
];
144 long startpos
, endpos
;
146 strcpy( tmpfile
, g_lpstrInputFile
);
147 strcat( tmpfile
, "-tmp" );
148 /* FIXME: should use better tmp name and create with O_EXCL */
149 if( (ftemp
= fopen( tmpfile
, "w")) == NULL
) goto error_open_file
;
151 if( (fin
= fopen( g_lpstrInputFile
, "r")) == NULL
|| stat(g_lpstrInputFile
, &st
)) goto error_open_file
;
152 tinput
= st
.st_ctime
;
154 while ( NULL
!= fgets(buffer
, 255, fin
))
156 fputs(buffer
, ftemp
);
158 if ( (pos
= strstr(buffer
, "BINRES")) != NULL
)
160 /* get the out-file name */
161 len
= 0; pos
+= 6; startpos
=0; endpos
=0;
162 while ( *pos
== ' ') pos
++;
163 while ( pos
[len
] != ' ') len
++;
164 strncpy(infile
, pos
, len
);
167 if ( (!stat(infile
, &st
) && st
.st_ctime
> tinput
) || b_force_overwrite
)
169 /* write a output file */
170 printf("[%s:c]", infile
);
171 while((c
= fgetc(fin
))!='{' && c
!= EOF
) fputc(c
, ftemp
);
172 if (c
== EOF
) goto error_rc_format
;
173 while((c
= fgetc(fin
))!='}' && c
!= EOF
);
174 if (c
== EOF
) goto error_rc_format
;
176 insert_hex(infile
, ftemp
);
180 printf("[%s:s]", infile
);
187 if (rename(tmpfile
, g_lpstrInputFile
) == -1)
196 fprintf(stderr
, errorOpenFile
);
200 fprintf(stderr
, errorRCFormat
, line
);
210 int len
, index
, in_resource
;
216 if( (fin
= fopen( g_lpstrInputFile
, "r")) == NULL
|| stat(g_lpstrInputFile
, &st
)) goto error_open_file
;
217 tinput
= st
.st_ctime
;
219 while ( NULL
!= fgets(buffer
, 255, fin
))
222 if ( (pos
= strstr(buffer
, "BINRES")) != NULL
)
224 /* get the out-file name */
226 while ( *pos
== ' ') pos
++;
227 while ( pos
[len
] != ' ') len
++;
228 strncpy(outfile
, pos
, len
);
231 if ( stat(outfile
, &st
) || st
.st_ctime
< tinput
|| b_force_overwrite
)
233 /* write a output file */
234 printf("[%s:c]", outfile
);
235 if ( (fout
= fopen( outfile
, "w")) == NULL
) goto error_open_file
;
240 if ( NULL
== fgets(buffer
, 255, fin
)) goto error_rc_format
;
244 for ( index
= 0; buffer
[index
] != 0; index
++ )
248 if ( buffer
[index
] == '{' ) in_resource
= 1;
252 if ( buffer
[index
] == ' ' || buffer
[index
] == '\''|| buffer
[index
] == '\n' ) continue;
253 if ( buffer
[index
] == '}' ) goto end_of_resource
;
254 if ( ! isxdigit(buffer
[index
])) goto error_rc_format
;
255 index
+= sscanf(&buffer
[index
], "%02x", &byte
);
263 printf("[%s:s]", outfile
);
273 fprintf(stderr
, errorOpenFile
);
277 fprintf(stderr
, errorRCFormat
, line
);
281 int main(int argc
, char **argv
)
283 parse_options( argc
, argv
);
285 if (b_to_binary
== 0)