/[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 130
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;
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 the flags byte");
696 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
697 if (res)
698 goto ERROR2;
699 (*result)->auto_recalc = temp & 0x01 ? psiconv_bool_true:psiconv_bool_false;
700 psiconv_debug(lev+2,off+len,"Auto recalculation: %02x",
701 (*result)->auto_recalc);
702 if ((temp & 0xfe) != 0x02) {
703 psiconv_warn(lev+2,off+len,"Sheet Info Section flags byte "
704 "contains unknown flags (ignored)");
705 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp &0xfe);
706 }
707
708 len ++;
709
710
711 if (length)
712 *length = len;
713
714 psiconv_progress(lev,off+len-1,
715 "End of sheet info section (total length: %08x)", len);
716 return 0;
717
718ERROR2:
719 free(*result);
720ERROR1:
721 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
722 if (length)
723 *length = 0;
724 if (!res)
725 return -PSICONV_E_NOMEM;
726 else
727 return res;
728}
729
271int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev, 730int psiconv_parse_sheet_formula_list(const psiconv_buffer buf, int lev,
272 psiconv_u32 off, int *length, 731 psiconv_u32 off, int *length,
273 psiconv_formula_list *result) 732 psiconv_formula_list *result)
274{ 733{
275 int res=0; 734 int res=0;
276 int len=0; 735 int len=0;
277 psiconv_u32 temp; 736 psiconv_u32 temp;
278 psiconv_formula formula; 737 psiconv_formula formula;
279 psiconv_u32 listlen,i; 738 psiconv_u32 listlen,i;
280 int leng; 739 int leng;
281 740
282 psiconv_progress(lev+1,off,"Going to read the sheet formula table"); 741 psiconv_progress(lev+1,off,"Going to read the sheet formula list");
283 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 742 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s))))
284 goto ERROR1; 743 goto ERROR1;
285 744
286 psiconv_progress(lev+2,off+len, 745 psiconv_progress(lev+2,off+len,
287 "Going to read the initial byte (%02x expected)",0x02); 746 "Going to read the initial byte (%02x expected)",0x02);
288 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 747 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
289 if (res) 748 if (res)
290 goto ERROR2; 749 goto ERROR2;
291 if (temp != 0x02) { 750 if (temp != 0x02) {
292 psiconv_warn(lev+2,off+len, 751 psiconv_warn(lev+2,off+len,
293 "Sheet formula table initial byte unknown value (ignored)"); 752 "Sheet formula list initial byte unknown value (ignored)");
294 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 753 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
295 } 754 }
296 len ++; 755 len ++;
297 756
298 psiconv_progress(lev+2,off+len, 757 psiconv_progress(lev+2,off+len,
315 774
316 if (length) 775 if (length)
317 *length = len; 776 *length = len;
318 777
319 psiconv_progress(lev,off+len-1, 778 psiconv_progress(lev,off+len-1,
320 "End of sheet formula table (total length: %08x)", len); 779 "End of sheet formula list (total length: %08x)", len);
321 return 0; 780 return 0;
322 781
323ERROR3: 782ERROR3:
324 psiconv_free_formula(formula); 783 psiconv_free_formula(formula);
325ERROR2: 784ERROR2:
326 psiconv_list_free(*result); 785 psiconv_list_free(*result);
327ERROR1: 786ERROR1:
328 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); 787 psiconv_warn(lev+1,off,"Reading of Sheet Formula list failed");
329 if (length) 788 if (length)
330 *length = 0; 789 *length = 0;
331 if (!res) 790 if (!res)
332 return -PSICONV_E_NOMEM; 791 return -PSICONV_E_NOMEM;
333 else 792 else
334 return res; 793 return res;
335} 794}
336 795
337int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, 796int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
338 psiconv_u32 off, int *length, 797 psiconv_u32 off, int *length,
339 psiconv_sheet_cell *result) 798 psiconv_sheet_cell *result,
799 const psiconv_sheet_cell_layout default_layout,
800 const psiconv_sheet_line_list row_default_layouts,
801 const psiconv_sheet_line_list col_default_layouts)
340{ 802{
341 int res=0; 803 int res=0;
342 int len=0; 804 int len=0;
343 psiconv_u32 temp; 805 psiconv_u32 temp;
344 psiconv_bool_t has_layout; 806 psiconv_bool_t has_layout;
346 808
347 psiconv_progress(lev+1,off,"Going to read a sheet cell structure"); 809 psiconv_progress(lev+1,off,"Going to read a sheet cell structure");
348 if (!(*result = malloc(sizeof(**result)))) 810 if (!(*result = malloc(sizeof(**result))))
349 goto ERROR1; 811 goto ERROR1;
350 812
813 (*result)->layout = NULL;
814 (*result)->type = psiconv_cell_blank;
815
351 psiconv_progress(lev+2,off+len,"Going to read the cell position"); 816 psiconv_progress(lev+2,off+len,"Going to read the cell position");
352 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 817 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
353 if (res) 818 if (res)
354 goto ERROR2; 819 goto ERROR2;
355 len ++; 820 len ++;
363 len ++; 828 len ++;
364 (*result)->column = (temp >> 2) & 0xFF; 829 (*result)->column = (temp >> 2) & 0xFF;
365 (*result)->row = (temp >> 10) & 0x3FFF; 830 (*result)->row = (temp >> 10) & 0x3FFF;
366 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x", 831 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x",
367 (*result)->column,(*result)->row); 832 (*result)->column,(*result)->row);
833 if (temp & 0x03) {
834 psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)");
835 psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03);
836 }
368 837
369 psiconv_progress(lev+2,off+len,"Going to read the cell type"); 838 psiconv_progress(lev+2,off+len,"Going to read the cell type");
370 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 839 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
371 if (res) 840 if (res)
372 goto ERROR2; 841 goto ERROR2;
386 len += 4; 855 len += 4;
387 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int); 856 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
388 857
389 } else if ((*result)->type == psiconv_cell_bool) { 858 } else if ((*result)->type == psiconv_cell_bool) {
390 psiconv_progress(lev+2,off+len,"Going to read a boolean"); 859 psiconv_progress(lev+2,off+len,"Going to read a boolean");
391 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 860 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
392 if (res) 861 &(*result)->data.dat_bool)))
393 goto ERROR2; 862 goto ERROR2;
394 len ++;
395 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp); 863 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp);
396 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false; 864 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
397 865 len += leng;
398 } else if ((*result)->type == psiconv_cell_error) { 866 } else if ((*result)->type == psiconv_cell_error) {
399 psiconv_progress(lev+2,off+len,"Going to read the error code"); 867 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); 868 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
401 if (res) 869 if (res)
402 goto ERROR2; 870 goto ERROR2;
403 len += 2; 871 if (temp == 0)
872 (*result)->data.dat_error = psiconv_sheet_error_none;
873 else if (temp == 1)
874 (*result)->data.dat_error = psiconv_sheet_error_null;
875 else if (temp == 2)
876 (*result)->data.dat_error = psiconv_sheet_error_divzero;
877 else if (temp == 3)
878 (*result)->data.dat_error = psiconv_sheet_error_value;
879 else if (temp == 4)
880 (*result)->data.dat_error = psiconv_sheet_error_reference;
881 else if (temp == 5)
882 (*result)->data.dat_error = psiconv_sheet_error_name;
883 else if (temp == 6)
884 (*result)->data.dat_error = psiconv_sheet_error_number;
885 else if (temp == 7)
886 (*result)->data.dat_error = psiconv_sheet_error_notavail;
887 else {
888 psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)");
889 psiconv_debug(lev+2,off+len,"Error code: %04x",temp);
890 (*result)->data.dat_error = psiconv_sheet_error_none;
891 }
404 psiconv_debug(lev+2,off+len,"Cell contents: %04x", 892 psiconv_debug(lev+2,off+len,"Cell contents: %04x",
405 (*result)->data.dat_error); 893 (*result)->data.dat_error);
406 894 len += 2;
407 } else if ((*result)->type == psiconv_cell_float) { 895 } else if ((*result)->type == psiconv_cell_float) {
408 psiconv_progress(lev+2,off+len,"Going to read a float"); 896 psiconv_progress(lev+2,off+len,"Going to read a float");
409 (*result)->data.dat_float = 897 (*result)->data.dat_float =
410 psiconv_read_float(buf,lev+2,off+len,&leng,&res); 898 psiconv_read_float(buf,lev+2,off+len,&leng,&res);
411 if (res) 899 if (res)
412 goto ERROR2; 900 goto ERROR2;
413 len += leng;
414 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float); 901 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
415 902 len += leng;
416 } else if ((*result)->type == psiconv_cell_string) { 903 } else if ((*result)->type == psiconv_cell_string) {
417 psiconv_progress(lev+2,off+len,"Going to read a string"); 904 psiconv_progress(lev+2,off+len,"Going to read a string");
418 (*result)->data.dat_string = 905 (*result)->data.dat_string =
419 psiconv_read_short_string(buf,lev+2,off+len,&leng,&res); 906 psiconv_read_string(buf,lev+2,off+len,&leng,&res);
420 if (res) 907 if (res)
421 goto ERROR2; 908 goto ERROR2;
422 len += leng;
423 psiconv_debug(lev+2,off+len,"Cell contents: `%s'", 909 psiconv_debug(lev+2,off+len,"Cell contents: `%s'",
424 (*result)->data.dat_string); 910 (*result)->data.dat_string);
911 len += leng;
425 } else { 912 } else {
426 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); 913 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
427 } 914 res = PSICONV_E_PARSE;
915 goto ERROR2;
428 916 }
917
918 if (!((*result)->layout = psiconv_clone_cell_layout(
919 psiconv_get_default_layout(row_default_layouts,
920 col_default_layouts,
921 default_layout,
922 (*result)->row,
923 (*result)->column))))
924 goto ERROR2;
429 if (has_layout) { 925 if (has_layout) {
430 psiconv_progress(lev+2,off+len,"Going to read the cell layout"); 926 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
431 927 &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; 928 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; 929 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 } 930 }
474 931
475 if ((*result)->calculated) { 932 if ((*result)->calculated) {
476 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference"); 933 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); 934 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
478 if (res) 935 if (res)
479 goto ERROR2; 936 goto ERROR2;
480 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp); 937 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp);
481 len += leng; 938 len += leng;
482 (*result)->ref_formula = temp; 939 (*result)->ref_formula = temp;
483 } 940 }
484 941
485 if (length) 942 if (length)
486 *length = len; 943 *length = len;
487 944
500 else 957 else
501 return res; 958 return res;
502} 959}
503 960
504int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, 961int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
505 psiconv_u32 off, int *length, 962 psiconv_u32 off, int *length,
506 psiconv_sheet_cell_list *result) 963 psiconv_sheet_cell_list *result,
964 const psiconv_sheet_cell_layout default_layout,
965 const psiconv_sheet_line_list row_default_layouts,
966 const psiconv_sheet_line_list col_default_layouts)
507{ 967{
508 int res=0; 968 int res=0;
509 int len=0; 969 int len=0;
510 psiconv_u32 temp; 970 psiconv_u32 temp;
511 psiconv_sheet_cell cell; 971 psiconv_sheet_cell cell;
512 psiconv_u32 listlen,i; 972 psiconv_u32 listlen,i;
513 int leng; 973 int leng;
514 974
515 psiconv_progress(lev+1,off,"Going to read the sheet cells list"); 975 psiconv_progress(lev+1,off,"Going to read the sheet cell list");
516 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s)))) 976 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
517 goto ERROR1; 977 goto ERROR1;
518 978
519 psiconv_progress(lev+2,off+len, 979 psiconv_progress(lev+2,off+len,
520 "Going to read the initial byte (%02x expected)",0x02); 980 "Going to read the initial byte (%02x expected)",0x02);
549 len += leng; 1009 len += leng;
550 1010
551 psiconv_progress(lev+2,off+len,"Going to read all cells"); 1011 psiconv_progress(lev+2,off+len,"Going to read all cells");
552 for (i = 0; i < listlen; i++) { 1012 for (i = 0; i < listlen; i++) {
553 psiconv_progress(lev+3,off+len,"Going to read cell %d",i); 1013 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))) 1014 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
1015 default_layout,row_default_layouts,
1016 col_default_layouts)))
555 goto ERROR2; 1017 goto ERROR2;
556 if ((res = psiconv_list_add(*result,cell))) 1018 if ((res = psiconv_list_add(*result,cell)))
557 goto ERROR3; 1019 goto ERROR3;
1020 free(cell);
558 len += leng; 1021 len += leng;
559 } 1022 }
560 1023
561 if (length) 1024 if (length)
562 *length = len; 1025 *length = len;
578 else 1041 else
579 return res; 1042 return res;
580} 1043}
581 1044
582 1045
583int psiconv_parse_sheet_worksheet_section(const psiconv_buffer buf, int lev, 1046int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
584 psiconv_u32 off, int *length, 1047 psiconv_u32 off, int *length,
585 psiconv_sheet_worksheet_section *result) 1048 psiconv_sheet_worksheet_list *result)
586{ 1049{
1050 psiconv_sheet_worksheet worksheet;
587 int res=0; 1051 int res=0;
588 psiconv_u32 temp,cells_off,grid_off;
589 int len=0; 1052 int len=0;
1053 psiconv_u8 temp;
1054 psiconv_u32 offset;
590 int leng; 1055 int leng,i,nr;
591 1056
592 /* TODO: this is the section that might be an XListE instead... */ 1057 psiconv_progress(lev+1,off,"Going to read the worksheet list");
1058 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
1059 goto ERROR1;
1060
593 psiconv_progress(lev+2,off+len, 1061 psiconv_progress(lev+2,off+len,
594 "Going to read the initial bytes (%02x expected)",0x02); 1062 "Going to read the initial bytes (%02x expected)",0x02);
595 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1063 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
596 if (res) 1064 if (res)
597 goto ERROR1; 1065 goto ERROR2;
598 if (temp != 0x04) { 1066 if (temp != 0x02) {
599 psiconv_warn(lev+2,off+len, 1067 psiconv_warn(lev+2,off+len,
600 "Sheet worksheet section initial byte unknown value (ignored)"); 1068 "Sheet worksheet list initial byte unknown value (ignored)");
601 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1069 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
602 } 1070 }
603 len ++; 1071 len ++;
604 1072
1073 psiconv_progress(lev+2,off+len,"Going to read the list length");
1074 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1075 if (res)
1076 goto ERROR2;
1077 psiconv_debug(lev+2,off+len,"Length: %02x",nr);
1078 len += leng;
1079
1080 psiconv_progress(lev+2,off+len,"Going to read the list");
1081 for (i=0 ; i < nr; i++) {
1082 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
605 psiconv_progress(lev+2,off+len, 1083 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); 1084 "Going to read the initial byte (%02x expected)",0x00);
619 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1085 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
620 if (res) 1086 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; 1087 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) { 1088 if (temp != 0x00) {
1089 psiconv_warn(lev+4,off+len,
1090 "Sheet worksheet element initial byte unknown value (ignored)");
1091 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp);
1092 }
1093 len ++;
1094
699 psiconv_progress(lev+3,off+len,"Going to read the default character codes"); 1095 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); 1096 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
711 if (res) 1097 if (res)
712 goto ERROR2_3; 1098 goto ERROR2;
1099 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
713 len ++; 1100 len += 4;
714 psiconv_read_u8(buf,lev+2,off+len,&res); 1101
715 if (res) 1102 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
1103 &worksheet)))
716 goto ERROR2_3; 1104 goto ERROR2;
717 len ++; 1105 if ((res = psiconv_list_add(*result,worksheet)))
718 psiconv_read_u8(buf,lev+2,off+len,&res);
719 if (res)
720 goto ERROR2_3; 1106 goto ERROR3;
721 len ++; 1107 free(worksheet);
722 }
723 1108 }
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 1109
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) 1110 if (length)
773 *length = len; 1111 *length = len;
774 1112
775 psiconv_progress(lev,off+len-1, 1113 psiconv_progress(lev,off+len-1,
776 "End of sheet worksheet section (total length: %08x)", len); 1114 "End of worksheet list (total length: %08x)", len);
1115
777 return 0; 1116 return 0;
778 1117
779ERROR2_3: 1118ERROR3:
780 psiconv_free_numberformat((*result)->numberformat); 1119 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: 1120ERROR2:
788 free (*result); 1121 psiconv_free_sheet_worksheet_list(*result);
789ERROR1: 1122ERROR1:
790 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed"); 1123 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
791 if (length) 1124 if (length)
792 *length = 0; 1125 *length = 0;
793 if (!res) 1126 if (!res)
794 return -PSICONV_E_NOMEM; 1127 return -PSICONV_E_NOMEM;
795 else 1128 else
796 return res; 1129 return res;
797} 1130}
798 1131
1132int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
1133 psiconv_u32 off, int *length,
1134 psiconv_sheet_cell_layout result)
799 1135
1136{
1137 int res=0;
1138 int len=0;
1139 int leng;
1140 psiconv_u8 temp;
1141
1142 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
1143
1144 psiconv_progress(lev+2,off+len,
1145 "Going to read the first byte (%02x expected)",0x02);
1146 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1147 if (res)
1148 goto ERROR1;
1149 if (temp != 0x02) {
1150 psiconv_warn(lev+2,off+len,
1151 "Worksheet section initial byte unknown value (ignored)");
1152 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1153 }
1154 len ++;
1155
1156 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
1157 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1158 if (res)
1159 goto ERROR1;
1160 len ++;
1161
1162 if (temp & 0x01) {
1163 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
1164 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
1165 result->paragraph)))
1166 goto ERROR1;
1167 len += leng;
1168 }
1169
1170 if (temp & 0x02) {
1171 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
1172 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
1173 result->character)))
1174 goto ERROR1;
1175 len += leng;
1176 }
1177
1178 if (temp & 0x04) {
1179 psiconv_progress(lev+3,off+len, "Going to read the default number format");
1180 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
1181 result->numberformat);
1182 len += leng;
1183 }
1184
1185 if (length)
1186 *length = len;
1187
1188 psiconv_progress(lev,off+len-1,
1189 "End of sheet cell layout (total length: %08x)", len);
1190
1191 return 0;
1192
1193ERROR1:
1194 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
1195 if (length)
1196 *length = 0;
1197 if (!res)
1198 return -PSICONV_E_NOMEM;
1199 else
1200 return res;
1201}
1202
1203
1204int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
1205 psiconv_u32 off, int *length,
1206 psiconv_sheet_worksheet *result)
1207{
1208 int res=0;
1209 psiconv_u32 temp,cells_off,grid_off,rows_off,cols_off;
1210 int len=0;
1211 int leng;
1212
1213 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
1214 if (!(*result = malloc(sizeof(**result))))
1215 goto ERROR1;
1216
1217 psiconv_progress(lev+2,off+len,
1218 "Going to read the initial bytes (%02x expected)",0x04);
1219 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1220 if (res)
1221 goto ERROR2;
1222 if (temp != 0x04) {
1223 psiconv_warn(lev+2,off+len,
1224 "Worksheet section initial byte unknown value (ignored)");
1225 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1226 }
1227 len ++;
1228
1229 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
1230 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1231 if (res)
1232 goto ERROR2;
1233 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp);
1234 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
1235 if (temp & 0xfe) {
1236 psiconv_warn(lev+2,off+len,
1237 "Worksheet section flags byte unknown bits (ignored)");
1238 }
1239 len ++;
1240
1241 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
1242 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
1243 goto ERROR2;
1244 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
1245 (*result)->default_layout)))
1246 goto ERROR3;
1247 len += leng;
1248
1249 psiconv_progress(lev+2,off+len,
1250 "Going to read the offset of the row defaults Section");
1251 rows_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1252 if (res)
1253 goto ERROR3;
1254 psiconv_debug(lev+2,off+len,"Offset: %04x",rows_off);
1255 len += 4;
1256
1257 psiconv_progress(lev+2,off+len,
1258 "Going to read the offset of the column defaults Section");
1259 cols_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1260 if (res)
1261 goto ERROR3;
1262 psiconv_debug(lev+2,off+len,"Offset: %04x",cols_off);
1263 len += 4;
1264
1265 psiconv_progress(lev+2,off+len,
1266 "Going to read the offset of the Cells List");
1267 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1268 if (res)
1269 goto ERROR3;
1270 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
1271 len += 4;
1272
1273 psiconv_progress(lev+2,off+len,
1274 "Going to read the offset of the Grid Section");
1275 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1276 if (res)
1277 goto ERROR3;
1278 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
1279 len += 4;
1280
1281 psiconv_progress(lev+2,off+len,
1282 "Going to read the offset of the 3rd ??? Section");
1283 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
1284 if (res)
1285 goto ERROR3;
1286 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
1287 len += 4;
1288
1289 psiconv_progress(lev+2,off+len,"Going to read the row defaults");
1290 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,rows_off,NULL,
1291 &(*result)->row_default_layouts,
1292 (*result)->default_layout)))
1293 goto ERROR3;
1294
1295 psiconv_progress(lev+2,off+len,"Going to read the column defaults");
1296 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,cols_off,NULL,
1297 &(*result)->col_default_layouts,
1298 (*result)->default_layout)))
1299 goto ERROR4;
1300
1301 psiconv_progress(lev+2,off+len,"Going to read the cells list");
1302 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
1303 &(*result)->cells,
1304 (*result)->default_layout,
1305 (*result)->row_default_layouts,
1306 (*result)->col_default_layouts)))
1307 goto ERROR5;
1308
1309
1310/* TODO: parse grid section */
1311
1312 if (length)
1313 *length = len;
1314
1315 psiconv_progress(lev,off+len-1,
1316 "End of sheet worksheet section (total length: %08x)", len);
1317 return 0;
1318
1319ERROR5:
1320 psiconv_free_sheet_line_list((*result)->col_default_layouts);
1321ERROR4:
1322 psiconv_free_sheet_line_list((*result)->row_default_layouts);
1323ERROR3:
1324 psiconv_free_sheet_cell_layout((*result)->default_layout);
1325ERROR2:
1326 free (*result);
1327ERROR1:
1328 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed");
1329 if (length)
1330 *length = 0;
1331 if (!res)
1332 return -PSICONV_E_NOMEM;
1333 else
1334 return res;
1335}
1336
1337int psiconv_parse_sheet_line(const psiconv_buffer buf, int lev,
1338 psiconv_u32 off, int *length,
1339 psiconv_sheet_line *result,
1340 const psiconv_sheet_cell_layout default_layout)
1341{
1342 int res=0;
1343 int len=0;
1344 int leng;
1345
1346
1347 psiconv_progress(lev+1,off,"Going to read a sheet line");
1348 if (!(*result = malloc(sizeof(**result))))
1349 goto ERROR1;
1350
1351 psiconv_progress(lev+2,off+len,"Going to read the line number");
1352 (*result)->position = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1353 if (res)
1354 goto ERROR2;
1355 psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->position);
1356 len += leng;
1357
1358 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
1359 goto ERROR2;
1360 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
1361 &leng,(*result)->layout)))
1362 goto ERROR3;
1363 len += leng;
1364
1365 if (length)
1366 *length = len;
1367
1368 psiconv_progress(lev,off+len-1,
1369 "End of the sheet line (total length: %08x)", len);
1370 return 0;
1371
1372ERROR3:
1373 psiconv_free_sheet_cell_layout((*result)->layout);
1374ERROR2:
1375 free (*result);
1376ERROR1:
1377 psiconv_warn(lev+1,off,"Reading of the sheet line failed");
1378 if (length)
1379 *length = 0;
1380 if (!res)
1381 return -PSICONV_E_NOMEM;
1382 else
1383 return res;
1384}
1385
1386
1387int psiconv_parse_sheet_line_list(const psiconv_buffer buf, int lev,
1388 psiconv_u32 off, int *length,
1389 psiconv_sheet_line_list *result,
1390 const psiconv_sheet_cell_layout default_layout)
1391{
1392 int res=0;
1393 int len=0;
1394 psiconv_u32 temp;
1395 psiconv_sheet_line line;
1396 psiconv_u32 listlen,i;
1397 int leng;
1398
1399 psiconv_progress(lev+1,off,"Going to read the sheet line list");
1400 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_line_s))))
1401 goto ERROR1;
1402
1403 psiconv_progress(lev+2,off+len,
1404 "Going to read the initial byte (%02x expected)",0x02);
1405 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1406 if (res)
1407 goto ERROR2;
1408 if (temp != 0x02) {
1409 psiconv_warn(lev+2,off+len,
1410 "Sheet line list initial byte unknown value (ignored)");
1411 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1412 }
1413 len ++;
1414
1415 psiconv_progress(lev+2,off+len,
1416 "Going to read the number of defined lines");
1417 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1418 if (res)
1419 goto ERROR2;
1420 psiconv_debug(lev+2,off+len,"Number of defined lines: %d",listlen);
1421 len += leng;
1422
1423 psiconv_progress(lev+2,off+len,"Going to read all lines");
1424 for (i = 0; i < listlen; i++) {
1425 psiconv_progress(lev+3,off+len,"Going to read line %d",i);
1426 if ((res = psiconv_parse_sheet_line(buf,lev+3,off+len,&leng,&line,
1427 default_layout)))
1428 goto ERROR2;
1429 if ((res = psiconv_list_add(*result,line)))
1430 goto ERROR3;
1431 free(line);
1432 len += leng;
1433 }
1434
1435 if (length)
1436 *length = len;
1437
1438 psiconv_progress(lev,off+len-1,
1439 "End of sheet line list (total length: %08x)", len);
1440 return 0;
1441
1442ERROR3:
1443 psiconv_free_sheet_line(line);
1444ERROR2:
1445 psiconv_free_sheet_line_list(*result);
1446ERROR1:
1447 psiconv_warn(lev+1,off,"Reading of Sheet Line List failed");
1448 if (length)
1449 *length = 0;
1450 if (!res)
1451 return -PSICONV_E_NOMEM;
1452 else
1453 return res;
1454}
1455
1456int psiconv_parse_sheet_variable(const psiconv_buffer buf, int lev,
1457 psiconv_u32 off, int *length,
1458 psiconv_sheet_variable *result)
1459{
1460 int res=0;
1461 int len=0;
1462 psiconv_u32 marker;
1463 int leng;
1464
1465 psiconv_progress(lev+1,off,"Going to read a sheet variable");
1466 if (!(*result = malloc(sizeof(**result))))
1467 goto ERROR1;
1468
1469 psiconv_progress(lev+2,off+len, "Going to read the variable name");
1470 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
1471 if (res)
1472 goto ERROR2;
1473 len += leng;
1474
1475 psiconv_progress(lev+2,off+len,"Going to read the type marker");
1476 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
1477 if (res)
1478 goto ERROR3;
1479 psiconv_debug(lev+2,off+len,"Marker: %02x",marker);
1480 len ++;
1481
1482 if (marker == 0x00) {
1483 (*result)->type = psiconv_var_int;
1484 psiconv_progress(lev+2,off+len,"Going to read a signed integer");
1485 (*result)->data.dat_int = psiconv_read_sint(buf,lev+2,off+len,&leng,&res);
1486 if (res)
1487 goto ERROR3;
1488 psiconv_debug(lev+2,off+len,"Value: %d",(*result)->data.dat_int);
1489 len += leng;
1490 } else if (marker == 0x01) {
1491 (*result)->type = psiconv_var_float;
1492 psiconv_progress(lev+2,off+len,"Going to read a floating point number");
1493 (*result)->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng,
1494 &res);
1495 if (res)
1496 goto ERROR3;
1497 psiconv_debug(lev+2,off+len,"Value: %f",(*result)->data.dat_float);
1498 len += leng;
1499 } else if (marker == 0x02) {
1500 (*result)->type = psiconv_var_string;
1501 psiconv_progress(lev+2,off+len,"Going to read a string");
1502 (*result)->data.dat_string = psiconv_read_string(buf,lev+2,off+len,&leng,
1503 &res);
1504 if (res)
1505 goto ERROR3;
1506 len += leng;
1507 } else if (marker == 0x03) {
1508 (*result)->type = psiconv_var_cellref;
1509 psiconv_progress(lev+2,off+len,"Going to read a cell reference");
1510 (*result)->data.dat_cellref = psiconv_read_var_cellref(buf,lev+2,off+len,
1511 &leng, &res);
1512 if (res)
1513 goto ERROR3;
1514 len += leng;
1515 } else if (marker == 0x04) {
1516 (*result)->type = psiconv_var_cellblock;
1517 psiconv_progress(lev+2,off+len,"Going to read a cell block reference");
1518 (*result)->data.dat_cellblock = psiconv_read_var_cellblock(buf,lev+2,
1519 off+len,
1520 &leng, &res);
1521 if (res)
1522 goto ERROR3;
1523 len += leng;
1524 } else {
1525 psiconv_warn(lev+2,off+len,"Sheet variable unknown type marker");
1526 res = -PSICONV_E_PARSE;
1527 goto ERROR3;
1528 }
1529
1530 psiconv_progress(lev+2,off+len,"Going to read the variable number");
1531 (*result)->number = psiconv_read_u32(buf,lev+2,off+len,&res);
1532 if (res)
1533 goto ERROR4;
1534 psiconv_debug(lev+2,off+len,"Number: %08x",(*result)->number);
1535 len += 4;
1536
1537 if (length)
1538 *length = len;
1539
1540 psiconv_progress(lev,off+len-1,
1541 "End of sheet variable (total length: %08x)", len);
1542 return 0;
1543
1544ERROR4:
1545 if ((*result)->type == psiconv_var_string)
1546 free((*result)->data.dat_string);
1547ERROR3:
1548 free((*result)->name);
1549ERROR2:
1550 free (*result);
1551ERROR1:
1552 psiconv_warn(lev+1,off,"Reading of Sheet Variable failed");
1553 if (length)
1554 *length = 0;
1555 if (!res)
1556 return -PSICONV_E_NOMEM;
1557 else
1558 return res;
1559}
1560
1561
1562int psiconv_parse_sheet_variable_list(const psiconv_buffer buf, int lev,
1563 psiconv_u32 off, int *length,
1564 psiconv_sheet_variable_list *result)
1565{
1566 int res=0;
1567 int len=0;
1568 psiconv_u32 temp;
1569 psiconv_sheet_variable variable;
1570 psiconv_u32 listlen,i;
1571 int leng;
1572
1573 psiconv_progress(lev+1,off,"Going to read the sheet variable list");
1574 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_variable_s))))
1575 goto ERROR1;
1576
1577 psiconv_progress(lev+2,off+len,
1578 "Going to read the initial byte (%02x expected)",0x02);
1579 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1580 if (res)
1581 goto ERROR2;
1582 if (temp != 0x02) {
1583 psiconv_warn(lev+2,off+len,
1584 "Sheet variable list initial byte unknown value (ignored)");
1585 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1586 }
1587 len ++;
1588
1589 psiconv_progress(lev+2,off+len,
1590 "Going to read the number of variables");
1591 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1592 if (res)
1593 goto ERROR2;
1594 psiconv_debug(lev+2,off+len,"Number of variables: %d",listlen);
1595 len += leng;
1596
1597 psiconv_progress(lev+2,off+len,"Going to read all variables");
1598 for (i = 0; i < listlen; i++) {
1599 psiconv_progress(lev+3,off+len,"Going to read variable %d",i);
1600 if ((res = psiconv_parse_sheet_variable(buf,lev+3,off+len,&leng,&variable)))
1601 goto ERROR2;
1602 if ((res = psiconv_list_add(*result,variable)))
1603 goto ERROR3;
1604 len += leng;
1605 }
1606
1607 if (length)
1608 *length = len;
1609
1610 psiconv_progress(lev,off+len-1,
1611 "End of sheet variabels list (total length: %08x)", len);
1612 return 0;
1613
1614ERROR3:
1615 psiconv_free_sheet_variable(variable);
1616ERROR2:
1617 psiconv_list_free(*result);
1618ERROR1:
1619 psiconv_warn(lev+1,off,"Reading of Sheet Variable list failed");
1620 if (length)
1621 *length = 0;
1622 if (!res)
1623 return -PSICONV_E_NOMEM;
1624 else
1625 return res;
1626}

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

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