Make sure that MSG_PeekMessage uses an internal variable to avoid
[wine/hacks.git] / tools / bin2res.c
blobdd5e8833c3d32275dc05899e3578dd72a0a23cc4
1 /************************************************
3 * Converting binary resources from/to *.rc files
5 * Copyright 1999 Juergen Schmied
7 * 11/99 first release
8 */
10 #include "config.h"
12 #ifdef HAVE_SYS_PARAM_H
13 # include <sys/param.h>
14 #endif
15 #include <sys/types.h>
16 #include <sys/stat.h>
18 #include <ctype.h>
19 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #ifdef HAVE_SYS_MMAN_H
27 # include <sys/mman.h>
28 #endif
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
35 extern char* g_lpstrFileName;
37 /* global options */
39 char* g_lpstrFileName = NULL;
40 char* g_lpstrInputFile = NULL;
41 int b_to_binary = 0;
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";
47 void usage(void)
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");
52 exit(-1);
55 void parse_options(int argc, char **argv)
57 int i;
59 switch( argc )
61 case 2:
62 g_lpstrInputFile = argv[1];
63 break;
65 case 3:
66 case 4:
67 case 5:
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)
77 b_to_binary =1;
78 i++;
80 else
82 usage();
86 else if ( argv[i][1] == 'f')
88 b_force_overwrite = 1;
90 else
92 usage();
95 if( i == argc - 1 )
97 g_lpstrInputFile = argv[i];
98 break;
100 default: usage();
104 int insert_hex (char * infile, FILE * outfile)
106 #ifdef HAVE_MMAP
107 unsigned int i;
108 int fd;
109 struct stat st;
110 LPBYTE p_in_file = NULL;
112 if( (fd = open( infile, O_RDONLY))==-1 )
114 fprintf(stderr, errorOpenFile );
115 exit(1);
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 );
120 close(fd);
121 exit(1);
124 fprintf (outfile, "{\n '");
125 i = 0;
126 while (1)
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);
134 close(fd);
135 return 1;
136 #else /* HAVE_MMAP */
137 FILE* fp;
138 struct stat st;
139 unsigned int i;
140 int c;
142 fp = fopen( infile, "r" );
143 if ( fp == NULL )
145 fprintf(stderr, errorOpenFile );
146 exit(1);
148 if (fstat(fileno(fp), &st) == -1)
150 fprintf(stderr, errorOpenFile );
151 fclose(fp);
152 exit(1);
155 fprintf (outfile, "{\n '");
156 i = 0;
157 while (1)
159 c = fgetc(fp);
160 if ( c == EOF )
162 fprintf(stderr, errorOpenFile );
163 fclose(fp);
164 exit(1);
166 fprintf(outfile, "%02X", c);
167 if (++i >= st.st_size) break;
168 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
170 fprintf (outfile, "'\n}");
172 fclose(fp);
173 return 1;
174 #endif /* HAVE_MMAP */
177 int convert_to_res ()
179 FILE *fin, *ftemp;
180 char buffer[255];
181 char infile[255];
182 char tmpfile[255];
183 char *pos;
184 int c, len;
185 struct stat st;
186 int line = 0;
187 time_t tinput;
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);
201 line++;
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);
209 infile[len]=0;
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);
222 else
224 printf("[%s:s]", infile);
229 fclose(fin);
230 fclose(ftemp);
231 if (rename(tmpfile, g_lpstrInputFile) == -1)
233 perror("rename");
234 unlink(tmpfile);
235 return 0;
237 return 1;
239 error_open_file:
240 fprintf(stderr, errorOpenFile );
241 return 0;
243 error_rc_format:
244 fprintf(stderr, errorRCFormat, line);
245 return 0;
248 int convert_to_bin()
250 FILE *fin, *fout;
251 char buffer[255];
252 char outfile[255];
253 char *pos;
254 int len, index, in_resource;
255 unsigned int byte;
256 struct stat st;
257 int line = 0;
258 time_t tinput;
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))
265 line++;
266 if ( (pos = strstr(buffer, "BINRES")) != NULL)
268 /* get the out-file name */
269 len = 0; pos += 6;
270 while ( *pos == ' ') pos++;
271 while ( pos[len] != ' ') len++;
272 strncpy(outfile, pos, len);
273 outfile[len]=0;
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;
281 in_resource = 0;
282 while (1)
284 if ( NULL == fgets(buffer, 255, fin)) goto error_rc_format;
285 line++;
287 /* parse a line */
288 for ( index = 0; buffer[index] != 0; index++ )
290 if ( ! in_resource )
292 if ( buffer[index] == '{' ) in_resource = 1;
293 continue;
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);
300 fputc(byte, fout);
303 fclose(fout);
305 else
307 printf("[%s:s]", outfile);
309 end_of_resource: ;
313 fclose(fin);
314 return 1;
316 error_open_file:
317 fprintf(stderr, errorOpenFile );
318 return 0;
320 error_rc_format:
321 fprintf(stderr, errorRCFormat, line);
322 return 0;
325 int main(int argc, char **argv)
327 parse_options( argc, argv);
329 if (b_to_binary == 0)
331 convert_to_res();
333 else
335 convert_to_bin();
337 printf("\n");
338 return 0;