/[public]/psiconv/trunk/lib/psiconv/parse_sheet.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/parse_sheet.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 122 Revision 133
72 psiconv_free_character_layout(result->character); 72 psiconv_free_character_layout(result->character);
73ERROR2: 73ERROR2:
74 free(result); 74 free(result);
75ERROR1: 75ERROR1:
76 return NULL; 76 return NULL;
77}
78
79static psiconv_sheet_cell_reference_t
80 psiconv_read_var_cellref (const psiconv_buffer buf, int lev,
81 psiconv_u32 off, int *length,
82 int *status)
83{
84 int len=0;
85 int res;
86 psiconv_sheet_cell_reference_t result;
87 psiconv_u32 temp;
88
89 psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference");
90 psiconv_progress(lev+2,off+len,
91 "Going to read the initial byte (%02x expected)",0x00);
92 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
93 if (res)
94 goto ERROR1;
95 if (temp != 0x00) {
96 psiconv_warn(lev+2,off+len,
97 "Sheet cell reference initial byte unknown value (ignored)");
98 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
99 }
100 len ++;
101
102 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
103 if (res)
104 goto ERROR1;
105 if (temp & 0xffff0000) {
106 psiconv_warn(lev+2,off+len,
107 "Sheet cell row reference to unknown row (reset)");
108 }
109 result.row.offset = temp;
110 result.row.absolute = psiconv_bool_true;
111 len += 4;
112
113 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
114 if (res)
115 goto ERROR1;
116 if (temp & 0xffff0000) {
117 psiconv_warn(lev+2,off+len,
118 "Sheet cell column reference to unknown row (reset)");
119 }
120 result.column.offset = temp;
121 result.column.absolute = psiconv_bool_true;
122 len += 4;
123
124 if (length)
125 *length = len;
126
127 psiconv_progress(lev,off+len-1,
128 "End of sheet column reference (total length: %08x)", len);
129 return result;
130ERROR1:
131 psiconv_warn(lev+1,off,"Reading of Sheet Column Reference failed");
132 if (length)
133 *length = 0;
134 if (status)
135 *status = res?res:-PSICONV_E_NOMEM;
136 return result;
137}
138
139static psiconv_sheet_cell_block_t
140 psiconv_read_var_cellblock (const psiconv_buffer buf, int lev,
141 psiconv_u32 off, int *length,
142 int *status)
143{
144 int len=0;
145 int res;
146 psiconv_sheet_cell_block_t result;
147 psiconv_u32 temp;
148
149 psiconv_progress(lev+1,off+len,"Going to read a sheet cell block reference");
150 psiconv_progress(lev+2,off+len,
151 "Going to read the initial byte (%02x expected)",0x00);
152 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
153 if (res)
154 goto ERROR1;
155 if (temp != 0x00) {
156 psiconv_warn(lev+2,off+len,
157 "Sheet cell reference initial byte unknown value (ignored)");
158 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
159 }
160 len ++;
161
162 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
163 if (res)
164 goto ERROR1;
165 if (temp & 0xffff0000) {
166 psiconv_warn(lev+2,off+len,
167 "Sheet block initial row reference to unknown row (reset)");
168 }
169 result.first.row.offset = temp;
170 result.first.row.absolute = psiconv_bool_true;
171 len += 4;
172
173 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
174 if (res)
175 goto ERROR1;
176 if (temp & 0xffff0000) {
177 psiconv_warn(lev+2,off+len,
178 "Sheet block initial column reference to unknown row (reset)");
179 }
180 result.first.column.offset = temp;
181 result.first.column.absolute = psiconv_bool_true;
182 len += 4;
183
184 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
185 if (res)
186 goto ERROR1;
187 if (temp & 0xffff0000) {
188 psiconv_warn(lev+2,off+len,
189 "Sheet block final row reference to unknown row (reset)");
190 }
191 result.last.row.offset = temp;
192 result.last.row.absolute = psiconv_bool_true;
193 len += 4;
194
195 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
196 if (res)
197 goto ERROR1;
198 if (temp & 0xffff0000) {
199 psiconv_warn(lev+2,off+len,
200 "Sheet block final column reference to unknown row (reset)");
201 }
202 result.last.column.offset = temp;
203 result.last.column.absolute = psiconv_bool_true;
204 len += 4;
205
206 if (length)
207 *length = len;
208
209 psiconv_progress(lev,off+len-1,
210 "End of sheet cell block reference (total length: %08x)",
211 len);
212 return result;
213ERROR1:
214 psiconv_warn(lev+1,off,"Reading of Sheet Cell Block Reference failed");
215 if (length)
216 *length = 0;
217 if (status)
218 *status = res?res:-PSICONV_E_NOMEM;
219 return result;
77} 220}
78 221
79int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev, 222int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev,
80 psiconv_u32 off, int *length, 223 psiconv_u32 off, int *length,
81 psiconv_sheet_numberformat result) 224 psiconv_sheet_numberformat result)
311 if (res) 454 if (res)
312 goto ERROR2; 455 goto ERROR2;
313 if (temp != 0x00) { 456 if (temp != 0x00) {
314 psiconv_warn(lev+2,off+len, 457 psiconv_warn(lev+2,off+len,
315 "Sheet status section unknown byte unknown value (ignored)"); 458 "Sheet status section unknown byte unknown value (ignored)");
316 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 459 psiconv_debug(lev+2,off+len,"Unknown byte: %02x",temp);
317 } 460 }
318 len ++; 461 len ++;
319 462
320 psiconv_progress(lev+2,off+len,"Going to read sheet display size"); 463 psiconv_progress(lev+2,off+len,"Going to read sheet display size");
321 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); 464 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res);
354 497
355int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev, 498int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev,
356 psiconv_u32 off, int *length, 499 psiconv_u32 off, int *length,
357 psiconv_sheet_workbook_section *result) 500 psiconv_sheet_workbook_section *result)
358{ 501{
359 int res=0; 502 int res=0,with_name;
360 psiconv_u32 temp,formulas_off,worksheets_off; 503 psiconv_u32 temp,formulas_off,worksheets_off,info_off,var_off,name_off=0;
361 int len=0; 504 int len=0;
362 505
363 psiconv_progress(lev+1,off,"Going to read the sheet workbook section"); 506 psiconv_progress(lev+1,off,"Going to read the sheet workbook section");
364 if (!(*result = malloc(sizeof(**result)))) 507 if (!(*result = malloc(sizeof(**result))))
365 goto ERROR1; 508 goto ERROR1;
366 509
367 psiconv_progress(lev+2,off+len, 510 psiconv_progress(lev+2,off+len,
368 "Going to read the initial byte (%02x expected)",0x04); 511 "Going to read the initial byte (%02x or %02x expected)",
512 0x02,0x04);
369 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 513 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
370 if (res) 514 if (res)
371 goto ERROR2; 515 goto ERROR2;
372 if (temp != 0x04) { 516 if ((temp != 0x04) && temp !=0x02) {
373 psiconv_warn(lev+2,off+len, 517 psiconv_warn(lev+2,off+len,
374 "Sheet workbook section initial byte unknown value (ignored)"); 518 "Sheet workbook section initial byte unknown value (ignored)");
375 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 519 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
376 } 520 }
521 with_name = temp ==0x04;
377 len ++; 522 len ++;
378 523
379 psiconv_progress(lev+2,off+len, 524 psiconv_progress(lev+2,off+len,
380 "Going to read the offset of the 1st ??? Section"); 525 "Going to read the offset of the sheet info Section");
381 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 526 info_off = psiconv_read_u32(buf,lev+2,off+len,&res);
382 if (res) 527 if (res)
383 goto ERROR2; 528 goto ERROR2;
384 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 529 psiconv_debug(lev+2,off+len,"Offset: %04x",info_off);
385 len += 4; 530 len += 4;
386 531
387 psiconv_progress(lev+2,off+len, 532 psiconv_progress(lev+2,off+len,
388 "Going to read the offset of the Formulas List"); 533 "Going to read the offset of the Formulas List");
389 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res); 534 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res);
399 goto ERROR2; 544 goto ERROR2;
400 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off); 545 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off);
401 len += 4; 546 len += 4;
402 547
403 psiconv_progress(lev+2,off+len, 548 psiconv_progress(lev+2,off+len,
404 "Going to read the offset of the 4th ??? Section"); 549 "Going to read the offset of the Variable List");
405 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 550 var_off = psiconv_read_u32(buf,lev+2,off+len,&res);
406 if (res) 551 if (res)
407 goto ERROR2; 552 goto ERROR2;
408 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 553 psiconv_debug(lev+2,off+len,"Offset: %04x",var_off);
409 len += 4; 554 len += 4;
410 555
556 if (with_name) {
557 psiconv_progress(lev+2,off+len,
558 "Going to read the offset of the Name Section");
559 name_off = psiconv_read_u32(buf,lev+2,off+len,&res);
560 if (res)
561 goto ERROR2;
562 psiconv_debug(lev+2,off+len,"Offset: %04x",name_off);
563 len += 4;
564 }
565
566
567 psiconv_progress(lev+2,off+len,"Going to read the info section");
568 if ((res = psiconv_parse_sheet_info_section(buf,lev+2,info_off,NULL,
569 &(*result)->info)))
570 goto ERROR2;
571
572 psiconv_progress(lev+2,off+len,"Going to read the variables list");
573 if ((res = psiconv_parse_sheet_variable_list(buf,lev+2,var_off,NULL,
574 &(*result)->variables)))
575 goto ERROR3;
576
411 psiconv_progress(lev+2,off+len,"Going to read the formulas list"); 577 psiconv_progress(lev+2,off+len,"Going to read the formulas list");
412 if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL, 578 if ((res = psiconv_parse_sheet_formula_list(buf,lev+2,formulas_off,NULL,
413 &(*result)->formulas))) 579 &(*result)->formulas)))
414 goto ERROR2; 580 goto ERROR4;
415 581
416 psiconv_progress(lev+2,off+len,"Going to read the worksheet list"); 582 psiconv_progress(lev+2,off+len,"Going to read the worksheet list");
417 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off, 583 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off,
418 NULL,&(*result)->worksheets))) 584 NULL,&(*result)->worksheets)))
419 goto ERROR2; 585 goto ERROR5;
420 586
587 if (with_name) {
588 psiconv_progress(lev+2,off+len,"Going to read the name section");
589 if ((res = psiconv_parse_sheet_name_section(buf,lev+2,name_off,NULL,
590 &(*result)->name)))
591 goto ERROR6;
592 } else
593 (*result)->name = NULL;
421 594
422 if (length) 595 if (length)
423 *length = len; 596 *length = len;
424 597
425 psiconv_progress(lev,off+len-1, 598 psiconv_progress(lev,off+len-1,
426 "End of sheet workbook section (total length: %08x)", len); 599 "End of sheet workbook section (total length: %08x)", len);
427 return 0; 600 return 0;
428 601
602ERROR6:
603 psiconv_free_sheet_worksheet_list((*result)->worksheets);
604ERROR5:
605 psiconv_free_formula_list((*result)->formulas);
606ERROR4:
607 psiconv_free_sheet_variable_list((*result)->variables);
608ERROR3:
609 psiconv_free_sheet_info_section((*result)->info);
429ERROR2: 610ERROR2:
430 free (*result); 611 free (*result);
431ERROR1: 612ERROR1:
432 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed"); 613 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed");
433 if (length) 614 if (length)
436 return -PSICONV_E_NOMEM; 617 return -PSICONV_E_NOMEM;
437 else 618 else
438 return res; 619 return res;
439} 620}
440 621
622int psiconv_parse_sheet_name_section(const psiconv_buffer buf, int lev,
623 psiconv_u32 off, int *length,
624 psiconv_sheet_name_section *result)
625{
626 int res=0;
627 psiconv_u32 temp;
628 int len=0,leng;
629
630 psiconv_progress(lev+1,off,"Going to read the sheet name section");
631 if (!(*result = malloc(sizeof(**result))))
632 goto ERROR1;
633
634 psiconv_progress(lev+2,off+len,
635 "Going to read the initial byte (%02x expected)",0x02);
636 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
637 if (res)
638 goto ERROR2;
639 if (temp != 0x02) {
640 psiconv_warn(lev+2,off+len,
641 "Sheet name section initial byte unknown value (ignored)");
642 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
643 }
644 len ++;
645
646 psiconv_progress(lev+2,off+len, "Going to read the sheet name");
647 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
648 if (res)
649 goto ERROR2;
650 len += leng;
651
652 if (length)
653 *length = len;
654
655 psiconv_progress(lev,off+len-1,
656 "End of sheet name section (total length: %08x)", len);
657 return 0;
658
659ERROR2:
660 free(*result);
661ERROR1:
662 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
663 if (length)
664 *length = 0;
665 if (!res)
666 return -PSICONV_E_NOMEM;
667 else
668 return res;
669}
670
671int psiconv_parse_sheet_info_section(const psiconv_buffer buf, int lev,
672 psiconv_u32 off, int *length,
673 psiconv_sheet_info_section *result)
674{
675 int res=0;
676 psiconv_u32 temp;
677 int len=0,leng;
678
679 psiconv_progress(lev+1,off,"Going to read the sheet info section");
680 if (!(*result = malloc(sizeof(**result))))
681 goto ERROR1;
682
683 psiconv_progress(lev+2,off+len,
684 "Going to read the initial byte (%02x expected)",0x02);
685 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
686 if (res)
687 goto ERROR2;
688 if (temp != 0x02) {
689 psiconv_warn(lev+2,off+len,
690 "Sheet info section initial byte unknown value (ignored)");
691 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
692 }
693 len ++;
694
695 psiconv_progress(lev+2,off+len, "Going to read an unknown Xint");
696 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
697 if (res)
698 goto ERROR2;
699 psiconv_debug(lev+2,off+len,"Value: %d\n",temp);
700 len += leng;
701
702 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
703 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
704 if (res)
705 goto ERROR2;
706 (*result)->auto_recalc = temp & 0x01 ? psiconv_bool_true:psiconv_bool_false;
707 psiconv_debug(lev+2,off+len,"Auto recalculation: %02x",
708 (*result)->auto_recalc);
709 if ((temp & 0xfe) != 0x02) {
710 psiconv_warn(lev+2,off+len,"Sheet Info Section flags byte "
711 "contains unknown flags (ignored)");
712 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp &0xfe);
713 }
714
715 len ++;
716
717
718 if (length)
719 *length = len;
720
721 psiconv_progress(lev,off+len-1,
722 "End of sheet info section (total length: %08x)", len);
723 return 0;
724
725ERROR2:
726 free(*result);
727ERROR1:
728 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
729 if (length)
730 *length = 0;
731 if (!res)
732 return -PSICONV_E_NOMEM;
733 else
734 return res;
735}
736
441int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev, 737int psiconv_parse_sheet_formula_list(const psiconv_buffer buf, int lev,
442 psiconv_u32 off, int *length, 738 psiconv_u32 off, int *length,
443 psiconv_formula_list *result) 739 psiconv_formula_list *result)
444{ 740{
445 int res=0; 741 int res=0;
446 int len=0; 742 int len=0;
447 psiconv_u32 temp; 743 psiconv_u32 temp;
448 psiconv_formula formula; 744 psiconv_formula formula;
449 psiconv_u32 listlen,i; 745 psiconv_u32 listlen,i;
450 int leng; 746 int leng;
451 747
452 psiconv_progress(lev+1,off,"Going to read the sheet formula table"); 748 psiconv_progress(lev+1,off,"Going to read the sheet formula list");
453 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 749 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s))))
454 goto ERROR1; 750 goto ERROR1;
455 751
456 psiconv_progress(lev+2,off+len, 752 psiconv_progress(lev+2,off+len,
457 "Going to read the initial byte (%02x expected)",0x02); 753 "Going to read the initial byte (%02x expected)",0x02);
458 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 754 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
459 if (res) 755 if (res)
460 goto ERROR2; 756 goto ERROR2;
461 if (temp != 0x02) { 757 if (temp != 0x02) {
462 psiconv_warn(lev+2,off+len, 758 psiconv_warn(lev+2,off+len,
463 "Sheet formula table initial byte unknown value (ignored)"); 759 "Sheet formula list initial byte unknown value (ignored)");
464 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 760 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
465 } 761 }
466 len ++; 762 len ++;
467 763
468 psiconv_progress(lev+2,off+len, 764 psiconv_progress(lev+2,off+len,
485 781
486 if (length) 782 if (length)
487 *length = len; 783 *length = len;
488 784
489 psiconv_progress(lev,off+len-1, 785 psiconv_progress(lev,off+len-1,
490 "End of sheet formula table (total length: %08x)", len); 786 "End of sheet formula list (total length: %08x)", len);
491 return 0; 787 return 0;
492 788
493ERROR3: 789ERROR3:
494 psiconv_free_formula(formula); 790 psiconv_free_formula(formula);
495ERROR2: 791ERROR2:
496 psiconv_list_free(*result); 792 psiconv_list_free(*result);
497ERROR1: 793ERROR1:
498 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); 794 psiconv_warn(lev+1,off,"Reading of Sheet Formula list failed");
499 if (length) 795 if (length)
500 *length = 0; 796 *length = 0;
501 if (!res) 797 if (!res)
502 return -PSICONV_E_NOMEM; 798 return -PSICONV_E_NOMEM;
503 else 799 else
505} 801}
506 802
507int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, 803int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
508 psiconv_u32 off, int *length, 804 psiconv_u32 off, int *length,
509 psiconv_sheet_cell *result, 805 psiconv_sheet_cell *result,
510 const psiconv_sheet_cell_layout default_layout) 806 const psiconv_sheet_cell_layout default_layout,
807 const psiconv_sheet_line_list row_default_layouts,
808 const psiconv_sheet_line_list col_default_layouts)
511{ 809{
512 int res=0; 810 int res=0;
513 int len=0; 811 int len=0;
514 psiconv_u32 temp; 812 psiconv_u32 temp;
515 psiconv_bool_t has_layout; 813 psiconv_bool_t has_layout;
622 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); 920 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
623 res = PSICONV_E_PARSE; 921 res = PSICONV_E_PARSE;
624 goto ERROR2; 922 goto ERROR2;
625 } 923 }
626 924
627 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout))) 925 if (!((*result)->layout = psiconv_clone_cell_layout(
926 psiconv_get_default_layout(row_default_layouts,
927 col_default_layouts,
928 default_layout,
929 (*result)->row,
930 (*result)->column))))
628 goto ERROR2; 931 goto ERROR2;
629 if (has_layout) { 932 if (has_layout) {
630 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len, 933 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
631 &leng,(*result)->layout))) 934 &leng,(*result)->layout)))
632 goto ERROR2; 935 goto ERROR2;
661 else 964 else
662 return res; 965 return res;
663} 966}
664 967
665int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, 968int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
666 psiconv_u32 off, int *length, 969 psiconv_u32 off, int *length,
667 psiconv_sheet_cell_list *result, 970 psiconv_sheet_cell_list *result,
668 const psiconv_sheet_cell_layout default_layout) 971 const psiconv_sheet_cell_layout default_layout,
972 const psiconv_sheet_line_list row_default_layouts,
973 const psiconv_sheet_line_list col_default_layouts)
669{ 974{
670 int res=0; 975 int res=0;
671 int len=0; 976 int len=0;
672 psiconv_u32 temp; 977 psiconv_u32 temp;
673 psiconv_sheet_cell cell; 978 psiconv_sheet_cell cell;
712 1017
713 psiconv_progress(lev+2,off+len,"Going to read all cells"); 1018 psiconv_progress(lev+2,off+len,"Going to read all cells");
714 for (i = 0; i < listlen; i++) { 1019 for (i = 0; i < listlen; i++) {
715 psiconv_progress(lev+3,off+len,"Going to read cell %d",i); 1020 psiconv_progress(lev+3,off+len,"Going to read cell %d",i);
716 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell, 1021 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
1022 default_layout,row_default_layouts,
717 default_layout))) 1023 col_default_layouts)))
718 goto ERROR2; 1024 goto ERROR2;
719 if ((res = psiconv_list_add(*result,cell))) 1025 if ((res = psiconv_list_add(*result,cell)))
720 goto ERROR3; 1026 goto ERROR3;
721 free(cell); 1027 free(cell);
722 len += leng; 1028 len += leng;
905int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev, 1211int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
906 psiconv_u32 off, int *length, 1212 psiconv_u32 off, int *length,
907 psiconv_sheet_worksheet *result) 1213 psiconv_sheet_worksheet *result)
908{ 1214{
909 int res=0; 1215 int res=0;
910 psiconv_u32 temp,cells_off,grid_off; 1216 psiconv_u32 temp,cells_off,grid_off,rows_off,cols_off,unknown_off;
911 int len=0; 1217 int len=0;
912 int leng; 1218 int leng;
913 1219
914 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section"); 1220 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
915 if (!(*result = malloc(sizeof(**result)))) 1221 if (!(*result = malloc(sizeof(**result))))
946 (*result)->default_layout))) 1252 (*result)->default_layout)))
947 goto ERROR3; 1253 goto ERROR3;
948 len += leng; 1254 len += leng;
949 1255
950 psiconv_progress(lev+2,off+len, 1256 psiconv_progress(lev+2,off+len,
951 "Going to read the offset of the 1st ??? Section"); 1257 "Going to read the offset of the row defaults Section");
952 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 1258 rows_off = psiconv_read_u32(buf,lev+2,off+len,&res);
953 if (res) 1259 if (res)
954 goto ERROR3; 1260 goto ERROR3;
955 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 1261 psiconv_debug(lev+2,off+len,"Offset: %04x",rows_off);
956 len += 4; 1262 len += 4;
957 1263
958 psiconv_progress(lev+2,off+len, 1264 psiconv_progress(lev+2,off+len,
959 "Going to read the offset of the 2nd ??? Section"); 1265 "Going to read the offset of the column defaults Section");
960 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 1266 cols_off = psiconv_read_u32(buf,lev+2,off+len,&res);
961 if (res) 1267 if (res)
962 goto ERROR3; 1268 goto ERROR3;
963 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 1269 psiconv_debug(lev+2,off+len,"Offset: %04x",cols_off);
964 len += 4; 1270 len += 4;
965 1271
966 psiconv_progress(lev+2,off+len, 1272 psiconv_progress(lev+2,off+len,
967 "Going to read the offset of the Cells List"); 1273 "Going to read the offset of the Cells List");
968 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res); 1274 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
979 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off); 1285 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
980 len += 4; 1286 len += 4;
981 1287
982 psiconv_progress(lev+2,off+len, 1288 psiconv_progress(lev+2,off+len,
983 "Going to read the offset of the 3rd ??? Section"); 1289 "Going to read the offset of the 3rd ??? Section");
984 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 1290 unknown_off = psiconv_read_u32(buf,lev+2,off+len,&res);
985 if (res) 1291 if (res)
986 goto ERROR3; 1292 goto ERROR3;
987 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 1293 psiconv_debug(lev+2,off+len,"Offset: %04x",unknown_off);
988 len += 4; 1294 len += 4;
1295
1296 psiconv_progress(lev+2,off+len,
1297 "Going to read a long of the 3rd ??? Section "
1298 "(%08x expected)",0x00);
1299 temp = psiconv_read_u32(buf,lev+2,unknown_off,&res);
1300 if (res)
1301 goto ERROR3;
1302 if (temp != 0x00) {
1303 psiconv_warn(lev+2,unknown_off,
1304 "Unknown worksheet subsection has unknown contents (ignored)");
1305 psiconv_debug(lev+2,unknown_off,"Offset: %04x",temp);
1306 }
1307 len += 4;
1308
1309 psiconv_progress(lev+2,off+len,"Going to read the row defaults");
1310 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,rows_off,NULL,
1311 &(*result)->row_default_layouts,
1312 (*result)->default_layout)))
1313 goto ERROR3;
1314
1315 psiconv_progress(lev+2,off+len,"Going to read the column defaults");
1316 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,cols_off,NULL,
1317 &(*result)->col_default_layouts,
1318 (*result)->default_layout)))
1319 goto ERROR4;
989 1320
990 psiconv_progress(lev+2,off+len,"Going to read the cells list"); 1321 psiconv_progress(lev+2,off+len,"Going to read the cells list");
991 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL, 1322 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
992 &(*result)->cells, 1323 &(*result)->cells,
993 (*result)->default_layout))) 1324 (*result)->default_layout,
1325 (*result)->row_default_layouts,
1326 (*result)->col_default_layouts)))
994 goto ERROR3; 1327 goto ERROR5;
995 1328
996 1329
997/* TODO: parse grid section */ 1330/* TODO: parse grid section */
998 1331
999 if (length) 1332 if (length)
1001 1334
1002 psiconv_progress(lev,off+len-1, 1335 psiconv_progress(lev,off+len-1,
1003 "End of sheet worksheet section (total length: %08x)", len); 1336 "End of sheet worksheet section (total length: %08x)", len);
1004 return 0; 1337 return 0;
1005 1338
1339ERROR5:
1340 psiconv_free_sheet_line_list((*result)->col_default_layouts);
1341ERROR4:
1342 psiconv_free_sheet_line_list((*result)->row_default_layouts);
1006ERROR3: 1343ERROR3:
1007 psiconv_free_sheet_cell_layout((*result)->default_layout); 1344 psiconv_free_sheet_cell_layout((*result)->default_layout);
1008ERROR2: 1345ERROR2:
1009 free (*result); 1346 free (*result);
1010ERROR1: 1347ERROR1:
1015 return -PSICONV_E_NOMEM; 1352 return -PSICONV_E_NOMEM;
1016 else 1353 else
1017 return res; 1354 return res;
1018} 1355}
1019 1356
1357int psiconv_parse_sheet_line(const psiconv_buffer buf, int lev,
1358 psiconv_u32 off, int *length,
1359 psiconv_sheet_line *result,
1360 const psiconv_sheet_cell_layout default_layout)
1361{
1362 int res=0;
1363 int len=0;
1364 int leng;
1020 1365
1366
1367 psiconv_progress(lev+1,off,"Going to read a sheet line");
1368 if (!(*result = malloc(sizeof(**result))))
1369 goto ERROR1;
1370
1371 psiconv_progress(lev+2,off+len,"Going to read the line number");
1372 (*result)->position = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1373 if (res)
1374 goto ERROR2;
1375 psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->position);
1376 len += leng;
1377
1378 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
1379 goto ERROR2;
1380 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
1381 &leng,(*result)->layout)))
1382 goto ERROR3;
1383 len += leng;
1384
1385 if (length)
1386 *length = len;
1387
1388 psiconv_progress(lev,off+len-1,
1389 "End of the sheet line (total length: %08x)", len);
1390 return 0;
1391
1392ERROR3:
1393 psiconv_free_sheet_cell_layout((*result)->layout);
1394ERROR2:
1395 free (*result);
1396ERROR1:
1397 psiconv_warn(lev+1,off,"Reading of the sheet line failed");
1398 if (length)
1399 *length = 0;
1400 if (!res)
1401 return -PSICONV_E_NOMEM;
1402 else
1403 return res;
1404}
1405
1406
1407int psiconv_parse_sheet_line_list(const psiconv_buffer buf, int lev,
1408 psiconv_u32 off, int *length,
1409 psiconv_sheet_line_list *result,
1410 const psiconv_sheet_cell_layout default_layout)
1411{
1412 int res=0;
1413 int len=0;
1414 psiconv_u32 temp;
1415 psiconv_sheet_line line;
1416 psiconv_u32 listlen,i;
1417 int leng;
1418
1419 psiconv_progress(lev+1,off,"Going to read the sheet line list");
1420 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_line_s))))
1421 goto ERROR1;
1422
1423 psiconv_progress(lev+2,off+len,
1424 "Going to read the initial byte (%02x expected)",0x02);
1425 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1426 if (res)
1427 goto ERROR2;
1428 if (temp != 0x02) {
1429 psiconv_warn(lev+2,off+len,
1430 "Sheet line list initial byte unknown value (ignored)");
1431 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1432 }
1433 len ++;
1434
1435 psiconv_progress(lev+2,off+len,
1436 "Going to read the number of defined lines");
1437 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1438 if (res)
1439 goto ERROR2;
1440 psiconv_debug(lev+2,off+len,"Number of defined lines: %d",listlen);
1441 len += leng;
1442
1443 psiconv_progress(lev+2,off+len,"Going to read all lines");
1444 for (i = 0; i < listlen; i++) {
1445 psiconv_progress(lev+3,off+len,"Going to read line %d",i);
1446 if ((res = psiconv_parse_sheet_line(buf,lev+3,off+len,&leng,&line,
1447 default_layout)))
1448 goto ERROR2;
1449 if ((res = psiconv_list_add(*result,line)))
1450 goto ERROR3;
1451 free(line);
1452 len += leng;
1453 }
1454
1455 if (length)
1456 *length = len;
1457
1458 psiconv_progress(lev,off+len-1,
1459 "End of sheet line list (total length: %08x)", len);
1460 return 0;
1461
1462ERROR3:
1463 psiconv_free_sheet_line(line);
1464ERROR2:
1465 psiconv_free_sheet_line_list(*result);
1466ERROR1:
1467 psiconv_warn(lev+1,off,"Reading of Sheet Line List failed");
1468 if (length)
1469 *length = 0;
1470 if (!res)
1471 return -PSICONV_E_NOMEM;
1472 else
1473 return res;
1474}
1475
1476int psiconv_parse_sheet_variable(const psiconv_buffer buf, int lev,
1477 psiconv_u32 off, int *length,
1478 psiconv_sheet_variable *result)
1479{
1480 int res=0;
1481 int len=0;
1482 psiconv_u32 marker;
1483 int leng;
1484
1485 psiconv_progress(lev+1,off,"Going to read a sheet variable");
1486 if (!(*result = malloc(sizeof(**result))))
1487 goto ERROR1;
1488
1489 psiconv_progress(lev+2,off+len, "Going to read the variable name");
1490 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
1491 if (res)
1492 goto ERROR2;
1493 len += leng;
1494
1495 psiconv_progress(lev+2,off+len,"Going to read the type marker");
1496 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
1497 if (res)
1498 goto ERROR3;
1499 psiconv_debug(lev+2,off+len,"Marker: %02x",marker);
1500 len ++;
1501
1502 if (marker == 0x00) {
1503 (*result)->type = psiconv_var_int;
1504 psiconv_progress(lev+2,off+len,"Going to read a signed integer");
1505 (*result)->data.dat_int = psiconv_read_sint(buf,lev+2,off+len,&leng,&res);
1506 if (res)
1507 goto ERROR3;
1508 psiconv_debug(lev+2,off+len,"Value: %d",(*result)->data.dat_int);
1509 len += leng;
1510 } else if (marker == 0x01) {
1511 (*result)->type = psiconv_var_float;
1512 psiconv_progress(lev+2,off+len,"Going to read a floating point number");
1513 (*result)->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng,
1514 &res);
1515 if (res)
1516 goto ERROR3;
1517 psiconv_debug(lev+2,off+len,"Value: %f",(*result)->data.dat_float);
1518 len += leng;
1519 } else if (marker == 0x02) {
1520 (*result)->type = psiconv_var_string;
1521 psiconv_progress(lev+2,off+len,"Going to read a string");
1522 (*result)->data.dat_string = psiconv_read_string(buf,lev+2,off+len,&leng,
1523 &res);
1524 if (res)
1525 goto ERROR3;
1526 len += leng;
1527 } else if (marker == 0x03) {
1528 (*result)->type = psiconv_var_cellref;
1529 psiconv_progress(lev+2,off+len,"Going to read a cell reference");
1530 (*result)->data.dat_cellref = psiconv_read_var_cellref(buf,lev+2,off+len,
1531 &leng, &res);
1532 if (res)
1533 goto ERROR3;
1534 len += leng;
1535 } else if (marker == 0x04) {
1536 (*result)->type = psiconv_var_cellblock;
1537 psiconv_progress(lev+2,off+len,"Going to read a cell block reference");
1538 (*result)->data.dat_cellblock = psiconv_read_var_cellblock(buf,lev+2,
1539 off+len,
1540 &leng, &res);
1541 if (res)
1542 goto ERROR3;
1543 len += leng;
1544 } else {
1545 psiconv_warn(lev+2,off+len,"Sheet variable unknown type marker");
1546 res = -PSICONV_E_PARSE;
1547 goto ERROR3;
1548 }
1549
1550 psiconv_progress(lev+2,off+len,"Going to read the variable number");
1551 (*result)->number = psiconv_read_u32(buf,lev+2,off+len,&res);
1552 if (res)
1553 goto ERROR4;
1554 psiconv_debug(lev+2,off+len,"Number: %08x",(*result)->number);
1555 len += 4;
1556
1557 if (length)
1558 *length = len;
1559
1560 psiconv_progress(lev,off+len-1,
1561 "End of sheet variable (total length: %08x)", len);
1562 return 0;
1563
1564ERROR4:
1565 if ((*result)->type == psiconv_var_string)
1566 free((*result)->data.dat_string);
1567ERROR3:
1568 free((*result)->name);
1569ERROR2:
1570 free (*result);
1571ERROR1:
1572 psiconv_warn(lev+1,off,"Reading of Sheet Variable failed");
1573 if (length)
1574 *length = 0;
1575 if (!res)
1576 return -PSICONV_E_NOMEM;
1577 else
1578 return res;
1579}
1580
1581
1582int psiconv_parse_sheet_variable_list(const psiconv_buffer buf, int lev,
1583 psiconv_u32 off, int *length,
1584 psiconv_sheet_variable_list *result)
1585{
1586 int res=0;
1587 int len=0;
1588 psiconv_u32 temp;
1589 psiconv_sheet_variable variable;
1590 psiconv_u32 listlen,i;
1591 int leng;
1592
1593 psiconv_progress(lev+1,off,"Going to read the sheet variable list");
1594 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_variable_s))))
1595 goto ERROR1;
1596
1597 psiconv_progress(lev+2,off+len,
1598 "Going to read the initial byte (%02x expected)",0x02);
1599 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1600 if (res)
1601 goto ERROR2;
1602 if (temp != 0x02) {
1603 psiconv_warn(lev+2,off+len,
1604 "Sheet variable list initial byte unknown value (ignored)");
1605 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1606 }
1607 len ++;
1608
1609 psiconv_progress(lev+2,off+len,
1610 "Going to read the number of variables");
1611 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1612 if (res)
1613 goto ERROR2;
1614 psiconv_debug(lev+2,off+len,"Number of variables: %d",listlen);
1615 len += leng;
1616
1617 psiconv_progress(lev+2,off+len,"Going to read all variables");
1618 for (i = 0; i < listlen; i++) {
1619 psiconv_progress(lev+3,off+len,"Going to read variable %d",i);
1620 if ((res = psiconv_parse_sheet_variable(buf,lev+3,off+len,&leng,&variable)))
1621 goto ERROR2;
1622 if ((res = psiconv_list_add(*result,variable)))
1623 goto ERROR3;
1624 len += leng;
1625 }
1626
1627 if (length)
1628 *length = len;
1629
1630 psiconv_progress(lev,off+len-1,
1631 "End of sheet variabels list (total length: %08x)", len);
1632 return 0;
1633
1634ERROR3:
1635 psiconv_free_sheet_variable(variable);
1636ERROR2:
1637 psiconv_list_free(*result);
1638ERROR1:
1639 psiconv_warn(lev+1,off,"Reading of Sheet Variable list failed");
1640 if (length)
1641 *length = 0;
1642 if (!res)
1643 return -PSICONV_E_NOMEM;
1644 else
1645 return res;
1646}

Legend:
Removed from v.122  
changed lines
  Added in v.133

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26