Set a default size for windows when CW_USEDEFAULT is used.
[wine.git] / tools / bin2res.c
blobed27db26c9cb1db729222edd958aa8d1c8a07311
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
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "winuser.h"
33 extern char* g_lpstrFileName;
35 /* global options */
37 char* g_lpstrFileName = NULL;
38 char* g_lpstrInputFile = NULL;
39 int b_to_binary = 0;
40 int b_force_overwrite = 0;
41 LPBYTE p_in_file = NULL;
43 static char* errorOpenFile = "Unable to open file.\n";
44 static char* errorRCFormat = "Unexpexted syntax in rc file line %i\n";
46 void usage(void)
48 printf("Usage: bin2res [-d bin] [input file]\n");
49 printf(" -d bin convert a *.res back to a binary\n");
50 printf(" -f force overwriting newer files\n");
51 exit(-1);
54 void parse_options(int argc, char **argv)
56 int i;
58 switch( argc )
60 case 2:
61 g_lpstrInputFile = argv[1];
62 break;
64 case 3:
65 case 4:
66 case 5:
67 for( i = 1; i < argc - 1; i++ )
69 if( argv[i][0] != '-' ||
70 strlen(argv[i]) != 2 ) break;
72 if( argv[i][1] == 'd')
74 if (strcmp ("bin", argv[i+1])==0)
76 b_to_binary =1;
77 i++;
79 else
81 usage();
85 else if ( argv[i][1] == 'f')
87 b_force_overwrite = 1;
89 else
91 usage();
94 if( i == argc - 1 )
96 g_lpstrInputFile = argv[i];
97 break;
99 default: usage();
103 int insert_hex (char * infile, FILE * outfile)
105 int i;
106 int fd;
107 struct stat st;
109 if( (fd = open( infile, O_RDONLY))==-1 )
111 fprintf(stderr, errorOpenFile );
112 exit(1);
114 if ((fstat(fd, &st) == -1) || (p_in_file = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
116 fprintf(stderr, errorOpenFile );
117 close(fd);
118 exit(1);
121 fprintf (outfile, "{\n '");
122 i = 0;
123 while (1)
125 fprintf(outfile, "%02X", p_in_file[i]);
126 if (++i >= st.st_size) break;
127 fprintf(outfile, "%s", (i == (i & 0xfffffff0)) ? "'\n '" :" ");
129 fprintf (outfile, "'\n}");
130 munmap(p_in_file, st.st_size);
131 close(fd);
132 return 1;
135 int convert_to_res ()
137 FILE *fin, *ftemp;
138 char buffer[255];
139 char infile[255];
140 char tmpfile[L_tmpnam];
141 char *pos;
142 int c, len;
143 struct stat st;
144 int line = 0;
145 time_t tinput;
146 long startpos, endpos;
148 strcpy( tmpfile, g_lpstrInputFile );
149 strcat( tmpfile, "-tmp" );
150 /* FIXME: should use better tmp name and create with O_EXCL */
151 if( (ftemp = fopen( tmpfile, "w")) == NULL ) goto error_open_file;
153 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
154 tinput = st.st_ctime;
156 while ( NULL != fgets(buffer, 255, fin))
158 fputs(buffer, ftemp);
159 line++;
160 if ( (pos = strstr(buffer, "BINRES")) != NULL)
162 /* get the out-file name */
163 len = 0; pos += 6; startpos=0; endpos=0;
164 while ( *pos == ' ') pos++;
165 while ( pos[len] != ' ') len++;
166 strncpy(infile, pos, len);
167 infile[len]=0;
169 if ( (!stat(infile, &st) && st.st_ctime > tinput) || b_force_overwrite)
171 /* write a output file */
172 printf("[%s:c]", infile);
173 while((c = fgetc(fin))!='{' && c != EOF) fputc(c, ftemp);
174 if (c == EOF ) goto error_rc_format;
175 while((c = fgetc(fin))!='}' && c != EOF);
176 if (c == EOF ) goto error_rc_format;
178 insert_hex(infile, ftemp);
180 else
182 printf("[%s:s]", infile);
187 fclose(fin);
188 fclose(ftemp);
189 if (rename(tmpfile, g_lpstrInputFile) == -1)
191 perror("rename");
192 unlink(tmpfile);
193 return 0;
195 return 1;
197 error_open_file:
198 fprintf(stderr, errorOpenFile );
199 return 0;
201 error_rc_format:
202 fprintf(stderr, errorRCFormat, line);
203 return 0;
206 int convert_to_bin()
208 FILE *fin, *fout;
209 char buffer[255];
210 char outfile[255];
211 char *pos;
212 int len, index, in_resource;
213 unsigned int byte;
214 struct stat st;
215 int line = 0;
216 time_t tinput;
218 if( (fin = fopen( g_lpstrInputFile, "r")) == NULL || stat(g_lpstrInputFile, &st)) goto error_open_file;
219 tinput = st.st_ctime;
221 while ( NULL != fgets(buffer, 255, fin))
223 line++;
224 if ( (pos = strstr(buffer, "BINRES")) != NULL)
226 /* get the out-file name */
227 len = 0; pos += 6;
228 while ( *pos == ' ') pos++;
229 while ( pos[len] != ' ') len++;
230 strncpy(outfile, pos, len);
231 outfile[len]=0;
233 if ( stat(outfile, &st) || st.st_ctime < tinput || b_force_overwrite)
235 /* write a output file */
236 printf("[%s:c]", outfile);
237 if ( (fout = fopen( outfile, "w")) == NULL) goto error_open_file;
239 in_resource = 0;
240 while (1)
242 if ( NULL == fgets(buffer, 255, fin)) goto error_rc_format;
243 line++;
245 /* parse a line */
246 for ( index = 0; buffer[index] != 0; index++ )
248 if ( ! in_resource )
250 if ( buffer[index] == '{' ) in_resource = 1;
251 continue;
254 if ( buffer[index] == ' ' || buffer[index] == '\''|| buffer[index] == '\n' ) continue;
255 if ( buffer[index] == '}' ) goto end_of_resource;
256 if ( ! isxdigit(buffer[index])) goto error_rc_format;
257 index += sscanf(&buffer[index], "%02x", &byte);
258 fputc(byte, fout);
261 fclose(fout);
263 else
265 printf("[%s:s]", outfile);
267 end_of_resource: ;
271 fclose(fin);
272 return 1;
274 error_open_file:
275 fprintf(stderr, errorOpenFile );
276 return 0;
278 error_rc_format:
279 fprintf(stderr, errorRCFormat, line);
280 return 0;
283 int main(int argc, char **argv)
285 parse_options( argc, argv);
287 if (b_to_binary == 0)
289 convert_to_res();
291 else
293 convert_to_bin();
295 printf("\n");
296 return 0;