… | |
… | |
447 | { |
447 | { |
448 | int res=0; |
448 | int res=0; |
449 | int len=0; |
449 | int len=0; |
450 | int leng; |
450 | int leng; |
451 | int eof = 0; |
451 | int eof = 0; |
452 | psiconv_u8 marker; |
452 | psiconv_u8 marker,submarker; |
453 | psiconv_formula_list formula_stack; |
453 | psiconv_formula_list formula_stack; |
454 | psiconv_formula formula,subformula1,subformula2,subformula3,subformula4; |
454 | psiconv_formula formula,subformula,subformula1,subformula2, |
|
|
455 | subformula3,subformula4; |
|
|
456 | psiconv_u16 temp,nr_of_subs; |
455 | |
457 | |
456 | psiconv_progress(lev+1,off,"Going to read a formula element list"); |
458 | psiconv_progress(lev+1,off,"Going to read a formula element list"); |
457 | if (!(*result = malloc(sizeof(**result)))) |
459 | if (!(*result = malloc(sizeof(**result)))) |
458 | goto ERROR1; |
460 | goto ERROR1; |
459 | if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
461 | if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
… | |
… | |
530 | formula->type = formula_elements[marker].formula_type; |
532 | formula->type = formula_elements[marker].formula_type; |
531 | len += leng; |
533 | len += leng; |
532 | if ((res = psiconv_list_add(formula_stack,formula))) |
534 | if ((res = psiconv_list_add(formula_stack,formula))) |
533 | goto ERROR8; |
535 | goto ERROR8; |
534 | formula->type = psiconv_formula_unknown; |
536 | formula->type = psiconv_formula_unknown; |
535 | } else if (formula_elements[marker].formula_type == |
537 | } else if ((formula_elements[marker].formula_type == |
|
|
538 | psiconv_formula_dat_cellblock) || |
|
|
539 | (formula_elements[marker].formula_type == |
536 | psiconv_formula_dat_cellblock) { |
540 | psiconv_formula_dat_vcellblock)) { |
537 | psiconv_progress(lev+3,off+len,"Next item: a cell block"); |
541 | psiconv_progress(lev+3,off+len,"Next item: a cell block"); |
538 | if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng, |
542 | if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng, |
539 | &formula->data.dat_cellblock))) |
543 | &formula->data.dat_cellblock))) |
540 | goto ERROR8; |
544 | goto ERROR8; |
541 | formula->type = formula_elements[marker].formula_type; |
545 | formula->type = formula_elements[marker].formula_type; |
… | |
… | |
560 | (formula_elements[marker].formula_type == |
564 | (formula_elements[marker].formula_type == |
561 | psiconv_formula_dat_vcellblock)) { |
565 | psiconv_formula_dat_vcellblock)) { |
562 | psiconv_warn(lev+3,off+len,"Not yet supported formula mark!"); |
566 | psiconv_warn(lev+3,off+len,"Not yet supported formula mark!"); |
563 | goto ERROR8; |
567 | goto ERROR8; |
564 | } else if (formula_elements[marker].number_of_args == -1) { |
568 | } else if (formula_elements[marker].number_of_args == -1) { |
565 | psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); |
569 | psiconv_progress(lev+3,off+len,"Going to parse a vararg function"); |
|
|
570 | if (!(formula->data.fun_operands = |
|
|
571 | psiconv_list_new(sizeof(*formula)))) |
|
|
572 | goto ERROR8; |
|
|
573 | formula->type = formula_elements[marker].formula_type; |
|
|
574 | nr_of_subs = 0; |
|
|
575 | do { |
|
|
576 | nr_of_subs ++; |
|
|
577 | psiconv_progress(lev+4,off+len,"Going to read vararg argument %d", |
|
|
578 | nr_of_subs); |
|
|
579 | if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng, |
|
|
580 | &subformula,maxlen))) |
|
|
581 | goto ERROR8; |
|
|
582 | len += leng; |
|
|
583 | if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) { |
|
|
584 | psiconv_free_formula(subformula); |
|
|
585 | goto ERROR8; |
|
|
586 | } |
|
|
587 | free(subformula); |
|
|
588 | psiconv_progress(lev+4,off+len,"Going to read the next marker"); |
|
|
589 | submarker = psiconv_read_u8(buf,lev+4,off+len,&res); |
|
|
590 | len ++; |
|
|
591 | if (res) |
|
|
592 | goto ERROR8; |
|
|
593 | } while (formula_elements[submarker].formula_type |
|
|
594 | == psiconv_formula_mark_opsep); |
|
|
595 | if (formula_elements[submarker].formula_type |
|
|
596 | != psiconv_formula_mark_opend) { |
|
|
597 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
|
|
598 | psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker); |
566 | goto ERROR8; |
599 | goto ERROR8; |
|
|
600 | } |
|
|
601 | psiconv_progress(lev+3,off+len,"Going to read the repeated marker %02x", |
|
|
602 | marker); |
|
|
603 | submarker = psiconv_read_u8(buf,lev+3,off+len,&res); |
|
|
604 | if (res) |
|
|
605 | goto ERROR8; |
|
|
606 | if (submarker != marker) { |
|
|
607 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
|
|
608 | psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x", |
|
|
609 | marker,submarker); |
|
|
610 | goto ERROR8; |
|
|
611 | } |
|
|
612 | len++; |
|
|
613 | psiconv_progress(lev+3,off+len, |
|
|
614 | "Going to read the number of arguments (%d expected)", |
|
|
615 | nr_of_subs); |
|
|
616 | temp = psiconv_read_u16(buf,lev+3,off+len,&res); |
|
|
617 | if (res) |
|
|
618 | goto ERROR8; |
|
|
619 | if (temp != nr_of_subs) { |
|
|
620 | psiconv_warn(lev+3,off+len,"Formula corrupted!"); |
|
|
621 | psiconv_debug(lev+3,off+len, |
|
|
622 | "Read %d arguments, but formula says there are %d", |
|
|
623 | nr_of_subs,temp); |
|
|
624 | goto ERROR8; |
|
|
625 | } |
|
|
626 | len += 2; |
|
|
627 | if ((res = psiconv_list_add(formula_stack,formula))) |
|
|
628 | goto ERROR8; |
|
|
629 | formula->type = psiconv_formula_unknown; |
567 | } else { |
630 | } else { |
568 | if (formula_elements[marker].number_of_args > 0) |
631 | if (formula_elements[marker].number_of_args > 0) |
569 | if ((res = psiconv_list_pop(formula_stack,subformula1))) |
632 | if ((res = psiconv_list_pop(formula_stack,subformula1))) |
570 | goto ERROR8; |
633 | goto ERROR8; |
571 | if (formula_elements[marker].number_of_args > 1) |
634 | if (formula_elements[marker].number_of_args > 1) |