msvcp110: Always return empty string in _Read_dir on end of enumeration or error.
[wine.git] / tools / wrc / genres.c
blob8993a873b67cbc08801ca7279c8b2b1d0b31e043
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 "wrc.h"
38 #include "genres.h"
39 #include "utils.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
45 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
47 res_t *new_res(void)
49 res_t *r;
50 r = xmalloc(sizeof(res_t));
51 r->allocsize = RES_BLOCKSIZE;
52 r->size = 0;
53 r->dataidx = 0;
54 r->data = xmalloc(RES_BLOCKSIZE);
55 return r;
58 res_t *grow_res(res_t *r, unsigned int add)
60 r->allocsize += add;
61 r->data = xrealloc(r->data, r->allocsize);
62 return r;
66 *****************************************************************************
67 * Function : put_byte
68 * put_word
69 * put_dword
70 * Syntax : void put_byte(res_t *res, unsigned c)
71 * void put_word(res_t *res, unsigned w)
72 * void put_dword(res_t *res, unsigned d)
73 * Input :
74 * res - Binary resource to put the data in
75 * c, w, d - Data to put
76 * Output : nop
77 * Description : Put primitives that put an item in the binary resource.
78 * The data array grows automatically.
79 * Remarks :
80 *****************************************************************************
82 void put_byte(res_t *res, unsigned c)
84 if(res->allocsize - res->size < sizeof(char))
85 grow_res(res, RES_BLOCKSIZE);
86 res->data[res->size] = (char)c;
87 res->size += sizeof(char);
90 void put_word(res_t *res, unsigned w)
92 if(res->allocsize - res->size < sizeof(WORD))
93 grow_res(res, RES_BLOCKSIZE);
94 switch(byteorder)
96 #ifdef WORDS_BIGENDIAN
97 default:
98 #endif
99 case WRC_BO_BIG:
100 res->data[res->size+0] = HIBYTE(w);
101 res->data[res->size+1] = LOBYTE(w);
102 break;
104 #ifndef WORDS_BIGENDIAN
105 default:
106 #endif
107 case WRC_BO_LITTLE:
108 res->data[res->size+1] = HIBYTE(w);
109 res->data[res->size+0] = LOBYTE(w);
110 break;
112 res->size += sizeof(WORD);
115 void put_dword(res_t *res, unsigned d)
117 if(res->allocsize - res->size < sizeof(DWORD))
118 grow_res(res, RES_BLOCKSIZE);
119 switch(byteorder)
121 #ifdef WORDS_BIGENDIAN
122 default:
123 #endif
124 case WRC_BO_BIG:
125 res->data[res->size+0] = HIBYTE(HIWORD(d));
126 res->data[res->size+1] = LOBYTE(HIWORD(d));
127 res->data[res->size+2] = HIBYTE(LOWORD(d));
128 res->data[res->size+3] = LOBYTE(LOWORD(d));
129 break;
131 #ifndef WORDS_BIGENDIAN
132 default:
133 #endif
134 case WRC_BO_LITTLE:
135 res->data[res->size+3] = HIBYTE(HIWORD(d));
136 res->data[res->size+2] = LOBYTE(HIWORD(d));
137 res->data[res->size+1] = HIBYTE(LOWORD(d));
138 res->data[res->size+0] = LOBYTE(LOWORD(d));
139 break;
141 res->size += sizeof(DWORD);
144 static void put_pad(res_t *res)
146 while(res->size & 0x3)
147 put_byte(res, 0);
151 *****************************************************************************
152 * Function : set_word
153 * set_dword
154 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
155 * void set_dword(res_t *res, int ofs, unsigned d)
156 * Input :
157 * res - Binary resource to put the data in
158 * ofs - Byte offset in data-array
159 * w, d - Data to put
160 * Output : nop
161 * Description : Set the value of a binary resource data array to a
162 * specific value.
163 * Remarks :
164 *****************************************************************************
166 static void set_word(res_t *res, int ofs, unsigned w)
168 switch(byteorder)
170 #ifdef WORDS_BIGENDIAN
171 default:
172 #endif
173 case WRC_BO_BIG:
174 res->data[ofs+0] = HIBYTE(w);
175 res->data[ofs+1] = LOBYTE(w);
176 break;
178 #ifndef WORDS_BIGENDIAN
179 default:
180 #endif
181 case WRC_BO_LITTLE:
182 res->data[ofs+1] = HIBYTE(w);
183 res->data[ofs+0] = LOBYTE(w);
184 break;
188 static void set_dword(res_t *res, int ofs, unsigned d)
190 switch(byteorder)
192 #ifdef WORDS_BIGENDIAN
193 default:
194 #endif
195 case WRC_BO_BIG:
196 res->data[ofs+0] = HIBYTE(HIWORD(d));
197 res->data[ofs+1] = LOBYTE(HIWORD(d));
198 res->data[ofs+2] = HIBYTE(LOWORD(d));
199 res->data[ofs+3] = LOBYTE(LOWORD(d));
200 break;
202 #ifndef WORDS_BIGENDIAN
203 default:
204 #endif
205 case WRC_BO_LITTLE:
206 res->data[ofs+3] = HIBYTE(HIWORD(d));
207 res->data[ofs+2] = LOBYTE(HIWORD(d));
208 res->data[ofs+1] = HIBYTE(LOWORD(d));
209 res->data[ofs+0] = LOBYTE(LOWORD(d));
210 break;
215 *****************************************************************************
216 * Function : get_dword
217 * Input :
218 * res - Binary resource to put the data in
219 * ofs - Byte offset in data-array
220 * Output : The data in native endian
221 * Description : Get the value of a binary resource data array in native
222 * endian.
223 * Remarks :
224 *****************************************************************************
226 static DWORD get_dword(res_t *res, int ofs)
228 switch(byteorder)
230 #ifdef WORDS_BIGENDIAN
231 default:
232 #endif
233 case WRC_BO_BIG:
234 return (res->data[ofs+0] << 24)
235 | (res->data[ofs+1] << 16)
236 | (res->data[ofs+2] << 8)
237 | res->data[ofs+3];
239 #ifndef WORDS_BIGENDIAN
240 default:
241 #endif
242 case WRC_BO_LITTLE:
243 return (res->data[ofs+3] << 24)
244 | (res->data[ofs+2] << 16)
245 | (res->data[ofs+1] << 8)
246 | res->data[ofs+0];
251 *****************************************************************************
252 * Function : string_to_upper
253 * Syntax : void string_to_upper(string_t *str)
254 * Input :
255 * Output :
256 * Description :
257 * Remarks : FIXME: codepages...
258 *****************************************************************************
260 static void string_to_upper(string_t *str)
262 int i;
264 if(str->type == str_char)
266 for (i = 0; i < str->size; i++) str->str.cstr[i] = toupper((unsigned char)str->str.cstr[i]);
268 else if(str->type == str_unicode)
270 for (i = 0; i < str->size; i++) str->str.wstr[i] = toupperW(str->str.wstr[i]);
272 else
274 internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
278 static int parse_accel_string( const string_t *key, int flags )
280 int keycode;
282 if(key->type == str_char)
284 if (key->str.cstr[0] == '#') return 0; /* ignore message contexts */
285 if((flags & WRC_AF_VIRTKEY) &&
286 !((key->str.cstr[0] >= 'A' && key->str.cstr[0] <= 'Z') ||
287 (key->str.cstr[0] >= '0' && key->str.cstr[0] <= '9')))
289 print_location( &key->loc );
290 error("VIRTKEY code is not equal to ascii value\n");
293 if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
295 print_location( &key->loc );
296 error("Cannot use both '^' and CONTROL modifier\n");
298 else if(key->str.cstr[0] == '^')
300 keycode = toupper((unsigned char)key->str.cstr[1]) - '@';
301 if(keycode >= ' ')
303 print_location( &key->loc );
304 error("Control-code out of range\n");
307 else
308 keycode = key->str.cstr[0];
310 else
312 if (key->str.wstr[0] == '#') return 0; /* ignore message contexts */
313 if((flags & WRC_AF_VIRTKEY) &&
314 !((key->str.wstr[0] >= 'A' && key->str.wstr[0] <= 'Z') ||
315 (key->str.wstr[0] >= '0' && key->str.wstr[0] <= '9')))
317 print_location( &key->loc );
318 error("VIRTKEY code is not equal to ascii value\n");
320 if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
322 print_location( &key->loc );
323 error("Cannot use both '^' and CONTROL modifier\n");
325 else if(key->str.wstr[0] == '^')
327 keycode = toupperW(key->str.wstr[1]) - '@';
328 if(keycode >= ' ')
330 print_location( &key->loc );
331 error("Control-code out of range\n");
334 else
335 keycode = key->str.wstr[0];
337 return keycode;
341 *****************************************************************************
342 * Function : put_string
343 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
344 * int isterm, const language_t *lang)
345 * Input :
346 * res - Binary resource to put the data in
347 * str - String to put
348 * type - Data has to be written in either str_char or str_unicode
349 * isterm - The string is '\0' terminated (disregard the string's
350 * size member)
351 * Output : nop
352 * Description :
353 * Remarks :
354 *****************************************************************************
356 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
357 const language_t *lang)
359 int cnt, codepage;
360 string_t *newstr;
362 assert(res != NULL);
363 assert(str != NULL);
365 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
366 else codepage = get_language_codepage( 0, 0 );
368 assert( codepage != -1 );
370 newstr = convert_string(str, type, codepage);
371 if (type == str_unicode)
373 if (str->type == str_char)
375 if (!check_unicode_conversion( str, newstr, codepage ))
377 print_location( &str->loc );
378 error( "String %s does not convert identically to Unicode and back in codepage %d. "
379 "Try using a Unicode string instead\n", str->str.cstr, codepage );
381 if (check_valid_utf8( str, codepage ))
383 print_location( &str->loc );
384 warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use.\n",
385 str->str.cstr, codepage );
388 if (!isterm) put_word(res, newstr->size);
389 for(cnt = 0; cnt < newstr->size; cnt++)
391 WCHAR c = newstr->str.wstr[cnt];
392 if (isterm && !c) break;
393 put_word(res, c);
395 if (isterm) put_word(res, 0);
397 else /* str_char */
399 if (!isterm) put_byte(res, newstr->size);
400 for(cnt = 0; cnt < newstr->size; cnt++)
402 char c = newstr->str.cstr[cnt];
403 if (isterm && !c) break;
404 put_byte(res, c);
406 if (isterm) put_byte(res, 0);
408 free_string(newstr);
412 *****************************************************************************
413 * Function : put_name_id
414 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
415 * Input :
416 * Output :
417 * Description :
418 * Remarks :
419 *****************************************************************************
421 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
423 if(nid->type == name_ord)
425 if(win32)
426 put_word(res, 0xffff);
427 else
428 put_byte(res, 0xff);
429 put_word(res, (WORD)nid->name.i_name);
431 else if(nid->type == name_str)
433 if(upcase)
434 string_to_upper(nid->name.s_name);
435 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
437 else
439 internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
444 *****************************************************************************
445 * Function : put_lvc
446 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
447 * Input :
448 * Output :
449 * Description :
450 * Remarks :
451 *****************************************************************************
453 static void put_lvc(res_t *res, lvc_t *lvc)
455 if(lvc && lvc->language)
456 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
457 else
458 put_word(res, 0); /* Neutral */
459 if(lvc && lvc->version)
460 put_dword(res, *(lvc->version));
461 else
462 put_dword(res, 0);
463 if(lvc && lvc->characts)
464 put_dword(res, *(lvc->characts));
465 else
466 put_dword(res, 0);
470 *****************************************************************************
471 * Function : put_raw_data
472 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
473 * Input :
474 * Output :
475 * Description :
476 * Remarks :
477 *****************************************************************************
479 static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
481 unsigned int wsize = raw->size - offset;
482 if(res->allocsize - res->size < wsize)
483 grow_res(res, wsize);
484 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
485 res->size += wsize;
489 *****************************************************************************
490 * Function : put_res_header
491 * Syntax : input_res_header(res_t *res, int type, name_id_t *ntype,
492 * name_id_t *name, DWORD memopt, lvc_t *lvc)
494 * Input :
495 * res - Binary resource descriptor to write to
496 * type - Resource identifier (if ntype == NULL)
497 * ntype - Name id of type
498 * name - Resource's name
499 * memopt - Resource's memory options to write
500 * lvc - Language, version and characteristics (win32 only)
501 * Output : An index to the resource size field. The resource size field
502 * contains the header size upon exit.
503 * Description :
504 * Remarks :
505 *****************************************************************************
507 static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
508 DWORD memopt, lvc_t *lvc)
510 if(win32)
512 put_dword(res, 0); /* We will overwrite these later */
513 put_dword(res, 0);
514 if(!ntype)
516 put_word(res, 0xffff); /* ResType */
517 put_word(res, type);
519 else
520 put_name_id(res, ntype, TRUE, lvc->language);
521 put_name_id(res, name, TRUE, lvc->language); /* ResName */
522 put_pad(res);
523 put_dword(res, 0); /* DataVersion */
524 put_word(res, memopt); /* Memory options */
525 put_lvc(res, lvc); /* Language, version and characts */
526 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
527 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
528 res->dataidx = res->size;
529 return 0;
531 else /* win16 */
533 int tag;
534 if(!ntype)
536 put_byte(res, 0xff); /* ResType */
537 put_word(res, type);
539 else
540 put_name_id(res, ntype, TRUE, NULL);
541 put_name_id(res, name, TRUE, NULL); /* ResName */
542 put_word(res, memopt); /* Memory options */
543 tag = res->size;
544 put_dword(res, 0); /* ResSize overwritten later*/
545 set_dword(res, tag, res->size);
546 res->dataidx = res->size;
547 return tag;
552 *****************************************************************************
553 * Function : accelerator2res
554 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
555 * Input :
556 * name - Name/ordinal of the resource
557 * acc - The accelerator descriptor
558 * Output : New .res format structure
559 * Description :
560 * Remarks :
561 *****************************************************************************
563 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
565 int restag;
566 res_t *res;
567 event_t *ev;
568 assert(name != NULL);
569 assert(acc != NULL);
571 ev = acc->events;
572 res = new_res();
573 if(win32)
575 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
576 while(ev)
578 int key = ev->key;
579 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
580 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
581 put_word(res, key);
582 put_word(res, ev->id);
583 put_word(res, 0); /* Padding */
584 ev = ev->next;
586 put_pad(res);
588 else /* win16 */
590 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
591 while(ev)
593 int key = ev->key;
594 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
595 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
596 put_word(res, key);
597 put_word(res, ev->id);
598 ev = ev->next;
601 /* Set ResourceSize */
602 SetResSize(res, restag);
603 return res;
607 *****************************************************************************
608 * Function : dialog2res
609 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
610 * Input :
611 * name - Name/ordinal of the resource
612 * dlg - The dialog descriptor
613 * Output : New .res format structure
614 * Description :
615 * Remarks :
616 *****************************************************************************
618 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
620 int restag;
621 res_t *res;
622 control_t *ctrl;
623 int tag_nctrl;
624 int nctrl = 0;
625 assert(name != NULL);
626 assert(dlg != NULL);
628 ctrl = dlg->controls;
629 res = new_res();
630 if(win32)
632 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
634 if (dlg->is_ex)
636 /* FIXME: MS doc says that the first word must contain 0xffff
637 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
638 * compiler reverses the two words.
639 * I don't know which one to choose, but I write it as Mr. B
640 * writes it.
642 put_word(res, 1); /* Signature */
643 put_word(res, 0xffff); /* DlgVer */
644 put_dword(res, dlg->gothelpid ? dlg->helpid : 0);
645 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
646 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
648 else
650 put_dword(res, dlg->style->or_mask);
651 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
653 tag_nctrl = res->size;
654 put_word(res, 0); /* Number of controls */
655 put_word(res, dlg->x);
656 put_word(res, dlg->y);
657 put_word(res, dlg->width);
658 put_word(res, dlg->height);
659 if(dlg->menu)
660 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
661 else
662 put_word(res, 0);
663 if(dlg->dlgclass)
664 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
665 else
666 put_word(res, 0);
667 if(dlg->title)
668 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
669 else
670 put_word(res, 0);
671 if(dlg->font)
673 put_word(res, dlg->font->size);
674 if (dlg->is_ex)
676 put_word(res, dlg->font->weight);
677 /* FIXME: ? TRUE should be sufficient to say that it's
678 * italic, but Borland's compiler says it's 0x0101.
679 * I just copy it here, and hope for the best.
681 put_word(res, dlg->font->italic ? 0x0101 : 0);
683 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
685 else if (dlg->style->or_mask & DS_SETFONT) put_word( res, 0x7fff );
687 put_pad(res);
688 while(ctrl)
690 if (dlg->is_ex)
692 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
693 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
694 /* FIXME: what is default control style? */
695 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
697 else
699 /* FIXME: what is default control style? */
700 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
701 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
703 put_word(res, ctrl->x);
704 put_word(res, ctrl->y);
705 put_word(res, ctrl->width);
706 put_word(res, ctrl->height);
707 if (dlg->is_ex)
708 put_dword(res, ctrl->id);
709 else
710 put_word(res, ctrl->id);
711 if(ctrl->ctlclass)
712 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
713 else
714 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
715 if(ctrl->title)
716 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
717 else
718 put_word(res, 0);
719 if(ctrl->extra)
721 put_word(res, ctrl->extra->size+2);
722 put_pad(res);
723 put_raw_data(res, ctrl->extra, 0);
725 else
726 put_word(res, 0);
728 if(ctrl->next)
729 put_pad(res);
730 nctrl++;
731 ctrl = ctrl->next;
733 /* Set number of controls */
734 set_word(res, tag_nctrl, (WORD)nctrl);
736 else /* win16 */
738 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
740 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
741 tag_nctrl = res->size;
742 put_byte(res, 0); /* Number of controls */
743 put_word(res, dlg->x);
744 put_word(res, dlg->y);
745 put_word(res, dlg->width);
746 put_word(res, dlg->height);
747 if(dlg->menu)
748 put_name_id(res, dlg->menu, TRUE, NULL);
749 else
750 put_byte(res, 0);
751 if(dlg->dlgclass)
752 put_name_id(res, dlg->dlgclass, TRUE, NULL);
753 else
754 put_byte(res, 0);
755 if(dlg->title)
756 put_string(res, dlg->title, str_char, TRUE, NULL);
757 else
758 put_byte(res, 0);
759 if(dlg->font)
761 put_word(res, dlg->font->size);
762 put_string(res, dlg->font->name, str_char, TRUE, NULL);
765 while(ctrl)
767 put_word(res, ctrl->x);
768 put_word(res, ctrl->y);
769 put_word(res, ctrl->width);
770 put_word(res, ctrl->height);
771 put_word(res, ctrl->id);
772 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
773 if(ctrl->ctlclass)
775 if(ctrl->ctlclass->type == name_ord
776 && ctrl->ctlclass->name.i_name >= 0x80
777 && ctrl->ctlclass->name.i_name <= 0x85)
778 put_byte(res, ctrl->ctlclass->name.i_name);
779 else if(ctrl->ctlclass->type == name_str)
780 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
781 else
782 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
784 else
785 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
786 if(ctrl->title)
787 put_name_id(res, ctrl->title, FALSE, NULL);
788 else
789 put_byte(res, 0);
791 /* FIXME: What is this extra byte doing here? */
792 put_byte(res, 0);
794 nctrl++;
795 ctrl = ctrl->next;
797 /* Set number of controls */
798 ((char *)res->data)[tag_nctrl] = (char)nctrl;
800 /* Set ResourceSize */
801 SetResSize(res, restag);
802 return res;
806 *****************************************************************************
807 * Function : menuitem2res
808 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
809 * Input :
810 * Output :
811 * Description :
812 * Remarks : Self recursive
813 *****************************************************************************
815 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
817 menu_item_t *itm = menitem;
818 if(win32)
820 while(itm)
822 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
823 if(!itm->popup)
824 put_word(res, itm->id);
825 if(itm->name)
826 put_string(res, itm->name, str_unicode, TRUE, lang);
827 else
828 put_word(res, 0);
829 if(itm->popup)
830 menuitem2res(res, itm->popup, lang);
831 itm = itm->next;
834 else /* win16 */
836 while(itm)
838 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
839 if(!itm->popup)
840 put_word(res, itm->id);
841 if(itm->name)
842 put_string(res, itm->name, str_char, TRUE, lang);
843 else
844 put_byte(res, 0);
845 if(itm->popup)
846 menuitem2res(res, itm->popup, lang);
847 itm = itm->next;
854 *****************************************************************************
855 * Function : menuexitem2res
856 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
857 * Input :
858 * Output : nop
859 * Description :
860 * Remarks : Self recursive
861 *****************************************************************************
863 static void menuexitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
865 menu_item_t *itm = menitem;
866 assert(win32 != 0);
867 while(itm)
869 put_dword(res, itm->gottype ? itm->type : 0);
870 put_dword(res, itm->gotstate ? itm->state : 0);
871 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
872 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
873 if(itm->name)
874 put_string(res, itm->name, str_unicode, TRUE, lang);
875 else
876 put_word(res, 0);
877 put_pad(res);
878 if(itm->popup)
880 put_dword(res, itm->gothelpid ? itm->helpid : 0);
881 menuexitem2res(res, itm->popup, lang);
883 itm = itm->next;
889 *****************************************************************************
890 * Function : menu2res
891 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
892 * Input :
893 * name - Name/ordinal of the resource
894 * menex - The menuex descriptor
895 * Output : New .res format structure
896 * Description :
897 * Remarks :
898 *****************************************************************************
900 static res_t *menu2res(name_id_t *name, menu_t *men)
902 int restag;
903 res_t *res;
904 assert(name != NULL);
905 assert(men != NULL);
907 res = new_res();
908 if(win32)
910 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, &(men->lvc));
912 if (men->is_ex)
914 put_word(res, 1); /* Menuheader: Version */
915 put_word(res, 4); /* Offset */
916 put_dword(res, 0); /* HelpId */
917 put_pad(res);
918 menuexitem2res(res, men->items, men->lvc.language);
920 else
922 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
923 menuitem2res(res, men->items, men->lvc.language);
925 /* Set ResourceSize */
926 SetResSize(res, restag);
927 put_pad(res);
929 else /* win16 */
931 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, NULL);
933 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
934 menuitem2res(res, men->items, NULL);
935 /* Set ResourceSize */
936 SetResSize(res, restag);
938 return res;
942 *****************************************************************************
943 * Function : cursorgroup2res
944 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
945 * Input :
946 * name - Name/ordinal of the resource
947 * curg - The cursor descriptor
948 * Output : New .res format structure
949 * Description :
950 * Remarks :
951 *****************************************************************************
953 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
955 int restag;
956 res_t *res;
957 cursor_t *cur;
958 assert(name != NULL);
959 assert(curg != NULL);
961 res = new_res();
962 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
964 put_word(res, 0); /* Reserved */
965 /* FIXME: The ResType in the NEWHEADER structure should
966 * contain 14 according to the MS win32 doc. This is
967 * not the case with the BRC compiler and I really doubt
968 * the latter. Putting one here is compliant to win16 spec,
969 * but who knows the true value?
971 put_word(res, 2); /* ResType */
972 put_word(res, curg->ncursor);
973 #if 0
974 for(cur = curg->cursorlist; cur; cur = cur->next)
975 #else
976 cur = curg->cursorlist;
977 while(cur->next)
978 cur = cur->next;
979 for(; cur; cur = cur->prev)
980 #endif
982 put_word(res, cur->width);
983 /* FIXME: The height of a cursor is half the size of
984 * the bitmap's height. BRC puts the height from the
985 * BITMAPINFOHEADER here instead of the cursorfile's
986 * height. MS doesn't seem to care...
988 put_word(res, cur->height);
989 /* FIXME: The next two are reversed in BRC and I don't
990 * know why. Probably a bug. But, we can safely ignore
991 * it because win16 does not support color cursors.
992 * A warning should have been generated by the parser.
994 put_word(res, cur->planes);
995 put_word(res, cur->bits);
996 /* FIXME: The +4 is the hotspot in the cursor resource.
997 * However, I could not find this in the documentation.
998 * The hotspot bytes must either be included or MS
999 * doesn't care.
1001 put_dword(res, cur->data->size +4);
1002 put_word(res, cur->id);
1005 SetResSize(res, restag); /* Set ResourceSize */
1006 if(win32)
1007 put_pad(res);
1009 return res;
1013 *****************************************************************************
1014 * Function : cursor2res
1015 * Syntax : res_t *cursor2res(cursor_t *cur)
1016 * Input :
1017 * cur - The cursor descriptor
1018 * Output : New .res format structure
1019 * Description :
1020 * Remarks :
1021 *****************************************************************************
1023 static res_t *cursor2res(cursor_t *cur)
1025 int restag;
1026 res_t *res;
1027 name_id_t name;
1029 assert(cur != NULL);
1031 res = new_res();
1032 name.type = name_ord;
1033 name.name.i_name = cur->id;
1034 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1035 put_word(res, cur->xhot);
1036 put_word(res, cur->yhot);
1037 put_raw_data(res, cur->data, 0);
1039 SetResSize(res, restag); /* Set ResourceSize */
1040 if(win32)
1041 put_pad(res);
1043 return res;
1047 *****************************************************************************
1048 * Function : icongroup2res
1049 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1050 * Input :
1051 * name - Name/ordinal of the resource
1052 * icog - The icon group descriptor
1053 * Output : New .res format structure
1054 * Description :
1055 * Remarks :
1056 *****************************************************************************
1058 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1060 int restag;
1061 res_t *res;
1062 icon_t *ico;
1063 assert(name != NULL);
1064 assert(icog != NULL);
1066 res = new_res();
1067 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1069 put_word(res, 0); /* Reserved */
1070 /* FIXME: The ResType in the NEWHEADER structure should
1071 * contain 14 according to the MS win32 doc. This is
1072 * not the case with the BRC compiler and I really doubt
1073 * the latter. Putting one here is compliant to win16 spec,
1074 * but who knows the true value?
1076 put_word(res, 1); /* ResType */
1077 put_word(res, icog->nicon);
1078 for(ico = icog->iconlist; ico; ico = ico->next)
1080 put_byte(res, ico->width);
1081 put_byte(res, ico->height);
1082 put_byte(res, ico->nclr);
1083 put_byte(res, 0); /* Reserved */
1084 put_word(res, ico->planes);
1085 put_word(res, ico->bits);
1086 put_dword(res, ico->data->size);
1087 put_word(res, ico->id);
1090 SetResSize(res, restag); /* Set ResourceSize */
1091 if(win32)
1092 put_pad(res);
1094 return res;
1098 *****************************************************************************
1099 * Function : icon2res
1100 * Syntax : res_t *icon2res(icon_t *ico)
1101 * Input :
1102 * ico - The icon descriptor
1103 * Output : New .res format structure
1104 * Description :
1105 * Remarks :
1106 *****************************************************************************
1108 static res_t *icon2res(icon_t *ico)
1110 int restag;
1111 res_t *res;
1112 name_id_t name;
1114 assert(ico != NULL);
1116 res = new_res();
1117 name.type = name_ord;
1118 name.name.i_name = ico->id;
1119 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1120 put_raw_data(res, ico->data, 0);
1122 SetResSize(res, restag); /* Set ResourceSize */
1123 if(win32)
1124 put_pad(res);
1126 return res;
1130 *****************************************************************************
1131 * Function : anicurico2res
1132 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1133 * Input :
1134 * name - Name/ordinal of the resource
1135 * ani - The animated object descriptor
1136 * Output : New .res format structure
1137 * Description :
1138 * Remarks : The endian of the object's structures have been converted
1139 * by the loader.
1140 * There are rumors that win311 could handle animated stuff.
1141 * That is why they are generated for both win16 and win32
1142 * compile.
1143 *****************************************************************************
1145 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1147 int restag;
1148 res_t *res;
1149 assert(name != NULL);
1150 assert(ani != NULL);
1152 res = new_res();
1153 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1154 NULL, name, ani->memopt, NULL);
1155 put_raw_data(res, ani->data, 0);
1156 /* Set ResourceSize */
1157 SetResSize(res, restag);
1158 if(win32)
1159 put_pad(res);
1160 return res;
1164 *****************************************************************************
1165 * Function : bitmap2res
1166 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1167 * Input :
1168 * name - Name/ordinal of the resource
1169 * bmp - The bitmap descriptor
1170 * Output : New .res format structure
1171 * Description :
1172 * Remarks : The endian of the bitmap structures have been converted
1173 * by the loader.
1174 *****************************************************************************
1176 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1178 int restag;
1179 res_t *res;
1180 assert(name != NULL);
1181 assert(bmp != NULL);
1183 res = new_res();
1184 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1185 if(bmp->data->data[0] == 'B'
1186 && bmp->data->data[1] == 'M'
1187 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1188 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1190 /* The File header is still attached, don't write it */
1191 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1193 else
1195 put_raw_data(res, bmp->data, 0);
1197 /* Set ResourceSize */
1198 SetResSize(res, restag);
1199 if(win32)
1200 put_pad(res);
1201 return res;
1205 *****************************************************************************
1206 * Function : font2res
1207 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1208 * Input :
1209 * name - Name/ordinal of the resource
1210 * fnt - The font descriptor
1211 * Output : New .res format structure
1212 * Description :
1213 * Remarks : The data has been prepared just after parsing.
1214 *****************************************************************************
1216 static res_t *font2res(name_id_t *name, font_t *fnt)
1218 int restag;
1219 res_t *res;
1220 assert(name != NULL);
1221 assert(fnt != NULL);
1223 res = new_res();
1224 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1225 put_raw_data(res, fnt->data, 0);
1226 /* Set ResourceSize */
1227 SetResSize(res, restag);
1228 if(win32)
1229 put_pad(res);
1230 return res;
1234 *****************************************************************************
1235 * Function : fontdir2res
1236 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1237 * Input :
1238 * name - Name/ordinal of the resource
1239 * fntdir - The fontdir descriptor
1240 * Output : New .res format structure
1241 * Description :
1242 * Remarks : The data has been prepared just after parsing.
1243 *****************************************************************************
1245 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1247 int restag;
1248 res_t *res;
1249 assert(name != NULL);
1250 assert(fnd != NULL);
1252 res = new_res();
1253 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1254 put_raw_data(res, fnd->data, 0);
1255 /* Set ResourceSize */
1256 SetResSize(res, restag);
1257 if(win32)
1258 put_pad(res);
1259 return res;
1263 *****************************************************************************
1264 * Function : html2res
1265 * Syntax : res_t *html2res(name_id_t *name, html_t *html)
1266 * Input :
1267 * name - Name/ordinal of the resource
1268 * rdt - The html descriptor
1269 * Output : New .res format structure
1270 * Description :
1271 * Remarks :
1272 *****************************************************************************
1274 static res_t *html2res(name_id_t *name, html_t *html)
1276 int restag;
1277 res_t *res;
1278 assert(name != NULL);
1279 assert(html != NULL);
1281 res = new_res();
1282 restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
1283 put_raw_data(res, html->data, 0);
1284 /* Set ResourceSize */
1285 SetResSize(res, restag);
1286 if(win32)
1287 put_pad(res);
1288 return res;
1292 *****************************************************************************
1293 * Function : rcdata2res
1294 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1295 * Input :
1296 * name - Name/ordinal of the resource
1297 * rdt - The rcdata descriptor
1298 * Output : New .res format structure
1299 * Description :
1300 * Remarks :
1301 *****************************************************************************
1303 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1305 int restag;
1306 res_t *res;
1307 assert(name != NULL);
1308 assert(rdt != NULL);
1310 res = new_res();
1311 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1312 put_raw_data(res, rdt->data, 0);
1313 /* Set ResourceSize */
1314 SetResSize(res, restag);
1315 if(win32)
1316 put_pad(res);
1317 return res;
1321 *****************************************************************************
1322 * Function : messagetable2res
1323 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1324 * Input :
1325 * name - Name/ordinal of the resource
1326 * msg - The messagetable descriptor
1327 * Output : New .res format structure
1328 * Description :
1329 * Remarks : The data has been converted to the appropriate endian
1330 * after it was parsed.
1331 *****************************************************************************
1333 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1335 int restag;
1336 res_t *res;
1337 assert(name != NULL);
1338 assert(msg != NULL);
1340 res = new_res();
1341 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1342 put_raw_data(res, msg->data, 0);
1343 /* Set ResourceSize */
1344 SetResSize(res, restag);
1345 if(win32)
1346 put_pad(res);
1347 return res;
1351 *****************************************************************************
1352 * Function : stringtable2res
1353 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1354 * Input :
1355 * stt - The stringtable descriptor
1356 * Output : New .res format structure
1357 * Description :
1358 * Remarks :
1359 *****************************************************************************
1361 static res_t *stringtable2res(stringtable_t *stt)
1363 res_t *res;
1364 name_id_t name;
1365 int i;
1366 int restag;
1367 DWORD lastsize = 0;
1369 assert(stt != NULL);
1370 res = new_res();
1372 for(; stt; stt = stt->next)
1374 if(!stt->nentries)
1376 warning("Empty internal stringtable\n");
1377 continue;
1379 name.type = name_ord;
1380 name.name.i_name = (stt->idbase >> 4) + 1;
1381 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1382 for(i = 0; i < stt->nentries; i++)
1384 if(stt->entries[i].str && stt->entries[i].str->size)
1386 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1387 FALSE, win32 ? stt->lvc.language : NULL);
1389 else
1391 if (win32)
1392 put_word(res, 0);
1393 else
1394 put_byte(res, 0);
1397 /* Set ResourceSize */
1398 SetResSize(res, restag - lastsize);
1399 if(win32)
1400 put_pad(res);
1401 lastsize = res->size;
1403 return res;
1407 *****************************************************************************
1408 * Function : user2res
1409 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1410 * Input :
1411 * name - Name/ordinal of the resource
1412 * usr - The userresource descriptor
1413 * Output : New .res format structure
1414 * Description :
1415 * Remarks :
1416 *****************************************************************************
1418 static res_t *user2res(name_id_t *name, user_t *usr)
1420 int restag;
1421 res_t *res;
1422 assert(name != NULL);
1423 assert(usr != NULL);
1425 res = new_res();
1426 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1427 put_raw_data(res, usr->data, 0);
1428 /* Set ResourceSize */
1429 SetResSize(res, restag);
1430 if(win32)
1431 put_pad(res);
1432 return res;
1436 *****************************************************************************
1437 * Function : versionblock2res
1438 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1439 * Input :
1440 * res - Binary resource to write to
1441 * blk - The version block to be written
1442 * Output :
1443 * Description :
1444 * Remarks : Self recursive
1445 *****************************************************************************
1447 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1449 ver_value_t *val;
1450 int blksizetag;
1451 int valblksizetag;
1452 int valvalsizetag;
1453 int tag;
1454 int i;
1456 blksizetag = res->size;
1457 put_word(res, 0); /* Will be overwritten later */
1458 put_word(res, 0);
1459 if(win32)
1460 put_word(res, 0); /* level ? */
1461 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1462 put_pad(res);
1463 for(val = blk->values; val; val = val->next)
1465 if(val->type == val_str)
1467 valblksizetag = res->size;
1468 put_word(res, 0); /* Will be overwritten later */
1469 valvalsizetag = res->size;
1470 put_word(res, 0); /* Will be overwritten later */
1471 if(win32)
1473 put_word(res, level);
1475 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1476 put_pad(res);
1477 tag = res->size;
1478 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1479 if(win32)
1480 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1481 else
1482 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1483 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1484 put_pad(res);
1486 else if(val->type == val_words)
1488 valblksizetag = res->size;
1489 put_word(res, 0); /* Will be overwritten later */
1490 valvalsizetag = res->size;
1491 put_word(res, 0); /* Will be overwritten later */
1492 if(win32)
1494 put_word(res, level);
1496 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1497 put_pad(res);
1498 tag = res->size;
1499 for(i = 0; i < val->value.words->nwords; i++)
1501 put_word(res, val->value.words->words[i]);
1503 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1504 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1505 put_pad(res);
1507 else if(val->type == val_block)
1509 versionblock2res(res, val->value.block, level+1, lang);
1511 else
1513 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
1517 /* Set blocksize */
1518 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1522 *****************************************************************************
1523 * Function : versioninfo2res
1524 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1525 * Input :
1526 * name - Name/ordinal of the resource
1527 * ver - The versioninfo descriptor
1528 * Output : New .res format structure
1529 * Description :
1530 * Remarks :
1531 *****************************************************************************
1533 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1535 int restag;
1536 int rootblocksizetag;
1537 int valsizetag;
1538 int tag;
1539 res_t *res;
1540 string_t vsvi;
1541 ver_block_t *blk;
1543 assert(name != NULL);
1544 assert(ver != NULL);
1546 vsvi.type = str_char;
1547 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1548 vsvi.size = 15; /* Excl. termination */
1550 res = new_res();
1551 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1552 rootblocksizetag = res->size;
1553 put_word(res, 0); /* BlockSize filled in later */
1554 valsizetag = res->size;
1555 put_word(res, 0); /* ValueSize filled in later*/
1556 if(win32)
1557 put_word(res, 0); /* Tree-level ? */
1558 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1559 TRUE, win32 ? ver->lvc.language : NULL);
1560 if(win32)
1561 put_pad(res);
1562 tag = res->size;
1563 put_dword(res, VS_FFI_SIGNATURE);
1564 put_dword(res, VS_FFI_STRUCVERSION);
1565 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1566 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1567 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1568 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1569 put_dword(res, ver->fileflagsmask);
1570 put_dword(res, ver->fileflags);
1571 put_dword(res, ver->fileos);
1572 put_dword(res, ver->filetype);
1573 put_dword(res, ver->filesubtype);
1574 put_dword(res, 0); /* FileDateMS */
1575 put_dword(res, 0); /* FileDateLS */
1576 /* Set ValueSize */
1577 set_word(res, valsizetag, (WORD)(res->size - tag));
1578 /* Descend into the blocks */
1579 for(blk = ver->blocks; blk; blk = blk->next)
1580 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1581 /* Set root block's size */
1582 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1584 SetResSize(res, restag);
1585 if(win32)
1586 put_pad(res);
1588 free(vsvi.str.cstr);
1589 return res;
1593 *****************************************************************************
1594 * Function : toolbaritem2res
1595 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1596 * Input :
1597 * Output : nop
1598 * Description :
1599 * Remarks : Self recursive
1600 *****************************************************************************
1602 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1604 toolbar_item_t *itm = tbitem;
1605 assert(win32 != 0);
1606 while(itm)
1608 put_word(res, itm->id);
1609 itm = itm->next;
1615 *****************************************************************************
1616 * Function : toolbar2res
1617 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1618 * Input :
1619 * name - Name/ordinal of the resource
1620 * toolbar - The toolbar descriptor
1621 * Output : New .res format structure
1622 * Description :
1623 * Remarks :
1624 *****************************************************************************
1626 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1628 int restag;
1629 res_t *res;
1630 assert(name != NULL);
1631 assert(toolbar != NULL);
1633 res = new_res();
1634 if(win32)
1636 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1638 put_word(res, 1); /* Menuheader: Version */
1639 put_word(res, toolbar->button_width); /* (in pixels?) */
1640 put_word(res, toolbar->button_height); /* (in pixels?) */
1641 put_word(res, toolbar->nitems);
1642 put_pad(res);
1643 toolbaritem2res(res, toolbar->items);
1644 /* Set ResourceSize */
1645 SetResSize(res, restag);
1646 put_pad(res);
1648 else /* win16 */
1650 /* Do not generate anything in 16-bit mode */
1651 free(res->data);
1652 free(res);
1653 return NULL;
1655 return res;
1659 *****************************************************************************
1660 * Function : dlginit2res
1661 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1662 * Input :
1663 * name - Name/ordinal of the resource
1664 * rdt - The dlginit descriptor
1665 * Output : New .res format structure
1666 * Description :
1667 * Remarks :
1668 *****************************************************************************
1670 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1672 int restag;
1673 res_t *res;
1674 assert(name != NULL);
1675 assert(dit != NULL);
1677 res = new_res();
1678 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1679 put_raw_data(res, dit->data, 0);
1680 /* Set ResourceSize */
1681 SetResSize(res, restag);
1682 if(win32)
1683 put_pad(res);
1684 return res;
1688 *****************************************************************************
1689 * Function : prep_nid_for_label
1690 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1691 * Input :
1692 * Output :
1693 * Description : Converts a resource name into the first 32 (or less)
1694 * characters of the name with conversions.
1695 * Remarks :
1696 *****************************************************************************
1698 #define MAXNAMELEN 32
1699 char *prep_nid_for_label(const name_id_t *nid)
1701 static char buf[MAXNAMELEN+1];
1703 assert(nid != NULL);
1705 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1707 WCHAR *sptr;
1708 int i;
1709 sptr = nid->name.s_name->str.wstr;
1710 buf[0] = '\0';
1711 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1713 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1714 buf[i] = *sptr++;
1715 else
1716 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
1718 buf[i] = '\0';
1720 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1722 char *cptr;
1723 int i;
1724 cptr = nid->name.s_name->str.cstr;
1725 buf[0] = '\0';
1726 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1728 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1729 buf[i] = *cptr++;
1730 else
1731 warning("Resourcename (str_char) contain unprintable characters, ignored\n");
1733 buf[i] = '\0';
1735 else if(nid->type == name_ord)
1737 sprintf(buf, "%u", nid->name.i_name);
1739 else
1741 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
1743 return buf;
1745 #undef MAXNAMELEN
1748 *****************************************************************************
1749 * Function : make_c_name
1750 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1751 * Input :
1752 * Output :
1753 * Description : Converts a resource name into a valid c-identifier in the
1754 * form "_base_nid".
1755 * Remarks :
1756 *****************************************************************************
1758 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1760 char *buf = prep_nid_for_label(nid);
1761 return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0);
1765 *****************************************************************************
1766 * Function : get_c_typename
1767 * Syntax : const char *get_c_typename(enum res_e type)
1768 * Input :
1769 * Output :
1770 * Description : Convert resource enum to char string to be used in c-name
1771 * creation.
1772 * Remarks :
1773 *****************************************************************************
1775 const char *get_c_typename(enum res_e type)
1777 switch(type)
1779 case res_acc: return "Acc";
1780 case res_anicur:return "AniCur";
1781 case res_aniico:return "AniIco";
1782 case res_bmp: return "Bmp";
1783 case res_cur: return "Cur";
1784 case res_curg: return "CurGrp";
1785 case res_dlg: return "Dlg";
1786 case res_fnt: return "Fnt";
1787 case res_fntdir:return "FntDir";
1788 case res_ico: return "Ico";
1789 case res_icog: return "IcoGrp";
1790 case res_men: return "Men";
1791 case res_rdt: return "RCDat";
1792 case res_stt: return "StrTab";
1793 case res_usr: return "Usr";
1794 case res_msg: return "MsgTab";
1795 case res_ver: return "VerInf";
1796 case res_toolbar: return "TlBr";
1797 case res_dlginit: return "DlgInit";
1798 default: return "Oops";
1803 *****************************************************************************
1804 * Function : resources2res
1805 * Syntax : void resources2res(resource_t *top)
1806 * Input :
1807 * top - The resource-tree to convert
1808 * Output :
1809 * Description : Convert logical resource descriptors into binary data
1810 * Remarks :
1811 *****************************************************************************
1813 void resources2res(resource_t *top)
1815 while(top)
1817 switch(top->type)
1819 case res_acc:
1820 if(!top->binres)
1821 top->binres = accelerator2res(top->name, top->res.acc);
1822 break;
1823 case res_bmp:
1824 if(!top->binres)
1825 top->binres = bitmap2res(top->name, top->res.bmp);
1826 break;
1827 case res_cur:
1828 if(!top->binres)
1829 top->binres = cursor2res(top->res.cur);
1830 break;
1831 case res_curg:
1832 if(!top->binres)
1833 top->binres = cursorgroup2res(top->name, top->res.curg);
1834 break;
1835 case res_dlg:
1836 if(!top->binres)
1837 top->binres = dialog2res(top->name, top->res.dlg);
1838 break;
1839 case res_fnt:
1840 if(!top->binres)
1841 top->binres = font2res(top->name, top->res.fnt);
1842 break;
1843 case res_fntdir:
1844 if(!top->binres)
1845 top->binres = fontdir2res(top->name, top->res.fnd);
1846 break;
1847 case res_ico:
1848 if(!top->binres)
1849 top->binres = icon2res(top->res.ico);
1850 break;
1851 case res_icog:
1852 if(!top->binres)
1853 top->binres = icongroup2res(top->name, top->res.icog);
1854 break;
1855 case res_men:
1856 if(!top->binres)
1857 top->binres = menu2res(top->name, top->res.men);
1858 break;
1859 case res_html:
1860 if(!top->binres)
1861 top->binres = html2res(top->name, top->res.html);
1862 break;
1863 case res_rdt:
1864 if(!top->binres)
1865 top->binres = rcdata2res(top->name, top->res.rdt);
1866 break;
1867 case res_stt:
1868 if(!top->binres)
1869 top->binres = stringtable2res(top->res.stt);
1870 break;
1871 case res_usr:
1872 if(!top->binres)
1873 top->binres = user2res(top->name, top->res.usr);
1874 break;
1875 case res_msg:
1876 if(!top->binres)
1877 top->binres = messagetable2res(top->name, top->res.msg);
1878 break;
1879 case res_ver:
1880 if(!top->binres)
1881 top->binres = versioninfo2res(top->name, top->res.ver);
1882 break;
1883 case res_toolbar:
1884 if(!top->binres)
1885 top->binres = toolbar2res(top->name, top->res.tbt);
1886 break;
1887 case res_dlginit:
1888 if(!top->binres)
1889 top->binres = dlginit2res(top->name, top->res.dlgi);
1890 break;
1891 case res_anicur:
1892 case res_aniico:
1893 if(!top->binres)
1894 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1895 break;
1896 default:
1897 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
1899 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
1900 top = top->next;