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

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

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