/[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 111
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 len += leng;
670 }
671
672 if (length)
673 *length = len;
674
675 psiconv_progress(lev,off+len-1,
676 "End of sheet cell list (total length: %08x)", len);
677 return 0;
678
679ERROR3:
680 psiconv_free_sheet_cell(cell);
681ERROR2:
682 psiconv_free_sheet_cell_list(*result);
683ERROR1:
684 psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed");
685 if (length)
686 *length = 0;
687 if (!res)
688 return -PSICONV_E_NOMEM;
689 else
690 return res;
691}
692
693
694int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
695 psiconv_u32 off, int *length,
696 psiconv_sheet_worksheet_list *result)
697{
698 psiconv_sheet_worksheet worksheet;
699 int res=0;
700 int len=0;
701 psiconv_u8 temp;
702 psiconv_u32 offset;
703 int leng,i,nr;
704
705 psiconv_progress(lev+1,off,"Going to read the worksheet list");
706 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
707 goto ERROR1;
708
709 psiconv_progress(lev+2,off+len,
710 "Going to read the initial bytes (%02x expected)",0x02);
711 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
712 if (res)
713 goto ERROR2;
714 if (temp != 0x02) {
715 psiconv_warn(lev+2,off+len,
716 "Sheet worksheet list initial byte unknown value (ignored)");
717 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
718 }
719 len ++;
720
721 psiconv_progress(lev+2,off+len,"Going to read the list length");
722 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
723 if (res)
724 goto ERROR2;
725 psiconv_debug(lev+2,off+len,"Length: %02x",nr);
726 len += leng;
727
728 psiconv_progress(lev+2,off+len,"Going to read the list");
729 for (i=0 ; i < nr; i++) {
730 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
731 psiconv_progress(lev+4,off+len,
732 "Going to read the initial byte (%02x expected)",0x00);
733 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
734 if (res)
735 goto ERROR2;
736 if (temp != 0x00) {
737 psiconv_warn(lev+4,off+len,
738 "Sheet worksheet element initial byte unknown value (ignored)");
739 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp);
740 }
741 len ++;
742
743 psiconv_progress(lev+4,off+len,"Going to read the worksheet offset");
744 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
745 if (res)
746 goto ERROR2;
747 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
748 len += 4;
749
750 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
751 &worksheet)))
752 goto ERROR2;
753 if ((res = psiconv_list_add(*result,worksheet)))
754 goto ERROR3;
755 psiconv_free_sheet_worksheet(worksheet);
756 }
757
758 if (length)
759 *length = len;
760
761 psiconv_progress(lev,off+len-1,
762 "End of worksheet list (total length: %08x)", len);
763
764 return 0;
765
766ERROR3:
767 psiconv_free_sheet_worksheet(worksheet);
768ERROR2:
769 psiconv_free_sheet_worksheet_list(*result);
770ERROR1:
771 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
772 if (length)
773 *length = 0;
774 if (!res)
775 return -PSICONV_E_NOMEM;
776 else
777 return res;
778}
779
780int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
781 psiconv_u32 off, int *length,
782 psiconv_sheet_cell_layout *result)
783
784{
785 int res=0;
786 int len=0;
787 int leng;
788 psiconv_u8 temp;
789
790 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
791 if (!(*result = malloc(sizeof(**result))))
792 goto ERROR1;
793
794 (*result)->character = NULL;
795 (*result)->paragraph = NULL;
796 (*result)->numberformat = NULL;
797
798 psiconv_progress(lev+2,off+len,
799 "Going to read the first byte (%02x expected)",0x02);
800 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
801 if (res)
802 goto ERROR2;
803 if (temp != 0x02) {
804 psiconv_warn(lev+2,off+len,
805 "Worksheet section initial byte unknown value (ignored)");
806 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
807 }
808 len ++;
809
810 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
811 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
812 if (res)
813 goto ERROR2;
814 len ++;
815
816 if (temp & 0x01) {
817 if (!((*result)->paragraph = psiconv_basic_paragraph_layout()))
818 goto ERROR2;
819 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
820 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
821 (*result)->paragraph)))
822 goto ERROR2;
823 len += leng;
824 }
825
826 if (temp & 0x02) {
827 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
828 if (!((*result)->character = psiconv_basic_character_layout()))
829 goto ERROR2;
830 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
831 (*result)->character)))
832 goto ERROR2;
833 len += leng;
834 }
835
836 if (temp & 0x04) {
837 psiconv_progress(lev+3,off+len, "Going to read the default number format");
838 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
839 &(*result)->numberformat);
840 len += leng;
841 }
842
843 if (length)
844 *length = len;
845
846 psiconv_progress(lev,off+len-1,
847 "End of sheet cell layout (total length: %08x)", len);
848
849 return 0;
850
851ERROR2:
852 psiconv_free_sheet_cell_layout(*result);
853ERROR1:
854 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
855 if (length)
856 *length = 0;
857 if (!res)
858 return -PSICONV_E_NOMEM;
859 else
860 return res;
861}
862
863
864int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
865 psiconv_u32 off, int *length,
866 psiconv_sheet_worksheet *result)
867{
868 int res=0;
869 psiconv_u32 temp,cells_off,grid_off;
870 int len=0;
871 int leng;
872
873 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
874 if (!(*result = malloc(sizeof(**result))))
875 goto ERROR1;
876
877 psiconv_progress(lev+2,off+len,
878 "Going to read the initial bytes (%02x expected)",0x04);
879 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
880 if (res)
881 goto ERROR2;
882 if (temp != 0x04) {
883 psiconv_warn(lev+2,off+len,
884 "Worksheet section initial byte unknown value (ignored)");
885 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
886 }
887 len ++;
888
889 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
890 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
891 if (res)
892 goto ERROR2;
893 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp);
894 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
895 if (temp & 0xfe) {
896 psiconv_warn(lev+2,off+len,
897 "Worksheet section flags byte unknown bits (ignored)");
898 }
899 len ++;
900
901 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
902 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
903 &(*result)->default_layout)))
904 goto ERROR2;
905 len += leng;
906
907 psiconv_progress(lev+2,off+len,
908 "Going to read the offset of the 1st ??? Section");
909 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
910 if (res)
911 goto ERROR3;
912 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
913 len += 4;
914
915 psiconv_progress(lev+2,off+len,
916 "Going to read the offset of the 2nd ??? Section");
917 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
918 if (res)
919 goto ERROR3;
920 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
921 len += 4;
922
923 psiconv_progress(lev+2,off+len,
924 "Going to read the offset of the Cells List");
925 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
926 if (res)
927 goto ERROR3;
928 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
929 len += 4;
930
931 psiconv_progress(lev+2,off+len,
932 "Going to read the offset of the Grid Section");
933 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
934 if (res)
935 goto ERROR3;
936 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
937 len += 4;
938
939 psiconv_progress(lev+2,off+len,
940 "Going to read the offset of the 3rd ??? Section");
941 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
942 if (res)
943 goto ERROR3;
944 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
945 len += 4;
946
947 psiconv_progress(lev+2,off+len,"Going to read the cells list");
948 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
949 &(*result)->cells)))
950 goto ERROR3;
951
952
953/* TODO: parse grid section */
954
955 if (length)
956 *length = len;
957
958 psiconv_progress(lev,off+len-1,
959 "End of sheet worksheet section (total length: %08x)", len);
960 return 0;
961
962ERROR3:
963 psiconv_free_sheet_cell_layout((*result)->default_layout);
964ERROR2:
965 free (*result);
966ERROR1:
967 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed");
968 if (length)
969 *length = 0;
970 if (!res)
971 return -PSICONV_E_NOMEM;
972 else
973 return res;
974}
975
976

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

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