fusion: Return interface pointer from QI instead of impl pointer.
[wine/multimedia.git] / tools / wmc / write.c
blob19cd5b3ee06cc6bc07d7a7ca86ee525e5ef5f248
1 /*
2 * Wine Message Compiler output generation
4 * Copyright 2000 Bertho A. Stultiens (BS)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
30 #include "wmc.h"
31 #include "utils.h"
32 #include "lang.h"
33 #include "write.h"
36 * The binary resource layout is as follows:
38 * +===============+
39 * Header | NBlocks |
40 * +===============+
41 * Block 0 | Low ID |
42 * +---------------+
43 * | High ID |
44 * +---------------+
45 * | Offset |---+
46 * +===============+ |
47 * Block 1 | Low ID | |
48 * +---------------+ |
49 * | High ID | |
50 * +---------------+ |
51 * | Offset |------+
52 * +===============+ | |
53 * | | | |
54 * ... ... | |
55 * | | | |
56 * +===============+ <-+ |
57 * B0 LoID | Len | Flags | |
58 * +---+---+---+---+ |
59 * | b | l | a | b | |
60 * +---+---+---+---+ |
61 * | l | a | \0| \0| |
62 * +===============+ |
63 * | | |
64 * ... ... |
65 * | | |
66 * +===============+ |
67 * B0 HiID | Len | Flags | |
68 * +---+---+---+---+ |
69 * | M | o | r | e | |
70 * +---+---+---+---+ |
71 * | b | l | a | \0| |
72 * +===============+ <----+
73 * B1 LoID | Len | Flags |
74 * +---+---+---+---+
75 * | J | u | n | k |
76 * +---+---+---+---+
77 * | \0| \0| \0| \0|
78 * +===============+
79 * | |
80 * ... ...
81 * | |
82 * +===============+
84 * All Fields are aligned on their natural boundaries. The length
85 * field (Len) covers both the length of the string and the header
86 * fields (Len and Flags). Strings are '\0' terminated. Flags is 0
87 * for normal character strings and 1 for unicode strings.
90 static const char str_header[] =
91 "/* This file is generated with wmc version " PACKAGE_VERSION ". Do not edit! */\n"
92 "/* Source : %s */\n"
93 "/* Cmdline: %s */\n"
94 "\n"
97 static char *dup_u2c(int cp, const WCHAR *uc)
99 int len;
100 char *cptr;
101 const union cptable *cpdef = find_codepage(cp);
103 if(cpdef)
104 len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
105 else
106 len = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, NULL, 0);
107 cptr = xmalloc(len);
108 if (cpdef)
109 len = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, cptr, len, NULL, NULL);
110 else
111 len = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, cptr, len);
112 if (len < 0)
113 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d\n", len);
114 return cptr;
117 static void killnl(char *s, int ddd)
119 char *tmp;
120 tmp = strstr(s, "\r\n");
121 if(tmp)
123 if(ddd && tmp - s > 3)
125 tmp[0] = tmp[1] = tmp[2] = '.';
126 tmp[3] = '\0';
128 else
129 *tmp = '\0';
131 tmp = strchr(s, '\n');
132 if(tmp)
134 if(ddd && tmp - s > 3)
136 tmp[0] = tmp[1] = tmp[2] = '.';
137 tmp[3] = '\0';
139 else
140 *tmp = '\0';
144 static int killcomment(char *s)
146 char *tmp = s;
147 int b = 0;
148 while((tmp = strstr(tmp, "/*")))
150 tmp[1] = 'x';
151 b++;
153 tmp = s;
154 while((tmp = strstr(tmp, "*/")))
156 tmp[0] = 'x';
157 b++;
159 return b;
162 void write_h_file(const char *fname)
164 node_t *ndp;
165 char *cptr;
166 char *cast;
167 FILE *fp;
168 token_t *ttab;
169 int ntab;
170 int i;
171 int once = 0;
172 int idx_en = 0;
174 fp = fopen(fname, "w");
175 if(!fp)
177 perror(fname);
178 exit(1);
180 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline);
181 fprintf(fp, "#ifndef __WMCGENERATED_H\n");
182 fprintf(fp, "#define __WMCGENERATED_H\n");
183 fprintf(fp, "\n");
185 /* Write severity and facility aliases */
186 get_tokentable(&ttab, &ntab);
187 fprintf(fp, "/* Severity codes */\n");
188 for(i = 0; i < ntab; i++)
190 if(ttab[i].type == tok_severity && ttab[i].alias)
192 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
193 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
194 free(cptr);
197 fprintf(fp, "\n");
199 fprintf(fp, "/* Facility codes */\n");
200 for(i = 0; i < ntab; i++)
202 if(ttab[i].type == tok_facility && ttab[i].alias)
204 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
205 fprintf(fp, "#define %s\t0x%x\n", cptr, ttab[i].token);
206 free(cptr);
209 fprintf(fp, "\n");
211 /* Write the message codes */
212 fprintf(fp, "/* Message definitions */\n");
213 for(ndp = nodehead; ndp; ndp = ndp->next)
215 switch(ndp->type)
217 case nd_comment:
218 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.comment+1);
219 killnl(cptr, 0);
220 killcomment(cptr);
221 if(*cptr)
222 fprintf(fp, "/* %s */\n", cptr);
223 else
224 fprintf(fp, "\n");
225 free(cptr);
226 break;
227 case nd_msg:
228 if(!once)
231 * Search for an English text.
232 * If not found, then use the first in the list
234 once++;
235 for(i = 0; i < ndp->u.msg->nmsgs; i++)
237 if(ndp->u.msg->msgs[i]->lan == 0x409)
239 idx_en = i;
240 break;
243 fprintf(fp, "\n");
245 fprintf(fp, "/* MessageId : 0x%08x */\n", ndp->u.msg->realid);
246 cptr = dup_u2c(ndp->u.msg->msgs[idx_en]->cp, ndp->u.msg->msgs[idx_en]->msg);
247 killnl(cptr, 0);
248 killcomment(cptr);
249 fprintf(fp, "/* Approximate msg: %s */\n", cptr);
250 free(cptr);
251 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->sym);
252 if(ndp->u.msg->cast)
253 cast = dup_u2c(WMC_DEFAULT_CODEPAGE, ndp->u.msg->cast);
254 else
255 cast = NULL;
256 switch(ndp->u.msg->base)
258 case 8:
259 if(cast)
260 fprintf(fp, "#define %s\t((%s)0%oL)\n\n", cptr, cast, ndp->u.msg->realid);
261 else
262 fprintf(fp, "#define %s\t0%oL\n\n", cptr, ndp->u.msg->realid);
263 break;
264 case 10:
265 if(cast)
266 fprintf(fp, "#define %s\t((%s)%dL)\n\n", cptr, cast, ndp->u.msg->realid);
267 else
268 fprintf(fp, "#define %s\t%dL\n\n", cptr, ndp->u.msg->realid);
269 break;
270 case 16:
271 if(cast)
272 fprintf(fp, "#define %s\t((%s)0x%08xL)\n\n", cptr, cast, ndp->u.msg->realid);
273 else
274 fprintf(fp, "#define %s\t0x%08xL\n\n", cptr, ndp->u.msg->realid);
275 break;
276 default:
277 internal_error(__FILE__, __LINE__, "Invalid base for number print\n");
279 free(cptr);
280 free(cast);
281 break;
282 default:
283 internal_error(__FILE__, __LINE__, "Invalid node type %d\n", ndp->type);
286 fprintf(fp, "\n#endif\n");
287 fclose(fp);
290 static void write_rcbin(FILE *fp)
292 lan_blk_t *lbp;
293 token_t *ttab;
294 int ntab;
295 int i;
297 get_tokentable(&ttab, &ntab);
299 for(lbp = lanblockhead; lbp; lbp = lbp->next)
301 char *cptr = NULL;
302 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
303 for(i = 0; i < ntab; i++)
305 if(ttab[i].type == tok_language && ttab[i].token == lbp->lan)
307 if(ttab[i].alias)
308 cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
309 break;
312 if(!cptr)
313 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
314 fprintf(fp, "1 MESSAGETABLE \"%s.bin\"\n", cptr);
315 free(cptr);
319 static char *make_string(WCHAR *uc, int len, int codepage)
321 char *str = xmalloc(7*len + 1);
322 char *cptr = str;
323 int i;
324 int b;
326 if(!codepage)
328 *cptr++ = ' ';
329 *cptr++ = 'L';
330 *cptr++ = '"';
331 for(i = b = 0; i < len; i++, uc++)
333 switch(*uc)
335 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
336 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
337 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
338 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
339 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
340 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
341 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
342 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
343 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
344 default:
345 if (*uc < 0x100 && isprint(*uc))
347 *cptr++ = *uc;
348 b++;
350 else
352 int n = sprintf(cptr, "\\x%04x", *uc & 0xffff);
353 cptr += n;
354 b += n;
356 break;
358 if(i < len-1 && b >= 72)
360 *cptr++ = '"';
361 *cptr++ = ',';
362 *cptr++ = '\n';
363 *cptr++ = ' ';
364 *cptr++ = 'L';
365 *cptr++ = '"';
366 b = 0;
369 if (unicodeout)
370 len = (len + 1) & ~1;
371 else
372 len = (len + 3) & ~3;
373 for(; i < len; i++)
375 *cptr++ = '\\';
376 *cptr++ = 'x';
377 *cptr++ = '0';
378 *cptr++ = '0';
379 *cptr++ = '0';
380 *cptr++ = '0';
382 *cptr++ = '"';
383 *cptr = '\0';
385 else
387 char *tmp, *cc;
388 int mlen;
389 const union cptable *cpdef = find_codepage(codepage);
391 if (cpdef)
392 mlen = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, NULL, 0, NULL, NULL);
393 else
394 mlen = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, NULL, 0);
395 cc = tmp = xmalloc(mlen);
396 if (cpdef) {
397 if((i = wine_cp_wcstombs(cpdef, 0, uc, unistrlen(uc)+1, tmp, mlen, NULL, NULL)) < 0)
398 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d\n", i);
399 } else {
400 if((i = wine_utf8_wcstombs(0, uc, unistrlen(uc)+1, tmp, mlen)) < 0)
401 internal_error(__FILE__, __LINE__, "Buffer overflow? code %d\n", i);
403 *cptr++ = ' ';
404 *cptr++ = '"';
405 for(i = b = 0; i < len; i++, cc++)
407 switch(*cc)
409 case '\a': *cptr++ = '\\'; *cptr++ = 'a'; b += 2; break;
410 case '\b': *cptr++ = '\\'; *cptr++ = 'b'; b += 2; break;
411 case '\f': *cptr++ = '\\'; *cptr++ = 'f'; b += 2; break;
412 case '\n': *cptr++ = '\\'; *cptr++ = 'n'; b += 2; break;
413 case '\r': *cptr++ = '\\'; *cptr++ = 'r'; b += 2; break;
414 case '\t': *cptr++ = '\\'; *cptr++ = 't'; b += 2; break;
415 case '\v': *cptr++ = '\\'; *cptr++ = 'v'; b += 2; break;
416 case '\\': *cptr++ = '\\'; *cptr++ = '\\'; b += 2; break;
417 case '"': *cptr++ = '\\'; *cptr++ = '"'; b += 2; break;
418 default:
419 if(isprint(*cc))
421 *cptr++ = *cc;
422 b++;
424 else
426 int n = sprintf(cptr, "\\x%02x", *cc & 0xff);
427 cptr += n;
428 b += n;
430 break;
432 if(i < len-1 && b >= 72)
434 *cptr++ = '"';
435 *cptr++ = ',';
436 *cptr++ = '\n';
437 *cptr++ = ' ';
438 *cptr++ = '"';
439 b = 0;
442 len = (len + 3) & ~3;
443 for(; i < len; i++)
445 *cptr++ = '\\';
446 *cptr++ = 'x';
447 *cptr++ = '0';
448 *cptr++ = '0';
450 *cptr++ = '"';
451 *cptr = '\0';
452 free(tmp);
454 return str;
457 static void write_rcinline(FILE *fp)
459 lan_blk_t *lbp;
460 int i;
461 int j;
463 for(lbp = lanblockhead; lbp; lbp = lbp->next)
465 unsigned offs = 4 * (lbp->nblk * 3 + 1);
466 fprintf(fp, "\n1 MESSAGETABLE\n");
467 fprintf(fp, "LANGUAGE 0x%x, 0x%x\n", lbp->lan & 0x3ff, lbp->lan >> 10);
468 fprintf(fp, "{\n");
469 fprintf(fp, " /* NBlocks */ 0x%08xL,\n", lbp->nblk);
470 for(i = 0; i < lbp->nblk; i++)
472 fprintf(fp, " /* Lo,Hi,Offs */ 0x%08xL, 0x%08xL, 0x%08xL,\n",
473 lbp->blks[i].idlo,
474 lbp->blks[i].idhi,
475 offs);
476 offs += lbp->blks[i].size;
478 for(i = 0; i < lbp->nblk; i++)
480 block_t *blk = &lbp->blks[i];
481 for(j = 0; j < blk->nmsg; j++)
483 char *cptr;
484 int l = blk->msgs[j]->len;
485 const char *comma = j == blk->nmsg-1 && i == lbp->nblk-1 ? "" : ",";
486 cptr = make_string(blk->msgs[j]->msg, l, unicodeout ? 0 : blk->msgs[j]->cp);
487 fprintf(fp, "\n /* Msg 0x%08x */ 0x%04x, 0x000%c,\n",
488 blk->idlo + j,
489 (unicodeout ? (l*2+3)&~3 : (l+3)&~3) + 4,
490 unicodeout ? '1' : '0');
491 fprintf(fp, "%s%s\n", cptr, comma);
492 free(cptr);
495 fprintf(fp, "}\n");
499 void write_rc_file(const char *fname)
501 FILE *fp = fopen(fname, "w");
503 if(!fp)
505 perror(fname);
506 exit(1);
508 fprintf(fp, str_header, input_name ? input_name : "<stdin>", cmdline);
510 if(rcinline)
511 write_rcinline(fp);
512 else
513 write_rcbin(fp);
514 fclose(fp);
517 static void output_bin_data( lan_blk_t *lbp )
519 unsigned int offs = 4 * (lbp->nblk * 3 + 1);
520 int i, j, k;
522 put_dword( lbp->nblk ); /* NBlocks */
523 for (i = 0; i < lbp->nblk; i++)
525 put_dword( lbp->blks[i].idlo ); /* Lo */
526 put_dword( lbp->blks[i].idhi ); /* Hi */
527 put_dword( offs ); /* Offs */
528 offs += lbp->blks[i].size;
530 for (i = 0; i < lbp->nblk; i++)
532 block_t *blk = &lbp->blks[i];
533 for (j = 0; j < blk->nmsg; j++)
535 int len = (2 * blk->msgs[j]->len + 3) & ~3;
536 put_word( len + 4 );
537 put_word( 1 );
538 for (k = 0; k < blk->msgs[j]->len; k++) put_word( blk->msgs[j]->msg[k] );
539 align_output( 4 );
544 void write_bin_files(void)
546 lan_blk_t *lbp;
547 token_t *ttab;
548 int ntab;
549 int i;
551 get_tokentable(&ttab, &ntab);
553 for (lbp = lanblockhead; lbp; lbp = lbp->next)
555 char *cptr = NULL;
557 for (i = 0; i < ntab; i++)
559 if (ttab[i].type == tok_language && ttab[i].token == lbp->lan)
561 if (ttab[i].alias) cptr = dup_u2c(WMC_DEFAULT_CODEPAGE, ttab[i].alias);
562 break;
565 if(!cptr)
566 internal_error(__FILE__, __LINE__, "Filename vanished for language 0x%0x\n", lbp->lan);
567 init_output_buffer();
568 output_bin_data( lbp );
569 cptr = xrealloc( cptr, strlen(cptr) + 5 );
570 strcat( cptr, ".bin" );
571 flush_output_buffer( cptr );
572 free(cptr);
576 void write_res_file( const char *name )
578 lan_blk_t *lbp;
579 int i, j;
581 init_output_buffer();
583 put_dword( 0 ); /* ResSize */
584 put_dword( 32 ); /* HeaderSize */
585 put_word( 0xffff ); /* ResType */
586 put_word( 0x0000 );
587 put_word( 0xffff ); /* ResName */
588 put_word( 0x0000 );
589 put_dword( 0 ); /* DataVersion */
590 put_word( 0 ); /* Memory options */
591 put_word( 0 ); /* Language */
592 put_dword( 0 ); /* Version */
593 put_dword( 0 ); /* Characteristics */
595 for (lbp = lanblockhead; lbp; lbp = lbp->next)
597 unsigned int data_size = 4 * (lbp->nblk * 3 + 1);
598 unsigned int header_size = 5 * sizeof(unsigned int) + 6 * sizeof(unsigned short);
600 for (i = 0; i < lbp->nblk; i++)
602 block_t *blk = &lbp->blks[i];
603 for (j = 0; j < blk->nmsg; j++) data_size += 4 + ((blk->msgs[j]->len * 2 + 3) & ~3);
606 put_dword( data_size ); /* ResSize */
607 put_dword( header_size ); /* HeaderSize */
608 put_word( 0xffff ); /* ResType */
609 put_word( 0x000b /*RT_MESSAGETABLE*/ );
610 put_word( 0xffff ); /* ResName */
611 put_word( 0x0001 );
612 align_output( 4 );
613 put_dword( 0 ); /* DataVersion */
614 put_word( 0x30 ); /* Memory options */
615 put_word( lbp->lan ); /* Language */
616 put_dword( lbp->version ); /* Version */
617 put_dword( 0 ); /* Characteristics */
619 output_bin_data( lbp );
621 flush_output_buffer( name );