mlang/tests: Fix a pointer casting warning on 64-bit.
[wine/testsucceed.git] / dlls / d3dxof / parsing.c
blob06a1385bf06a0813e606b2d3687b9078801a4a2e
1 /*
2 * X Files parsing
4 * Copyright 2008 Christian Costa
6 * This file contains the (internal) driver registration functions,
7 * driver enumeration APIs and DirectDraw creation functions.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/debug.h"
27 #define COBJMACROS
29 #include "winbase.h"
30 #include "wingdi.h"
32 #include "d3dxof_private.h"
33 #include "dxfile.h"
35 #include <stdio.h>
37 WINE_DEFAULT_DEBUG_CHANNEL(d3dxof);
39 static const char* get_primitive_string(WORD token);
40 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional);
41 static WORD check_TOKEN(parse_buffer * buf);
43 #define TOKEN_NAME 1
44 #define TOKEN_STRING 2
45 #define TOKEN_INTEGER 3
46 #define TOKEN_GUID 5
47 #define TOKEN_INTEGER_LIST 6
48 #define TOKEN_FLOAT_LIST 7
49 #define TOKEN_OBRACE 10
50 #define TOKEN_CBRACE 11
51 #define TOKEN_OPAREN 12
52 #define TOKEN_CPAREN 13
53 #define TOKEN_OBRACKET 14
54 #define TOKEN_CBRACKET 15
55 #define TOKEN_OANGLE 16
56 #define TOKEN_CANGLE 17
57 #define TOKEN_DOT 18
58 #define TOKEN_COMMA 19
59 #define TOKEN_SEMICOLON 20
60 #define TOKEN_TEMPLATE 31
61 #define TOKEN_WORD 40
62 #define TOKEN_DWORD 41
63 #define TOKEN_FLOAT 42
64 #define TOKEN_DOUBLE 43
65 #define TOKEN_CHAR 44
66 #define TOKEN_UCHAR 45
67 #define TOKEN_SWORD 46
68 #define TOKEN_SDWORD 47
69 #define TOKEN_VOID 48
70 #define TOKEN_LPSTR 49
71 #define TOKEN_UNICODE 50
72 #define TOKEN_CSTRING 51
73 #define TOKEN_ARRAY 52
75 #define CLSIDFMT "<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>"
77 void dump_template(xtemplate* templates_array, xtemplate* ptemplate)
79 int j, k;
80 GUID* clsid;
82 clsid = &ptemplate->class_id;
84 DPRINTF("template %s\n", ptemplate->name);
85 DPRINTF("{\n");
86 DPRINTF(CLSIDFMT "\n", clsid->Data1, clsid->Data2, clsid->Data3, clsid->Data4[0],
87 clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7]);
88 for (j = 0; j < ptemplate->nb_members; j++)
90 if (ptemplate->members[j].nb_dims)
91 DPRINTF("array ");
92 if (ptemplate->members[j].type == TOKEN_NAME)
93 DPRINTF("%s ", templates_array[ptemplate->members[j].idx_template].name);
94 else
95 DPRINTF("%s ", get_primitive_string(ptemplate->members[j].type));
96 DPRINTF("%s", ptemplate->members[j].name);
97 for (k = 0; k < ptemplate->members[j].nb_dims; k++)
99 if (ptemplate->members[j].dim_fixed[k])
100 DPRINTF("[%d]", ptemplate->members[j].dim_value[k]);
101 else
102 DPRINTF("[%s]", ptemplate->members[ptemplate->members[j].dim_value[k]].name);
104 DPRINTF(";\n");
106 if (ptemplate->open)
107 DPRINTF("[...]\n");
108 else if (ptemplate->nb_childs)
110 DPRINTF("[%s", ptemplate->childs[0]);
111 for (j = 1; j < ptemplate->nb_childs; j++)
112 DPRINTF(",%s", ptemplate->childs[j]);
113 DPRINTF("]\n");
115 DPRINTF("}\n");
118 BOOL is_template_available(parse_buffer * buf)
120 return check_TOKEN(buf) == TOKEN_TEMPLATE;
123 BOOL read_bytes(parse_buffer * buf, LPVOID data, DWORD size)
125 if (buf->rem_bytes < size)
126 return FALSE;
127 memcpy(data, buf->buffer, size);
128 buf->buffer += size;
129 buf->rem_bytes -= size;
130 return TRUE;
133 static void dump_TOKEN(WORD token)
135 #define DUMP_TOKEN(t) case t: TRACE(#t "\n"); break
136 switch(token)
138 DUMP_TOKEN(TOKEN_NAME);
139 DUMP_TOKEN(TOKEN_STRING);
140 DUMP_TOKEN(TOKEN_INTEGER);
141 DUMP_TOKEN(TOKEN_GUID);
142 DUMP_TOKEN(TOKEN_INTEGER_LIST);
143 DUMP_TOKEN(TOKEN_FLOAT_LIST);
144 DUMP_TOKEN(TOKEN_OBRACE);
145 DUMP_TOKEN(TOKEN_CBRACE);
146 DUMP_TOKEN(TOKEN_OPAREN);
147 DUMP_TOKEN(TOKEN_CPAREN);
148 DUMP_TOKEN(TOKEN_OBRACKET);
149 DUMP_TOKEN(TOKEN_CBRACKET);
150 DUMP_TOKEN(TOKEN_OANGLE);
151 DUMP_TOKEN(TOKEN_CANGLE);
152 DUMP_TOKEN(TOKEN_DOT);
153 DUMP_TOKEN(TOKEN_COMMA);
154 DUMP_TOKEN(TOKEN_SEMICOLON);
155 DUMP_TOKEN(TOKEN_TEMPLATE);
156 DUMP_TOKEN(TOKEN_WORD);
157 DUMP_TOKEN(TOKEN_DWORD);
158 DUMP_TOKEN(TOKEN_FLOAT);
159 DUMP_TOKEN(TOKEN_DOUBLE);
160 DUMP_TOKEN(TOKEN_CHAR);
161 DUMP_TOKEN(TOKEN_UCHAR);
162 DUMP_TOKEN(TOKEN_SWORD);
163 DUMP_TOKEN(TOKEN_SDWORD);
164 DUMP_TOKEN(TOKEN_VOID);
165 DUMP_TOKEN(TOKEN_LPSTR);
166 DUMP_TOKEN(TOKEN_UNICODE);
167 DUMP_TOKEN(TOKEN_CSTRING);
168 DUMP_TOKEN(TOKEN_ARRAY);
169 default:
170 if (0)
171 TRACE("Unknown token %d\n", token);
172 break;
174 #undef DUMP_TOKEN
177 static BOOL is_space(char c)
179 switch (c)
181 case 0x00:
182 case 0x0D:
183 case 0x0A:
184 case ' ':
185 case '\t':
186 return TRUE;
188 return FALSE;
191 static BOOL is_operator(char c)
193 switch(c)
195 case '{':
196 case '}':
197 case '[':
198 case ']':
199 case '(':
200 case ')':
201 case '<':
202 case '>':
203 case ',':
204 case ';':
205 return TRUE;
207 return FALSE;
210 static inline BOOL is_separator(char c)
212 return is_space(c) || is_operator(c);
215 static WORD get_operator_token(char c)
217 switch(c)
219 case '{':
220 return TOKEN_OBRACE;
221 case '}':
222 return TOKEN_CBRACE;
223 case '[':
224 return TOKEN_OBRACKET;
225 case ']':
226 return TOKEN_CBRACKET;
227 case '(':
228 return TOKEN_OPAREN;
229 case ')':
230 return TOKEN_CPAREN;
231 case '<':
232 return TOKEN_OANGLE;
233 case '>':
234 return TOKEN_CANGLE;
235 case ',':
236 return TOKEN_COMMA;
237 case ';':
238 return TOKEN_SEMICOLON;
240 return 0;
243 static BOOL is_keyword(parse_buffer* buf, const char* keyword)
245 DWORD len = strlen(keyword);
246 if (!strncasecmp((char*)buf->buffer, keyword,len) && is_separator(*(buf->buffer+len)))
248 buf->buffer += len;
249 buf->rem_bytes -= len;
250 return TRUE;
252 return FALSE;
255 static WORD get_keyword_token(parse_buffer* buf)
257 if (is_keyword(buf, "template"))
258 return TOKEN_TEMPLATE;
259 if (is_keyword(buf, "WORD"))
260 return TOKEN_WORD;
261 if (is_keyword(buf, "DWORD"))
262 return TOKEN_DWORD;
263 if (is_keyword(buf, "FLOAT"))
264 return TOKEN_FLOAT;
265 if (is_keyword(buf, "DOUBLE"))
266 return TOKEN_DOUBLE;
267 if (is_keyword(buf, "CHAR"))
268 return TOKEN_CHAR;
269 if (is_keyword(buf, "UCHAR"))
270 return TOKEN_UCHAR;
271 if (is_keyword(buf, "SWORD"))
272 return TOKEN_SWORD;
273 if (is_keyword(buf, "SDWORD"))
274 return TOKEN_SDWORD;
275 if (is_keyword(buf, "VOID"))
276 return TOKEN_VOID;
277 if (is_keyword(buf, "STRING"))
278 return TOKEN_LPSTR;
279 if (is_keyword(buf, "UNICODE"))
280 return TOKEN_UNICODE;
281 if (is_keyword(buf, "CSTRING"))
282 return TOKEN_CSTRING;
283 if (is_keyword(buf, "array"))
284 return TOKEN_ARRAY;
286 return 0;
289 static BOOL is_guid(parse_buffer* buf)
291 char tmp[50];
292 DWORD pos = 1;
293 GUID class_id;
294 DWORD tab[10];
295 int ret;
297 if (*buf->buffer != '<')
298 return FALSE;
299 tmp[0] = '<';
300 while (*(buf->buffer+pos) != '>')
302 tmp[pos] = *(buf->buffer+pos);
303 pos++;
305 tmp[pos++] = '>';
306 tmp[pos] = 0;
307 if (pos != 38 /* <+36+> */)
309 TRACE("Wrong guid %s (%d)\n", tmp, pos);
310 return FALSE;
312 buf->buffer += pos;
313 buf->rem_bytes -= pos;
315 ret = sscanf(tmp, CLSIDFMT, &class_id.Data1, tab, tab+1, tab+2, tab+3, tab+4, tab+5, tab+6, tab+7, tab+8, tab+9);
316 if (ret != 11)
318 TRACE("Wrong guid %s (%d)\n", tmp, pos);
319 return FALSE;
321 TRACE("Found guid %s (%d)\n", tmp, pos);
323 class_id.Data2 = tab[0];
324 class_id.Data3 = tab[1];
325 class_id.Data4[0] = tab[2];
326 class_id.Data4[1] = tab[3];
327 class_id.Data4[2] = tab[4];
328 class_id.Data4[3] = tab[5];
329 class_id.Data4[4] = tab[6];
330 class_id.Data4[5] = tab[7];
331 class_id.Data4[6] = tab[8];
332 class_id.Data4[7] = tab[9];
334 *(GUID*)buf->value = class_id;
336 return TRUE;
339 static BOOL is_name(parse_buffer* buf)
341 char tmp[50];
342 DWORD pos = 0;
343 char c;
344 BOOL error = 0;
345 while (!is_separator(c = *(buf->buffer+pos)))
347 if (!(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '_') || (c == '-')))
348 error = 1;
349 tmp[pos++] = c;
351 tmp[pos] = 0;
353 if (error)
355 TRACE("Wrong name %s\n", tmp);
356 return FALSE;
359 buf->buffer += pos;
360 buf->rem_bytes -= pos;
362 TRACE("Found name %s\n", tmp);
363 strcpy((char*)buf->value, tmp);
365 return TRUE;
368 static BOOL is_float(parse_buffer* buf)
370 char tmp[50];
371 DWORD pos = 0;
372 char c;
373 float decimal;
374 BOOL dot = 0;
376 while (!is_separator(c = *(buf->buffer+pos)))
378 if (!((!pos && (c == '-')) || ((c >= '0') && (c <= '9')) || (!dot && (c == '.'))))
379 return FALSE;
380 if (c == '.')
381 dot = TRUE;
382 tmp[pos++] = c;
384 tmp[pos] = 0;
386 buf->buffer += pos;
387 buf->rem_bytes -= pos;
389 sscanf(tmp, "%f", &decimal);
391 TRACE("Found float %s - %f\n", tmp, decimal);
393 *(float*)buf->value = decimal;
395 return TRUE;
398 static BOOL is_integer(parse_buffer* buf)
400 char tmp[50];
401 DWORD pos = 0;
402 char c;
403 DWORD integer;
405 while (!is_separator(c = *(buf->buffer+pos)))
407 if (!((c >= '0') && (c <= '9')))
408 return FALSE;
409 tmp[pos++] = c;
411 tmp[pos] = 0;
413 buf->buffer += pos;
414 buf->rem_bytes -= pos;
416 sscanf(tmp, "%d", &integer);
418 TRACE("Found integer %s - %d\n", tmp, integer);
420 *(DWORD*)buf->value = integer;
422 return TRUE;
425 static BOOL is_string(parse_buffer* buf)
427 char tmp[32];
428 DWORD pos = 0;
429 char c;
430 BOOL ok = 0;
432 if (*buf->buffer != '"')
433 return FALSE;
435 while (!is_separator(c = *(buf->buffer+pos+1)) && (pos < 31))
437 if (c == '"')
439 ok = 1;
440 break;
442 tmp[pos++] = c;
444 tmp[pos] = 0;
446 if (!ok)
448 TRACE("Wrong string %s\n", tmp);
449 return FALSE;
452 buf->buffer += pos + 2;
453 buf->rem_bytes -= pos + 2;
455 TRACE("Found string %s\n", tmp);
456 strcpy((char*)buf->value, tmp);
458 return TRUE;
461 static WORD parse_TOKEN(parse_buffer * buf)
463 WORD token;
465 if (buf->txt)
467 while(1)
469 char c;
470 if (!read_bytes(buf, &c, 1))
471 return 0;
472 /*TRACE("char = '%c'\n", is_space(c) ? ' ' : c);*/
473 if ((c == '#') || (c == '/'))
475 /* Handle comment (# or //) */
476 if (c == '/')
478 if (!read_bytes(buf, &c, 1))
479 return 0;
480 if (c != '/')
481 return 0;
483 c = 0;
484 while (c != 0x0A)
486 if (!read_bytes(buf, &c, 1))
487 return 0;
489 continue;
491 if (is_space(c))
492 continue;
493 if (is_operator(c) && (c != '<'))
495 token = get_operator_token(c);
496 break;
498 else if (c == '.')
500 token = TOKEN_DOT;
501 break;
503 else
505 buf->buffer -= 1;
506 buf->rem_bytes += 1;
508 if ((token = get_keyword_token(buf)))
509 break;
511 if (is_guid(buf))
513 token = TOKEN_GUID;
514 break;
516 if (is_integer(buf))
518 token = TOKEN_INTEGER;
519 break;
521 if (is_float(buf))
523 token = TOKEN_FLOAT;
524 break;
526 if (is_string(buf))
528 token = TOKEN_LPSTR;
529 break;
531 if (is_name(buf))
533 token = TOKEN_NAME;
534 break;
537 FIXME("Unrecognize element\n");
538 return 0;
542 else
544 static int nb_elem;
545 static int is_float;
547 if (!nb_elem)
549 if (!read_bytes(buf, &token, 2))
550 return 0;
552 /* Convert integer and float list into separate elements */
553 if (token == TOKEN_INTEGER_LIST)
555 if (!read_bytes(buf, &nb_elem, 4))
556 return 0;
557 token = TOKEN_INTEGER;
558 is_float = FALSE;
559 TRACE("Integer list (TOKEN_INTEGER_LIST) of size %d\n", nb_elem);
561 else if (token == TOKEN_FLOAT_LIST)
563 if (!read_bytes(buf, &nb_elem, 4))
564 return 0;
565 token = TOKEN_FLOAT;
566 is_float = TRUE;
567 TRACE("Float list (TOKEN_FLOAT_LIST) of size %d\n", nb_elem);
571 if (nb_elem)
573 token = is_float ? TOKEN_FLOAT : TOKEN_INTEGER;
574 nb_elem--;
576 DWORD integer;
578 if (!read_bytes(buf, &integer, 4))
579 return 0;
581 *(DWORD*)buf->value = integer;
583 dump_TOKEN(token);
584 return token;
587 switch (token)
589 case TOKEN_NAME:
591 DWORD count;
592 char strname[100];
594 if (!read_bytes(buf, &count, 4))
595 return 0;
596 if (!read_bytes(buf, strname, count))
597 return 0;
598 strname[count] = 0;
599 /*TRACE("name = %s\n", strname);*/
601 strcpy((char*)buf->value, strname);
603 break;
604 case TOKEN_INTEGER:
606 DWORD integer;
608 if (!read_bytes(buf, &integer, 4))
609 return 0;
610 /*TRACE("integer = %ld\n", integer);*/
612 *(DWORD*)buf->value = integer;
614 break;
615 case TOKEN_GUID:
617 char strguid[39];
618 GUID class_id;
620 if (!read_bytes(buf, &class_id, 16))
621 return 0;
622 sprintf(strguid, CLSIDFMT, class_id.Data1, class_id.Data2, class_id.Data3, class_id.Data4[0],
623 class_id.Data4[1], class_id.Data4[2], class_id.Data4[3], class_id.Data4[4], class_id.Data4[5],
624 class_id.Data4[6], class_id.Data4[7]);
625 /*TRACE("guid = {%s}\n", strguid);*/
627 *(GUID*)buf->value = class_id;
629 break;
630 case TOKEN_STRING:
632 DWORD count;
633 WORD tmp_token;
634 char strname[100];
635 if (!read_bytes(buf, &count, 4))
636 return 0;
637 if (!read_bytes(buf, strname, count))
638 return 0;
639 strname[count] = 0;
640 if (!read_bytes(buf, &tmp_token, 2))
641 return 0;
642 if ((tmp_token != TOKEN_COMMA) && (tmp_token != TOKEN_SEMICOLON))
643 ERR("No comma or semicolon (got %d)\n", tmp_token);
644 /*TRACE("name = %s\n", strname);*/
646 strcpy((char*)buf->value, strname);
647 token = TOKEN_LPSTR;
649 break;
650 case TOKEN_OBRACE:
651 case TOKEN_CBRACE:
652 case TOKEN_OPAREN:
653 case TOKEN_CPAREN:
654 case TOKEN_OBRACKET:
655 case TOKEN_CBRACKET:
656 case TOKEN_OANGLE:
657 case TOKEN_CANGLE:
658 case TOKEN_DOT:
659 case TOKEN_COMMA:
660 case TOKEN_SEMICOLON:
661 case TOKEN_TEMPLATE:
662 case TOKEN_WORD:
663 case TOKEN_DWORD:
664 case TOKEN_FLOAT:
665 case TOKEN_DOUBLE:
666 case TOKEN_CHAR:
667 case TOKEN_UCHAR:
668 case TOKEN_SWORD:
669 case TOKEN_SDWORD:
670 case TOKEN_VOID:
671 case TOKEN_LPSTR:
672 case TOKEN_UNICODE:
673 case TOKEN_CSTRING:
674 case TOKEN_ARRAY:
675 break;
676 default:
677 return 0;
681 dump_TOKEN(token);
683 return token;
686 static const char* get_primitive_string(WORD token)
688 switch(token)
690 case TOKEN_WORD:
691 return "WORD";
692 case TOKEN_DWORD:
693 return "DWORD";
694 case TOKEN_FLOAT:
695 return "FLOAT";
696 case TOKEN_DOUBLE:
697 return "DOUBLE";
698 case TOKEN_CHAR:
699 return "CHAR";
700 case TOKEN_UCHAR:
701 return "UCHAR";
702 case TOKEN_SWORD:
703 return "SWORD";
704 case TOKEN_SDWORD:
705 return "SDWORD";
706 case TOKEN_VOID:
707 return "VOID";
708 case TOKEN_LPSTR:
709 return "STRING";
710 case TOKEN_UNICODE:
711 return "UNICODE";
712 case TOKEN_CSTRING:
713 return "CSTRING ";
714 default:
715 break;
717 return NULL;
720 static WORD get_TOKEN(parse_buffer * buf)
722 if (buf->token_present)
724 buf->token_present = FALSE;
725 return buf->current_token;
728 buf->current_token = parse_TOKEN(buf);
730 return buf->current_token;
733 static WORD check_TOKEN(parse_buffer * buf)
735 if (buf->token_present)
736 return buf->current_token;
738 buf->current_token = parse_TOKEN(buf);
739 buf->token_present = TRUE;
741 return buf->current_token;
744 static inline BOOL is_primitive_type(WORD token)
746 BOOL ret;
747 switch(token)
749 case TOKEN_WORD:
750 case TOKEN_DWORD:
751 case TOKEN_FLOAT:
752 case TOKEN_DOUBLE:
753 case TOKEN_CHAR:
754 case TOKEN_UCHAR:
755 case TOKEN_SWORD:
756 case TOKEN_SDWORD:
757 case TOKEN_LPSTR:
758 case TOKEN_UNICODE:
759 case TOKEN_CSTRING:
760 ret = 1;
761 break;
762 default:
763 ret = 0;
764 break;
766 return ret;
769 BOOL parse_template_option_info(parse_buffer * buf)
771 xtemplate* cur_template = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates];
773 if (check_TOKEN(buf) == TOKEN_DOT)
775 get_TOKEN(buf);
776 if (get_TOKEN(buf) != TOKEN_DOT)
777 return FALSE;
778 if (get_TOKEN(buf) != TOKEN_DOT)
779 return FALSE;
780 cur_template->open = TRUE;
782 else
784 while (1)
786 if (get_TOKEN(buf) != TOKEN_NAME)
787 return FALSE;
788 strcpy(cur_template->childs[cur_template->nb_childs], (char*)buf->value);
789 if (check_TOKEN(buf) == TOKEN_GUID)
790 get_TOKEN(buf);
791 cur_template->nb_childs++;
792 if (check_TOKEN(buf) != TOKEN_COMMA)
793 break;
794 get_TOKEN(buf);
796 cur_template->open = FALSE;
799 return TRUE;
802 static BOOL parse_template_members_list(parse_buffer * buf)
804 int idx_member = 0;
805 member* cur_member;
807 while (1)
809 BOOL array = 0;
810 int nb_dims = 0;
811 cur_member = &buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[idx_member];
813 if (check_TOKEN(buf) == TOKEN_ARRAY)
815 get_TOKEN(buf);
816 array = 1;
819 if (check_TOKEN(buf) == TOKEN_NAME)
821 cur_member->type = get_TOKEN(buf);
822 cur_member->idx_template = 0;
823 while (cur_member->idx_template < buf->pdxf->nb_xtemplates)
825 if (!strcasecmp((char*)buf->value, buf->pdxf->xtemplates[cur_member->idx_template].name))
826 break;
827 cur_member->idx_template++;
829 if (cur_member->idx_template == buf->pdxf->nb_xtemplates)
831 ERR("Reference to a nonexistent template '%s'\n", (char*)buf->value);
832 return FALSE;
835 else if (is_primitive_type(check_TOKEN(buf)))
836 cur_member->type = get_TOKEN(buf);
837 else
838 break;
840 if (get_TOKEN(buf) != TOKEN_NAME)
841 return FALSE;
842 strcpy(cur_member->name, (char*)buf->value);
844 if (array)
846 while (check_TOKEN(buf) == TOKEN_OBRACKET)
848 if (nb_dims >= MAX_ARRAY_DIM)
850 FIXME("Too many dimensions (%d) for multi-dimensional array\n", nb_dims + 1);
851 return FALSE;
853 get_TOKEN(buf);
854 if (check_TOKEN(buf) == TOKEN_INTEGER)
856 get_TOKEN(buf);
857 cur_member->dim_fixed[nb_dims] = TRUE;
858 cur_member->dim_value[nb_dims] = *(DWORD*)buf->value;
860 else
862 int i;
863 if (get_TOKEN(buf) != TOKEN_NAME)
864 return FALSE;
865 for (i = 0; i < idx_member; i++)
867 if (!strcmp((char*)buf->value, buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].name))
869 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].nb_dims)
871 ERR("Array cannot be used to specify variable array size\n");
872 return FALSE;
874 if (buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].members[i].type != TOKEN_DWORD)
876 FIXME("Only DWORD supported to specify variable array size\n");
877 return FALSE;
879 break;
882 if (i == idx_member)
884 ERR("Reference to unknown member %s\n", (char*)buf->value);
885 return FALSE;
887 cur_member->dim_fixed[nb_dims] = FALSE;
888 cur_member->dim_value[nb_dims] = i;
890 if (get_TOKEN(buf) != TOKEN_CBRACKET)
891 return FALSE;
892 nb_dims++;
894 if (!nb_dims)
895 return FALSE;
896 cur_member->nb_dims = nb_dims;
898 if (get_TOKEN(buf) != TOKEN_SEMICOLON)
899 return FALSE;
901 idx_member++;
904 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].nb_members = idx_member;
906 return TRUE;
909 static BOOL parse_template_parts(parse_buffer * buf)
911 if (!parse_template_members_list(buf))
912 return FALSE;
913 if (check_TOKEN(buf) == TOKEN_OBRACKET)
915 get_TOKEN(buf);
916 if (!parse_template_option_info(buf))
917 return FALSE;
918 if (get_TOKEN(buf) != TOKEN_CBRACKET)
919 return FALSE;
922 return TRUE;
925 static void go_to_next_definition(parse_buffer * buf)
927 while (buf->rem_bytes)
929 char c = *buf->buffer;
930 if ((c == '#') || (c == '/'))
932 read_bytes(buf, &c, 1);
933 /* Handle comment (# or //) */
934 if (c == '/')
936 if (!read_bytes(buf, &c, 1))
937 return;
938 if (c != '/')
939 return;
941 c = 0;
942 while (c != 0x0A)
944 if (!read_bytes(buf, &c, 1))
945 return;
947 continue;
949 else if (is_space(*buf->buffer))
951 buf->buffer++;
952 buf->rem_bytes--;
954 else
955 break;
959 BOOL parse_template(parse_buffer * buf)
961 if (get_TOKEN(buf) != TOKEN_TEMPLATE)
962 return FALSE;
963 if (get_TOKEN(buf) != TOKEN_NAME)
964 return FALSE;
965 strcpy(buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].name, (char*)buf->value);
966 if (get_TOKEN(buf) != TOKEN_OBRACE)
967 return FALSE;
968 if (get_TOKEN(buf) != TOKEN_GUID)
969 return FALSE;
970 buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].class_id = *(GUID*)buf->value;
971 if (!parse_template_parts(buf))
972 return FALSE;
973 if (get_TOKEN(buf) != TOKEN_CBRACE)
974 return FALSE;
975 if (buf->txt)
977 /* Go to the next template */
978 go_to_next_definition(buf);
981 TRACE("%d - %s - %s\n", buf->pdxf->nb_xtemplates, buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].name, debugstr_guid(&buf->pdxf->xtemplates[buf->pdxf->nb_xtemplates].class_id));
982 buf->pdxf->nb_xtemplates++;
984 return TRUE;
987 static BOOL parse_object_members_list(parse_buffer * buf)
989 DWORD token;
990 int i;
991 xtemplate* pt = buf->pxt[buf->level];
992 DWORD last_dword = 0;
994 for (i = 0; i < pt->nb_members; i++)
996 int k;
997 int nb_elems = 1;
999 buf->pxo->members[i].name = pt->members[i].name;
1000 buf->pxo->members[i].start = buf->cur_pdata;
1002 for (k = 0; k < pt->members[i].nb_dims; k++)
1004 if (pt->members[i].dim_fixed[k])
1005 nb_elems *= pt->members[i].dim_value[k];
1006 else
1007 nb_elems *= *(DWORD*)buf->pxo->members[pt->members[i].dim_value[k]].start;
1010 TRACE("Elements to consider: %d\n", nb_elems);
1012 for (k = 0; k < nb_elems; k++)
1014 if (buf->txt && k)
1016 token = check_TOKEN(buf);
1017 if (token == TOKEN_COMMA)
1019 get_TOKEN(buf);
1021 else
1023 /* Allow comma omission */
1024 if (!((token == TOKEN_FLOAT) || (token == TOKEN_INTEGER)))
1025 return FALSE;
1029 if (pt->members[i].type == TOKEN_NAME)
1031 int j;
1033 TRACE("Found sub-object %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1034 buf->level++;
1035 /* To do template lookup */
1036 for (j = 0; j < buf->pdxf->nb_xtemplates; j++)
1038 if (!strcasecmp(buf->pdxf->xtemplates[pt->members[i].idx_template].name, buf->pdxf->xtemplates[j].name))
1040 buf->pxt[buf->level] = &buf->pdxf->xtemplates[j];
1041 break;
1044 if (j == buf->pdxf->nb_xtemplates)
1046 ERR("Unknown template %s\n", (char*)buf->value);
1047 buf->level--;
1048 return FALSE;
1050 TRACE("Enter %s\n", buf->pdxf->xtemplates[pt->members[i].idx_template].name);
1051 if (!parse_object_parts(buf, FALSE))
1053 buf->level--;
1054 return FALSE;
1056 buf->level--;
1058 else
1060 token = check_TOKEN(buf);
1061 if (token == TOKEN_INTEGER)
1063 get_TOKEN(buf);
1064 last_dword = *(DWORD*)buf->value;
1065 TRACE("%s = %d\n", pt->members[i].name, *(DWORD*)buf->value);
1066 /* Assume larger size */
1067 if ((buf->cur_pdata - buf->pdata + 4) > MAX_DATA_SIZE)
1069 FIXME("Buffer too small\n");
1070 return FALSE;
1072 if (pt->members[i].type == TOKEN_WORD)
1074 *(((WORD*)(buf->cur_pdata))) = (WORD)(*(DWORD*)buf->value);
1075 buf->cur_pdata += 2;
1077 else if (pt->members[i].type == TOKEN_DWORD)
1079 *(((DWORD*)(buf->cur_pdata))) = (DWORD)(*(DWORD*)buf->value);
1080 buf->cur_pdata += 4;
1082 else
1084 FIXME("Token %d not supported\n", pt->members[i].type);
1085 return FALSE;
1088 else if (token == TOKEN_FLOAT)
1090 get_TOKEN(buf);
1091 TRACE("%s = %f\n", pt->members[i].name, *(float*)buf->value);
1092 /* Assume larger size */
1093 if ((buf->cur_pdata - buf->pdata + 4) > MAX_DATA_SIZE)
1095 FIXME("Buffer too small\n");
1096 return FALSE;
1098 if (pt->members[i].type == TOKEN_FLOAT)
1100 *(((float*)(buf->cur_pdata))) = (float)(*(float*)buf->value);
1101 buf->cur_pdata += 4;
1103 else
1105 FIXME("Token %d not supported\n", pt->members[i].type);
1106 return FALSE;
1109 else if (token == TOKEN_LPSTR)
1111 get_TOKEN(buf);
1112 TRACE("%s = %s\n", pt->members[i].name, (char*)buf->value);
1113 /* Assume larger size */
1114 if ((buf->cur_pdata - buf->pdata + 4) > MAX_DATA_SIZE)
1116 FIXME("Buffer too small\n");
1117 return FALSE;
1119 if (pt->members[i].type == TOKEN_LPSTR)
1121 int len = strlen((char*)buf->value) + 1;
1122 if ((buf->cur_pstrings - buf->pstrings + len) > MAX_STRINGS_BUFFER)
1124 FIXME("Buffer too small %p %p %d\n", buf->cur_pstrings, buf->pstrings, len);
1125 return FALSE;
1127 strcpy((char*)buf->cur_pstrings, (char*)buf->value);
1128 *(((LPCSTR*)(buf->cur_pdata))) = (char*)buf->cur_pstrings;
1129 buf->cur_pstrings += len;
1130 buf->cur_pdata += 4;
1132 else
1134 FIXME("Token %d not supported\n", pt->members[i].type);
1135 return FALSE;
1138 else
1140 FIXME("Unexpected token %d\n", token);
1141 return FALSE;
1146 if (buf->txt && (check_TOKEN(buf) != TOKEN_CBRACE))
1148 token = get_TOKEN(buf);
1149 if ((token != TOKEN_SEMICOLON) && (token != TOKEN_COMMA))
1151 /* Allow comma instead of semicolon in some specific cases */
1152 if (!((token == TOKEN_COMMA) && ((i+1) < pt->nb_members) && (pt->members[i].type == pt->members[i+1].type)
1153 && (!pt->members[i].nb_dims) && (!pt->members[i+1].nb_dims)))
1154 return FALSE;
1159 return TRUE;
1162 static BOOL parse_object_parts(parse_buffer * buf, BOOL allow_optional)
1164 if (!parse_object_members_list(buf))
1165 return FALSE;
1167 if (allow_optional)
1169 buf->pxo->size = buf->cur_pdata - buf->pxo->pdata;
1171 /* Skip trailing semicolon */
1172 while (check_TOKEN(buf) == TOKEN_SEMICOLON)
1173 get_TOKEN(buf);
1175 while (1)
1177 if (check_TOKEN(buf) == TOKEN_OBRACE)
1179 int i, j;
1180 get_TOKEN(buf);
1181 if (get_TOKEN(buf) != TOKEN_NAME)
1182 return FALSE;
1183 if (get_TOKEN(buf) != TOKEN_CBRACE)
1184 return FALSE;
1185 TRACE("Found optional reference %s\n", (char*)buf->value);
1186 for (i = 0; i < buf->nb_pxo_globals; i++)
1188 for (j = 0; j < (buf->pxo_globals[i])[0].nb_subobjects; j++)
1190 if (!strcmp((buf->pxo_globals[i])[j].name, (char*)buf->value))
1191 goto _exit;
1194 _exit:
1195 if (i == buf->nb_pxo_globals)
1197 ERR("Reference to unknown object %s\n", (char*)buf->value);
1198 return FALSE;
1200 buf->pxo->childs[buf->pxo->nb_childs] = &buf->pxo_tab[buf->cur_subobject++];
1201 buf->pxo->childs[buf->pxo->nb_childs]->ptarget = &(buf->pxo_globals[i])[j];
1202 buf->pxo->nb_childs++;
1204 else if (check_TOKEN(buf) == TOKEN_NAME)
1206 xobject* pxo = buf->pxo;
1207 buf->pxo = buf->pxo->childs[buf->pxo->nb_childs] = &buf->pxo_tab[buf->cur_subobject++];
1209 TRACE("Enter optional %s\n", (char*)buf->value);
1210 buf->level++;
1211 if (!parse_object(buf))
1213 buf->level--;
1214 return FALSE;
1216 buf->level--;
1217 buf->pxo = pxo;
1218 buf->pxo->nb_childs++;
1220 else
1221 break;
1225 if (buf->pxo->nb_childs > MAX_CHILDS)
1227 FIXME("Too many childs %d\n", buf->pxo->nb_childs);
1228 return FALSE;
1231 return TRUE;
1234 BOOL parse_object(parse_buffer * buf)
1236 int i;
1238 buf->pxo->pdata = buf->cur_pdata;
1239 buf->pxo->ptarget = NULL;
1241 if (get_TOKEN(buf) != TOKEN_NAME)
1242 return FALSE;
1244 /* To do template lookup */
1245 for (i = 0; i < buf->pdxf->nb_xtemplates; i++)
1247 if (!strcasecmp((char*)buf->value, buf->pdxf->xtemplates[i].name))
1249 buf->pxt[buf->level] = &buf->pdxf->xtemplates[i];
1250 memcpy(&buf->pxo->type, &buf->pdxf->xtemplates[i].class_id, 16);
1251 break;
1254 if (i == buf->pdxf->nb_xtemplates)
1256 ERR("Unknown template %s\n", (char*)buf->value);
1257 return FALSE;
1260 if (check_TOKEN(buf) == TOKEN_NAME)
1262 get_TOKEN(buf);
1263 strcpy(buf->pxo->name, (char*)buf->value);
1265 else
1266 buf->pxo->name[0] = 0;
1268 if (get_TOKEN(buf) != TOKEN_OBRACE)
1269 return FALSE;
1270 if (check_TOKEN(buf) == TOKEN_GUID)
1272 get_TOKEN(buf);
1273 memcpy(&buf->pxo->class_id, buf->value, 16);
1275 else
1276 memset(&buf->pxo->class_id, 0, 16);
1278 if (!parse_object_parts(buf, TRUE))
1279 return FALSE;
1280 if (get_TOKEN(buf) != TOKEN_CBRACE)
1281 return FALSE;
1283 if (buf->txt)
1285 /* Go to the next object */
1286 go_to_next_definition(buf);
1289 return TRUE;