Differentiate tracing functions between request and reply.
[wine/multimedia.git] / tools / bin2res.c
blob88589bab0ba6190c624a491acbc5044ba20ef173
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 #include <sys/mman.h>
27 #include "winuser.h"
29 extern char* g_lpstrFileName;
31 /* global options */
33 char* g_lpstrFileName = NULL;
34 char* g_lpstrInputFile = NULL;
35 int b_to_binary = 0;
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";
42 void usage(void)
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");
47 exit(-1);
50 void parse_options(int argc, char **argv)
52 int i;
54 switch( argc )
56 case 2:
57 g_lpstrInputFile = argv[1];
58 break;
60 case 3:
61 case 4:
62 case 5:
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)
72 b_to_binary =1;
73 i++;
75 else
77 usage();
81 else if ( argv[i][1] == 'f')
83 b_force_overwrite = 1;
85 else
87 usage();
90 if( i == argc - 1 )
92 g_lpstrInputFile = argv[i];
93 break;
95 default: usage();
99 int insert_hex (char * infile, FILE * outfile)
101 int i;
102 int fd;
103 struct stat st;
105 if( (fd = open( infile, O_RDONLY))==-1 )
107 fprintf(stderr, errorOpenFile );
108 exit(1);
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 );
113 close(fd);
114 exit(1);
117 fprintf (outfile, "{\n '");
118 i = 0;
119 while (1)
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}\n");
126 munmap(p_in_file, st.st_size);
127 close(fd);
128 return 1;
131 int convert_to_res ()
133 FILE *fin, *ftemp;
134 char buffer[255];
135 char infile[255];
136 char tmpfile[L_tmpnam];
137 char *pos;
138 int c, len;
139 struct stat st;
140 int line = 0;
141 time_t tinput;
142 long startpos, endpos;
144 tmpnam(tmpfile);
145 if( (ftemp = fopen( tmpfile, "w")) == NULL ) goto error_open_file;
147 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
148 tinput = st.st_ctime;
150 while ( NULL != fgets(buffer, 255, fin))
152 fputs(buffer, ftemp);
153 line++;
154 if ( (pos = strstr(buffer, "BINRES")) != NULL)
156 /* get the out-file name */
157 len = 0; pos += 6; startpos=0; endpos=0;
158 while ( *pos == ' ') pos++;
159 while ( pos[len] != ' ') len++;
160 strncpy(infile, pos, len);
161 infile[len]=0;
163 if ( (!stat(infile, &st) && st.st_ctime > tinput) || b_force_overwrite)
165 /* write a output file */
166 printf("[%s:c]", infile);
167 while((c = fgetc(fin))!='{' && c != EOF) fputc(c, ftemp);
168 if (c == EOF ) goto error_rc_format;
169 while((c = fgetc(fin))!='}' && c != EOF);
170 if (c == EOF ) goto error_rc_format;
172 insert_hex(infile, ftemp);
174 else
176 printf("[%s:s]", infile);
181 fclose(fin);
182 fclose(ftemp);
183 if (rename(tmpfile, g_lpstrInputFile) == -1)
185 perror("rename");
186 unlink(tmpfile);
187 return 0;
189 return 1;
191 error_open_file:
192 fprintf(stderr, errorOpenFile );
193 return 0;
195 error_rc_format:
196 fprintf(stderr, errorRCFormat, line);
197 return 0;
200 int convert_to_bin()
202 FILE *fin, *fout;
203 char buffer[255];
204 char outfile[255];
205 char *pos;
206 int len, index, in_resource;
207 unsigned int byte;
208 struct stat st;
209 int line = 0;
210 time_t tinput;
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 line++;
218 if ( (pos = strstr(buffer, "BINRES")) != NULL)
220 /* get the out-file name */
221 len = 0; pos += 6;
222 while ( *pos == ' ') pos++;
223 while ( pos[len] != ' ') len++;
224 strncpy(outfile, pos, len);
225 outfile[len]=0;
227 if ( stat(outfile, &st) || st.st_ctime < tinput || b_force_overwrite)
229 /* write a output file */
230 printf("[%s:c]", outfile);
231 if ( (fout = fopen( outfile, "w")) == NULL) goto error_open_file;
233 in_resource = 0;
234 while (1)
236 if ( NULL == fgets(buffer, 255, fin)) goto error_rc_format;
237 line++;
239 /* parse a line */
240 for ( index = 0; buffer[index] != 0; index++ )
242 if ( ! in_resource )
244 if ( buffer[index] == '{' ) in_resource = 1;
245 continue;
248 if ( buffer[index] == ' ' || buffer[index] == '\''|| buffer[index] == '\n' ) continue;
249 if ( buffer[index] == '}' ) goto end_of_resource;
250 if ( ! isxdigit(buffer[index])) goto error_rc_format;
251 index += sscanf(&buffer[index], "%02x", &byte);
252 fputc(byte, fout);
255 fclose(fout);
257 else
259 printf("[%s:s]", outfile);
261 end_of_resource:
265 fclose(fin);
266 return 1;
268 error_open_file:
269 fprintf(stderr, errorOpenFile );
270 return 0;
272 error_rc_format:
273 fprintf(stderr, errorRCFormat, line);
274 return 0;
277 int main(int argc, char **argv)
279 parse_options( argc, argv);
281 if (b_to_binary == 0)
283 convert_to_res();
285 else
287 convert_to_bin();
289 printf("\n");
290 return 0;