/[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 97 Revision 128
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{
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,formulas_off; 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;
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,
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,
226 "Going to read the offset of the 3rd ??? Section"); 404 "Going to read the offset of the 4th ??? Section");
227 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 405 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
228 if (res) 406 if (res)
229 goto ERROR2; 407 goto ERROR2;
230 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 408 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
231 len += 4; 409 len += 4;
232 410
233 psiconv_progress(lev+2,off+len,
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;
238 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
239 len += 4;
240
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_list(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
416 psiconv_progress(lev+2,off+len,"Going to read the worksheet list");
417 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off,
418 NULL,&(*result)->worksheets)))
419 goto ERROR2;
246 420
421
247 if (length) 422 if (length)
248 *length = len; 423 *length = len;
249 424
250 psiconv_progress(lev,off+len-1, 425 psiconv_progress(lev,off+len-1,
251 "End of sheet workbook section (total length: %08x)", len); 426 "End of sheet workbook section (total length: %08x)", len);
261 return -PSICONV_E_NOMEM; 436 return -PSICONV_E_NOMEM;
262 else 437 else
263 return res; 438 return res;
264} 439}
265 440
266int psiconv_parse_sheet_formula(const psiconv_buffer buf, int lev, 441int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev,
267 psiconv_u32 off, int *length, 442 psiconv_u32 off, int *length,
268 psiconv_sheet_formula *result) 443 psiconv_formula_list *result)
269{ 444{
270 int res=0; 445 int res=0;
271 int len=0; 446 int len=0;
447 psiconv_u32 temp;
448 psiconv_formula formula;
449 psiconv_u32 listlen,i;
272 // int leng; 450 int leng;
273 451
274 psiconv_progress(lev+1,off,"Going to read a sheet formula"); 452 psiconv_progress(lev+1,off,"Going to read the sheet formula table");
275 if (!(*result = malloc(sizeof(**result)))) 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)
276 goto ERROR1; 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 ++;
277 467
278 psiconv_progress(lev+2,off+len,"COP OUT: DOING NOTHING..."); 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 }
279 485
280 if (length) 486 if (length)
281 *length = len; 487 *length = len;
282 488
283 psiconv_progress(lev,off+len-1, 489 psiconv_progress(lev,off+len-1,
284 "End of the sheet formula (total length: %08x)", len); 490 "End of sheet formula table (total length: %08x)", len);
285 return 0; 491 return 0;
286 492
287// ERROR2:
288 free(*result);
289ERROR1: 493ERROR3:
494 psiconv_free_formula(formula);
495ERROR2:
496 psiconv_list_free(*result);
497ERROR1:
290 psiconv_warn(lev+1,off,"Reading of Sheet Formula failed"); 498 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed");
291 if (length) 499 if (length)
292 *length = 0; 500 *length = 0;
293 if (!res) 501 if (!res)
294 return -PSICONV_E_NOMEM; 502 return -PSICONV_E_NOMEM;
295 else 503 else
296 return res; 504 return res;
297} 505}
298 506
299int psiconv_parse_sheet_formula_list(const psiconv_buffer buf, int lev, 507int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
300 psiconv_u32 off, int *length, 508 psiconv_u32 off, int *length,
301 psiconv_sheet_formula_list *result) 509 psiconv_sheet_cell *result,
510 const psiconv_sheet_cell_layout default_layout,
511 const psiconv_sheet_line_list row_default_layouts,
512 const psiconv_sheet_line_list col_default_layouts)
302{ 513{
303 int res=0; 514 int res=0;
304 int len=0; 515 int len=0;
305 psiconv_u32 temp; 516 psiconv_u32 temp;
306 psiconv_sheet_formula formula; 517 psiconv_bool_t has_layout;
307 psiconv_u32 listlen,i;
308 int leng; 518 int leng;
309 519
310 psiconv_progress(lev+1,off,"Going to read the sheet formula table"); 520 psiconv_progress(lev+1,off,"Going to read a sheet cell structure");
311 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_formula_s)))) 521 if (!(*result = malloc(sizeof(**result))))
522 goto ERROR1;
523
524 (*result)->layout = NULL;
525 (*result)->type = psiconv_cell_blank;
526
527 psiconv_progress(lev+2,off+len,"Going to read the cell position");
528 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
529 if (res)
312 goto ERROR1; 530 goto ERROR2;
531 len ++;
532 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 8;
533 if (res)
534 goto ERROR2;
535 len ++;
536 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 16;
537 if (res)
538 goto ERROR2;
539 len ++;
540 (*result)->column = (temp >> 2) & 0xFF;
541 (*result)->row = (temp >> 10) & 0x3FFF;
542 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x",
543 (*result)->column,(*result)->row);
544 if (temp & 0x03) {
545 psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)");
546 psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03);
547 }
313 548
314 psiconv_progress(lev+2,off+len, 549 psiconv_progress(lev+2,off+len,"Going to read the cell type");
315 "Going to read the initial byte (%02x expected)",0x02); 550 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
551 if (res)
552 goto ERROR2;
553 len ++;
554 (*result)->type = (temp >> 5) & 0x07;
555 (*result)->calculated = (temp & 0x08)?psiconv_bool_true:psiconv_bool_false;
556 has_layout = (temp & 0x10)?psiconv_bool_true:psiconv_bool_false;
557
558 psiconv_progress(lev+2,off+len,"Going to read the cell value");
559 if ((*result)->type == psiconv_cell_blank) {
560 psiconv_debug(lev+2,off+len,"Cell type is blank: no value given.");
561 } else if ((*result)->type == psiconv_cell_int) {
562 psiconv_progress(lev+2,off+len,"Going to read an integer");
563 (*result)->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
564 if (res)
565 goto ERROR2;
566 len += 4;
567 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
568
569 } else if ((*result)->type == psiconv_cell_bool) {
570 psiconv_progress(lev+2,off+len,"Going to read a boolean");
571 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
572 &(*result)->data.dat_bool)))
573 goto ERROR2;
574 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp);
575 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
576 len += leng;
577 } else if ((*result)->type == psiconv_cell_error) {
578 psiconv_progress(lev+2,off+len,"Going to read the error code");
316 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 579 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
317 if (res) 580 if (res)
318 goto ERROR2;
319 if (temp != 0x02) {
320 psiconv_warn(lev+2,off+len,
321 "Sheet formula table initial byte unknown value (ignored)");
322 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
323 }
324 len ++;
325
326 psiconv_progress(lev+2,off+len,
327 "Going to read the number of formulas");
328 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
329 if (res)
330 goto ERROR2;
331 psiconv_debug(lev+2,off+len,"Number of formulas: %d",listlen);
332 len += leng;
333
334 psiconv_progress(lev+2,off+len,"Going to read all formulas");
335 for (i = 0; i < listlen; i++) {
336 psiconv_progress(lev+3,off+len,"Going to read formula %d",i);
337 if ((res = psiconv_parse_sheet_formula(buf,lev+3,off+len,&leng,&formula)))
338 goto ERROR2; 581 goto ERROR2;
339 if ((res = psiconv_list_add(*result,formula))) 582 if (temp == 0)
583 (*result)->data.dat_error = psiconv_sheet_error_none;
584 else if (temp == 1)
585 (*result)->data.dat_error = psiconv_sheet_error_null;
586 else if (temp == 2)
587 (*result)->data.dat_error = psiconv_sheet_error_divzero;
588 else if (temp == 3)
589 (*result)->data.dat_error = psiconv_sheet_error_value;
590 else if (temp == 4)
591 (*result)->data.dat_error = psiconv_sheet_error_reference;
592 else if (temp == 5)
593 (*result)->data.dat_error = psiconv_sheet_error_name;
594 else if (temp == 6)
595 (*result)->data.dat_error = psiconv_sheet_error_number;
596 else if (temp == 7)
597 (*result)->data.dat_error = psiconv_sheet_error_notavail;
598 else {
599 psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)");
600 psiconv_debug(lev+2,off+len,"Error code: %04x",temp);
601 (*result)->data.dat_error = psiconv_sheet_error_none;
602 }
603 psiconv_debug(lev+2,off+len,"Cell contents: %04x",
604 (*result)->data.dat_error);
605 len += 2;
606 } else if ((*result)->type == psiconv_cell_float) {
607 psiconv_progress(lev+2,off+len,"Going to read a float");
608 (*result)->data.dat_float =
609 psiconv_read_float(buf,lev+2,off+len,&leng,&res);
610 if (res)
340 goto ERROR3; 611 goto ERROR2;
612 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
341 len += leng; 613 len += leng;
614 } else if ((*result)->type == psiconv_cell_string) {
615 psiconv_progress(lev+2,off+len,"Going to read a string");
616 (*result)->data.dat_string =
617 psiconv_read_string(buf,lev+2,off+len,&leng,&res);
618 if (res)
619 goto ERROR2;
620 psiconv_debug(lev+2,off+len,"Cell contents: `%s'",
621 (*result)->data.dat_string);
622 len += leng;
623 } else {
624 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
625 res = PSICONV_E_PARSE;
626 goto ERROR2;
627 }
342 } 628
629 if (!((*result)->layout = psiconv_clone_cell_layout(
630 psiconv_get_default_layout(row_default_layouts,
631 col_default_layouts,
632 default_layout,
633 (*result)->row,
634 (*result)->column))))
635 goto ERROR2;
636 if (has_layout) {
637 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
638 &leng,(*result)->layout)))
639 goto ERROR2;
640 len += leng;
641 }
343 642
643 if ((*result)->calculated) {
644 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference");
645 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
646 if (res)
647 goto ERROR2;
648 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp);
649 len += leng;
650 (*result)->ref_formula = temp;
651 }
652
344 if (length) 653 if (length)
345 *length = len; 654 *length = len;
346 655
347 psiconv_progress(lev,off+len-1, 656 psiconv_progress(lev,off+len-1,
348 "End of sheet formula table (total length: %08x)", len); 657 "End of sheet cell structure (total length: %08x)", len);
349 return 0; 658 return 0;
350 659
351ERROR3:
352 psiconv_free_sheet_formula(formula);
353ERROR2: 660ERROR2:
354 psiconv_list_free(*result); 661 psiconv_free_sheet_cell(*result);
355ERROR1: 662ERROR1:
356 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); 663 psiconv_warn(lev+1,off,"Reading of Sheet Cell Structure failed");
357 if (length) 664 if (length)
358 *length = 0; 665 *length = 0;
359 if (!res) 666 if (!res)
360 return -PSICONV_E_NOMEM; 667 return -PSICONV_E_NOMEM;
361 else 668 else
362 return res; 669 return res;
363} 670}
671
672int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
673 psiconv_u32 off, int *length,
674 psiconv_sheet_cell_list *result,
675 const psiconv_sheet_cell_layout default_layout,
676 const psiconv_sheet_line_list row_default_layouts,
677 const psiconv_sheet_line_list col_default_layouts)
678{
679 int res=0;
680 int len=0;
681 psiconv_u32 temp;
682 psiconv_sheet_cell cell;
683 psiconv_u32 listlen,i;
684 int leng;
685
686 psiconv_progress(lev+1,off,"Going to read the sheet cell list");
687 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
688 goto ERROR1;
689
690 psiconv_progress(lev+2,off+len,
691 "Going to read the initial byte (%02x expected)",0x02);
692 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
693 if (res)
694 goto ERROR2;
695 if (temp != 0x02) {
696 psiconv_warn(lev+2,off+len,
697 "Sheet cell list initial byte unknown value (ignored)");
698 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
699 }
700 len ++;
701
702 psiconv_progress(lev+2,off+len,
703 "Going to read the initial byte (%02x expected)",0x00);
704 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
705 if (res)
706 goto ERROR2;
707 if (temp != 0x00) {
708 psiconv_warn(lev+2,off+len,
709 "Sheet cell list initial byte unknown value (ignored)");
710 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
711 }
712 len ++;
713
714 psiconv_progress(lev+2,off+len,
715 "Going to read the number of defined cells");
716 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
717 if (res)
718 goto ERROR2;
719 psiconv_debug(lev+2,off+len,"Number of defined cells: %d",listlen);
720 len += leng;
721
722 psiconv_progress(lev+2,off+len,"Going to read all cells");
723 for (i = 0; i < listlen; i++) {
724 psiconv_progress(lev+3,off+len,"Going to read cell %d",i);
725 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
726 default_layout,row_default_layouts,
727 col_default_layouts)))
728 goto ERROR2;
729 if ((res = psiconv_list_add(*result,cell)))
730 goto ERROR3;
731 free(cell);
732 len += leng;
733 }
734
735 if (length)
736 *length = len;
737
738 psiconv_progress(lev,off+len-1,
739 "End of sheet cell list (total length: %08x)", len);
740 return 0;
741
742ERROR3:
743 psiconv_free_sheet_cell(cell);
744ERROR2:
745 psiconv_free_sheet_cell_list(*result);
746ERROR1:
747 psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed");
748 if (length)
749 *length = 0;
750 if (!res)
751 return -PSICONV_E_NOMEM;
752 else
753 return res;
754}
755
756
757int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
758 psiconv_u32 off, int *length,
759 psiconv_sheet_worksheet_list *result)
760{
761 psiconv_sheet_worksheet worksheet;
762 int res=0;
763 int len=0;
764 psiconv_u8 temp;
765 psiconv_u32 offset;
766 int leng,i,nr;
767
768 psiconv_progress(lev+1,off,"Going to read the worksheet list");
769 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
770 goto ERROR1;
771
772 psiconv_progress(lev+2,off+len,
773 "Going to read the initial bytes (%02x expected)",0x02);
774 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
775 if (res)
776 goto ERROR2;
777 if (temp != 0x02) {
778 psiconv_warn(lev+2,off+len,
779 "Sheet worksheet list initial byte unknown value (ignored)");
780 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
781 }
782 len ++;
783
784 psiconv_progress(lev+2,off+len,"Going to read the list length");
785 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
786 if (res)
787 goto ERROR2;
788 psiconv_debug(lev+2,off+len,"Length: %02x",nr);
789 len += leng;
790
791 psiconv_progress(lev+2,off+len,"Going to read the list");
792 for (i=0 ; i < nr; i++) {
793 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
794 psiconv_progress(lev+4,off+len,
795 "Going to read the initial byte (%02x expected)",0x00);
796 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
797 if (res)
798 goto ERROR2;
799 if (temp != 0x00) {
800 psiconv_warn(lev+4,off+len,
801 "Sheet worksheet element initial byte unknown value (ignored)");
802 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp);
803 }
804 len ++;
805
806 psiconv_progress(lev+4,off+len,"Going to read the worksheet offset");
807 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
808 if (res)
809 goto ERROR2;
810 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
811 len += 4;
812
813 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
814 &worksheet)))
815 goto ERROR2;
816 if ((res = psiconv_list_add(*result,worksheet)))
817 goto ERROR3;
818 free(worksheet);
819 }
820
821 if (length)
822 *length = len;
823
824 psiconv_progress(lev,off+len-1,
825 "End of worksheet list (total length: %08x)", len);
826
827 return 0;
828
829ERROR3:
830 psiconv_free_sheet_worksheet(worksheet);
831ERROR2:
832 psiconv_free_sheet_worksheet_list(*result);
833ERROR1:
834 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
835 if (length)
836 *length = 0;
837 if (!res)
838 return -PSICONV_E_NOMEM;
839 else
840 return res;
841}
842
843int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
844 psiconv_u32 off, int *length,
845 psiconv_sheet_cell_layout result)
846
847{
848 int res=0;
849 int len=0;
850 int leng;
851 psiconv_u8 temp;
852
853 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
854
855 psiconv_progress(lev+2,off+len,
856 "Going to read the first byte (%02x expected)",0x02);
857 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
858 if (res)
859 goto ERROR1;
860 if (temp != 0x02) {
861 psiconv_warn(lev+2,off+len,
862 "Worksheet section initial byte unknown value (ignored)");
863 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
864 }
865 len ++;
866
867 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
868 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
869 if (res)
870 goto ERROR1;
871 len ++;
872
873 if (temp & 0x01) {
874 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
875 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
876 result->paragraph)))
877 goto ERROR1;
878 len += leng;
879 }
880
881 if (temp & 0x02) {
882 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
883 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
884 result->character)))
885 goto ERROR1;
886 len += leng;
887 }
888
889 if (temp & 0x04) {
890 psiconv_progress(lev+3,off+len, "Going to read the default number format");
891 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
892 result->numberformat);
893 len += leng;
894 }
895
896 if (length)
897 *length = len;
898
899 psiconv_progress(lev,off+len-1,
900 "End of sheet cell layout (total length: %08x)", len);
901
902 return 0;
903
904ERROR1:
905 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
906 if (length)
907 *length = 0;
908 if (!res)
909 return -PSICONV_E_NOMEM;
910 else
911 return res;
912}
913
914
915int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
916 psiconv_u32 off, int *length,
917 psiconv_sheet_worksheet *result)
918{
919 int res=0;
920 psiconv_u32 temp,cells_off,grid_off,rows_off,cols_off;
921 int len=0;
922 int leng;
923
924 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
925 if (!(*result = malloc(sizeof(**result))))
926 goto ERROR1;
927
928 psiconv_progress(lev+2,off+len,
929 "Going to read the initial bytes (%02x expected)",0x04);
930 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
931 if (res)
932 goto ERROR2;
933 if (temp != 0x04) {
934 psiconv_warn(lev+2,off+len,
935 "Worksheet section initial byte unknown value (ignored)");
936 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
937 }
938 len ++;
939
940 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
941 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
942 if (res)
943 goto ERROR2;
944 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp);
945 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
946 if (temp & 0xfe) {
947 psiconv_warn(lev+2,off+len,
948 "Worksheet section flags byte unknown bits (ignored)");
949 }
950 len ++;
951
952 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
953 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
954 goto ERROR2;
955 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
956 (*result)->default_layout)))
957 goto ERROR3;
958 len += leng;
959
960 psiconv_progress(lev+2,off+len,
961 "Going to read the offset of the row defaults Section");
962 rows_off = psiconv_read_u32(buf,lev+2,off+len,&res);
963 if (res)
964 goto ERROR3;
965 psiconv_debug(lev+2,off+len,"Offset: %04x",rows_off);
966 len += 4;
967
968 psiconv_progress(lev+2,off+len,
969 "Going to read the offset of the column defaults Section");
970 cols_off = psiconv_read_u32(buf,lev+2,off+len,&res);
971 if (res)
972 goto ERROR3;
973 psiconv_debug(lev+2,off+len,"Offset: %04x",cols_off);
974 len += 4;
975
976 psiconv_progress(lev+2,off+len,
977 "Going to read the offset of the Cells List");
978 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
979 if (res)
980 goto ERROR3;
981 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
982 len += 4;
983
984 psiconv_progress(lev+2,off+len,
985 "Going to read the offset of the Grid Section");
986 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
987 if (res)
988 goto ERROR3;
989 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
990 len += 4;
991
992 psiconv_progress(lev+2,off+len,
993 "Going to read the offset of the 3rd ??? Section");
994 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
995 if (res)
996 goto ERROR3;
997 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
998 len += 4;
999
1000 psiconv_progress(lev+2,off+len,"Going to read the row defaults");
1001 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,rows_off,NULL,
1002 &(*result)->row_default_layouts,
1003 (*result)->default_layout)))
1004 goto ERROR3;
1005
1006 psiconv_progress(lev+2,off+len,"Going to read the column defaults");
1007 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,cols_off,NULL,
1008 &(*result)->col_default_layouts,
1009 (*result)->default_layout)))
1010 goto ERROR4;
1011
1012 psiconv_progress(lev+2,off+len,"Going to read the cells list");
1013 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
1014 &(*result)->cells,
1015 (*result)->default_layout,
1016 (*result)->row_default_layouts,
1017 (*result)->col_default_layouts)))
1018 goto ERROR5;
1019
1020
1021/* TODO: parse grid section */
1022
1023 if (length)
1024 *length = len;
1025
1026 psiconv_progress(lev,off+len-1,
1027 "End of sheet worksheet section (total length: %08x)", len);
1028 return 0;
1029
1030ERROR5:
1031 psiconv_free_sheet_line_list((*result)->col_default_layouts);
1032ERROR4:
1033 psiconv_free_sheet_line_list((*result)->row_default_layouts);
1034ERROR3:
1035 psiconv_free_sheet_cell_layout((*result)->default_layout);
1036ERROR2:
1037 free (*result);
1038ERROR1:
1039 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed");
1040 if (length)
1041 *length = 0;
1042 if (!res)
1043 return -PSICONV_E_NOMEM;
1044 else
1045 return res;
1046}
1047
1048int psiconv_parse_sheet_line(const psiconv_buffer buf, int lev,
1049 psiconv_u32 off, int *length,
1050 psiconv_sheet_line *result,
1051 const psiconv_sheet_cell_layout default_layout)
1052{
1053 int res=0;
1054 int len=0;
1055 int leng;
1056
1057
1058 psiconv_progress(lev+1,off,"Going to read a sheet line");
1059 if (!(*result = malloc(sizeof(**result))))
1060 goto ERROR1;
1061
1062 psiconv_progress(lev+2,off+len,"Going to read the line number");
1063 (*result)->position = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1064 if (res)
1065 goto ERROR2;
1066 psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->position);
1067 len += leng;
1068
1069 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
1070 goto ERROR2;
1071 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
1072 &leng,(*result)->layout)))
1073 goto ERROR3;
1074 len += leng;
1075
1076 if (length)
1077 *length = len;
1078
1079 psiconv_progress(lev,off+len-1,
1080 "End of the sheet line (total length: %08x)", len);
1081 return 0;
1082
1083ERROR3:
1084 psiconv_free_sheet_cell_layout((*result)->layout);
1085ERROR2:
1086 free (*result);
1087ERROR1:
1088 psiconv_warn(lev+1,off,"Reading of the sheet line failed");
1089 if (length)
1090 *length = 0;
1091 if (!res)
1092 return -PSICONV_E_NOMEM;
1093 else
1094 return res;
1095}
1096
1097
1098int psiconv_parse_sheet_line_list(const psiconv_buffer buf, int lev,
1099 psiconv_u32 off, int *length,
1100 psiconv_sheet_line_list *result,
1101 const psiconv_sheet_cell_layout default_layout)
1102{
1103 int res=0;
1104 int len=0;
1105 psiconv_u32 temp;
1106 psiconv_sheet_line line;
1107 psiconv_u32 listlen,i;
1108 int leng;
1109
1110 psiconv_progress(lev+1,off,"Going to read the sheet line list");
1111 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_line_s))))
1112 goto ERROR1;
1113
1114 psiconv_progress(lev+2,off+len,
1115 "Going to read the initial byte (%02x expected)",0x02);
1116 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1117 if (res)
1118 goto ERROR2;
1119 if (temp != 0x02) {
1120 psiconv_warn(lev+2,off+len,
1121 "Sheet line list initial byte unknown value (ignored)");
1122 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1123 }
1124 len ++;
1125
1126 psiconv_progress(lev+2,off+len,
1127 "Going to read the number of defined lines");
1128 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1129 if (res)
1130 goto ERROR2;
1131 psiconv_debug(lev+2,off+len,"Number of defined lines: %d",listlen);
1132 len += leng;
1133
1134 psiconv_progress(lev+2,off+len,"Going to read all lines");
1135 for (i = 0; i < listlen; i++) {
1136 psiconv_progress(lev+3,off+len,"Going to read line %d",i);
1137 if ((res = psiconv_parse_sheet_line(buf,lev+3,off+len,&leng,&line,
1138 default_layout)))
1139 goto ERROR2;
1140 if ((res = psiconv_list_add(*result,line)))
1141 goto ERROR3;
1142 free(line);
1143 len += leng;
1144 }
1145
1146 if (length)
1147 *length = len;
1148
1149 psiconv_progress(lev,off+len-1,
1150 "End of sheet line list (total length: %08x)", len);
1151 return 0;
1152
1153ERROR3:
1154 psiconv_free_sheet_line(line);
1155ERROR2:
1156 psiconv_free_sheet_line_list(*result);
1157ERROR1:
1158 psiconv_warn(lev+1,off,"Reading of Sheet Line List failed");
1159 if (length)
1160 *length = 0;
1161 if (!res)
1162 return -PSICONV_E_NOMEM;
1163 else
1164 return res;
1165}
1166

Legend:
Removed from v.97  
changed lines
  Added in v.128

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