combase/tests: Build without -DWINE_NO_LONG_TYPES.
[wine.git] / tools / wrc / genres.c
blob54c2504d2326c662e060f4029cd4a274361d9b92
1 /*
2 * Generate .res format from a resource-tree
4 * Copyright 1998 Bertho A. Stultiens
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
20 * History:
21 * 05-May-2000 BS - Added code to support endian conversions. The
22 * extra functions also aid unaligned access, but
23 * this is not yet implemented.
24 * 25-May-1998 BS - Added simple unicode -> char conversion for resource
25 * names in .s and .h files.
28 #include "config.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <assert.h>
35 #include <ctype.h>
37 #include "../tools.h"
38 #include "wrc.h"
39 #include "utils.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
45 unsigned char *output_buffer = NULL;
46 size_t output_buffer_pos = 0;
47 size_t output_buffer_size = 0;
49 static void set_word(int ofs, unsigned int w)
51 output_buffer[ofs+0] = w;
52 output_buffer[ofs+1] = w >> 8;
55 static void set_dword(int ofs, unsigned int d)
57 output_buffer[ofs+0] = d;
58 output_buffer[ofs+1] = d >> 8;
59 output_buffer[ofs+2] = d >> 16;
60 output_buffer[ofs+3] = d >> 24;
63 static unsigned int get_dword(int ofs)
65 return (output_buffer[ofs+3] << 24)
66 | (output_buffer[ofs+2] << 16)
67 | (output_buffer[ofs+1] << 8)
68 | output_buffer[ofs+0];
71 static void set_res_size(int tag)
73 set_dword( tag, output_buffer_pos - tag - get_dword(tag) );
77 *****************************************************************************
78 * Function : string_to_upper
79 * Syntax : void string_to_upper(string_t *str)
80 *****************************************************************************
82 static void string_to_upper(string_t *str)
84 int i;
86 switch (str->type)
88 case str_char:
89 for (i = 0; i < str->size; i++)
90 if (str->str.cstr[i] >= 'a' && str->str.cstr[i] <= 'z') str->str.cstr[i] -= 32;
91 break;
92 case str_unicode:
93 for (i = 0; i < str->size; i++)
94 if (str->str.wstr[i] >= 'a' && str->str.wstr[i] <= 'z') str->str.wstr[i] -= 32;
95 break;
99 static int parse_accel_string( const string_t *key, int flags )
101 int keycode = 0;
103 switch (key->type)
105 case str_char:
106 if (key->str.cstr[0] == '#') return 0; /* ignore message contexts */
107 if((flags & WRC_AF_VIRTKEY) &&
108 !((key->str.cstr[0] >= 'A' && key->str.cstr[0] <= 'Z') ||
109 (key->str.cstr[0] >= '0' && key->str.cstr[0] <= '9')))
111 print_location( &key->loc );
112 error("VIRTKEY code is not equal to ascii value\n");
115 if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
117 print_location( &key->loc );
118 error("Cannot use both '^' and CONTROL modifier\n");
120 else if(key->str.cstr[0] == '^')
122 if (key->str.cstr[1] >= 'a' && key->str.cstr[1] <= 'z')
123 keycode = key->str.cstr[1] - 'a' + 1;
124 else if (key->str.cstr[1] >= 'A' && key->str.cstr[1] <= 'Z')
125 keycode = key->str.cstr[1] - 'A' + 1;
126 else
128 print_location( &key->loc );
129 error("Control-code out of range\n");
132 else
133 keycode = key->str.cstr[0];
134 break;
136 case str_unicode:
137 if (key->str.wstr[0] == '#') return 0; /* ignore message contexts */
138 if((flags & WRC_AF_VIRTKEY) &&
139 !((key->str.wstr[0] >= 'A' && key->str.wstr[0] <= 'Z') ||
140 (key->str.wstr[0] >= '0' && key->str.wstr[0] <= '9')))
142 print_location( &key->loc );
143 error("VIRTKEY code is not equal to ascii value\n");
145 if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
147 print_location( &key->loc );
148 error("Cannot use both '^' and CONTROL modifier\n");
150 else if(key->str.wstr[0] == '^')
152 if (key->str.wstr[1] >= 'a' && key->str.wstr[1] <= 'z')
153 keycode = key->str.wstr[1] - 'a' + 1;
154 else if (key->str.wstr[1] >= 'A' && key->str.wstr[1] <= 'Z')
155 keycode = key->str.wstr[1] - 'A' + 1;
156 else
158 print_location( &key->loc );
159 error("Control-code out of range\n");
162 else
163 keycode = key->str.wstr[0];
164 break;
166 return keycode;
170 *****************************************************************************
171 * Function : put_string
172 * Input :
173 * str - String to put
174 * isterm - The string is '\0' terminated (disregard the string's
175 * size member)
176 *****************************************************************************
178 static void put_string(const string_t *str, int isterm, const language_t *lang)
180 int cnt, codepage;
182 if (win32)
184 string_t *newstr;
186 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
187 else codepage = get_language_codepage( 0, 0 );
188 assert( codepage != -1 );
190 newstr = convert_string_unicode( str, codepage );
191 if (str->type == str_char && check_valid_utf8( str, codepage ))
193 print_location( &str->loc );
194 warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use, maybe use --utf8?\n",
195 str->str.cstr, codepage );
197 if (!isterm) put_word(newstr->size);
198 for(cnt = 0; cnt < newstr->size; cnt++)
200 WCHAR c = newstr->str.wstr[cnt];
201 if (isterm && !c) break;
202 put_word(c);
204 if (isterm) put_word(0);
205 free_string(newstr);
207 else
209 assert( str->type == str_char );
210 if (!isterm) put_byte(str->size);
211 for(cnt = 0; cnt < str->size; cnt++)
213 char c = str->str.cstr[cnt];
214 if (isterm && !c) break;
215 put_byte(c);
217 if (isterm) put_byte(0);
222 *****************************************************************************
223 * Function : put_name_id
224 *****************************************************************************
226 static void put_name_id(name_id_t *nid, int upcase, const language_t *lang)
228 switch (nid->type)
230 case name_ord:
231 if(win32)
232 put_word(0xffff);
233 else
234 put_byte(0xff);
235 put_word(nid->name.i_name);
236 break;
237 case name_str:
238 if(upcase)
239 string_to_upper(nid->name.s_name);
240 put_string(nid->name.s_name, TRUE, lang);
241 break;
246 *****************************************************************************
247 * Function : put_lvc
248 *****************************************************************************
250 static void put_lvc(lvc_t *lvc)
252 if(lvc && lvc->language)
253 put_word(MAKELANGID(lvc->language->id, lvc->language->sub));
254 else
255 put_word(0); /* Neutral */
256 if(lvc && lvc->version)
257 put_dword(*lvc->version);
258 else
259 put_dword(0);
260 if(lvc && lvc->characts)
261 put_dword(*lvc->characts);
262 else
263 put_dword(0);
267 *****************************************************************************
268 * Function : put_res_header
269 * Input :
270 * type - Resource identifier
271 * name - Resource's name
272 * memopt - Resource's memory options to write
273 * lvc - Language, version and characteristics (win32 only)
274 * Output : An index to the resource size field. The resource size field
275 * contains the header size upon exit.
276 *****************************************************************************
278 static int put_res_header(int type, name_id_t *name, unsigned int memopt, lvc_t *lvc)
280 int tag = output_buffer_pos;
281 if(win32)
283 put_dword(0); /* We will overwrite these later */
284 put_dword(0);
285 put_word(0xffff); /* ResType */
286 put_word(type);
287 put_name_id(name, TRUE, lvc->language); /* ResName */
288 align_output(4);
289 put_dword(0); /* DataVersion */
290 put_word(memopt); /* Memory options */
291 put_lvc(lvc); /* Language, version and characts */
292 set_dword(tag + 0, output_buffer_pos - tag);
293 set_dword(tag + 4, output_buffer_pos - tag); /* Set HeaderSize */
295 else /* win16 */
297 put_byte(0xff); /* ResType */
298 put_word(type);
299 put_name_id(name, TRUE, NULL); /* ResName */
300 put_word(memopt); /* Memory options */
301 tag = output_buffer_pos;
302 put_dword(4); /* ResSize overwritten later*/
304 return tag;
308 *****************************************************************************
309 * Function : accelerator2res
310 * Input :
311 * name - Name/ordinal of the resource
312 * acc - The accelerator descriptor
313 *****************************************************************************
315 static void accelerator2res(name_id_t *name, accelerator_t *acc)
317 int restag;
318 event_t *ev;
320 restag = put_res_header(WRC_RT_ACCELERATOR, name, acc->memopt, &acc->lvc);
321 if(win32)
323 for (ev = acc->events; ev; ev = ev->next)
325 int key = ev->key;
326 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
327 put_word(ev->flags | (ev->next ? 0 : 0x80));
328 put_word(key);
329 put_word(ev->id);
330 align_output(4);
333 else /* win16 */
335 for (ev = acc->events; ev; ev = ev->next)
337 int key = ev->key;
338 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
339 put_byte(ev->flags | (ev->next ? 0 : 0x80));
340 put_word(key);
341 put_word(ev->id);
344 set_res_size(restag);
348 *****************************************************************************
349 * Function : dialog2res
350 * Input :
351 * name - Name/ordinal of the resource
352 * dlg - The dialog descriptor
353 *****************************************************************************
355 static void dialog2res(name_id_t *name, dialog_t *dlg)
357 int restag;
358 control_t *ctrl;
359 int nctrl;
361 restag = put_res_header(WRC_RT_DIALOG, name, dlg->memopt, &dlg->lvc);
362 for (ctrl = dlg->controls, nctrl = 0; ctrl; ctrl = ctrl->next) nctrl++;
363 if(win32)
365 if (dlg->is_ex)
367 /* FIXME: MS doc says that the first word must contain 0xffff
368 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
369 * compiler reverses the two words.
370 * I don't know which one to choose, but I write it as Mr. B
371 * writes it.
373 put_word(1); /* Signature */
374 put_word(0xffff); /* DlgVer */
375 put_dword(dlg->gothelpid ? dlg->helpid : 0);
376 put_dword(dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
377 put_dword(dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
379 else
381 put_dword(dlg->style->or_mask);
382 put_dword(dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
384 put_word(nctrl);
385 put_word(dlg->x);
386 put_word(dlg->y);
387 put_word(dlg->width);
388 put_word(dlg->height);
389 if(dlg->menu)
390 put_name_id(dlg->menu, FALSE, dlg->lvc.language);
391 else
392 put_word(0);
393 if(dlg->dlgclass)
394 put_name_id(dlg->dlgclass, FALSE, dlg->lvc.language);
395 else
396 put_word(0);
397 if(dlg->title)
398 put_string(dlg->title, TRUE, dlg->lvc.language);
399 else
400 put_word(0);
401 if(dlg->font)
403 put_word(dlg->font->size);
404 if (dlg->is_ex)
406 put_word(dlg->font->weight);
407 /* FIXME: ? TRUE should be sufficient to say that it's
408 * italic, but Borland's compiler says it's 0x0101.
409 * I just copy it here, and hope for the best.
411 put_word(dlg->font->italic ? 0x0101 : 0);
413 put_string(dlg->font->name, TRUE, dlg->lvc.language);
415 else if (dlg->style->or_mask & DS_SETFONT) put_word( 0x7fff );
417 align_output(4);
418 for (ctrl = dlg->controls; ctrl; ctrl = ctrl->next)
420 if (dlg->is_ex)
422 put_dword(ctrl->gothelpid ? ctrl->helpid : 0);
423 put_dword(ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
424 /* FIXME: what is default control style? */
425 put_dword(ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
427 else
429 /* FIXME: what is default control style? */
430 put_dword(ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
431 put_dword(ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
433 put_word(ctrl->x);
434 put_word(ctrl->y);
435 put_word(ctrl->width);
436 put_word(ctrl->height);
437 if (dlg->is_ex)
438 put_dword(ctrl->id);
439 else
440 put_word(ctrl->id);
441 put_name_id(ctrl->ctlclass, FALSE, dlg->lvc.language);
442 if(ctrl->title)
443 put_name_id(ctrl->title, FALSE, dlg->lvc.language);
444 else
445 put_word(0);
446 if(ctrl->extra)
448 put_word(ctrl->extra->size);
449 put_data(ctrl->extra->data, ctrl->extra->size);
451 else
452 put_word(0);
454 if(ctrl->next)
455 align_output(4);
458 else /* win16 */
460 restag = put_res_header(WRC_RT_DIALOG, name, dlg->memopt, NULL);
462 put_dword(dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
463 put_byte(nctrl);
464 put_word(dlg->x);
465 put_word(dlg->y);
466 put_word(dlg->width);
467 put_word(dlg->height);
468 if(dlg->menu)
469 put_name_id(dlg->menu, TRUE, NULL);
470 else
471 put_byte(0);
472 if(dlg->dlgclass)
473 put_name_id(dlg->dlgclass, TRUE, NULL);
474 else
475 put_byte(0);
476 if(dlg->title)
477 put_string(dlg->title, TRUE, NULL);
478 else
479 put_byte(0);
480 if(dlg->font)
482 put_word(dlg->font->size);
483 put_string(dlg->font->name, TRUE, NULL);
486 for (ctrl = dlg->controls; ctrl; ctrl = ctrl->next)
488 put_word(ctrl->x);
489 put_word(ctrl->y);
490 put_word(ctrl->width);
491 put_word(ctrl->height);
492 put_word(ctrl->id);
493 put_dword(ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
494 if(ctrl->ctlclass->type == name_ord
495 && ctrl->ctlclass->name.i_name >= 0x80
496 && ctrl->ctlclass->name.i_name <= 0x85)
497 put_byte(ctrl->ctlclass->name.i_name);
498 else if(ctrl->ctlclass->type == name_str)
499 put_name_id(ctrl->ctlclass, FALSE, NULL);
500 else
501 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
502 if(ctrl->title)
503 put_name_id(ctrl->title, FALSE, NULL);
504 else
505 put_byte(0);
507 /* FIXME: What is this extra byte doing here? */
508 put_byte(0);
511 set_res_size(restag);
515 *****************************************************************************
516 * Function : menuitem2res
517 * Remarks : Self recursive
518 *****************************************************************************
520 static void menuitem2res(menu_item_t *menitem, const language_t *lang)
522 menu_item_t *itm = menitem;
524 while(itm)
526 put_word(itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
527 if(!itm->popup)
528 put_word(itm->id);
529 if(itm->name)
530 put_string(itm->name, TRUE, lang);
531 else
533 if (win32) put_word(0);
534 else put_byte(0);
536 if(itm->popup)
537 menuitem2res(itm->popup, lang);
538 itm = itm->next;
543 *****************************************************************************
544 * Function : menuexitem2res
545 * Remarks : Self recursive
546 *****************************************************************************
548 static void menuexitem2res(menu_item_t *menitem, const language_t *lang)
550 menu_item_t *itm = menitem;
551 assert(win32 != 0);
552 while(itm)
554 put_dword(itm->gottype ? itm->type : 0);
555 put_dword(itm->gotstate ? itm->state : 0);
556 put_dword(itm->gotid ? itm->id : 0);
557 put_word((itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
558 if(itm->name)
559 put_string(itm->name, TRUE, lang);
560 else
561 put_word(0);
562 align_output(4);
563 if(itm->popup)
565 put_dword(itm->gothelpid ? itm->helpid : 0);
566 menuexitem2res(itm->popup, lang);
568 itm = itm->next;
574 *****************************************************************************
575 * Function : menu2res
576 * Input :
577 * name - Name/ordinal of the resource
578 * menex - The menuex descriptor
579 *****************************************************************************
581 static void menu2res(name_id_t *name, menu_t *men)
583 int restag;
584 assert(name != NULL);
585 assert(men != NULL);
587 restag = put_res_header(WRC_RT_MENU, name, men->memopt, &men->lvc);
588 if(win32)
590 if (men->is_ex)
592 put_word(1); /* Menuheader: Version */
593 put_word(4); /* Offset */
594 put_dword(0); /* HelpId */
595 align_output(4);
596 menuexitem2res(men->items, men->lvc.language);
598 else
600 put_dword(0); /* Menuheader: Version and HeaderSize */
601 menuitem2res(men->items, men->lvc.language);
604 else /* win16 */
606 put_dword(0); /* Menuheader: Version and HeaderSize */
607 menuitem2res(men->items, NULL);
609 set_res_size(restag);
613 *****************************************************************************
614 * Function : cursorgroup2res
615 * Input :
616 * name - Name/ordinal of the resource
617 * curg - The cursor descriptor
618 *****************************************************************************
620 static void cursorgroup2res(name_id_t *name, cursor_group_t *curg)
622 int restag;
623 cursor_t *cur;
625 restag = put_res_header(WRC_RT_GROUP_CURSOR, name, curg->memopt, &curg->lvc);
627 put_word(0); /* Reserved */
628 /* FIXME: The ResType in the NEWHEADER structure should
629 * contain 14 according to the MS win32 doc. This is
630 * not the case with the BRC compiler and I really doubt
631 * the latter. Putting one here is compliant to win16 spec,
632 * but who knows the true value?
634 put_word(2); /* ResType */
635 put_word(curg->ncursor);
636 #if 0
637 for(cur = curg->cursorlist; cur; cur = cur->next)
638 #else
639 cur = curg->cursorlist;
640 while(cur->next)
641 cur = cur->next;
642 for(; cur; cur = cur->prev)
643 #endif
645 put_word(cur->width);
646 /* FIXME: The height of a cursor is half the size of
647 * the bitmap's height. BRC puts the height from the
648 * BITMAPINFOHEADER here instead of the cursorfile's
649 * height. MS doesn't seem to care...
651 put_word(cur->height);
652 /* FIXME: The next two are reversed in BRC and I don't
653 * know why. Probably a bug. But, we can safely ignore
654 * it because win16 does not support color cursors.
655 * A warning should have been generated by the parser.
657 put_word(cur->planes);
658 put_word(cur->bits);
659 /* FIXME: The +4 is the hotspot in the cursor resource.
660 * However, I could not find this in the documentation.
661 * The hotspot bytes must either be included or MS
662 * doesn't care.
664 put_dword(cur->data->size +4);
665 put_word(cur->id);
668 set_res_size(restag);
672 *****************************************************************************
673 * Function : cursor2res
674 * Input :
675 * cur - The cursor descriptor
676 *****************************************************************************
678 static void cursor2res(cursor_t *cur)
680 int restag;
681 name_id_t name;
683 name.type = name_ord;
684 name.name.i_name = cur->id;
685 restag = put_res_header(WRC_RT_CURSOR, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &cur->lvc);
686 put_word(cur->xhot);
687 put_word(cur->yhot);
688 put_data(cur->data->data, cur->data->size);
690 set_res_size(restag);
694 *****************************************************************************
695 * Function : icongroup2res
696 * Input :
697 * name - Name/ordinal of the resource
698 * icog - The icon group descriptor
699 *****************************************************************************
701 static void icongroup2res(name_id_t *name, icon_group_t *icog)
703 int restag;
704 icon_t *ico;
706 restag = put_res_header(WRC_RT_GROUP_ICON, name, icog->memopt, &icog->lvc);
708 put_word(0); /* Reserved */
709 /* FIXME: The ResType in the NEWHEADER structure should
710 * contain 14 according to the MS win32 doc. This is
711 * not the case with the BRC compiler and I really doubt
712 * the latter. Putting one here is compliant to win16 spec,
713 * but who knows the true value?
715 put_word(1); /* ResType */
716 put_word(icog->nicon);
717 for(ico = icog->iconlist; ico; ico = ico->next)
719 put_byte(ico->width);
720 put_byte(ico->height);
721 put_byte(ico->nclr);
722 put_byte(0); /* Reserved */
723 put_word(ico->planes);
724 put_word(ico->bits);
725 put_dword(ico->data->size);
726 put_word(ico->id);
729 set_res_size(restag);
733 *****************************************************************************
734 * Function : icon2res
735 * Input :
736 * ico - The icon descriptor
737 *****************************************************************************
739 static void icon2res(icon_t *ico)
741 int restag;
742 name_id_t name;
744 name.type = name_ord;
745 name.name.i_name = ico->id;
746 restag = put_res_header(WRC_RT_ICON, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &ico->lvc);
747 put_data(ico->data->data, ico->data->size);
748 set_res_size(restag);
752 *****************************************************************************
753 * Function : anicurico2res
754 * Input :
755 * name - Name/ordinal of the resource
756 * ani - The animated object descriptor
757 * Remarks : There are rumors that win311 could handle animated stuff.
758 * That is why they are generated for both win16 and win32
759 * compile.
760 *****************************************************************************
762 static void anicur2res(name_id_t *name, ani_curico_t *ani)
764 int restag = put_res_header(WRC_RT_ANICURSOR, name, ani->memopt, NULL);
766 put_data(ani->data->data, ani->data->size);
767 set_res_size(restag);
770 static void aniico2res(name_id_t *name, ani_curico_t *ani)
772 int restag = put_res_header(WRC_RT_ANIICON, name, ani->memopt, NULL);
774 put_data(ani->data->data, ani->data->size);
775 set_res_size(restag);
779 *****************************************************************************
780 * Function : bitmap2res
781 * Input :
782 * name - Name/ordinal of the resource
783 * bmp - The bitmap descriptor
784 *****************************************************************************
786 static void bitmap2res(name_id_t *name, bitmap_t *bmp)
788 int restag = put_res_header(WRC_RT_BITMAP, name, bmp->memopt, &bmp->data->lvc);
790 if(bmp->data->data[0] == 'B'
791 && bmp->data->data[1] == 'M'
792 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
793 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
795 /* The File header is still attached, don't write it */
796 put_data((BITMAPFILEHEADER *)bmp->data->data + 1, bmp->data->size - sizeof(BITMAPFILEHEADER));
798 else
800 put_data(bmp->data->data, bmp->data->size);
802 set_res_size(restag);
806 *****************************************************************************
807 * Function : font2res
808 * Input :
809 * name - Name/ordinal of the resource
810 * fnt - The font descriptor
811 * Remarks : The data has been prepared just after parsing.
812 *****************************************************************************
814 static void font2res(name_id_t *name, font_t *fnt)
816 int restag = put_res_header(WRC_RT_FONT, name, fnt->memopt, &fnt->data->lvc);
818 put_data(fnt->data->data, fnt->data->size);
819 set_res_size(restag);
823 *****************************************************************************
824 * Function : fontdir2res
825 * Input :
826 * name - Name/ordinal of the resource
827 * fntdir - The fontdir descriptor
828 * Remarks : The data has been prepared just after parsing.
829 *****************************************************************************
831 static void fontdir2res(name_id_t *name, fontdir_t *fnd)
833 int restag = put_res_header(WRC_RT_FONTDIR, name, fnd->memopt, &fnd->data->lvc);
835 put_data(fnd->data->data, fnd->data->size);
836 set_res_size(restag);
840 *****************************************************************************
841 * Function : html2res
842 * Input :
843 * name - Name/ordinal of the resource
844 * rdt - The html descriptor
845 *****************************************************************************
847 static void html2res(name_id_t *name, html_t *html)
849 int restag = put_res_header(WRC_RT_HTML, name, html->memopt, &html->data->lvc);
851 put_data(html->data->data, html->data->size);
852 set_res_size(restag);
856 *****************************************************************************
857 * Function : rcdata2res
858 * Input :
859 * name - Name/ordinal of the resource
860 * rdt - The rcdata descriptor
861 *****************************************************************************
863 static void rcdata2res(name_id_t *name, rcdata_t *rdt)
865 int restag = put_res_header(WRC_RT_RCDATA, name, rdt->memopt, &rdt->data->lvc);
867 put_data(rdt->data->data, rdt->data->size);
868 set_res_size(restag);
872 *****************************************************************************
873 * Function : messagetable2res
874 * Input :
875 * name - Name/ordinal of the resource
876 * msg - The messagetable descriptor
877 *****************************************************************************
879 static void messagetable2res(name_id_t *name, messagetable_t *msg)
881 int restag = put_res_header(WRC_RT_MESSAGETABLE, name, msg->memopt, &msg->data->lvc);
883 put_data(msg->data->data, msg->data->size);
884 set_res_size(restag);
888 *****************************************************************************
889 * Function : stringtable2res
890 * Input :
891 * stt - The stringtable descriptor
892 *****************************************************************************
894 static void stringtable2res(stringtable_t *stt)
896 name_id_t name;
897 int i;
898 int restag;
900 for(; stt; stt = stt->next)
902 if(!stt->nentries)
904 warning("Empty internal stringtable\n");
905 continue;
907 name.type = name_ord;
908 name.name.i_name = (stt->idbase >> 4) + 1;
909 restag = put_res_header(WRC_RT_STRING, &name, stt->memopt, &stt->lvc);
910 for(i = 0; i < stt->nentries; i++)
912 if(stt->entries[i].str && stt->entries[i].str->size)
914 put_string(stt->entries[i].str, FALSE, stt->lvc.language);
916 else
918 if (win32)
919 put_word(0);
920 else
921 put_byte(0);
924 set_res_size(restag);
929 *****************************************************************************
930 * Function : user2res
931 * Input :
932 * name - Name/ordinal of the resource
933 * usr - The userresource descriptor
934 *****************************************************************************
936 static void user2res(name_id_t *name, user_t *usr)
938 int tag = output_buffer_pos;
940 if(win32)
942 put_dword(0); /* We will overwrite these later */
943 put_dword(0);
944 put_name_id(usr->type, TRUE, usr->data->lvc.language);
945 put_name_id(name, TRUE, usr->data->lvc.language); /* ResName */
946 align_output(4);
947 put_dword(0); /* DataVersion */
948 put_word(usr->memopt); /* Memory options */
949 put_lvc(&usr->data->lvc); /* Language, version and characts */
950 set_dword(tag + 0, output_buffer_pos - tag);
951 set_dword(tag + 4, output_buffer_pos - tag); /* Set HeaderSize */
953 else /* win16 */
955 put_name_id(usr->type, TRUE, NULL);
956 put_name_id(name, TRUE, NULL); /* ResName */
957 put_word(usr->memopt); /* Memory options */
958 tag = output_buffer_pos;
959 put_dword(4); /* ResSize overwritten later*/
962 put_data(usr->data->data, usr->data->size);
963 set_res_size(tag);
967 *****************************************************************************
968 * Function : versionblock2res
969 * Input :
970 * blk - The version block to be written
971 * Remarks : Self recursive
972 *****************************************************************************
974 static void versionblock2res(ver_block_t *blk, int level, const language_t *lang)
976 ver_value_t *val;
977 int blksizetag;
978 int valblksizetag;
979 int valvalsizetag;
980 int tag;
981 int i;
983 blksizetag = output_buffer_pos;
984 put_word(0); /* Will be overwritten later */
985 put_word(0);
986 if(win32)
987 put_word(0); /* level ? */
988 put_string(blk->name, TRUE, NULL);
989 align_output(4);
990 for(val = blk->values; val; val = val->next)
992 switch(val->type)
994 case val_str:
995 valblksizetag = output_buffer_pos;
996 put_word(0); /* Will be overwritten later */
997 valvalsizetag = output_buffer_pos;
998 put_word(0); /* Will be overwritten later */
999 if(win32)
1001 put_word(level);
1003 put_string(val->key, TRUE, NULL);
1004 align_output(4);
1005 tag = output_buffer_pos;
1006 put_string(val->value.str, TRUE, lang);
1007 if(win32)
1008 set_word(valvalsizetag, (output_buffer_pos - tag) >> 1);
1009 else
1010 set_word(valvalsizetag, output_buffer_pos - tag);
1011 set_word(valblksizetag, output_buffer_pos - valblksizetag);
1012 align_output(4);
1013 break;
1014 case val_words:
1015 valblksizetag = output_buffer_pos;
1016 put_word(0); /* Will be overwritten later */
1017 valvalsizetag = output_buffer_pos;
1018 put_word(0); /* Will be overwritten later */
1019 if(win32)
1021 put_word(level);
1023 put_string(val->key, TRUE, NULL);
1024 align_output(4);
1025 tag = output_buffer_pos;
1026 for(i = 0; i < val->value.words->nwords; i++)
1028 put_word(val->value.words->words[i]);
1030 set_word(valvalsizetag, output_buffer_pos - tag);
1031 set_word(valblksizetag, output_buffer_pos - valblksizetag);
1032 align_output(4);
1033 break;
1034 case val_block:
1035 versionblock2res(val->value.block, level+1, lang);
1036 break;
1040 /* Set blocksize */
1041 set_word(blksizetag, output_buffer_pos - blksizetag);
1045 *****************************************************************************
1046 * Function : versioninfo2res
1047 * Input :
1048 * name - Name/ordinal of the resource
1049 * ver - The versioninfo descriptor
1050 *****************************************************************************
1052 static void versioninfo2res(name_id_t *name, versioninfo_t *ver)
1054 static const char info[] = "VS_VERSION_INFO";
1055 unsigned int i;
1056 int restag, rootblocksizetag, valsizetag, tag;
1057 ver_block_t *blk;
1059 restag = put_res_header(WRC_RT_VERSION, name, ver->memopt, &ver->lvc);
1060 rootblocksizetag = output_buffer_pos;
1061 put_word(0); /* BlockSize filled in later */
1062 valsizetag = output_buffer_pos;
1063 put_word(0); /* ValueSize filled in later*/
1064 if(win32)
1066 put_word(0); /* Tree-level ? */
1067 for (i = 0; i < sizeof(info); i++) put_word(info[i]);
1068 align_output(4);
1070 else
1072 for (i = 0; i < sizeof(info); i++) put_byte(info[i]);
1074 tag = output_buffer_pos;
1075 put_dword(VS_FFI_SIGNATURE);
1076 put_dword(VS_FFI_STRUCVERSION);
1077 put_dword((ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1078 put_dword((ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1079 put_dword((ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1080 put_dword((ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1081 put_dword(ver->fileflagsmask);
1082 put_dword(ver->fileflags);
1083 put_dword(ver->fileos);
1084 put_dword(ver->filetype);
1085 put_dword(ver->filesubtype);
1086 put_dword(0); /* FileDateMS */
1087 put_dword(0); /* FileDateLS */
1088 /* Set ValueSize */
1089 set_word(valsizetag, output_buffer_pos - tag);
1090 /* Descend into the blocks */
1091 for(blk = ver->blocks; blk; blk = blk->next)
1092 versionblock2res(blk, 0, win32 ? ver->lvc.language : NULL);
1093 /* Set root block's size */
1094 set_word(rootblocksizetag, output_buffer_pos - rootblocksizetag);
1095 set_res_size(restag);
1099 *****************************************************************************
1100 * Function : toolbar2res
1101 * Input :
1102 * name - Name/ordinal of the resource
1103 * toolbar - The toolbar descriptor
1104 *****************************************************************************
1106 static void toolbar2res(name_id_t *name, toolbar_t *toolbar)
1108 toolbar_item_t *itm;
1109 int restag;
1111 if (!win32) return;
1113 restag = put_res_header(WRC_RT_TOOLBAR, name, toolbar->memopt, &toolbar->lvc);
1114 put_word(1); /* Menuheader: Version */
1115 put_word(toolbar->button_width); /* (in pixels?) */
1116 put_word(toolbar->button_height); /* (in pixels?) */
1117 put_word(toolbar->nitems);
1118 align_output(4);
1119 for (itm = toolbar->items; itm; itm = itm->next) put_word(itm->id);
1120 set_res_size(restag);
1124 *****************************************************************************
1125 * Function : dlginit2res
1126 * Input :
1127 * name - Name/ordinal of the resource
1128 * rdt - The dlginit descriptor
1129 *****************************************************************************
1131 static void dlginit2res(name_id_t *name, dlginit_t *dit)
1133 int restag = put_res_header(WRC_RT_DLGINIT, name, dit->memopt, &dit->data->lvc);
1135 put_data(dit->data->data, dit->data->size);
1136 set_res_size(restag);
1140 *****************************************************************************
1141 * Function : write_resfile
1142 * Syntax : void write_resfile(char *outname, resource_t *top)
1143 * Input :
1144 * outname - Filename to write to
1145 * top - The resource-tree to convert
1146 *****************************************************************************
1148 void write_resfile(char *outname, resource_t *top)
1150 init_output_buffer();
1152 if(win32)
1154 /* Put an empty resource first to signal win32 format */
1155 put_dword(0); /* ResSize */
1156 put_dword(0x00000020); /* HeaderSize */
1157 put_word(0xffff); /* ResType */
1158 put_word(0);
1159 put_word(0xffff); /* ResName */
1160 put_word(0);
1161 put_dword(0); /* DataVersion */
1162 put_word(0); /* Memory options */
1163 put_word(0); /* Language */
1164 put_dword(0); /* Version */
1165 put_dword(0); /* Characteristics */
1168 for ( ; top; top = top->next)
1170 switch(top->type)
1172 case res_acc: accelerator2res(top->name, top->res.acc); break;
1173 case res_bmp: bitmap2res(top->name, top->res.bmp); break;
1174 case res_cur: cursor2res(top->res.cur); break;
1175 case res_curg: cursorgroup2res(top->name, top->res.curg); break;
1176 case res_dlg: dialog2res(top->name, top->res.dlg); break;
1177 case res_fnt: font2res(top->name, top->res.fnt); break;
1178 case res_fntdir: fontdir2res(top->name, top->res.fnd); break;
1179 case res_ico: icon2res(top->res.ico); break;
1180 case res_icog: icongroup2res(top->name, top->res.icog); break;
1181 case res_men: menu2res(top->name, top->res.men); break;
1182 case res_html: html2res(top->name, top->res.html); break;
1183 case res_rdt: rcdata2res(top->name, top->res.rdt); break;
1184 case res_stt: stringtable2res(top->res.stt); break;
1185 case res_usr: user2res(top->name, top->res.usr); break;
1186 case res_msg: messagetable2res(top->name, top->res.msg); break;
1187 case res_ver: versioninfo2res(top->name, top->res.ver); break;
1188 case res_toolbar: toolbar2res(top->name, top->res.tbt); break;
1189 case res_dlginit: dlginit2res(top->name, top->res.dlgi); break;
1190 case res_anicur: anicur2res(top->name, top->res.ani); break;
1191 case res_aniico: aniico2res(top->name, top->res.ani); break;
1192 default: assert(0);
1194 if (win32) align_output( 4 );
1197 flush_output_buffer( outname );