/[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 131
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
79static psiconv_sheet_cell_reference_t
80 psiconv_read_var_cellref (const psiconv_buffer buf, int lev,
81 psiconv_u32 off, int *length,
82 int *status)
83{
84 int len=0;
85 int res;
86 psiconv_sheet_cell_reference_t result;
87 psiconv_u32 temp;
88
89 psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference");
90 psiconv_progress(lev+2,off+len,
91 "Going to read the initial byte (%02x expected)",0x00);
92 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
93 if (res)
94 goto ERROR1;
95 if (temp != 0x00) {
96 psiconv_warn(lev+2,off+len,
97 "Sheet cell reference initial byte unknown value (ignored)");
98 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
99 }
100 len ++;
101
102 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
103 if (res)
104 goto ERROR1;
105 if (temp & 0xffff0000) {
106 psiconv_warn(lev+2,off+len,
107 "Sheet cell row reference to unknown row (reset)");
108 }
109 result.row.offset = temp;
110 result.row.absolute = psiconv_bool_true;
111 len += 4;
112
113 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
114 if (res)
115 goto ERROR1;
116 if (temp & 0xffff0000) {
117 psiconv_warn(lev+2,off+len,
118 "Sheet cell column reference to unknown row (reset)");
119 }
120 result.column.offset = temp;
121 result.column.absolute = psiconv_bool_true;
122 len += 4;
123
124 if (length)
125 *length = len;
126
127 psiconv_progress(lev,off+len-1,
128 "End of sheet column reference (total length: %08x)", len);
129 return result;
130ERROR1:
131 psiconv_warn(lev+1,off,"Reading of Sheet Column Reference failed");
132 if (length)
133 *length = 0;
134 if (status)
135 *status = res?res:-PSICONV_E_NOMEM;
136 return result;
137}
138
139static psiconv_sheet_cell_block_t
140 psiconv_read_var_cellblock (const psiconv_buffer buf, int lev,
141 psiconv_u32 off, int *length,
142 int *status)
143{
144 int len=0;
145 int res;
146 psiconv_sheet_cell_block_t result;
147 psiconv_u32 temp;
148
149 psiconv_progress(lev+1,off+len,"Going to read a sheet cell block reference");
150 psiconv_progress(lev+2,off+len,
151 "Going to read the initial byte (%02x expected)",0x00);
152 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
153 if (res)
154 goto ERROR1;
155 if (temp != 0x00) {
156 psiconv_warn(lev+2,off+len,
157 "Sheet cell reference initial byte unknown value (ignored)");
158 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
159 }
160 len ++;
161
162 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
163 if (res)
164 goto ERROR1;
165 if (temp & 0xffff0000) {
166 psiconv_warn(lev+2,off+len,
167 "Sheet block initial row reference to unknown row (reset)");
168 }
169 result.first.row.offset = temp;
170 result.first.row.absolute = psiconv_bool_true;
171 len += 4;
172
173 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
174 if (res)
175 goto ERROR1;
176 if (temp & 0xffff0000) {
177 psiconv_warn(lev+2,off+len,
178 "Sheet block initial column reference to unknown row (reset)");
179 }
180 result.first.column.offset = temp;
181 result.first.column.absolute = psiconv_bool_true;
182 len += 4;
183
184 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
185 if (res)
186 goto ERROR1;
187 if (temp & 0xffff0000) {
188 psiconv_warn(lev+2,off+len,
189 "Sheet block final row reference to unknown row (reset)");
190 }
191 result.last.row.offset = temp;
192 result.last.row.absolute = psiconv_bool_true;
193 len += 4;
194
195 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
196 if (res)
197 goto ERROR1;
198 if (temp & 0xffff0000) {
199 psiconv_warn(lev+2,off+len,
200 "Sheet block final column reference to unknown row (reset)");
201 }
202 result.last.column.offset = temp;
203 result.last.column.absolute = psiconv_bool_true;
204 len += 4;
205
206 if (length)
207 *length = len;
208
209 psiconv_progress(lev,off+len-1,
210 "End of sheet cell block reference (total length: %08x)",
211 len);
212 return result;
213ERROR1:
214 psiconv_warn(lev+1,off,"Reading of Sheet Cell Block Reference failed");
215 if (length)
216 *length = 0;
217 if (status)
218 *status = res?res:-PSICONV_E_NOMEM;
219 return result;
220}
221
222int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev,
223 psiconv_u32 off, int *length,
224 psiconv_sheet_numberformat result)
225{
226 int res=0;
227 int len=0;
228 psiconv_u8 temp;
229
230 psiconv_progress(lev+1,off,"Going to read a sheet numberformat");
231
232 psiconv_progress(lev+2,off+len,
233 "Going to read the initial byte (%02x expected)",0x02);
234 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
235 if (res)
236 goto ERROR1;
237 if (temp != 0x02) {
238 psiconv_warn(lev+2,off+len,
239 "Sheet numberformat initial byte unknown value (ignored)");
240 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
241 }
242 len ++;
243
244 psiconv_progress(lev+2,off+len, "Going to read the code byte");
245 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
246 if (res)
247 goto ERROR1;
248 psiconv_debug(lev+2,off+len,"Code: %02x",temp);
249 if (temp == 0x00)
250 result->code = psiconv_numberformat_general;
251 else if (temp == 0x02)
252 result->code = psiconv_numberformat_fixeddecimal;
253 else if (temp == 0x04)
254 result->code = psiconv_numberformat_scientific;
255 else if (temp == 0x06)
256 result->code = psiconv_numberformat_currency;
257 else if (temp == 0x08)
258 result->code = psiconv_numberformat_percent;
259 else if (temp == 0x0A)
260 result->code = psiconv_numberformat_triads;
261 else if (temp == 0x0C)
262 result->code = psiconv_numberformat_boolean;
263 else if (temp == 0x0E)
264 result->code = psiconv_numberformat_text;
265 else if (temp == 0x10)
266 result->code = psiconv_numberformat_date_dmm;
267 else if (temp == 0x12)
268 result->code = psiconv_numberformat_date_mmd;
269 else if (temp == 0x14)
270 result->code = psiconv_numberformat_date_ddmmyy;
271 else if (temp == 0x16)
272 result->code = psiconv_numberformat_date_mmddyy;
273 else if (temp == 0x18)
274 result->code = psiconv_numberformat_date_yymmdd;
275 else if (temp == 0x1A)
276 result->code = psiconv_numberformat_date_dmmm;
277 else if (temp == 0x1C)
278 result->code = psiconv_numberformat_date_dmmmyy;
279 else if (temp == 0x1E)
280 result->code = psiconv_numberformat_date_ddmmmyy;
281 else if (temp == 0x20)
282 result->code = psiconv_numberformat_date_mmm;
283 else if (temp == 0x22)
284 result->code = psiconv_numberformat_date_monthname;
285 else if (temp == 0x24)
286 result->code = psiconv_numberformat_date_mmmyy;
287 else if (temp == 0x26)
288 result->code = psiconv_numberformat_date_monthnameyy;
289 else if (temp == 0x28)
290 result->code = psiconv_numberformat_date_monthnamedyyyy;
291 else if (temp == 0x2A)
292 result->code = psiconv_numberformat_datetime_ddmmyyyyhhii;
293 else if (temp == 0x2C)
294 result->code = psiconv_numberformat_datetime_ddmmyyyyHHii;
295 else if (temp == 0x2E)
296 result->code = psiconv_numberformat_datetime_mmddyyyyhhii;
297 else if (temp == 0x30)
298 result->code = psiconv_numberformat_datetime_mmddyyyyHHii;
299 else if (temp == 0x32)
300 result->code = psiconv_numberformat_datetime_yyyymmddhhii;
301 else if (temp == 0x34)
302 result->code = psiconv_numberformat_datetime_yyyymmddHHii;
303 else if (temp == 0x36)
304 result->code = psiconv_numberformat_time_hhii;
305 else if (temp == 0x38)
306 result->code = psiconv_numberformat_time_hhiiss;
307 else if (temp == 0x3A)
308 result->code = psiconv_numberformat_time_HHii;
309 else if (temp == 0x3C)
310 result->code = psiconv_numberformat_time_HHiiss;
311 else {
312 psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)");
313 result->code = psiconv_numberformat_general;
314 }
315 len ++;
316
317 psiconv_progress(lev+2,off+len, "Going to read the number of decimals");
318 result->decimal = psiconv_read_u8(buf,lev+2,off+len,&res) >> 1;
319 if (res)
320 goto ERROR1;
321 psiconv_debug(lev+2,off+len,"Decimals: %d",result->decimal);
322 len ++;
323
324 if (length)
325 *length = len;
326
327 psiconv_progress(lev,off+len-1,
328 "End of sheet number format (total length: %08x)", len);
329 return 0;
330
331ERROR1:
332 psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed");
333 if (length)
334 *length = 0;
335 if (!res)
336 return -PSICONV_E_NOMEM;
337 else
338 return res;
339}
27 340
28int psiconv_parse_sheet_status_section(const psiconv_buffer buf, int lev, 341int psiconv_parse_sheet_status_section(const psiconv_buffer buf, int lev,
29 psiconv_u32 off, int *length, 342 psiconv_u32 off, int *length,
30 psiconv_sheet_status_section *result) 343 psiconv_sheet_status_section *result)
31{ 344{
141 if (res) 454 if (res)
142 goto ERROR2; 455 goto ERROR2;
143 if (temp != 0x00) { 456 if (temp != 0x00) {
144 psiconv_warn(lev+2,off+len, 457 psiconv_warn(lev+2,off+len,
145 "Sheet status section unknown byte unknown value (ignored)"); 458 "Sheet status section unknown byte unknown value (ignored)");
146 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 459 psiconv_debug(lev+2,off+len,"Unknown byte: %02x",temp);
147 } 460 }
148 len ++; 461 len ++;
149 462
150 psiconv_progress(lev+2,off+len,"Going to read sheet display size"); 463 psiconv_progress(lev+2,off+len,"Going to read sheet display size");
151 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); 464 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res);
184 497
185int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev, 498int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev,
186 psiconv_u32 off, int *length, 499 psiconv_u32 off, int *length,
187 psiconv_sheet_workbook_section *result) 500 psiconv_sheet_workbook_section *result)
188{ 501{
189 int res=0; 502 int res=0,with_name;
190 psiconv_u32 temp,formulas_off,worksheets_off; 503 psiconv_u32 temp,formulas_off,worksheets_off,info_off,var_off,name_off=0;
191 int len=0; 504 int len=0;
192 505
193 psiconv_progress(lev+1,off,"Going to read the sheet workbook section"); 506 psiconv_progress(lev+1,off,"Going to read the sheet workbook section");
194 if (!(*result = malloc(sizeof(**result)))) 507 if (!(*result = malloc(sizeof(**result))))
195 goto ERROR1; 508 goto ERROR1;
196 509
197 psiconv_progress(lev+2,off+len, 510 psiconv_progress(lev+2,off+len,
198 "Going to read the initial byte (%02x expected)",0x04); 511 "Going to read the initial byte (%02x or %02x expected)",
512 0x02,0x04);
199 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 513 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
200 if (res) 514 if (res)
201 goto ERROR2; 515 goto ERROR2;
202 if (temp != 0x04) { 516 if ((temp != 0x04) && temp !=0x02) {
203 psiconv_warn(lev+2,off+len, 517 psiconv_warn(lev+2,off+len,
204 "Sheet workbook section initial byte unknown value (ignored)"); 518 "Sheet workbook section initial byte unknown value (ignored)");
205 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 519 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
206 } 520 }
521 with_name = temp ==0x04;
207 len ++; 522 len ++;
208 523
209 psiconv_progress(lev+2,off+len, 524 psiconv_progress(lev+2,off+len,
210 "Going to read the offset of the 1st ??? Section"); 525 "Going to read the offset of the sheet info Section");
211 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 526 info_off = psiconv_read_u32(buf,lev+2,off+len,&res);
212 if (res) 527 if (res)
213 goto ERROR2; 528 goto ERROR2;
214 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 529 psiconv_debug(lev+2,off+len,"Offset: %04x",info_off);
215 len += 4; 530 len += 4;
216 531
217 psiconv_progress(lev+2,off+len, 532 psiconv_progress(lev+2,off+len,
218 "Going to read the offset of the Formulas List"); 533 "Going to read the offset of the Formulas List");
219 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res); 534 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res);
221 goto ERROR2; 536 goto ERROR2;
222 psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off); 537 psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off);
223 len += 4; 538 len += 4;
224 539
225 psiconv_progress(lev+2,off+len, 540 psiconv_progress(lev+2,off+len,
226 "Going to read the offset of the Worksheets Section"); 541 "Going to read the offset of the Worksheet List");
227 worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res); 542 worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res);
228 if (res) 543 if (res)
229 goto ERROR2; 544 goto ERROR2;
230 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 545 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off);
231 len += 4; 546 len += 4;
232 547
233 psiconv_progress(lev+2,off+len, 548 psiconv_progress(lev+2,off+len,
234 "Going to read the offset of the 4th ??? Section"); 549 "Going to read the offset of the Variable List");
235 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 550 var_off = psiconv_read_u32(buf,lev+2,off+len,&res);
236 if (res) 551 if (res)
237 goto ERROR2; 552 goto ERROR2;
238 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 553 psiconv_debug(lev+2,off+len,"Offset: %04x",var_off);
239 len += 4; 554 len += 4;
240 555
556 if (with_name) {
557 psiconv_progress(lev+2,off+len,
558 "Going to read the offset of the Name Section");
559 name_off = psiconv_read_u32(buf,lev+2,off+len,&res);
560 if (res)
561 goto ERROR2;
562 psiconv_debug(lev+2,off+len,"Offset: %04x",name_off);
563 len += 4;
564 }
565
566
567 psiconv_progress(lev+2,off+len,"Going to read the info section");
568 if ((res = psiconv_parse_sheet_info_section(buf,lev+2,info_off,NULL,
569 &(*result)->info)))
570 goto ERROR2;
571
572 psiconv_progress(lev+2,off+len,"Going to read the variables list");
573 if ((res = psiconv_parse_sheet_variable_list(buf,lev+2,var_off,NULL,
574 &(*result)->variables)))
575 goto ERROR3;
576
241 psiconv_progress(lev+2,off+len,"Going to read the formulas list"); 577 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, 578 if ((res = psiconv_parse_sheet_formula_list(buf,lev+2,formulas_off,NULL,
243 &(*result)->formulas))) 579 &(*result)->formulas)))
244 goto ERROR2; 580 goto ERROR4;
245 581
246 psiconv_progress(lev+2,off+len,"Going to read the worksheets"); 582 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, 583 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off,
248 NULL,&(*result)->worksheet))) 584 NULL,&(*result)->worksheets)))
249 goto ERROR2; 585 goto ERROR5;
250 586
587 if (with_name) {
588 psiconv_progress(lev+2,off+len,"Going to read the name section");
589 if ((res = psiconv_parse_sheet_name_section(buf,lev+2,name_off,NULL,
590 &(*result)->name)))
591 goto ERROR6;
592 } else
593 (*result)->name = NULL;
251 594
252 if (length) 595 if (length)
253 *length = len; 596 *length = len;
254 597
255 psiconv_progress(lev,off+len-1, 598 psiconv_progress(lev,off+len-1,
256 "End of sheet workbook section (total length: %08x)", len); 599 "End of sheet workbook section (total length: %08x)", len);
257 return 0; 600 return 0;
258 601
602ERROR6:
603 psiconv_free_sheet_worksheet_list((*result)->worksheets);
604ERROR5:
605 psiconv_free_formula_list((*result)->formulas);
606ERROR4:
607 psiconv_free_sheet_variable_list((*result)->variables);
608ERROR3:
609 psiconv_free_sheet_info_section((*result)->info);
259ERROR2: 610ERROR2:
260 free (*result); 611 free (*result);
261ERROR1: 612ERROR1:
262 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed"); 613 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed");
263 if (length) 614 if (length)
266 return -PSICONV_E_NOMEM; 617 return -PSICONV_E_NOMEM;
267 else 618 else
268 return res; 619 return res;
269} 620}
270 621
622int psiconv_parse_sheet_name_section(const psiconv_buffer buf, int lev,
623 psiconv_u32 off, int *length,
624 psiconv_sheet_name_section *result)
625{
626 int res=0;
627 psiconv_u32 temp;
628 int len=0,leng;
629
630 psiconv_progress(lev+1,off,"Going to read the sheet name section");
631 if (!(*result = malloc(sizeof(**result))))
632 goto ERROR1;
633
634 psiconv_progress(lev+2,off+len,
635 "Going to read the initial byte (%02x expected)",0x02);
636 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
637 if (res)
638 goto ERROR2;
639 if (temp != 0x02) {
640 psiconv_warn(lev+2,off+len,
641 "Sheet name section initial byte unknown value (ignored)");
642 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
643 }
644 len ++;
645
646 psiconv_progress(lev+2,off+len, "Going to read the sheet name");
647 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
648 if (res)
649 goto ERROR2;
650 len += leng;
651
652 if (length)
653 *length = len;
654
655 psiconv_progress(lev,off+len-1,
656 "End of sheet name section (total length: %08x)", len);
657 return 0;
658
659ERROR2:
660 free(*result);
661ERROR1:
662 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
663 if (length)
664 *length = 0;
665 if (!res)
666 return -PSICONV_E_NOMEM;
667 else
668 return res;
669}
670
671int psiconv_parse_sheet_info_section(const psiconv_buffer buf, int lev,
672 psiconv_u32 off, int *length,
673 psiconv_sheet_info_section *result)
674{
675 int res=0;
676 psiconv_u32 temp;
677 int len=0,leng;
678
679 psiconv_progress(lev+1,off,"Going to read the sheet info section");
680 if (!(*result = malloc(sizeof(**result))))
681 goto ERROR1;
682
683 psiconv_progress(lev+2,off+len,
684 "Going to read the initial byte (%02x expected)",0x02);
685 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
686 if (res)
687 goto ERROR2;
688 if (temp != 0x02) {
689 psiconv_warn(lev+2,off+len,
690 "Sheet info section initial byte unknown value (ignored)");
691 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
692 }
693 len ++;
694
695 psiconv_progress(lev+2,off+len, "Going to read an unknown Xint");
696 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
697 if (res)
698 goto ERROR2;
699 psiconv_debug(lev+2,off+len,"Value: %d\n",temp);
700 len += leng;
701
702 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
703 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
704 if (res)
705 goto ERROR2;
706 (*result)->auto_recalc = temp & 0x01 ? psiconv_bool_true:psiconv_bool_false;
707 psiconv_debug(lev+2,off+len,"Auto recalculation: %02x",
708 (*result)->auto_recalc);
709 if ((temp & 0xfe) != 0x02) {
710 psiconv_warn(lev+2,off+len,"Sheet Info Section flags byte "
711 "contains unknown flags (ignored)");
712 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp &0xfe);
713 }
714
715 len ++;
716
717
718 if (length)
719 *length = len;
720
721 psiconv_progress(lev,off+len-1,
722 "End of sheet info section (total length: %08x)", len);
723 return 0;
724
725ERROR2:
726 free(*result);
727ERROR1:
728 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
729 if (length)
730 *length = 0;
731 if (!res)
732 return -PSICONV_E_NOMEM;
733 else
734 return res;
735}
736
271int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev, 737int psiconv_parse_sheet_formula_list(const psiconv_buffer buf, int lev,
272 psiconv_u32 off, int *length, 738 psiconv_u32 off, int *length,
273 psiconv_formula_list *result) 739 psiconv_formula_list *result)
274{ 740{
275 int res=0; 741 int res=0;
276 int len=0; 742 int len=0;
277 psiconv_u32 temp; 743 psiconv_u32 temp;
278 psiconv_formula formula; 744 psiconv_formula formula;
279 psiconv_u32 listlen,i; 745 psiconv_u32 listlen,i;
280 int leng; 746 int leng;
281 747
282 psiconv_progress(lev+1,off,"Going to read the sheet formula table"); 748 psiconv_progress(lev+1,off,"Going to read the sheet formula list");
283 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 749 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s))))
284 goto ERROR1; 750 goto ERROR1;
285 751
286 psiconv_progress(lev+2,off+len, 752 psiconv_progress(lev+2,off+len,
287 "Going to read the initial byte (%02x expected)",0x02); 753 "Going to read the initial byte (%02x expected)",0x02);
288 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 754 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
289 if (res) 755 if (res)
290 goto ERROR2; 756 goto ERROR2;
291 if (temp != 0x02) { 757 if (temp != 0x02) {
292 psiconv_warn(lev+2,off+len, 758 psiconv_warn(lev+2,off+len,
293 "Sheet formula table initial byte unknown value (ignored)"); 759 "Sheet formula list initial byte unknown value (ignored)");
294 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 760 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
295 } 761 }
296 len ++; 762 len ++;
297 763
298 psiconv_progress(lev+2,off+len, 764 psiconv_progress(lev+2,off+len,
315 781
316 if (length) 782 if (length)
317 *length = len; 783 *length = len;
318 784
319 psiconv_progress(lev,off+len-1, 785 psiconv_progress(lev,off+len-1,
320 "End of sheet formula table (total length: %08x)", len); 786 "End of sheet formula list (total length: %08x)", len);
321 return 0; 787 return 0;
322 788
323ERROR3: 789ERROR3:
324 psiconv_free_formula(formula); 790 psiconv_free_formula(formula);
325ERROR2: 791ERROR2:
326 psiconv_list_free(*result); 792 psiconv_list_free(*result);
327ERROR1: 793ERROR1:
328 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); 794 psiconv_warn(lev+1,off,"Reading of Sheet Formula list failed");
329 if (length) 795 if (length)
330 *length = 0; 796 *length = 0;
331 if (!res) 797 if (!res)
332 return -PSICONV_E_NOMEM; 798 return -PSICONV_E_NOMEM;
333 else 799 else
334 return res; 800 return res;
335} 801}
336 802
337int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, 803int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
338 psiconv_u32 off, int *length, 804 psiconv_u32 off, int *length,
339 psiconv_sheet_cell *result) 805 psiconv_sheet_cell *result,
806 const psiconv_sheet_cell_layout default_layout,
807 const psiconv_sheet_line_list row_default_layouts,
808 const psiconv_sheet_line_list col_default_layouts)
340{ 809{
341 int res=0; 810 int res=0;
342 int len=0; 811 int len=0;
343 psiconv_u32 temp; 812 psiconv_u32 temp;
344 psiconv_bool_t has_layout; 813 psiconv_bool_t has_layout;
346 815
347 psiconv_progress(lev+1,off,"Going to read a sheet cell structure"); 816 psiconv_progress(lev+1,off,"Going to read a sheet cell structure");
348 if (!(*result = malloc(sizeof(**result)))) 817 if (!(*result = malloc(sizeof(**result))))
349 goto ERROR1; 818 goto ERROR1;
350 819
820 (*result)->layout = NULL;
821 (*result)->type = psiconv_cell_blank;
822
351 psiconv_progress(lev+2,off+len,"Going to read the cell position"); 823 psiconv_progress(lev+2,off+len,"Going to read the cell position");
352 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 824 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
353 if (res) 825 if (res)
354 goto ERROR2; 826 goto ERROR2;
355 len ++; 827 len ++;
363 len ++; 835 len ++;
364 (*result)->column = (temp >> 2) & 0xFF; 836 (*result)->column = (temp >> 2) & 0xFF;
365 (*result)->row = (temp >> 10) & 0x3FFF; 837 (*result)->row = (temp >> 10) & 0x3FFF;
366 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x", 838 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x",
367 (*result)->column,(*result)->row); 839 (*result)->column,(*result)->row);
840 if (temp & 0x03) {
841 psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)");
842 psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03);
843 }
368 844
369 psiconv_progress(lev+2,off+len,"Going to read the cell type"); 845 psiconv_progress(lev+2,off+len,"Going to read the cell type");
370 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 846 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
371 if (res) 847 if (res)
372 goto ERROR2; 848 goto ERROR2;
386 len += 4; 862 len += 4;
387 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int); 863 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
388 864
389 } else if ((*result)->type == psiconv_cell_bool) { 865 } else if ((*result)->type == psiconv_cell_bool) {
390 psiconv_progress(lev+2,off+len,"Going to read a boolean"); 866 psiconv_progress(lev+2,off+len,"Going to read a boolean");
391 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 867 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
392 if (res) 868 &(*result)->data.dat_bool)))
393 goto ERROR2; 869 goto ERROR2;
394 len ++;
395 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp); 870 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp);
396 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false; 871 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
397 872 len += leng;
398 } else if ((*result)->type == psiconv_cell_error) { 873 } else if ((*result)->type == psiconv_cell_error) {
399 psiconv_progress(lev+2,off+len,"Going to read the error code"); 874 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); 875 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
401 if (res) 876 if (res)
402 goto ERROR2; 877 goto ERROR2;
403 len += 2; 878 if (temp == 0)
879 (*result)->data.dat_error = psiconv_sheet_error_none;
880 else if (temp == 1)
881 (*result)->data.dat_error = psiconv_sheet_error_null;
882 else if (temp == 2)
883 (*result)->data.dat_error = psiconv_sheet_error_divzero;
884 else if (temp == 3)
885 (*result)->data.dat_error = psiconv_sheet_error_value;
886 else if (temp == 4)
887 (*result)->data.dat_error = psiconv_sheet_error_reference;
888 else if (temp == 5)
889 (*result)->data.dat_error = psiconv_sheet_error_name;
890 else if (temp == 6)
891 (*result)->data.dat_error = psiconv_sheet_error_number;
892 else if (temp == 7)
893 (*result)->data.dat_error = psiconv_sheet_error_notavail;
894 else {
895 psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)");
896 psiconv_debug(lev+2,off+len,"Error code: %04x",temp);
897 (*result)->data.dat_error = psiconv_sheet_error_none;
898 }
404 psiconv_debug(lev+2,off+len,"Cell contents: %04x", 899 psiconv_debug(lev+2,off+len,"Cell contents: %04x",
405 (*result)->data.dat_error); 900 (*result)->data.dat_error);
406 901 len += 2;
407 } else if ((*result)->type == psiconv_cell_float) { 902 } else if ((*result)->type == psiconv_cell_float) {
408 psiconv_progress(lev+2,off+len,"Going to read a float"); 903 psiconv_progress(lev+2,off+len,"Going to read a float");
409 (*result)->data.dat_float = 904 (*result)->data.dat_float =
410 psiconv_read_float(buf,lev+2,off+len,&leng,&res); 905 psiconv_read_float(buf,lev+2,off+len,&leng,&res);
411 if (res) 906 if (res)
412 goto ERROR2; 907 goto ERROR2;
413 len += leng;
414 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float); 908 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
415 909 len += leng;
416 } else if ((*result)->type == psiconv_cell_string) { 910 } else if ((*result)->type == psiconv_cell_string) {
417 psiconv_progress(lev+2,off+len,"Going to read a string"); 911 psiconv_progress(lev+2,off+len,"Going to read a string");
418 (*result)->data.dat_string = 912 (*result)->data.dat_string =
419 psiconv_read_short_string(buf,lev+2,off+len,&leng,&res); 913 psiconv_read_string(buf,lev+2,off+len,&leng,&res);
420 if (res) 914 if (res)
421 goto ERROR2; 915 goto ERROR2;
422 len += leng;
423 psiconv_debug(lev+2,off+len,"Cell contents: `%s'", 916 psiconv_debug(lev+2,off+len,"Cell contents: `%s'",
424 (*result)->data.dat_string); 917 (*result)->data.dat_string);
918 len += leng;
425 } else { 919 } else {
426 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); 920 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
427 } 921 res = PSICONV_E_PARSE;
922 goto ERROR2;
428 923 }
924
925 if (!((*result)->layout = psiconv_clone_cell_layout(
926 psiconv_get_default_layout(row_default_layouts,
927 col_default_layouts,
928 default_layout,
929 (*result)->row,
930 (*result)->column))))
931 goto ERROR2;
429 if (has_layout) { 932 if (has_layout) {
430 psiconv_progress(lev+2,off+len,"Going to read the cell layout"); 933 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
431 934 &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; 935 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; 936 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 } 937 }
474 938
475 if ((*result)->calculated) { 939 if ((*result)->calculated) {
476 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference"); 940 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); 941 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
478 if (res) 942 if (res)
479 goto ERROR2; 943 goto ERROR2;
480 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp); 944 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp);
481 len += leng; 945 len += leng;
482 (*result)->ref_formula = temp; 946 (*result)->ref_formula = temp;
483 } 947 }
484 948
485 if (length) 949 if (length)
486 *length = len; 950 *length = len;
487 951
500 else 964 else
501 return res; 965 return res;
502} 966}
503 967
504int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, 968int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
505 psiconv_u32 off, int *length, 969 psiconv_u32 off, int *length,
506 psiconv_sheet_cell_list *result) 970 psiconv_sheet_cell_list *result,
971 const psiconv_sheet_cell_layout default_layout,
972 const psiconv_sheet_line_list row_default_layouts,
973 const psiconv_sheet_line_list col_default_layouts)
507{ 974{
508 int res=0; 975 int res=0;
509 int len=0; 976 int len=0;
510 psiconv_u32 temp; 977 psiconv_u32 temp;
511 psiconv_sheet_cell cell; 978 psiconv_sheet_cell cell;
512 psiconv_u32 listlen,i; 979 psiconv_u32 listlen,i;
513 int leng; 980 int leng;
514 981
515 psiconv_progress(lev+1,off,"Going to read the sheet cells list"); 982 psiconv_progress(lev+1,off,"Going to read the sheet cell list");
516 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s)))) 983 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
517 goto ERROR1; 984 goto ERROR1;
518 985
519 psiconv_progress(lev+2,off+len, 986 psiconv_progress(lev+2,off+len,
520 "Going to read the initial byte (%02x expected)",0x02); 987 "Going to read the initial byte (%02x expected)",0x02);
549 len += leng; 1016 len += leng;
550 1017
551 psiconv_progress(lev+2,off+len,"Going to read all cells"); 1018 psiconv_progress(lev+2,off+len,"Going to read all cells");
552 for (i = 0; i < listlen; i++) { 1019 for (i = 0; i < listlen; i++) {
553 psiconv_progress(lev+3,off+len,"Going to read cell %d",i); 1020 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))) 1021 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
1022 default_layout,row_default_layouts,
1023 col_default_layouts)))
555 goto ERROR2; 1024 goto ERROR2;
556 if ((res = psiconv_list_add(*result,cell))) 1025 if ((res = psiconv_list_add(*result,cell)))
557 goto ERROR3; 1026 goto ERROR3;
1027 free(cell);
558 len += leng; 1028 len += leng;
559 } 1029 }
560 1030
561 if (length) 1031 if (length)
562 *length = len; 1032 *length = len;
578 else 1048 else
579 return res; 1049 return res;
580} 1050}
581 1051
582 1052
583int psiconv_parse_sheet_worksheet_section(const psiconv_buffer buf, int lev, 1053int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
584 psiconv_u32 off, int *length, 1054 psiconv_u32 off, int *length,
585 psiconv_sheet_worksheet_section *result) 1055 psiconv_sheet_worksheet_list *result)
586{ 1056{
1057 psiconv_sheet_worksheet worksheet;
587 int res=0; 1058 int res=0;
588 psiconv_u32 temp,cells_off,grid_off;
589 int len=0; 1059 int len=0;
1060 psiconv_u8 temp;
1061 psiconv_u32 offset;
590 int leng; 1062 int leng,i,nr;
591 1063
592 /* TODO: this is the section that might be an XListE instead... */ 1064 psiconv_progress(lev+1,off,"Going to read the worksheet list");
1065 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
1066 goto ERROR1;
1067
593 psiconv_progress(lev+2,off+len, 1068 psiconv_progress(lev+2,off+len,
594 "Going to read the initial bytes (%02x expected)",0x02); 1069 "Going to read the initial bytes (%02x expected)",0x02);
595 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1070 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
596 if (res) 1071 if (res)
597 goto ERROR1; 1072 goto ERROR2;
598 if (temp != 0x04) { 1073 if (temp != 0x02) {
599 psiconv_warn(lev+2,off+len, 1074 psiconv_warn(lev+2,off+len,
600 "Sheet worksheet section initial byte unknown value (ignored)"); 1075 "Sheet worksheet list initial byte unknown value (ignored)");
601 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1076 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
602 } 1077 }
603 len ++; 1078 len ++;
604 1079
1080 psiconv_progress(lev+2,off+len,"Going to read the list length");
1081 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1082 if (res)
1083 goto ERROR2;
1084 psiconv_debug(lev+2,off+len,"Length: %02x",nr);
1085 len += leng;
1086
1087 psiconv_progress(lev+2,off+len,"Going to read the list");
1088 for (i=0 ; i < nr; i++) {
1089 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
605 psiconv_progress(lev+2,off+len, 1090 psiconv_progress(lev+4,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); 1091 "Going to read the initial byte (%02x expected)",0x00);
619 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1092 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
620 if (res) 1093 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");
643 if (!(*result = malloc(sizeof(**result))))
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;
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 }
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,
671 "Going to read the initial bytes (%02x expected)",0x02);
672 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
673 if (res)
674 goto ERROR2;
675 if (temp != 0x02) {
676 psiconv_warn(lev+2,off+len,
677 "Worksheet section initial byte unknown value (ignored)");
678 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
679 }
680 len ++;
681
682 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
683 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
684 if (res)
685 goto ERROR2;
686 len ++;
687
688 if (temp & 0x01) {
689 if (!((*result)->paragraph = psiconv_basic_paragraph_layout()))
690 goto ERROR2; 1094 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) { 1095 if (temp != 0x00) {
1096 psiconv_warn(lev+4,off+len,
1097 "Sheet worksheet element initial byte unknown value (ignored)");
1098 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp);
1099 }
1100 len ++;
1101
699 psiconv_progress(lev+3,off+len,"Going to read the default character codes"); 1102 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); 1103 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
711 if (res) 1104 if (res)
712 goto ERROR2_3; 1105 goto ERROR2;
1106 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
713 len ++; 1107 len += 4;
714 psiconv_read_u8(buf,lev+2,off+len,&res); 1108
715 if (res) 1109 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
1110 &worksheet)))
716 goto ERROR2_3; 1111 goto ERROR2;
717 len ++; 1112 if ((res = psiconv_list_add(*result,worksheet)))
718 psiconv_read_u8(buf,lev+2,off+len,&res);
719 if (res)
720 goto ERROR2_3; 1113 goto ERROR3;
721 len ++; 1114 free(worksheet);
722 }
723 1115 }
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 1116
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) 1117 if (length)
773 *length = len; 1118 *length = len;
774 1119
775 psiconv_progress(lev,off+len-1, 1120 psiconv_progress(lev,off+len-1,
776 "End of sheet worksheet section (total length: %08x)", len); 1121 "End of worksheet list (total length: %08x)", len);
1122
777 return 0; 1123 return 0;
778 1124
779ERROR2_3: 1125ERROR3:
780 psiconv_free_numberformat((*result)->numberformat); 1126 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: 1127ERROR2:
788 free (*result); 1128 psiconv_free_sheet_worksheet_list(*result);
789ERROR1: 1129ERROR1:
790 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed"); 1130 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
791 if (length) 1131 if (length)
792 *length = 0; 1132 *length = 0;
793 if (!res) 1133 if (!res)
794 return -PSICONV_E_NOMEM; 1134 return -PSICONV_E_NOMEM;
795 else 1135 else
796 return res; 1136 return res;
797} 1137}
798 1138
1139int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
1140 psiconv_u32 off, int *length,
1141 psiconv_sheet_cell_layout result)
799 1142
1143{
1144 int res=0;
1145 int len=0;
1146 int leng;
1147 psiconv_u8 temp;
1148
1149 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
1150
1151 psiconv_progress(lev+2,off+len,
1152 "Going to read the first byte (%02x expected)",0x02);
1153 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1154 if (res)
1155 goto ERROR1;
1156 if (temp != 0x02) {
1157 psiconv_warn(lev+2,off+len,
1158 "Worksheet section initial byte unknown value (ignored)");
1159 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1160 }
1161 len ++;
1162
1163 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
1164 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1165 if (res)
1166 goto ERROR1;
1167 len ++;
1168
1169 if (temp & 0x01) {
1170 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
1171 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
1172 result->paragraph)))
1173 goto ERROR1;
1174 len += leng;
1175 }
1176
1177 if (temp & 0x02) {
1178 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
1179 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
1180 result->character)))
1181 goto ERROR1;
1182 len += leng;
1183 }
1184
1185 if (temp & 0x04) {
1186 psiconv_progress(lev+3,off+len, "Going to read the default number format");
1187 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
1188 result->numberformat);
1189 len += leng;
1190 }
1191
1192 if (length)
1193 *length = len;
1194
1195 psiconv_progress(lev,off+len-1,
1196 "End of sheet cell layout (total length: %08x)", len);
1197
1198 return 0;
1199
1200ERROR1:
1201 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
1202 if (length)
1203 *length = 0;
1204 if (!res)
1205 return -PSICONV_E_NOMEM;
1206 else
1207 return res;
1208}
1209
1210
1211int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
1212 psiconv_u32 off, int *length,
1213 psiconv_sheet_worksheet *result)
1214{
1215 int res=0;
1216 psiconv_u32 temp,cells_off,grid_off,rows_off,cols_off;
1217 int len=0;
1218 int leng;
1219
1220 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
1221 if (!(*result = malloc(sizeof(**result))))
1222 goto ERROR1;
1223
1224 psiconv_progress(lev+2,off+len,
1225 "Going to read the initial bytes (%02x expected)",0x04);
1226 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1227 if (res)
1228 goto ERROR2;
1229 if (temp != 0x04) {
1230 psiconv_warn(lev+2,off+len,
1231 "Worksheet section initial byte unknown value (ignored)");
1232 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1233 }
1234 len ++;
1235
1236 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
1237 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1238 if (res)
1239 goto ERROR2;
1240 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp);
1241 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
1242 if (temp & 0xfe) {
1243 psiconv_warn(lev+2,off+len,
1244 "Worksheet section flags byte unknown bits (ignored)");
1245 }
1246 len ++;
1247
1248 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
1249 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
1250 goto ERROR2;
1251 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
1252 (*result)->default_layout)))
1253 goto ERROR3;
1254 len += leng;
1255
1256 psiconv_progress(lev+2,off+len,
1257 "Going to read the offset of the row defaults Section");
1258 rows_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1259 if (res)
1260 goto ERROR3;
1261 psiconv_debug(lev+2,off+len,"Offset: %04x",rows_off);
1262 len += 4;
1263
1264 psiconv_progress(lev+2,off+len,
1265 "Going to read the offset of the column defaults Section");
1266 cols_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1267 if (res)
1268 goto ERROR3;
1269 psiconv_debug(lev+2,off+len,"Offset: %04x",cols_off);
1270 len += 4;
1271
1272 psiconv_progress(lev+2,off+len,
1273 "Going to read the offset of the Cells List");
1274 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1275 if (res)
1276 goto ERROR3;
1277 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
1278 len += 4;
1279
1280 psiconv_progress(lev+2,off+len,
1281 "Going to read the offset of the Grid Section");
1282 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1283 if (res)
1284 goto ERROR3;
1285 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
1286 len += 4;
1287
1288 psiconv_progress(lev+2,off+len,
1289 "Going to read the offset of the 3rd ??? Section");
1290 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
1291 if (res)
1292 goto ERROR3;
1293 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
1294 len += 4;
1295
1296 psiconv_progress(lev+2,off+len,"Going to read the row defaults");
1297 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,rows_off,NULL,
1298 &(*result)->row_default_layouts,
1299 (*result)->default_layout)))
1300 goto ERROR3;
1301
1302 psiconv_progress(lev+2,off+len,"Going to read the column defaults");
1303 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,cols_off,NULL,
1304 &(*result)->col_default_layouts,
1305 (*result)->default_layout)))
1306 goto ERROR4;
1307
1308 psiconv_progress(lev+2,off+len,"Going to read the cells list");
1309 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
1310 &(*result)->cells,
1311 (*result)->default_layout,
1312 (*result)->row_default_layouts,
1313 (*result)->col_default_layouts)))
1314 goto ERROR5;
1315
1316
1317/* TODO: parse grid section */
1318
1319 if (length)
1320 *length = len;
1321
1322 psiconv_progress(lev,off+len-1,
1323 "End of sheet worksheet section (total length: %08x)", len);
1324 return 0;
1325
1326ERROR5:
1327 psiconv_free_sheet_line_list((*result)->col_default_layouts);
1328ERROR4:
1329 psiconv_free_sheet_line_list((*result)->row_default_layouts);
1330ERROR3:
1331 psiconv_free_sheet_cell_layout((*result)->default_layout);
1332ERROR2:
1333 free (*result);
1334ERROR1:
1335 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed");
1336 if (length)
1337 *length = 0;
1338 if (!res)
1339 return -PSICONV_E_NOMEM;
1340 else
1341 return res;
1342}
1343
1344int psiconv_parse_sheet_line(const psiconv_buffer buf, int lev,
1345 psiconv_u32 off, int *length,
1346 psiconv_sheet_line *result,
1347 const psiconv_sheet_cell_layout default_layout)
1348{
1349 int res=0;
1350 int len=0;
1351 int leng;
1352
1353
1354 psiconv_progress(lev+1,off,"Going to read a sheet line");
1355 if (!(*result = malloc(sizeof(**result))))
1356 goto ERROR1;
1357
1358 psiconv_progress(lev+2,off+len,"Going to read the line number");
1359 (*result)->position = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1360 if (res)
1361 goto ERROR2;
1362 psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->position);
1363 len += leng;
1364
1365 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
1366 goto ERROR2;
1367 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
1368 &leng,(*result)->layout)))
1369 goto ERROR3;
1370 len += leng;
1371
1372 if (length)
1373 *length = len;
1374
1375 psiconv_progress(lev,off+len-1,
1376 "End of the sheet line (total length: %08x)", len);
1377 return 0;
1378
1379ERROR3:
1380 psiconv_free_sheet_cell_layout((*result)->layout);
1381ERROR2:
1382 free (*result);
1383ERROR1:
1384 psiconv_warn(lev+1,off,"Reading of the sheet line failed");
1385 if (length)
1386 *length = 0;
1387 if (!res)
1388 return -PSICONV_E_NOMEM;
1389 else
1390 return res;
1391}
1392
1393
1394int psiconv_parse_sheet_line_list(const psiconv_buffer buf, int lev,
1395 psiconv_u32 off, int *length,
1396 psiconv_sheet_line_list *result,
1397 const psiconv_sheet_cell_layout default_layout)
1398{
1399 int res=0;
1400 int len=0;
1401 psiconv_u32 temp;
1402 psiconv_sheet_line line;
1403 psiconv_u32 listlen,i;
1404 int leng;
1405
1406 psiconv_progress(lev+1,off,"Going to read the sheet line list");
1407 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_line_s))))
1408 goto ERROR1;
1409
1410 psiconv_progress(lev+2,off+len,
1411 "Going to read the initial byte (%02x expected)",0x02);
1412 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1413 if (res)
1414 goto ERROR2;
1415 if (temp != 0x02) {
1416 psiconv_warn(lev+2,off+len,
1417 "Sheet line list initial byte unknown value (ignored)");
1418 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1419 }
1420 len ++;
1421
1422 psiconv_progress(lev+2,off+len,
1423 "Going to read the number of defined lines");
1424 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1425 if (res)
1426 goto ERROR2;
1427 psiconv_debug(lev+2,off+len,"Number of defined lines: %d",listlen);
1428 len += leng;
1429
1430 psiconv_progress(lev+2,off+len,"Going to read all lines");
1431 for (i = 0; i < listlen; i++) {
1432 psiconv_progress(lev+3,off+len,"Going to read line %d",i);
1433 if ((res = psiconv_parse_sheet_line(buf,lev+3,off+len,&leng,&line,
1434 default_layout)))
1435 goto ERROR2;
1436 if ((res = psiconv_list_add(*result,line)))
1437 goto ERROR3;
1438 free(line);
1439 len += leng;
1440 }
1441
1442 if (length)
1443 *length = len;
1444
1445 psiconv_progress(lev,off+len-1,
1446 "End of sheet line list (total length: %08x)", len);
1447 return 0;
1448
1449ERROR3:
1450 psiconv_free_sheet_line(line);
1451ERROR2:
1452 psiconv_free_sheet_line_list(*result);
1453ERROR1:
1454 psiconv_warn(lev+1,off,"Reading of Sheet Line List failed");
1455 if (length)
1456 *length = 0;
1457 if (!res)
1458 return -PSICONV_E_NOMEM;
1459 else
1460 return res;
1461}
1462
1463int psiconv_parse_sheet_variable(const psiconv_buffer buf, int lev,
1464 psiconv_u32 off, int *length,
1465 psiconv_sheet_variable *result)
1466{
1467 int res=0;
1468 int len=0;
1469 psiconv_u32 marker;
1470 int leng;
1471
1472 psiconv_progress(lev+1,off,"Going to read a sheet variable");
1473 if (!(*result = malloc(sizeof(**result))))
1474 goto ERROR1;
1475
1476 psiconv_progress(lev+2,off+len, "Going to read the variable name");
1477 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
1478 if (res)
1479 goto ERROR2;
1480 len += leng;
1481
1482 psiconv_progress(lev+2,off+len,"Going to read the type marker");
1483 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
1484 if (res)
1485 goto ERROR3;
1486 psiconv_debug(lev+2,off+len,"Marker: %02x",marker);
1487 len ++;
1488
1489 if (marker == 0x00) {
1490 (*result)->type = psiconv_var_int;
1491 psiconv_progress(lev+2,off+len,"Going to read a signed integer");
1492 (*result)->data.dat_int = psiconv_read_sint(buf,lev+2,off+len,&leng,&res);
1493 if (res)
1494 goto ERROR3;
1495 psiconv_debug(lev+2,off+len,"Value: %d",(*result)->data.dat_int);
1496 len += leng;
1497 } else if (marker == 0x01) {
1498 (*result)->type = psiconv_var_float;
1499 psiconv_progress(lev+2,off+len,"Going to read a floating point number");
1500 (*result)->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng,
1501 &res);
1502 if (res)
1503 goto ERROR3;
1504 psiconv_debug(lev+2,off+len,"Value: %f",(*result)->data.dat_float);
1505 len += leng;
1506 } else if (marker == 0x02) {
1507 (*result)->type = psiconv_var_string;
1508 psiconv_progress(lev+2,off+len,"Going to read a string");
1509 (*result)->data.dat_string = psiconv_read_string(buf,lev+2,off+len,&leng,
1510 &res);
1511 if (res)
1512 goto ERROR3;
1513 len += leng;
1514 } else if (marker == 0x03) {
1515 (*result)->type = psiconv_var_cellref;
1516 psiconv_progress(lev+2,off+len,"Going to read a cell reference");
1517 (*result)->data.dat_cellref = psiconv_read_var_cellref(buf,lev+2,off+len,
1518 &leng, &res);
1519 if (res)
1520 goto ERROR3;
1521 len += leng;
1522 } else if (marker == 0x04) {
1523 (*result)->type = psiconv_var_cellblock;
1524 psiconv_progress(lev+2,off+len,"Going to read a cell block reference");
1525 (*result)->data.dat_cellblock = psiconv_read_var_cellblock(buf,lev+2,
1526 off+len,
1527 &leng, &res);
1528 if (res)
1529 goto ERROR3;
1530 len += leng;
1531 } else {
1532 psiconv_warn(lev+2,off+len,"Sheet variable unknown type marker");
1533 res = -PSICONV_E_PARSE;
1534 goto ERROR3;
1535 }
1536
1537 psiconv_progress(lev+2,off+len,"Going to read the variable number");
1538 (*result)->number = psiconv_read_u32(buf,lev+2,off+len,&res);
1539 if (res)
1540 goto ERROR4;
1541 psiconv_debug(lev+2,off+len,"Number: %08x",(*result)->number);
1542 len += 4;
1543
1544 if (length)
1545 *length = len;
1546
1547 psiconv_progress(lev,off+len-1,
1548 "End of sheet variable (total length: %08x)", len);
1549 return 0;
1550
1551ERROR4:
1552 if ((*result)->type == psiconv_var_string)
1553 free((*result)->data.dat_string);
1554ERROR3:
1555 free((*result)->name);
1556ERROR2:
1557 free (*result);
1558ERROR1:
1559 psiconv_warn(lev+1,off,"Reading of Sheet Variable failed");
1560 if (length)
1561 *length = 0;
1562 if (!res)
1563 return -PSICONV_E_NOMEM;
1564 else
1565 return res;
1566}
1567
1568
1569int psiconv_parse_sheet_variable_list(const psiconv_buffer buf, int lev,
1570 psiconv_u32 off, int *length,
1571 psiconv_sheet_variable_list *result)
1572{
1573 int res=0;
1574 int len=0;
1575 psiconv_u32 temp;
1576 psiconv_sheet_variable variable;
1577 psiconv_u32 listlen,i;
1578 int leng;
1579
1580 psiconv_progress(lev+1,off,"Going to read the sheet variable list");
1581 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_variable_s))))
1582 goto ERROR1;
1583
1584 psiconv_progress(lev+2,off+len,
1585 "Going to read the initial byte (%02x expected)",0x02);
1586 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1587 if (res)
1588 goto ERROR2;
1589 if (temp != 0x02) {
1590 psiconv_warn(lev+2,off+len,
1591 "Sheet variable list initial byte unknown value (ignored)");
1592 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1593 }
1594 len ++;
1595
1596 psiconv_progress(lev+2,off+len,
1597 "Going to read the number of variables");
1598 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1599 if (res)
1600 goto ERROR2;
1601 psiconv_debug(lev+2,off+len,"Number of variables: %d",listlen);
1602 len += leng;
1603
1604 psiconv_progress(lev+2,off+len,"Going to read all variables");
1605 for (i = 0; i < listlen; i++) {
1606 psiconv_progress(lev+3,off+len,"Going to read variable %d",i);
1607 if ((res = psiconv_parse_sheet_variable(buf,lev+3,off+len,&leng,&variable)))
1608 goto ERROR2;
1609 if ((res = psiconv_list_add(*result,variable)))
1610 goto ERROR3;
1611 len += leng;
1612 }
1613
1614 if (length)
1615 *length = len;
1616
1617 psiconv_progress(lev,off+len-1,
1618 "End of sheet variabels list (total length: %08x)", len);
1619 return 0;
1620
1621ERROR3:
1622 psiconv_free_sheet_variable(variable);
1623ERROR2:
1624 psiconv_list_free(*result);
1625ERROR1:
1626 psiconv_warn(lev+1,off,"Reading of Sheet Variable list failed");
1627 if (length)
1628 *length = 0;
1629 if (!res)
1630 return -PSICONV_E_NOMEM;
1631 else
1632 return res;
1633}

Legend:
Removed from v.110  
changed lines
  Added in v.131

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