1 /************************************************
3 * Converting binary resources from/to *.rc files
5 * Copyright 1999 Juergen Schmied
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #ifdef HAVE_SYS_PARAM_H
27 # include <sys/param.h>
29 #include <sys/types.h>
43 #ifdef HAVE_SYS_MMAN_H
44 # include <sys/mman.h>
52 extern char* g_lpstrFileName
;
56 char* g_lpstrFileName
= NULL
;
57 char* g_lpstrInputFile
= NULL
;
59 int b_force_overwrite
= 0;
61 static char* errorOpenFile
= "Unable to open file.\n";
62 static char* errorRCFormat
= "Unexpexted syntax in rc file line %i\n";
66 printf("Usage: bin2res [-d bin] [input file]\n");
67 printf(" -d bin convert a *.res back to a binary\n");
68 printf(" -f force overwriting newer files\n");
72 void parse_options(int argc
, char **argv
)
79 g_lpstrInputFile
= argv
[1];
85 for( i
= 1; i
< argc
- 1; i
++ )
87 if( argv
[i
][0] != '-' ||
88 strlen(argv
[i
]) != 2 ) break;
90 if( argv
[i
][1] == 'd')
92 if (strcmp ("bin", argv
[i
+1])==0)
103 else if ( argv
[i
][1] == 'f')
105 b_force_overwrite
= 1;
114 g_lpstrInputFile
= argv
[i
];
121 int insert_hex (char * infile
, FILE * outfile
)
127 LPBYTE p_in_file
= NULL
;
129 if( (fd
= open( infile
, O_RDONLY
))==-1 )
131 fprintf(stderr
, errorOpenFile
);
134 if ((fstat(fd
, &st
) == -1) || (p_in_file
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0)) == (void *)-1)
136 fprintf(stderr
, errorOpenFile
);
141 fprintf (outfile
, "{\n '");
145 fprintf(outfile
, "%02X", p_in_file
[i
]);
146 if (++i
>= st
.st_size
) break;
147 fprintf(outfile
, "%s", (i
== (i
& 0xfffffff0)) ? "'\n '" :" ");
149 fprintf (outfile
, "'\n}");
150 munmap(p_in_file
, st
.st_size
);
153 #else /* HAVE_MMAP */
159 fp
= fopen( infile
, "r" );
162 fprintf(stderr
, errorOpenFile
);
165 if (fstat(fileno(fp
), &st
) == -1)
167 fprintf(stderr
, errorOpenFile
);
172 fprintf (outfile
, "{\n '");
179 fprintf(stderr
, errorOpenFile
);
183 fprintf(outfile
, "%02X", c
);
184 if (++i
>= st
.st_size
) break;
185 fprintf(outfile
, "%s", (i
== (i
& 0xfffffff0)) ? "'\n '" :" ");
187 fprintf (outfile
, "'\n}");
191 #endif /* HAVE_MMAP */
194 int convert_to_res ()
205 long startpos
, endpos
;
207 strcpy( tmpfile
, g_lpstrInputFile
);
208 strcat( tmpfile
, "-tmp" );
209 /* FIXME: should use better tmp name and create with O_EXCL */
210 if( (ftemp
= fopen( tmpfile
, "w")) == NULL
) goto error_open_file
;
212 if( (fin
= fopen( g_lpstrInputFile
, "r")) == NULL
|| stat(g_lpstrInputFile
, &st
)) goto error_open_file
;
213 tinput
= st
.st_ctime
;
215 while ( NULL
!= fgets(buffer
, 255, fin
))
217 fputs(buffer
, ftemp
);
219 if ( (pos
= strstr(buffer
, "BINRES")) != NULL
)
221 /* get the out-file name */
222 len
= 0; pos
+= 6; startpos
=0; endpos
=0;
223 while ( *pos
== ' ') pos
++;
224 while ( pos
[len
] != ' ') len
++;
225 strncpy(infile
, pos
, len
);
228 if ( (!stat(infile
, &st
) && st
.st_ctime
> tinput
) || b_force_overwrite
)
230 /* write a output file */
231 printf("[%s:c]", infile
);
232 while((c
= fgetc(fin
))!='{' && c
!= EOF
) fputc(c
, ftemp
);
233 if (c
== EOF
) goto error_rc_format
;
234 while((c
= fgetc(fin
))!='}' && c
!= EOF
);
235 if (c
== EOF
) goto error_rc_format
;
237 insert_hex(infile
, ftemp
);
241 printf("[%s:s]", infile
);
249 if (unlink(g_lpstrInputFile
) == -1)
255 if (rename(tmpfile
, g_lpstrInputFile
) == -1)
264 fprintf(stderr
, errorOpenFile
);
268 fprintf(stderr
, errorRCFormat
, line
);
278 int len
, index
, in_resource
;
284 if( (fin
= fopen( g_lpstrInputFile
, "r")) == NULL
|| stat(g_lpstrInputFile
, &st
)) goto error_open_file
;
285 tinput
= st
.st_ctime
;
287 while ( NULL
!= fgets(buffer
, 255, fin
))
290 if ( (pos
= strstr(buffer
, "BINRES")) != NULL
)
292 /* get the out-file name */
294 while ( *pos
== ' ') pos
++;
295 while ( pos
[len
] != ' ') len
++;
296 strncpy(outfile
, pos
, len
);
299 if ( stat(outfile
, &st
) || st
.st_ctime
< tinput
|| b_force_overwrite
)
301 /* write a output file */
302 printf("[%s:c]", outfile
);
303 if ( (fout
= fopen( outfile
, "w")) == NULL
) goto error_open_file
;
308 if ( NULL
== fgets(buffer
, 255, fin
)) goto error_rc_format
;
312 for ( index
= 0; buffer
[index
] != 0; index
++ )
316 if ( buffer
[index
] == '{' ) in_resource
= 1;
320 if ( buffer
[index
] == ' ' || buffer
[index
] == '\''|| buffer
[index
] == '\n' ) continue;
321 if ( buffer
[index
] == '}' ) goto end_of_resource
;
322 if ( ! isxdigit(buffer
[index
])) goto error_rc_format
;
323 index
+= sscanf(&buffer
[index
], "%02x", &byte
);
331 printf("[%s:s]", outfile
);
341 fprintf(stderr
, errorOpenFile
);
345 fprintf(stderr
, errorRCFormat
, line
);
349 int main(int argc
, char **argv
)
351 parse_options( argc
, argv
);
353 if (b_to_binary
== 0)