/[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 110 Revision 122
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_dmm;
124 else if (temp == 0x12)
125 result->code = psiconv_numberformat_date_mmd;
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_dmmm;
134 else if (temp == 0x1C)
135 result->code = psiconv_numberformat_date_dmmmyy;
136 else if (temp == 0x1E)
137 result->code = psiconv_numberformat_date_ddmmmyy;
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_monthnamedyyyy;
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) >> 1;
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{
221 goto ERROR2; 391 goto ERROR2;
222 psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off); 392 psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off);
223 len += 4; 393 len += 4;
224 394
225 psiconv_progress(lev+2,off+len, 395 psiconv_progress(lev+2,off+len,
226 "Going to read the offset of the Worksheets Section"); 396 "Going to read the offset of the Worksheet List");
227 worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res); 397 worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res);
228 if (res) 398 if (res)
229 goto ERROR2; 399 goto ERROR2;
230 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 400 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off);
231 len += 4; 401 len += 4;
232 402
233 psiconv_progress(lev+2,off+len, 403 psiconv_progress(lev+2,off+len,
234 "Going to read the offset of the 4th ??? Section"); 404 "Going to read the offset of the 4th ??? Section");
235 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 405 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
241 psiconv_progress(lev+2,off+len,"Going to read the formulas list"); 411 psiconv_progress(lev+2,off+len,"Going to read the formulas list");
242 if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL, 412 if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL,
243 &(*result)->formulas))) 413 &(*result)->formulas)))
244 goto ERROR2; 414 goto ERROR2;
245 415
246 psiconv_progress(lev+2,off+len,"Going to read the worksheets"); 416 psiconv_progress(lev+2,off+len,"Going to read the worksheet list");
247 if ((res = psiconv_parse_sheet_worksheet_section(buf,lev+2,worksheets_off, 417 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off,
248 NULL,&(*result)->worksheet))) 418 NULL,&(*result)->worksheets)))
249 goto ERROR2; 419 goto ERROR2;
250 420
251 421
252 if (length) 422 if (length)
253 *length = len; 423 *length = len;
333 else 503 else
334 return res; 504 return res;
335} 505}
336 506
337int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, 507int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
338 psiconv_u32 off, int *length, 508 psiconv_u32 off, int *length,
339 psiconv_sheet_cell *result) 509 psiconv_sheet_cell *result,
510 const psiconv_sheet_cell_layout default_layout)
340{ 511{
341 int res=0; 512 int res=0;
342 int len=0; 513 int len=0;
343 psiconv_u32 temp; 514 psiconv_u32 temp;
344 psiconv_bool_t has_layout; 515 psiconv_bool_t has_layout;
346 517
347 psiconv_progress(lev+1,off,"Going to read a sheet cell structure"); 518 psiconv_progress(lev+1,off,"Going to read a sheet cell structure");
348 if (!(*result = malloc(sizeof(**result)))) 519 if (!(*result = malloc(sizeof(**result))))
349 goto ERROR1; 520 goto ERROR1;
350 521
522 (*result)->layout = NULL;
523 (*result)->type = psiconv_cell_blank;
524
351 psiconv_progress(lev+2,off+len,"Going to read the cell position"); 525 psiconv_progress(lev+2,off+len,"Going to read the cell position");
352 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 526 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
353 if (res) 527 if (res)
354 goto ERROR2; 528 goto ERROR2;
355 len ++; 529 len ++;
363 len ++; 537 len ++;
364 (*result)->column = (temp >> 2) & 0xFF; 538 (*result)->column = (temp >> 2) & 0xFF;
365 (*result)->row = (temp >> 10) & 0x3FFF; 539 (*result)->row = (temp >> 10) & 0x3FFF;
366 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x", 540 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x",
367 (*result)->column,(*result)->row); 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 }
368 546
369 psiconv_progress(lev+2,off+len,"Going to read the cell type"); 547 psiconv_progress(lev+2,off+len,"Going to read the cell type");
370 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 548 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
371 if (res) 549 if (res)
372 goto ERROR2; 550 goto ERROR2;
386 len += 4; 564 len += 4;
387 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int); 565 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
388 566
389 } else if ((*result)->type == psiconv_cell_bool) { 567 } else if ((*result)->type == psiconv_cell_bool) {
390 psiconv_progress(lev+2,off+len,"Going to read a boolean"); 568 psiconv_progress(lev+2,off+len,"Going to read a boolean");
391 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 569 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
392 if (res) 570 &(*result)->data.dat_bool)))
393 goto ERROR2; 571 goto ERROR2;
394 len ++;
395 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp); 572 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp);
396 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false; 573 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
397 574 len += leng;
398 } else if ((*result)->type == psiconv_cell_error) { 575 } else if ((*result)->type == psiconv_cell_error) {
399 psiconv_progress(lev+2,off+len,"Going to read the error code"); 576 psiconv_progress(lev+2,off+len,"Going to read the error code");
400 (*result)->data.dat_error = psiconv_read_u16(buf,lev+2,off+len,&res); 577 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
401 if (res) 578 if (res)
402 goto ERROR2; 579 goto ERROR2;
403 len += 2; 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 }
404 psiconv_debug(lev+2,off+len,"Cell contents: %04x", 601 psiconv_debug(lev+2,off+len,"Cell contents: %04x",
405 (*result)->data.dat_error); 602 (*result)->data.dat_error);
406 603 len += 2;
407 } else if ((*result)->type == psiconv_cell_float) { 604 } else if ((*result)->type == psiconv_cell_float) {
408 psiconv_progress(lev+2,off+len,"Going to read a float"); 605 psiconv_progress(lev+2,off+len,"Going to read a float");
409 (*result)->data.dat_float = 606 (*result)->data.dat_float =
410 psiconv_read_float(buf,lev+2,off+len,&leng,&res); 607 psiconv_read_float(buf,lev+2,off+len,&leng,&res);
411 if (res) 608 if (res)
412 goto ERROR2; 609 goto ERROR2;
413 len += leng;
414 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float); 610 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
415 611 len += leng;
416 } else if ((*result)->type == psiconv_cell_string) { 612 } else if ((*result)->type == psiconv_cell_string) {
417 psiconv_progress(lev+2,off+len,"Going to read a string"); 613 psiconv_progress(lev+2,off+len,"Going to read a string");
418 (*result)->data.dat_string = 614 (*result)->data.dat_string =
419 psiconv_read_short_string(buf,lev+2,off+len,&leng,&res); 615 psiconv_read_string(buf,lev+2,off+len,&leng,&res);
420 if (res) 616 if (res)
421 goto ERROR2; 617 goto ERROR2;
422 len += leng;
423 psiconv_debug(lev+2,off+len,"Cell contents: `%s'", 618 psiconv_debug(lev+2,off+len,"Cell contents: `%s'",
424 (*result)->data.dat_string); 619 (*result)->data.dat_string);
620 len += leng;
425 } else { 621 } else {
426 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); 622 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
427 } 623 res = PSICONV_E_PARSE;
624 goto ERROR2;
428 625 }
626
627 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
628 goto ERROR2;
429 if (has_layout) { 629 if (has_layout) {
430 psiconv_progress(lev+2,off+len,"Going to read the cell layout"); 630 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
431 631 &leng,(*result)->layout)))
432 psiconv_progress(lev+2,off+len,"Going to read the cell layout flags");
433 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
434 if (res)
435 goto ERROR2; 632 goto ERROR2;
436 len ++;
437
438 if (temp & 0x01) {
439 if (!((*result)->paragraph = psiconv_basic_paragraph_layout()))
440 goto ERROR2;
441 psiconv_progress(lev+3,off+len,"Going to read the paragraph codes");
442 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
443 (*result)->paragraph)))
444 goto ERROR2;
445 len += leng; 633 len += leng;
446 }
447
448 if (temp & 0x02) {
449 psiconv_progress(lev+3,off+len,"Going to read the character codes");
450 if (!((*result)->character = psiconv_basic_character_layout()))
451 goto ERROR2;
452 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
453 (*result)->character)))
454 goto ERROR2;
455 len += leng;
456 }
457
458 if (temp & 0x04) {
459/* TODO: default number format */
460 psiconv_read_u8(buf,lev+2,off+len,&res);
461 if (res)
462 goto ERROR2;
463 len ++;
464 psiconv_read_u8(buf,lev+2,off+len,&res);
465 if (res)
466 goto ERROR2;
467 len ++;
468 psiconv_read_u8(buf,lev+2,off+len,&res);
469 if (res)
470 goto ERROR2;
471 len ++;
472 }
473 } 634 }
474 635
475 if ((*result)->calculated) { 636 if ((*result)->calculated) {
476 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference"); 637 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference");
477 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res); 638 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
478 if (res) 639 if (res)
479 goto ERROR2; 640 goto ERROR2;
480 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp); 641 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp);
481 len += leng; 642 len += leng;
482 (*result)->ref_formula = temp; 643 (*result)->ref_formula = temp;
483 } 644 }
484 645
485 if (length) 646 if (length)
486 *length = len; 647 *length = len;
487 648
500 else 661 else
501 return res; 662 return res;
502} 663}
503 664
504int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, 665int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
505 psiconv_u32 off, int *length, 666 psiconv_u32 off, int *length,
506 psiconv_sheet_cell_list *result) 667 psiconv_sheet_cell_list *result,
668 const psiconv_sheet_cell_layout default_layout)
507{ 669{
508 int res=0; 670 int res=0;
509 int len=0; 671 int len=0;
510 psiconv_u32 temp; 672 psiconv_u32 temp;
511 psiconv_sheet_cell cell; 673 psiconv_sheet_cell cell;
512 psiconv_u32 listlen,i; 674 psiconv_u32 listlen,i;
513 int leng; 675 int leng;
514 676
515 psiconv_progress(lev+1,off,"Going to read the sheet cells list"); 677 psiconv_progress(lev+1,off,"Going to read the sheet cell list");
516 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s)))) 678 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
517 goto ERROR1; 679 goto ERROR1;
518 680
519 psiconv_progress(lev+2,off+len, 681 psiconv_progress(lev+2,off+len,
520 "Going to read the initial byte (%02x expected)",0x02); 682 "Going to read the initial byte (%02x expected)",0x02);
549 len += leng; 711 len += leng;
550 712
551 psiconv_progress(lev+2,off+len,"Going to read all cells"); 713 psiconv_progress(lev+2,off+len,"Going to read all cells");
552 for (i = 0; i < listlen; i++) { 714 for (i = 0; i < listlen; i++) {
553 psiconv_progress(lev+3,off+len,"Going to read cell %d",i); 715 psiconv_progress(lev+3,off+len,"Going to read cell %d",i);
554 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell))) 716 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
717 default_layout)))
555 goto ERROR2; 718 goto ERROR2;
556 if ((res = psiconv_list_add(*result,cell))) 719 if ((res = psiconv_list_add(*result,cell)))
557 goto ERROR3; 720 goto ERROR3;
721 free(cell);
558 len += leng; 722 len += leng;
559 } 723 }
560 724
561 if (length) 725 if (length)
562 *length = len; 726 *length = len;
578 else 742 else
579 return res; 743 return res;
580} 744}
581 745
582 746
583int psiconv_parse_sheet_worksheet_section(const psiconv_buffer buf, int lev, 747int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
584 psiconv_u32 off, int *length, 748 psiconv_u32 off, int *length,
585 psiconv_sheet_worksheet_section *result) 749 psiconv_sheet_worksheet_list *result)
586{ 750{
751 psiconv_sheet_worksheet worksheet;
587 int res=0; 752 int res=0;
588 psiconv_u32 temp,cells_off,grid_off;
589 int len=0; 753 int len=0;
754 psiconv_u8 temp;
755 psiconv_u32 offset;
590 int leng; 756 int leng,i,nr;
591 757
592 /* TODO: this is the section that might be an XListE instead... */
593 psiconv_progress(lev+2,off+len,
594 "Going to read the initial bytes (%02x expected)",0x02);
595 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
596 if (res)
597 goto ERROR1;
598 if (temp != 0x04) {
599 psiconv_warn(lev+2,off+len,
600 "Sheet worksheet section initial byte unknown value (ignored)");
601 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
602 }
603 len ++;
604
605 psiconv_progress(lev+2,off+len,
606 "Going to read the initial bytes (%02x expected)",0x02);
607 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
608 if (res)
609 goto ERROR1;
610 if (temp != 0x04) {
611 psiconv_warn(lev+2,off+len,
612 "Sheet worksheet section initial byte unknown value (ignored)");
613 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
614 }
615 len ++;
616
617 psiconv_progress(lev+2,off+len,
618 "Going to read the initial bytes (%02x expected)",0x00);
619 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
620 if (res)
621 goto ERROR1;
622 if (temp != 0x04) {
623 psiconv_warn(lev+2,off+len,
624 "Sheet worksheet section initial byte unknown value (ignored)");
625 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
626 }
627 len ++;
628
629 psiconv_progress(lev+2,off+len,
630 "Going to read the offset of the Worksheet Section");
631 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
632 if (res)
633 goto ERROR1;
634 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
635 len += 4;
636
637 len = 0;
638 off = temp;
639
640 /* this is the real worksheet section from here */
641
642 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section"); 758 psiconv_progress(lev+1,off,"Going to read the worksheet list");
643 if (!(*result = malloc(sizeof(**result)))) 759 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
644 goto ERROR1;
645
646 psiconv_progress(lev+2,off+len,
647 "Going to read the initial bytes (%02x expected)",0x04);
648 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
649 if (res)
650 goto ERROR2; 760 goto ERROR1;
651 if (temp != 0x04) {
652 psiconv_warn(lev+2,off+len,
653 "Worksheet section initial byte unknown value (ignored)");
654 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
655 } 761
656 len ++;
657
658 psiconv_progress(lev+2,off+len,
659 "Going to read the initial bytes (%02x expected)",0x01);
660 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
661 if (res)
662 goto ERROR2;
663 if (temp != 0x01) {
664 psiconv_warn(lev+2,off+len,
665 "Worksheet section initial byte unknown value (ignored)");
666 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
667 }
668 len ++;
669
670 psiconv_progress(lev+2,off+len, 762 psiconv_progress(lev+2,off+len,
671 "Going to read the initial bytes (%02x expected)",0x02); 763 "Going to read the initial bytes (%02x expected)",0x02);
672 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 764 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
673 if (res) 765 if (res)
674 goto ERROR2; 766 goto ERROR2;
675 if (temp != 0x02) { 767 if (temp != 0x02) {
676 psiconv_warn(lev+2,off+len, 768 psiconv_warn(lev+2,off+len,
677 "Worksheet section initial byte unknown value (ignored)"); 769 "Sheet worksheet list initial byte unknown value (ignored)");
678 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 770 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
679 } 771 }
680 len ++; 772 len ++;
681 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
682 psiconv_progress(lev+2,off+len,"Going to read the default formats flag"); 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);
683 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 786 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
684 if (res) 787 if (res)
685 goto ERROR2;
686 len ++;
687
688 if (temp & 0x01) {
689 if (!((*result)->paragraph = psiconv_basic_paragraph_layout()))
690 goto ERROR2; 788 goto ERROR2;
691 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
692 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
693 (*result)->paragraph)))
694 goto ERROR2_1;
695 len += leng;
696 }
697
698 if (temp & 0x02) { 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
699 psiconv_progress(lev+3,off+len,"Going to read the default character codes"); 796 psiconv_progress(lev+4,off+len,"Going to read the worksheet offset");
700 if (!((*result)->character = psiconv_basic_character_layout()))
701 goto ERROR2_1;
702 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
703 (*result)->character)))
704 goto ERROR2_2;
705 len += leng;
706 }
707
708 if (temp & 0x04) {
709/* TODO: default number format */
710 psiconv_read_u8(buf,lev+2,off+len,&res); 797 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
711 if (res) 798 if (res)
712 goto ERROR2_3; 799 goto ERROR2;
800 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
713 len ++; 801 len += 4;
714 psiconv_read_u8(buf,lev+2,off+len,&res); 802
715 if (res) 803 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
804 &worksheet)))
716 goto ERROR2_3; 805 goto ERROR2;
717 len ++; 806 if ((res = psiconv_list_add(*result,worksheet)))
718 psiconv_read_u8(buf,lev+2,off+len,&res);
719 if (res)
720 goto ERROR2_3; 807 goto ERROR3;
721 len ++; 808 free(worksheet);
722 }
723 809 }
724 psiconv_progress(lev+2,off+len,
725 "Going to read the offset of the 1st ??? Section");
726 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
727 if (res)
728 goto ERROR2;
729 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
730 len += 4;
731 810
732 psiconv_progress(lev+2,off+len,
733 "Going to read the offset of the 2nd ??? Section");
734 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
735 if (res)
736 goto ERROR2;
737 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
738 len += 4;
739
740 psiconv_progress(lev+2,off+len,
741 "Going to read the offset of the Cells List");
742 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
743 if (res)
744 goto ERROR2;
745 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
746 len += 4;
747
748 psiconv_progress(lev+2,off+len,
749 "Going to read the offset of the Grid Section");
750 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
751 if (res)
752 goto ERROR2;
753 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
754 len += 4;
755
756 psiconv_progress(lev+2,off+len,
757 "Going to read the offset of the 3rd ??? Section");
758 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
759 if (res)
760 goto ERROR2;
761 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
762 len += 4;
763
764 psiconv_progress(lev+2,off+len,"Going to read the cells list");
765 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
766 &(*result)->cells)))
767 goto ERROR2;
768
769
770/* TODO: parse grid section */
771
772 if (length) 811 if (length)
773 *length = len; 812 *length = len;
774 813
775 psiconv_progress(lev,off+len-1, 814 psiconv_progress(lev,off+len-1,
776 "End of sheet worksheet section (total length: %08x)", len); 815 "End of worksheet list (total length: %08x)", len);
816
777 return 0; 817 return 0;
778 818
779ERROR2_3: 819ERROR3:
780 psiconv_free_numberformat((*result)->numberformat); 820 psiconv_free_sheet_worksheet(worksheet);
781ERROR2_2:
782 psiconv_free_character_layout((*result)->character);
783ERROR2_1:
784 psiconv_free_paragraph_layout((*result)->paragraph);
785goto ERROR2;
786
787ERROR2: 821ERROR2:
788 free (*result); 822 psiconv_free_sheet_worksheet_list(*result);
789ERROR1: 823ERROR1:
790 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed"); 824 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
791 if (length) 825 if (length)
792 *length = 0; 826 *length = 0;
793 if (!res) 827 if (!res)
794 return -PSICONV_E_NOMEM; 828 return -PSICONV_E_NOMEM;
795 else 829 else
796 return res; 830 return res;
797} 831}
798 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)
799 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.110  
changed lines
  Added in v.122

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