/[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 95 Revision 121
22 22
23#include <stdlib.h> 23#include <stdlib.h>
24 24
25#include "parse_routines.h" 25#include "parse_routines.h"
26#include "error.h" 26#include "error.h"
27
28static psiconv_sheet_cell_layout psiconv_basic_cell_layout(void)
29{
30 psiconv_sheet_cell_layout result;
31 if (!(result = malloc(sizeof(*result))))
32 goto ERROR1;
33 if (!(result->character = psiconv_basic_character_layout()))
34 goto ERROR2;
35 if (!(result->paragraph = psiconv_basic_paragraph_layout()))
36 goto ERROR3;
37 if (!(result->numberformat = malloc(sizeof(*result->numberformat))))
38 goto ERROR4;
39 result->numberformat->code = psiconv_numberformat_general;
40 result->numberformat->decimal = 2;
41 return result;
42ERROR4:
43 psiconv_free_paragraph_layout(result->paragraph);
44ERROR3:
45 psiconv_free_character_layout(result->character);
46ERROR2:
47 free(result);
48ERROR1:
49 return NULL;
50}
51
52static psiconv_sheet_cell_layout psiconv_clone_cell_layout
53 (psiconv_sheet_cell_layout original)
54{
55 psiconv_sheet_cell_layout result;
56 if (!(result = malloc(sizeof(*result))))
57 goto ERROR1;
58 if (!(result->character =
59 psiconv_clone_character_layout(original->character)))
60 goto ERROR2;
61 if (!(result->paragraph =
62 psiconv_clone_paragraph_layout(original->paragraph)))
63 goto ERROR3;
64 if (!(result->numberformat = malloc(sizeof(*result->numberformat))))
65 goto ERROR4;
66 result->numberformat->code = original->numberformat->code;
67 result->numberformat->decimal = original->numberformat->decimal;
68 return result;
69ERROR4:
70 psiconv_free_paragraph_layout(result->paragraph);
71ERROR3:
72 psiconv_free_character_layout(result->character);
73ERROR2:
74 free(result);
75ERROR1:
76 return NULL;
77}
78
79int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev,
80 psiconv_u32 off, int *length,
81 psiconv_sheet_numberformat result)
82{
83 int res=0;
84 int len=0;
85 psiconv_u8 temp;
86
87 psiconv_progress(lev+1,off,"Going to read a sheet numberformat");
88
89 psiconv_progress(lev+2,off+len,
90 "Going to read the initial byte (%02x expected)",0x02);
91 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
92 if (res)
93 goto ERROR1;
94 if (temp != 0x02) {
95 psiconv_warn(lev+2,off+len,
96 "Sheet numberformat initial byte unknown value (ignored)");
97 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
98 }
99 len ++;
100
101 psiconv_progress(lev+2,off+len, "Going to read the code byte");
102 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
103 if (res)
104 goto ERROR1;
105 psiconv_debug(lev+2,off+len,"Code: %02x",temp);
106 if (temp == 0x00)
107 result->code = psiconv_numberformat_general;
108 else if (temp == 0x02)
109 result->code = psiconv_numberformat_fixeddecimal;
110 else if (temp == 0x04)
111 result->code = psiconv_numberformat_scientific;
112 else if (temp == 0x06)
113 result->code = psiconv_numberformat_currency;
114 else if (temp == 0x08)
115 result->code = psiconv_numberformat_percent;
116 else if (temp == 0x0A)
117 result->code = psiconv_numberformat_triads;
118 else if (temp == 0x0C)
119 result->code = psiconv_numberformat_boolean;
120 else if (temp == 0x0E)
121 result->code = psiconv_numberformat_text;
122 else if (temp == 0x10)
123 result->code = psiconv_numberformat_date_ddmm;
124 else if (temp == 0x12)
125 result->code = psiconv_numberformat_date_mmdd;
126 else if (temp == 0x14)
127 result->code = psiconv_numberformat_date_ddmmyy;
128 else if (temp == 0x16)
129 result->code = psiconv_numberformat_date_mmddyy;
130 else if (temp == 0x18)
131 result->code = psiconv_numberformat_date_yymmdd;
132 else if (temp == 0x1A)
133 result->code = psiconv_numberformat_date_ddmmm;
134 else if (temp == 0x1C)
135 result->code = psiconv_numberformat_date_ddmmmyy;
136 else if (temp == 0x1E)
137 result->code = psiconv_numberformat_date_ddmmmyyyy;
138 else if (temp == 0x20)
139 result->code = psiconv_numberformat_date_mmm;
140 else if (temp == 0x22)
141 result->code = psiconv_numberformat_date_monthname;
142 else if (temp == 0x24)
143 result->code = psiconv_numberformat_date_mmmyy;
144 else if (temp == 0x26)
145 result->code = psiconv_numberformat_date_monthnameyy;
146 else if (temp == 0x28)
147 result->code = psiconv_numberformat_date_monthnameddyyyy;
148 else if (temp == 0x2A)
149 result->code = psiconv_numberformat_datetime_ddmmyyyyhhii;
150 else if (temp == 0x2C)
151 result->code = psiconv_numberformat_datetime_ddmmyyyyHHii;
152 else if (temp == 0x2E)
153 result->code = psiconv_numberformat_datetime_mmddyyyyhhii;
154 else if (temp == 0x30)
155 result->code = psiconv_numberformat_datetime_mmddyyyyHHii;
156 else if (temp == 0x32)
157 result->code = psiconv_numberformat_datetime_yyyymmddhhii;
158 else if (temp == 0x34)
159 result->code = psiconv_numberformat_datetime_yyyymmddHHii;
160 else if (temp == 0x36)
161 result->code = psiconv_numberformat_time_hhii;
162 else if (temp == 0x38)
163 result->code = psiconv_numberformat_time_hhiiss;
164 else if (temp == 0x3A)
165 result->code = psiconv_numberformat_time_HHii;
166 else if (temp == 0x3C)
167 result->code = psiconv_numberformat_time_HHiiss;
168 else {
169 psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)");
170 result->code = psiconv_numberformat_general;
171 }
172 len ++;
173
174 psiconv_progress(lev+2,off+len, "Going to read the number of decimals");
175 result->decimal = psiconv_read_u8(buf,lev+2,off+len,&res);
176 if (res)
177 goto ERROR1;
178 psiconv_debug(lev+2,off+len,"Decimals: %d",result->decimal);
179 len ++;
180
181 if (length)
182 *length = len;
183
184 psiconv_progress(lev,off+len-1,
185 "End of sheet number format (total length: %08x)", len);
186 return 0;
187
188ERROR1:
189 psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed");
190 if (length)
191 *length = 0;
192 if (!res)
193 return -PSICONV_E_NOMEM;
194 else
195 return res;
196}
27 197
28int psiconv_parse_sheet_status_section(const psiconv_buffer buf, int lev, 198int psiconv_parse_sheet_status_section(const psiconv_buffer buf, int lev,
29 psiconv_u32 off, int *length, 199 psiconv_u32 off, int *length,
30 psiconv_sheet_status_section *result) 200 psiconv_sheet_status_section *result)
31{ 201{
171 return 0; 341 return 0;
172 342
173ERROR2: 343ERROR2:
174 free (*result); 344 free (*result);
175ERROR1: 345ERROR1:
176 psiconv_warn(lev+1,off,"Reading of Sjeet Status Section failed"); 346 psiconv_warn(lev+1,off,"Reading of Sheet Status Section failed");
177 if (length) 347 if (length)
178 *length = 0; 348 *length = 0;
179 if (!res) 349 if (!res)
180 return -PSICONV_E_NOMEM; 350 return -PSICONV_E_NOMEM;
181 else 351 else
185int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev, 355int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev,
186 psiconv_u32 off, int *length, 356 psiconv_u32 off, int *length,
187 psiconv_sheet_workbook_section *result) 357 psiconv_sheet_workbook_section *result)
188{ 358{
189 int res=0; 359 int res=0;
190 psiconv_u32 temp; 360 psiconv_u32 temp,formulas_off,worksheets_off;
191 int len=0; 361 int len=0;
192 362
193 psiconv_progress(lev+1,off,"Going to read the sheet workbook section"); 363 psiconv_progress(lev+1,off,"Going to read the sheet workbook section");
194 if (!(*result = malloc(sizeof(**result)))) 364 if (!(*result = malloc(sizeof(**result))))
195 goto ERROR1; 365 goto ERROR1;
213 goto ERROR2; 383 goto ERROR2;
214 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 384 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
215 len += 4; 385 len += 4;
216 386
217 psiconv_progress(lev+2,off+len, 387 psiconv_progress(lev+2,off+len,
388 "Going to read the offset of the Formulas List");
389 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res);
390 if (res)
391 goto ERROR2;
392 psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off);
393 len += 4;
394
395 psiconv_progress(lev+2,off+len,
396 "Going to read the offset of the Worksheet List");
397 worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res);
398 if (res)
399 goto ERROR2;
400 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off);
401 len += 4;
402
403 psiconv_progress(lev+2,off+len,
218 "Going to read the offset of the 2nd ??? Section"); 404 "Going to read the offset of the 4th ??? Section");
219 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 405 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
220 if (res) 406 if (res)
221 goto ERROR2; 407 goto ERROR2;
222 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 408 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
223 len += 4; 409 len += 4;
224 410
225 psiconv_progress(lev+2,off+len, 411 psiconv_progress(lev+2,off+len,"Going to read the formulas list");
226 "Going to read the offset of the 3rd ??? Section"); 412 if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL,
227 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 413 &(*result)->formulas)))
228 if (res)
229 goto ERROR2; 414 goto ERROR2;
230 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 415
231 len += 4; 416 psiconv_progress(lev+2,off+len,"Going to read the worksheet list");
232 417 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off,
233 psiconv_progress(lev+2,off+len, 418 NULL,&(*result)->worksheets)))
234 "Going to read the offset of the 4th ??? Section");
235 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
236 if (res)
237 goto ERROR2; 419 goto ERROR2;
238 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
239 len += 4;
240 420
421
241 if (length) 422 if (length)
242 *length = len; 423 *length = len;
243 424
244 psiconv_progress(lev,off+len-1, 425 psiconv_progress(lev,off+len-1,
245 "End of sheet workbook section (total length: %08x)", len); 426 "End of sheet workbook section (total length: %08x)", len);
254 if (!res) 435 if (!res)
255 return -PSICONV_E_NOMEM; 436 return -PSICONV_E_NOMEM;
256 else 437 else
257 return res; 438 return res;
258} 439}
440
441int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev,
442 psiconv_u32 off, int *length,
443 psiconv_formula_list *result)
444{
445 int res=0;
446 int len=0;
447 psiconv_u32 temp;
448 psiconv_formula formula;
449 psiconv_u32 listlen,i;
450 int leng;
451
452 psiconv_progress(lev+1,off,"Going to read the sheet formula table");
453 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s))))
454 goto ERROR1;
455
456 psiconv_progress(lev+2,off+len,
457 "Going to read the initial byte (%02x expected)",0x02);
458 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
459 if (res)
460 goto ERROR2;
461 if (temp != 0x02) {
462 psiconv_warn(lev+2,off+len,
463 "Sheet formula table initial byte unknown value (ignored)");
464 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
465 }
466 len ++;
467
468 psiconv_progress(lev+2,off+len,
469 "Going to read the number of formulas");
470 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
471 if (res)
472 goto ERROR2;
473 psiconv_debug(lev+2,off+len,"Number of formulas: %d",listlen);
474 len += leng;
475
476 psiconv_progress(lev+2,off+len,"Going to read all formulas");
477 for (i = 0; i < listlen; i++) {
478 psiconv_progress(lev+3,off+len,"Going to read formula %d",i);
479 if ((res = psiconv_parse_formula(buf,lev+3,off+len,&leng,&formula)))
480 goto ERROR2;
481 if ((res = psiconv_list_add(*result,formula)))
482 goto ERROR3;
483 len += leng;
484 }
485
486 if (length)
487 *length = len;
488
489 psiconv_progress(lev,off+len-1,
490 "End of sheet formula table (total length: %08x)", len);
491 return 0;
492
493ERROR3:
494 psiconv_free_formula(formula);
495ERROR2:
496 psiconv_list_free(*result);
497ERROR1:
498 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed");
499 if (length)
500 *length = 0;
501 if (!res)
502 return -PSICONV_E_NOMEM;
503 else
504 return res;
505}
506
507int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
508 psiconv_u32 off, int *length,
509 psiconv_sheet_cell *result,
510 const psiconv_sheet_cell_layout default_layout)
511{
512 int res=0;
513 int len=0;
514 psiconv_u32 temp;
515 psiconv_bool_t has_layout;
516 int leng;
517
518 psiconv_progress(lev+1,off,"Going to read a sheet cell structure");
519 if (!(*result = malloc(sizeof(**result))))
520 goto ERROR1;
521
522 (*result)->layout = NULL;
523 (*result)->type = psiconv_cell_blank;
524
525 psiconv_progress(lev+2,off+len,"Going to read the cell position");
526 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
527 if (res)
528 goto ERROR2;
529 len ++;
530 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 8;
531 if (res)
532 goto ERROR2;
533 len ++;
534 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 16;
535 if (res)
536 goto ERROR2;
537 len ++;
538 (*result)->column = (temp >> 2) & 0xFF;
539 (*result)->row = (temp >> 10) & 0x3FFF;
540 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x",
541 (*result)->column,(*result)->row);
542 if (temp & 0x03) {
543 psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)");
544 psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03);
545 }
546
547 psiconv_progress(lev+2,off+len,"Going to read the cell type");
548 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
549 if (res)
550 goto ERROR2;
551 len ++;
552 (*result)->type = (temp >> 5) & 0x07;
553 (*result)->calculated = (temp & 0x08)?psiconv_bool_true:psiconv_bool_false;
554 has_layout = (temp & 0x10)?psiconv_bool_true:psiconv_bool_false;
555
556 psiconv_progress(lev+2,off+len,"Going to read the cell value");
557 if ((*result)->type == psiconv_cell_blank) {
558 psiconv_debug(lev+2,off+len,"Cell type is blank: no value given.");
559 } else if ((*result)->type == psiconv_cell_int) {
560 psiconv_progress(lev+2,off+len,"Going to read an integer");
561 (*result)->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
562 if (res)
563 goto ERROR2;
564 len += 4;
565 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
566
567 } else if ((*result)->type == psiconv_cell_bool) {
568 psiconv_progress(lev+2,off+len,"Going to read a boolean");
569 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
570 &(*result)->data.dat_bool)))
571 goto ERROR2;
572 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp);
573 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
574 len += leng;
575 } else if ((*result)->type == psiconv_cell_error) {
576 psiconv_progress(lev+2,off+len,"Going to read the error code");
577 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
578 if (res)
579 goto ERROR2;
580 if (temp == 0)
581 (*result)->data.dat_error = psiconv_sheet_error_none;
582 else if (temp == 1)
583 (*result)->data.dat_error = psiconv_sheet_error_null;
584 else if (temp == 2)
585 (*result)->data.dat_error = psiconv_sheet_error_divzero;
586 else if (temp == 3)
587 (*result)->data.dat_error = psiconv_sheet_error_value;
588 else if (temp == 4)
589 (*result)->data.dat_error = psiconv_sheet_error_reference;
590 else if (temp == 5)
591 (*result)->data.dat_error = psiconv_sheet_error_name;
592 else if (temp == 6)
593 (*result)->data.dat_error = psiconv_sheet_error_number;
594 else if (temp == 7)
595 (*result)->data.dat_error = psiconv_sheet_error_notavail;
596 else {
597 psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)");
598 psiconv_debug(lev+2,off+len,"Error code: %04x",temp);
599 (*result)->data.dat_error = psiconv_sheet_error_none;
600 }
601 psiconv_debug(lev+2,off+len,"Cell contents: %04x",
602 (*result)->data.dat_error);
603 len += 2;
604 } else if ((*result)->type == psiconv_cell_float) {
605 psiconv_progress(lev+2,off+len,"Going to read a float");
606 (*result)->data.dat_float =
607 psiconv_read_float(buf,lev+2,off+len,&leng,&res);
608 if (res)
609 goto ERROR2;
610 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
611 len += leng;
612 } else if ((*result)->type == psiconv_cell_string) {
613 psiconv_progress(lev+2,off+len,"Going to read a string");
614 (*result)->data.dat_string =
615 psiconv_read_string(buf,lev+2,off+len,&leng,&res);
616 if (res)
617 goto ERROR2;
618 psiconv_debug(lev+2,off+len,"Cell contents: `%s'",
619 (*result)->data.dat_string);
620 len += leng;
621 } else {
622 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
623 res = PSICONV_E_PARSE;
624 goto ERROR2;
625 }
626
627 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
628 goto ERROR2;
629 if (has_layout) {
630 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
631 &leng,(*result)->layout)))
632 goto ERROR2;
633 len += leng;
634 }
635
636 if ((*result)->calculated) {
637 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference");
638 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
639 if (res)
640 goto ERROR2;
641 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp);
642 len += leng;
643 (*result)->ref_formula = temp;
644 }
645
646 if (length)
647 *length = len;
648
649 psiconv_progress(lev,off+len-1,
650 "End of sheet cell structure (total length: %08x)", len);
651 return 0;
652
653ERROR2:
654 psiconv_free_sheet_cell(*result);
655ERROR1:
656 psiconv_warn(lev+1,off,"Reading of Sheet Cell Structure failed");
657 if (length)
658 *length = 0;
659 if (!res)
660 return -PSICONV_E_NOMEM;
661 else
662 return res;
663}
664
665int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
666 psiconv_u32 off, int *length,
667 psiconv_sheet_cell_list *result,
668 const psiconv_sheet_cell_layout default_layout)
669{
670 int res=0;
671 int len=0;
672 psiconv_u32 temp;
673 psiconv_sheet_cell cell;
674 psiconv_u32 listlen,i;
675 int leng;
676
677 psiconv_progress(lev+1,off,"Going to read the sheet cell list");
678 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
679 goto ERROR1;
680
681 psiconv_progress(lev+2,off+len,
682 "Going to read the initial byte (%02x expected)",0x02);
683 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
684 if (res)
685 goto ERROR2;
686 if (temp != 0x02) {
687 psiconv_warn(lev+2,off+len,
688 "Sheet cell list initial byte unknown value (ignored)");
689 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
690 }
691 len ++;
692
693 psiconv_progress(lev+2,off+len,
694 "Going to read the initial byte (%02x expected)",0x00);
695 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
696 if (res)
697 goto ERROR2;
698 if (temp != 0x00) {
699 psiconv_warn(lev+2,off+len,
700 "Sheet cell list initial byte unknown value (ignored)");
701 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
702 }
703 len ++;
704
705 psiconv_progress(lev+2,off+len,
706 "Going to read the number of defined cells");
707 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
708 if (res)
709 goto ERROR2;
710 psiconv_debug(lev+2,off+len,"Number of defined cells: %d",listlen);
711 len += leng;
712
713 psiconv_progress(lev+2,off+len,"Going to read all cells");
714 for (i = 0; i < listlen; i++) {
715 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,
717 default_layout)))
718 goto ERROR2;
719 if ((res = psiconv_list_add(*result,cell)))
720 goto ERROR3;
721 free(cell);
722 len += leng;
723 }
724
725 if (length)
726 *length = len;
727
728 psiconv_progress(lev,off+len-1,
729 "End of sheet cell list (total length: %08x)", len);
730 return 0;
731
732ERROR3:
733 psiconv_free_sheet_cell(cell);
734ERROR2:
735 psiconv_free_sheet_cell_list(*result);
736ERROR1:
737 psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed");
738 if (length)
739 *length = 0;
740 if (!res)
741 return -PSICONV_E_NOMEM;
742 else
743 return res;
744}
745
746
747int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
748 psiconv_u32 off, int *length,
749 psiconv_sheet_worksheet_list *result)
750{
751 psiconv_sheet_worksheet worksheet;
752 int res=0;
753 int len=0;
754 psiconv_u8 temp;
755 psiconv_u32 offset;
756 int leng,i,nr;
757
758 psiconv_progress(lev+1,off,"Going to read the worksheet list");
759 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
760 goto ERROR1;
761
762 psiconv_progress(lev+2,off+len,
763 "Going to read the initial bytes (%02x expected)",0x02);
764 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
765 if (res)
766 goto ERROR2;
767 if (temp != 0x02) {
768 psiconv_warn(lev+2,off+len,
769 "Sheet worksheet list initial byte unknown value (ignored)");
770 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
771 }
772 len ++;
773
774 psiconv_progress(lev+2,off+len,"Going to read the list length");
775 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
776 if (res)
777 goto ERROR2;
778 psiconv_debug(lev+2,off+len,"Length: %02x",nr);
779 len += leng;
780
781 psiconv_progress(lev+2,off+len,"Going to read the list");
782 for (i=0 ; i < nr; i++) {
783 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
784 psiconv_progress(lev+4,off+len,
785 "Going to read the initial byte (%02x expected)",0x00);
786 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
787 if (res)
788 goto ERROR2;
789 if (temp != 0x00) {
790 psiconv_warn(lev+4,off+len,
791 "Sheet worksheet element initial byte unknown value (ignored)");
792 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp);
793 }
794 len ++;
795
796 psiconv_progress(lev+4,off+len,"Going to read the worksheet offset");
797 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
798 if (res)
799 goto ERROR2;
800 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
801 len += 4;
802
803 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
804 &worksheet)))
805 goto ERROR2;
806 if ((res = psiconv_list_add(*result,worksheet)))
807 goto ERROR3;
808 free(worksheet);
809 }
810
811 if (length)
812 *length = len;
813
814 psiconv_progress(lev,off+len-1,
815 "End of worksheet list (total length: %08x)", len);
816
817 return 0;
818
819ERROR3:
820 psiconv_free_sheet_worksheet(worksheet);
821ERROR2:
822 psiconv_free_sheet_worksheet_list(*result);
823ERROR1:
824 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
825 if (length)
826 *length = 0;
827 if (!res)
828 return -PSICONV_E_NOMEM;
829 else
830 return res;
831}
832
833int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
834 psiconv_u32 off, int *length,
835 psiconv_sheet_cell_layout result)
836
837{
838 int res=0;
839 int len=0;
840 int leng;
841 psiconv_u8 temp;
842
843 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
844
845 psiconv_progress(lev+2,off+len,
846 "Going to read the first byte (%02x expected)",0x02);
847 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
848 if (res)
849 goto ERROR1;
850 if (temp != 0x02) {
851 psiconv_warn(lev+2,off+len,
852 "Worksheet section initial byte unknown value (ignored)");
853 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
854 }
855 len ++;
856
857 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
858 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
859 if (res)
860 goto ERROR1;
861 len ++;
862
863 if (temp & 0x01) {
864 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
865 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
866 result->paragraph)))
867 goto ERROR1;
868 len += leng;
869 }
870
871 if (temp & 0x02) {
872 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
873 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
874 result->character)))
875 goto ERROR1;
876 len += leng;
877 }
878
879 if (temp & 0x04) {
880 psiconv_progress(lev+3,off+len, "Going to read the default number format");
881 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
882 result->numberformat);
883 len += leng;
884 }
885
886 if (length)
887 *length = len;
888
889 psiconv_progress(lev,off+len-1,
890 "End of sheet cell layout (total length: %08x)", len);
891
892 return 0;
893
894ERROR1:
895 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
896 if (length)
897 *length = 0;
898 if (!res)
899 return -PSICONV_E_NOMEM;
900 else
901 return res;
902}
903
904
905int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
906 psiconv_u32 off, int *length,
907 psiconv_sheet_worksheet *result)
908{
909 int res=0;
910 psiconv_u32 temp,cells_off,grid_off;
911 int len=0;
912 int leng;
913
914 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
915 if (!(*result = malloc(sizeof(**result))))
916 goto ERROR1;
917
918 psiconv_progress(lev+2,off+len,
919 "Going to read the initial bytes (%02x expected)",0x04);
920 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
921 if (res)
922 goto ERROR2;
923 if (temp != 0x04) {
924 psiconv_warn(lev+2,off+len,
925 "Worksheet section initial byte unknown value (ignored)");
926 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
927 }
928 len ++;
929
930 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
931 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
932 if (res)
933 goto ERROR2;
934 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp);
935 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
936 if (temp & 0xfe) {
937 psiconv_warn(lev+2,off+len,
938 "Worksheet section flags byte unknown bits (ignored)");
939 }
940 len ++;
941
942 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
943 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
944 goto ERROR2;
945 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
946 (*result)->default_layout)))
947 goto ERROR3;
948 len += leng;
949
950 psiconv_progress(lev+2,off+len,
951 "Going to read the offset of the 1st ??? Section");
952 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
953 if (res)
954 goto ERROR3;
955 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
956 len += 4;
957
958 psiconv_progress(lev+2,off+len,
959 "Going to read the offset of the 2nd ??? Section");
960 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
961 if (res)
962 goto ERROR3;
963 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
964 len += 4;
965
966 psiconv_progress(lev+2,off+len,
967 "Going to read the offset of the Cells List");
968 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
969 if (res)
970 goto ERROR3;
971 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
972 len += 4;
973
974 psiconv_progress(lev+2,off+len,
975 "Going to read the offset of the Grid Section");
976 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
977 if (res)
978 goto ERROR3;
979 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
980 len += 4;
981
982 psiconv_progress(lev+2,off+len,
983 "Going to read the offset of the 3rd ??? Section");
984 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
985 if (res)
986 goto ERROR3;
987 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
988 len += 4;
989
990 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,
992 &(*result)->cells,
993 (*result)->default_layout)))
994 goto ERROR3;
995
996
997/* TODO: parse grid section */
998
999 if (length)
1000 *length = len;
1001
1002 psiconv_progress(lev,off+len-1,
1003 "End of sheet worksheet section (total length: %08x)", len);
1004 return 0;
1005
1006ERROR3:
1007 psiconv_free_sheet_cell_layout((*result)->default_layout);
1008ERROR2:
1009 free (*result);
1010ERROR1:
1011 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed");
1012 if (length)
1013 *length = 0;
1014 if (!res)
1015 return -PSICONV_E_NOMEM;
1016 else
1017 return res;
1018}
1019
1020

Legend:
Removed from v.95  
changed lines
  Added in v.121

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