/[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 134
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;
545 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off);
546 len += 4;
547
548 psiconv_progress(lev+2,off+len,
549 "Going to read the offset of the Variable List");
550 var_off = psiconv_read_u32(buf,lev+2,off+len,&res);
551 if (res)
552 goto ERROR2;
230 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 553 psiconv_debug(lev+2,off+len,"Offset: %04x",var_off);
231 len += 4; 554 len += 4;
232 555
556 if (with_name) {
233 psiconv_progress(lev+2,off+len, 557 psiconv_progress(lev+2,off+len,
234 "Going to read the offset of the 4th ??? Section"); 558 "Going to read the offset of the Name Section");
235 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 559 name_off = psiconv_read_u32(buf,lev+2,off+len,&res);
236 if (res) 560 if (res)
237 goto ERROR2; 561 goto ERROR2;
238 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 562 psiconv_debug(lev+2,off+len,"Offset: %04x",name_off);
239 len += 4; 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;
240 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,
308 psiconv_progress(lev+3,off+len,"Going to read formula %d",i); 774 psiconv_progress(lev+3,off+len,"Going to read formula %d",i);
309 if ((res = psiconv_parse_formula(buf,lev+3,off+len,&leng,&formula))) 775 if ((res = psiconv_parse_formula(buf,lev+3,off+len,&leng,&formula)))
310 goto ERROR2; 776 goto ERROR2;
311 if ((res = psiconv_list_add(*result,formula))) 777 if ((res = psiconv_list_add(*result,formula)))
312 goto ERROR3; 778 goto ERROR3;
779 free(formula);
313 len += leng; 780 len += leng;
314 } 781 }
315 782
316 if (length) 783 if (length)
317 *length = len; 784 *length = len;
318 785
319 psiconv_progress(lev,off+len-1, 786 psiconv_progress(lev,off+len-1,
320 "End of sheet formula table (total length: %08x)", len); 787 "End of sheet formula list (total length: %08x)", len);
321 return 0; 788 return 0;
322 789
323ERROR3: 790ERROR3:
324 psiconv_free_formula(formula); 791 psiconv_free_formula(formula);
325ERROR2: 792ERROR2:
326 psiconv_list_free(*result); 793 psiconv_list_free(*result);
327ERROR1: 794ERROR1:
328 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); 795 psiconv_warn(lev+1,off,"Reading of Sheet Formula list failed");
329 if (length) 796 if (length)
330 *length = 0; 797 *length = 0;
331 if (!res) 798 if (!res)
332 return -PSICONV_E_NOMEM; 799 return -PSICONV_E_NOMEM;
333 else 800 else
334 return res; 801 return res;
335} 802}
336 803
337int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, 804int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
338 psiconv_u32 off, int *length, 805 psiconv_u32 off, int *length,
339 psiconv_sheet_cell *result) 806 psiconv_sheet_cell *result,
807 const psiconv_sheet_cell_layout default_layout,
808 const psiconv_sheet_line_list row_default_layouts,
809 const psiconv_sheet_line_list col_default_layouts)
340{ 810{
341 int res=0; 811 int res=0;
342 int len=0; 812 int len=0;
343 psiconv_u32 temp; 813 psiconv_u32 temp;
344 psiconv_bool_t has_layout; 814 psiconv_bool_t has_layout;
346 816
347 psiconv_progress(lev+1,off,"Going to read a sheet cell structure"); 817 psiconv_progress(lev+1,off,"Going to read a sheet cell structure");
348 if (!(*result = malloc(sizeof(**result)))) 818 if (!(*result = malloc(sizeof(**result))))
349 goto ERROR1; 819 goto ERROR1;
350 820
821 (*result)->layout = NULL;
822 (*result)->type = psiconv_cell_blank;
823
351 psiconv_progress(lev+2,off+len,"Going to read the cell position"); 824 psiconv_progress(lev+2,off+len,"Going to read the cell position");
352 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 825 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
353 if (res) 826 if (res)
354 goto ERROR2; 827 goto ERROR2;
355 len ++; 828 len ++;
363 len ++; 836 len ++;
364 (*result)->column = (temp >> 2) & 0xFF; 837 (*result)->column = (temp >> 2) & 0xFF;
365 (*result)->row = (temp >> 10) & 0x3FFF; 838 (*result)->row = (temp >> 10) & 0x3FFF;
366 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x", 839 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x",
367 (*result)->column,(*result)->row); 840 (*result)->column,(*result)->row);
841 if (temp & 0x03) {
842 psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)");
843 psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03);
844 }
368 845
369 psiconv_progress(lev+2,off+len,"Going to read the cell type"); 846 psiconv_progress(lev+2,off+len,"Going to read the cell type");
370 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 847 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
371 if (res) 848 if (res)
372 goto ERROR2; 849 goto ERROR2;
386 len += 4; 863 len += 4;
387 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int); 864 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
388 865
389 } else if ((*result)->type == psiconv_cell_bool) { 866 } else if ((*result)->type == psiconv_cell_bool) {
390 psiconv_progress(lev+2,off+len,"Going to read a boolean"); 867 psiconv_progress(lev+2,off+len,"Going to read a boolean");
391 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 868 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
392 if (res) 869 &(*result)->data.dat_bool)))
393 goto ERROR2; 870 goto ERROR2;
394 len ++;
395 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp); 871 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp);
396 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false; 872 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
397 873 len += leng;
398 } else if ((*result)->type == psiconv_cell_error) { 874 } else if ((*result)->type == psiconv_cell_error) {
399 psiconv_progress(lev+2,off+len,"Going to read the error code"); 875 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); 876 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
401 if (res) 877 if (res)
402 goto ERROR2; 878 goto ERROR2;
403 len += 2; 879 if (temp == 0)
880 (*result)->data.dat_error = psiconv_sheet_error_none;
881 else if (temp == 1)
882 (*result)->data.dat_error = psiconv_sheet_error_null;
883 else if (temp == 2)
884 (*result)->data.dat_error = psiconv_sheet_error_divzero;
885 else if (temp == 3)
886 (*result)->data.dat_error = psiconv_sheet_error_value;
887 else if (temp == 4)
888 (*result)->data.dat_error = psiconv_sheet_error_reference;
889 else if (temp == 5)
890 (*result)->data.dat_error = psiconv_sheet_error_name;
891 else if (temp == 6)
892 (*result)->data.dat_error = psiconv_sheet_error_number;
893 else if (temp == 7)
894 (*result)->data.dat_error = psiconv_sheet_error_notavail;
895 else {
896 psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)");
897 psiconv_debug(lev+2,off+len,"Error code: %04x",temp);
898 (*result)->data.dat_error = psiconv_sheet_error_none;
899 }
404 psiconv_debug(lev+2,off+len,"Cell contents: %04x", 900 psiconv_debug(lev+2,off+len,"Cell contents: %04x",
405 (*result)->data.dat_error); 901 (*result)->data.dat_error);
406 902 len += 2;
407 } else if ((*result)->type == psiconv_cell_float) { 903 } else if ((*result)->type == psiconv_cell_float) {
408 psiconv_progress(lev+2,off+len,"Going to read a float"); 904 psiconv_progress(lev+2,off+len,"Going to read a float");
409 (*result)->data.dat_float = 905 (*result)->data.dat_float =
410 psiconv_read_float(buf,lev+2,off+len,&leng,&res); 906 psiconv_read_float(buf,lev+2,off+len,&leng,&res);
411 if (res) 907 if (res)
412 goto ERROR2; 908 goto ERROR2;
413 len += leng;
414 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float); 909 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
415 910 len += leng;
416 } else if ((*result)->type == psiconv_cell_string) { 911 } else if ((*result)->type == psiconv_cell_string) {
417 psiconv_progress(lev+2,off+len,"Going to read a string"); 912 psiconv_progress(lev+2,off+len,"Going to read a string");
418 (*result)->data.dat_string = 913 (*result)->data.dat_string =
419 psiconv_read_short_string(buf,lev+2,off+len,&leng,&res); 914 psiconv_read_string(buf,lev+2,off+len,&leng,&res);
420 if (res) 915 if (res)
421 goto ERROR2; 916 goto ERROR2;
422 len += leng;
423 psiconv_debug(lev+2,off+len,"Cell contents: `%s'", 917 psiconv_debug(lev+2,off+len,"Cell contents: `%s'",
424 (*result)->data.dat_string); 918 (*result)->data.dat_string);
919 len += leng;
425 } else { 920 } else {
426 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); 921 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
427 } 922 res = PSICONV_E_PARSE;
923 goto ERROR2;
428 924 }
925
926 if (!((*result)->layout = psiconv_clone_cell_layout(
927 psiconv_get_default_layout(row_default_layouts,
928 col_default_layouts,
929 default_layout,
930 (*result)->row,
931 (*result)->column))))
932 goto ERROR2;
429 if (has_layout) { 933 if (has_layout) {
430 psiconv_progress(lev+2,off+len,"Going to read the cell layout"); 934 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
431 935 &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; 936 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; 937 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 } 938 }
474 939
475 if ((*result)->calculated) { 940 if ((*result)->calculated) {
476 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference"); 941 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); 942 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
478 if (res) 943 if (res)
479 goto ERROR2; 944 goto ERROR2;
480 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp); 945 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp);
481 len += leng; 946 len += leng;
482 (*result)->ref_formula = temp; 947 (*result)->ref_formula = temp;
483 } 948 }
484 949
485 if (length) 950 if (length)
486 *length = len; 951 *length = len;
487 952
500 else 965 else
501 return res; 966 return res;
502} 967}
503 968
504int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, 969int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
505 psiconv_u32 off, int *length, 970 psiconv_u32 off, int *length,
506 psiconv_sheet_cell_list *result) 971 psiconv_sheet_cell_list *result,
972 const psiconv_sheet_cell_layout default_layout,
973 const psiconv_sheet_line_list row_default_layouts,
974 const psiconv_sheet_line_list col_default_layouts)
507{ 975{
508 int res=0; 976 int res=0;
509 int len=0; 977 int len=0;
510 psiconv_u32 temp; 978 psiconv_u32 temp;
511 psiconv_sheet_cell cell; 979 psiconv_sheet_cell cell;
512 psiconv_u32 listlen,i; 980 psiconv_u32 listlen,i;
513 int leng; 981 int leng;
514 982
515 psiconv_progress(lev+1,off,"Going to read the sheet cells list"); 983 psiconv_progress(lev+1,off,"Going to read the sheet cell list");
516 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s)))) 984 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
517 goto ERROR1; 985 goto ERROR1;
518 986
519 psiconv_progress(lev+2,off+len, 987 psiconv_progress(lev+2,off+len,
520 "Going to read the initial byte (%02x expected)",0x02); 988 "Going to read the initial byte (%02x expected)",0x02);
549 len += leng; 1017 len += leng;
550 1018
551 psiconv_progress(lev+2,off+len,"Going to read all cells"); 1019 psiconv_progress(lev+2,off+len,"Going to read all cells");
552 for (i = 0; i < listlen; i++) { 1020 for (i = 0; i < listlen; i++) {
553 psiconv_progress(lev+3,off+len,"Going to read cell %d",i); 1021 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))) 1022 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
1023 default_layout,row_default_layouts,
1024 col_default_layouts)))
555 goto ERROR2; 1025 goto ERROR2;
556 if ((res = psiconv_list_add(*result,cell))) 1026 if ((res = psiconv_list_add(*result,cell)))
557 goto ERROR3; 1027 goto ERROR3;
1028 free(cell);
558 len += leng; 1029 len += leng;
559 } 1030 }
560 1031
561 if (length) 1032 if (length)
562 *length = len; 1033 *length = len;
578 else 1049 else
579 return res; 1050 return res;
580} 1051}
581 1052
582 1053
583int psiconv_parse_sheet_worksheet_section(const psiconv_buffer buf, int lev, 1054int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
584 psiconv_u32 off, int *length, 1055 psiconv_u32 off, int *length,
585 psiconv_sheet_worksheet_section *result) 1056 psiconv_sheet_worksheet_list *result)
586{ 1057{
1058 psiconv_sheet_worksheet worksheet;
587 int res=0; 1059 int res=0;
588 psiconv_u32 temp,cells_off,grid_off;
589 int len=0; 1060 int len=0;
1061 psiconv_u8 temp;
1062 psiconv_u32 offset;
590 int leng; 1063 int leng,i,nr;
591 1064
592 /* TODO: this is the section that might be an XListE instead... */ 1065 psiconv_progress(lev+1,off,"Going to read the worksheet list");
1066 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
1067 goto ERROR1;
1068
593 psiconv_progress(lev+2,off+len, 1069 psiconv_progress(lev+2,off+len,
594 "Going to read the initial bytes (%02x expected)",0x02); 1070 "Going to read the initial bytes (%02x expected)",0x02);
595 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1071 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
596 if (res) 1072 if (res)
597 goto ERROR1; 1073 goto ERROR2;
598 if (temp != 0x04) { 1074 if (temp != 0x02) {
599 psiconv_warn(lev+2,off+len, 1075 psiconv_warn(lev+2,off+len,
600 "Sheet worksheet section initial byte unknown value (ignored)"); 1076 "Sheet worksheet list initial byte unknown value (ignored)");
601 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1077 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
602 } 1078 }
603 len ++; 1079 len ++;
604 1080
1081 psiconv_progress(lev+2,off+len,"Going to read the list length");
1082 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1083 if (res)
1084 goto ERROR2;
1085 psiconv_debug(lev+2,off+len,"Length: %02x",nr);
1086 len += leng;
1087
1088 psiconv_progress(lev+2,off+len,"Going to read the list");
1089 for (i=0 ; i < nr; i++) {
1090 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
605 psiconv_progress(lev+2,off+len, 1091 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); 1092 "Going to read the initial byte (%02x expected)",0x00);
619 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1093 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
620 if (res) 1094 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; 1095 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) { 1096 if (temp != 0x00) {
1097 psiconv_warn(lev+4,off+len,
1098 "Sheet worksheet element initial byte unknown value (ignored)");
1099 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp);
1100 }
1101 len ++;
1102
699 psiconv_progress(lev+3,off+len,"Going to read the default character codes"); 1103 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); 1104 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
711 if (res) 1105 if (res)
712 goto ERROR2_3; 1106 goto ERROR2;
1107 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
713 len ++; 1108 len += 4;
714 psiconv_read_u8(buf,lev+2,off+len,&res); 1109
715 if (res) 1110 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
1111 &worksheet)))
716 goto ERROR2_3; 1112 goto ERROR2;
717 len ++; 1113 if ((res = psiconv_list_add(*result,worksheet)))
718 psiconv_read_u8(buf,lev+2,off+len,&res);
719 if (res)
720 goto ERROR2_3; 1114 goto ERROR3;
721 len ++; 1115 free(worksheet);
722 }
723 1116 }
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 1117
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) 1118 if (length)
773 *length = len; 1119 *length = len;
774 1120
775 psiconv_progress(lev,off+len-1, 1121 psiconv_progress(lev,off+len-1,
776 "End of sheet worksheet section (total length: %08x)", len); 1122 "End of worksheet list (total length: %08x)", len);
1123
777 return 0; 1124 return 0;
778 1125
779ERROR2_3: 1126ERROR3:
780 psiconv_free_numberformat((*result)->numberformat); 1127 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: 1128ERROR2:
788 free (*result); 1129 psiconv_free_sheet_worksheet_list(*result);
789ERROR1: 1130ERROR1:
790 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed"); 1131 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
791 if (length) 1132 if (length)
792 *length = 0; 1133 *length = 0;
793 if (!res) 1134 if (!res)
794 return -PSICONV_E_NOMEM; 1135 return -PSICONV_E_NOMEM;
795 else 1136 else
796 return res; 1137 return res;
797} 1138}
798 1139
1140int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
1141 psiconv_u32 off, int *length,
1142 psiconv_sheet_cell_layout result)
799 1143
1144{
1145 int res=0;
1146 int len=0;
1147 int leng;
1148 psiconv_u8 temp;
1149
1150 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
1151
1152 psiconv_progress(lev+2,off+len,
1153 "Going to read the first byte (%02x expected)",0x02);
1154 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1155 if (res)
1156 goto ERROR1;
1157 if (temp != 0x02) {
1158 psiconv_warn(lev+2,off+len,
1159 "Worksheet section initial byte unknown value (ignored)");
1160 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1161 }
1162 len ++;
1163
1164 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
1165 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1166 if (res)
1167 goto ERROR1;
1168 len ++;
1169
1170 if (temp & 0x01) {
1171 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
1172 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
1173 result->paragraph)))
1174 goto ERROR1;
1175 len += leng;
1176 }
1177
1178 if (temp & 0x02) {
1179 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
1180 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
1181 result->character)))
1182 goto ERROR1;
1183 len += leng;
1184 }
1185
1186 if (temp & 0x04) {
1187 psiconv_progress(lev+3,off+len, "Going to read the default number format");
1188 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
1189 result->numberformat);
1190 len += leng;
1191 }
1192
1193 if (length)
1194 *length = len;
1195
1196 psiconv_progress(lev,off+len-1,
1197 "End of sheet cell layout (total length: %08x)", len);
1198
1199 return 0;
1200
1201ERROR1:
1202 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
1203 if (length)
1204 *length = 0;
1205 if (!res)
1206 return -PSICONV_E_NOMEM;
1207 else
1208 return res;
1209}
1210
1211
1212int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
1213 psiconv_u32 off, int *length,
1214 psiconv_sheet_worksheet *result)
1215{
1216 int res=0;
1217 psiconv_u32 temp,cells_off,grid_off,rows_off,cols_off,unknown_off;
1218 int len=0;
1219 int leng;
1220
1221 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
1222 if (!(*result = malloc(sizeof(**result))))
1223 goto ERROR1;
1224
1225 psiconv_progress(lev+2,off+len,
1226 "Going to read the initial bytes (%02x expected)",0x04);
1227 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1228 if (res)
1229 goto ERROR2;
1230 if (temp != 0x04) {
1231 psiconv_warn(lev+2,off+len,
1232 "Worksheet section initial byte unknown value (ignored)");
1233 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1234 }
1235 len ++;
1236
1237 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
1238 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1239 if (res)
1240 goto ERROR2;
1241 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp);
1242 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
1243 if (temp & 0xfe) {
1244 psiconv_warn(lev+2,off+len,
1245 "Worksheet section flags byte unknown bits (ignored)");
1246 }
1247 len ++;
1248
1249 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
1250 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
1251 goto ERROR2;
1252 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
1253 (*result)->default_layout)))
1254 goto ERROR3;
1255 len += leng;
1256
1257 psiconv_progress(lev+2,off+len,
1258 "Going to read the offset of the row defaults Section");
1259 rows_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",rows_off);
1263 len += 4;
1264
1265 psiconv_progress(lev+2,off+len,
1266 "Going to read the offset of the column defaults Section");
1267 cols_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",cols_off);
1271 len += 4;
1272
1273 psiconv_progress(lev+2,off+len,
1274 "Going to read the offset of the Cells List");
1275 cells_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",cells_off);
1279 len += 4;
1280
1281 psiconv_progress(lev+2,off+len,
1282 "Going to read the offset of the Grid Section");
1283 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1284 if (res)
1285 goto ERROR3;
1286 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
1287 len += 4;
1288
1289 psiconv_progress(lev+2,off+len,
1290 "Going to read the offset of the 3rd ??? Section");
1291 unknown_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1292 if (res)
1293 goto ERROR3;
1294 psiconv_debug(lev+2,off+len,"Offset: %04x",unknown_off);
1295 len += 4;
1296
1297 psiconv_progress(lev+2,off+len,
1298 "Going to read a long of the 3rd ??? Section "
1299 "(%08x expected)",0x00);
1300 temp = psiconv_read_u32(buf,lev+2,unknown_off,&res);
1301 if (res)
1302 goto ERROR3;
1303 if (temp != 0x00) {
1304 psiconv_warn(lev+2,unknown_off,
1305 "Unknown worksheet subsection has unknown contents (ignored)");
1306 psiconv_debug(lev+2,unknown_off,"Offset: %04x",temp);
1307 }
1308 len += 4;
1309
1310 psiconv_progress(lev+2,off+len,"Going to read the row defaults");
1311 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,rows_off,NULL,
1312 &(*result)->row_default_layouts,
1313 (*result)->default_layout)))
1314 goto ERROR3;
1315
1316 psiconv_progress(lev+2,off+len,"Going to read the column defaults");
1317 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,cols_off,NULL,
1318 &(*result)->col_default_layouts,
1319 (*result)->default_layout)))
1320 goto ERROR4;
1321
1322 psiconv_progress(lev+2,off+len,"Going to read the cells list");
1323 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
1324 &(*result)->cells,
1325 (*result)->default_layout,
1326 (*result)->row_default_layouts,
1327 (*result)->col_default_layouts)))
1328 goto ERROR5;
1329
1330
1331 psiconv_progress(lev+2,off+len,"Going to read the grid section");
1332 if ((res = psiconv_parse_sheet_grid_section(buf,lev+2,grid_off,NULL,
1333 &(*result)->grid)))
1334 goto ERROR6;
1335
1336
1337
1338 if (length)
1339 *length = len;
1340
1341 psiconv_progress(lev,off+len-1,
1342 "End of sheet worksheet section (total length: %08x)", len);
1343 return 0;
1344
1345ERROR6:
1346 psiconv_free_sheet_cell_list((*result)->cells);
1347ERROR5:
1348 psiconv_free_sheet_line_list((*result)->col_default_layouts);
1349ERROR4:
1350 psiconv_free_sheet_line_list((*result)->row_default_layouts);
1351ERROR3:
1352 psiconv_free_sheet_cell_layout((*result)->default_layout);
1353ERROR2:
1354 free (*result);
1355ERROR1:
1356 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed");
1357 if (length)
1358 *length = 0;
1359 if (!res)
1360 return -PSICONV_E_NOMEM;
1361 else
1362 return res;
1363}
1364
1365int psiconv_parse_sheet_line(const psiconv_buffer buf, int lev,
1366 psiconv_u32 off, int *length,
1367 psiconv_sheet_line *result,
1368 const psiconv_sheet_cell_layout default_layout)
1369{
1370 int res=0;
1371 int len=0;
1372 int leng;
1373
1374
1375 psiconv_progress(lev+1,off,"Going to read a sheet line");
1376 if (!(*result = malloc(sizeof(**result))))
1377 goto ERROR1;
1378
1379 psiconv_progress(lev+2,off+len,"Going to read the line number");
1380 (*result)->position = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1381 if (res)
1382 goto ERROR2;
1383 psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->position);
1384 len += leng;
1385
1386 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
1387 goto ERROR2;
1388 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
1389 &leng,(*result)->layout)))
1390 goto ERROR3;
1391 len += leng;
1392
1393 if (length)
1394 *length = len;
1395
1396 psiconv_progress(lev,off+len-1,
1397 "End of the sheet line (total length: %08x)", len);
1398 return 0;
1399
1400ERROR3:
1401 psiconv_free_sheet_cell_layout((*result)->layout);
1402ERROR2:
1403 free (*result);
1404ERROR1:
1405 psiconv_warn(lev+1,off,"Reading of the sheet line failed");
1406 if (length)
1407 *length = 0;
1408 if (!res)
1409 return -PSICONV_E_NOMEM;
1410 else
1411 return res;
1412}
1413
1414
1415int psiconv_parse_sheet_line_list(const psiconv_buffer buf, int lev,
1416 psiconv_u32 off, int *length,
1417 psiconv_sheet_line_list *result,
1418 const psiconv_sheet_cell_layout default_layout)
1419{
1420 int res=0;
1421 int len=0;
1422 psiconv_u32 temp;
1423 psiconv_sheet_line line;
1424 psiconv_u32 listlen,i;
1425 int leng;
1426
1427 psiconv_progress(lev+1,off,"Going to read the sheet line list");
1428 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_line_s))))
1429 goto ERROR1;
1430
1431 psiconv_progress(lev+2,off+len,
1432 "Going to read the initial byte (%02x expected)",0x02);
1433 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1434 if (res)
1435 goto ERROR2;
1436 if (temp != 0x02) {
1437 psiconv_warn(lev+2,off+len,
1438 "Sheet line list initial byte unknown value (ignored)");
1439 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1440 }
1441 len ++;
1442
1443 psiconv_progress(lev+2,off+len,
1444 "Going to read the number of defined lines");
1445 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1446 if (res)
1447 goto ERROR2;
1448 psiconv_debug(lev+2,off+len,"Number of defined lines: %d",listlen);
1449 len += leng;
1450
1451 psiconv_progress(lev+2,off+len,"Going to read all lines");
1452 for (i = 0; i < listlen; i++) {
1453 psiconv_progress(lev+3,off+len,"Going to read line %d",i);
1454 if ((res = psiconv_parse_sheet_line(buf,lev+3,off+len,&leng,&line,
1455 default_layout)))
1456 goto ERROR2;
1457 if ((res = psiconv_list_add(*result,line)))
1458 goto ERROR3;
1459 free(line);
1460 len += leng;
1461 }
1462
1463 if (length)
1464 *length = len;
1465
1466 psiconv_progress(lev,off+len-1,
1467 "End of sheet line list (total length: %08x)", len);
1468 return 0;
1469
1470ERROR3:
1471 psiconv_free_sheet_line(line);
1472ERROR2:
1473 psiconv_free_sheet_line_list(*result);
1474ERROR1:
1475 psiconv_warn(lev+1,off,"Reading of Sheet Line List failed");
1476 if (length)
1477 *length = 0;
1478 if (!res)
1479 return -PSICONV_E_NOMEM;
1480 else
1481 return res;
1482}
1483
1484int psiconv_parse_sheet_variable(const psiconv_buffer buf, int lev,
1485 psiconv_u32 off, int *length,
1486 psiconv_sheet_variable *result)
1487{
1488 int res=0;
1489 int len=0;
1490 psiconv_u32 marker;
1491 int leng;
1492
1493 psiconv_progress(lev+1,off,"Going to read a sheet variable");
1494 if (!(*result = malloc(sizeof(**result))))
1495 goto ERROR1;
1496
1497 psiconv_progress(lev+2,off+len, "Going to read the variable name");
1498 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
1499 if (res)
1500 goto ERROR2;
1501 len += leng;
1502
1503 psiconv_progress(lev+2,off+len,"Going to read the type marker");
1504 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
1505 if (res)
1506 goto ERROR3;
1507 psiconv_debug(lev+2,off+len,"Marker: %02x",marker);
1508 len ++;
1509
1510 if (marker == 0x00) {
1511 (*result)->type = psiconv_var_int;
1512 psiconv_progress(lev+2,off+len,"Going to read a signed integer");
1513 (*result)->data.dat_int = psiconv_read_sint(buf,lev+2,off+len,&leng,&res);
1514 if (res)
1515 goto ERROR3;
1516 psiconv_debug(lev+2,off+len,"Value: %d",(*result)->data.dat_int);
1517 len += leng;
1518 } else if (marker == 0x01) {
1519 (*result)->type = psiconv_var_float;
1520 psiconv_progress(lev+2,off+len,"Going to read a floating point number");
1521 (*result)->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng,
1522 &res);
1523 if (res)
1524 goto ERROR3;
1525 psiconv_debug(lev+2,off+len,"Value: %f",(*result)->data.dat_float);
1526 len += leng;
1527 } else if (marker == 0x02) {
1528 (*result)->type = psiconv_var_string;
1529 psiconv_progress(lev+2,off+len,"Going to read a string");
1530 (*result)->data.dat_string = psiconv_read_string(buf,lev+2,off+len,&leng,
1531 &res);
1532 if (res)
1533 goto ERROR3;
1534 len += leng;
1535 } else if (marker == 0x03) {
1536 (*result)->type = psiconv_var_cellref;
1537 psiconv_progress(lev+2,off+len,"Going to read a cell reference");
1538 (*result)->data.dat_cellref = psiconv_read_var_cellref(buf,lev+2,off+len,
1539 &leng, &res);
1540 if (res)
1541 goto ERROR3;
1542 len += leng;
1543 } else if (marker == 0x04) {
1544 (*result)->type = psiconv_var_cellblock;
1545 psiconv_progress(lev+2,off+len,"Going to read a cell block reference");
1546 (*result)->data.dat_cellblock = psiconv_read_var_cellblock(buf,lev+2,
1547 off+len,
1548 &leng, &res);
1549 if (res)
1550 goto ERROR3;
1551 len += leng;
1552 } else {
1553 psiconv_warn(lev+2,off+len,"Sheet variable unknown type marker");
1554 res = -PSICONV_E_PARSE;
1555 goto ERROR3;
1556 }
1557
1558 psiconv_progress(lev+2,off+len,"Going to read the variable number");
1559 (*result)->number = psiconv_read_u32(buf,lev+2,off+len,&res);
1560 if (res)
1561 goto ERROR4;
1562 psiconv_debug(lev+2,off+len,"Number: %08x",(*result)->number);
1563 len += 4;
1564
1565 if (length)
1566 *length = len;
1567
1568 psiconv_progress(lev,off+len-1,
1569 "End of sheet variable (total length: %08x)", len);
1570 return 0;
1571
1572ERROR4:
1573 if ((*result)->type == psiconv_var_string)
1574 free((*result)->data.dat_string);
1575ERROR3:
1576 free((*result)->name);
1577ERROR2:
1578 free (*result);
1579ERROR1:
1580 psiconv_warn(lev+1,off,"Reading of Sheet Variable failed");
1581 if (length)
1582 *length = 0;
1583 if (!res)
1584 return -PSICONV_E_NOMEM;
1585 else
1586 return res;
1587}
1588
1589
1590int psiconv_parse_sheet_variable_list(const psiconv_buffer buf, int lev,
1591 psiconv_u32 off, int *length,
1592 psiconv_sheet_variable_list *result)
1593{
1594 int res=0;
1595 int len=0;
1596 psiconv_u32 temp;
1597 psiconv_sheet_variable variable;
1598 psiconv_u32 listlen,i;
1599 int leng;
1600
1601 psiconv_progress(lev+1,off,"Going to read the sheet variable list");
1602 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_variable_s))))
1603 goto ERROR1;
1604
1605 psiconv_progress(lev+2,off+len,
1606 "Going to read the initial byte (%02x expected)",0x02);
1607 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1608 if (res)
1609 goto ERROR2;
1610 if (temp != 0x02) {
1611 psiconv_warn(lev+2,off+len,
1612 "Sheet variable list initial byte unknown value (ignored)");
1613 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1614 }
1615 len ++;
1616
1617 psiconv_progress(lev+2,off+len,
1618 "Going to read the number of variables");
1619 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1620 if (res)
1621 goto ERROR2;
1622 psiconv_debug(lev+2,off+len,"Number of variables: %d",listlen);
1623 len += leng;
1624
1625 psiconv_progress(lev+2,off+len,"Going to read all variables");
1626 for (i = 0; i < listlen; i++) {
1627 psiconv_progress(lev+3,off+len,"Going to read variable %d",i);
1628 if ((res = psiconv_parse_sheet_variable(buf,lev+3,off+len,&leng,&variable)))
1629 goto ERROR2;
1630 if ((res = psiconv_list_add(*result,variable)))
1631 goto ERROR3;
1632 len += leng;
1633 }
1634
1635 if (length)
1636 *length = len;
1637
1638 psiconv_progress(lev,off+len-1,
1639 "End of sheet variabels list (total length: %08x)", len);
1640 return 0;
1641
1642ERROR3:
1643 psiconv_free_sheet_variable(variable);
1644ERROR2:
1645 psiconv_list_free(*result);
1646ERROR1:
1647 psiconv_warn(lev+1,off,"Reading of Sheet Variable list failed");
1648 if (length)
1649 *length = 0;
1650 if (!res)
1651 return -PSICONV_E_NOMEM;
1652 else
1653 return res;
1654}
1655
1656int psiconv_parse_sheet_grid_section(const psiconv_buffer buf, int lev,
1657 psiconv_u32 off, int *length,
1658 psiconv_sheet_grid_section *result)
1659{
1660 int res=0,i;
1661 int len=0,leng;
1662 psiconv_u32 temp;
1663
1664 psiconv_progress(lev+1,off,"Going to read the sheet grid section");
1665 if (!(*result = malloc(sizeof(**result))))
1666 goto ERROR1;
1667
1668 psiconv_progress(lev+2,off+len, "Going to read the first flags byte");
1669 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1670 if (res)
1671 goto ERROR2;
1672 (*result)->show_column_titles = temp&0x01?psiconv_bool_true:
1673 psiconv_bool_false;
1674 psiconv_debug(lev+2,off+len,
1675 "Show column titles: %s",
1676 (*result)->show_column_titles?"true":"false");
1677 (*result)->show_row_titles = temp&0x02?psiconv_bool_true:psiconv_bool_false;
1678 psiconv_debug(lev+2,off+len,
1679 "Show row titles: %s",
1680 (*result)->show_row_titles?"true":"false");
1681 (*result)->show_vertical_grid = temp&0x04?psiconv_bool_true:
1682 psiconv_bool_false;
1683 psiconv_debug(lev+2,off+len,
1684 "Show vertical grid: %s",
1685 (*result)->show_vertical_grid?"true":"false");
1686 (*result)->show_horizontal_grid = temp&0x07?psiconv_bool_true:
1687 psiconv_bool_false;
1688 psiconv_debug(lev+2,off+len,
1689 "Show horizontal grid: %s",
1690 (*result)->show_horizontal_grid?"true":"false");
1691 (*result)->freeze_rows = temp&0x80?psiconv_bool_true:psiconv_bool_false;
1692 psiconv_debug(lev+2,off+len,
1693 "Freeze rows: %s",
1694 (*result)->freeze_rows?"true":"false");
1695 if ((temp & 0x70) != 0x30) {
1696 psiconv_warn(lev+2,off+len,
1697 "Grid section first flag byte has unknown bits (ignored)");
1698 psiconv_debug(lev+2,off+len,"Bits: %02x (%02x expected)",temp&0x70,0x30);
1699 }
1700 len ++;
1701
1702 psiconv_progress(lev+2,off+len, "Going to read the second flags byte");
1703 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1704 if (res)
1705 goto ERROR2;
1706 (*result)->freeze_columns = temp&0x01?psiconv_bool_true:psiconv_bool_false;
1707 psiconv_debug(lev+2,off+len,
1708 "Freeze columns: %s", (*result)->freeze_columns?"true":"false");
1709 if ((temp & 0xfe) != 0x80) {
1710 psiconv_warn(lev+2,off+len,
1711 "Grid section second flag byte has unknown bits (ignored)");
1712 psiconv_debug(lev+2,off+len,"Bits: %02x (%02x expected)",temp&0xfe,0x80);
1713 }
1714 len ++;
1715
1716 psiconv_progress(lev+2,off+len,
1717 "Going to an unknown byte (%02x expected)",0x90);
1718 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1719 if (res)
1720 goto ERROR2;
1721 if (temp != 0x90) {
1722 psiconv_warn(lev+2,off+len,
1723 "Grid section third byte unknown value (ignored)");
1724 psiconv_debug(lev+2,off+len,"Value: %02x",temp);
1725 }
1726 len ++;
1727
1728 psiconv_progress(lev+2,off+len, "Going to read the fourth flags byte");
1729 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1730 if (res)
1731 goto ERROR2;
1732 (*result)->show_page_breaks = temp&0x04?psiconv_bool_true:psiconv_bool_false;
1733 psiconv_debug(lev+2,off+len,
1734 "Show page breaks: %s",
1735 (*result)->show_page_breaks?"true":"false");
1736 if ((temp & 0xfc) != 0x00) {
1737 psiconv_warn(lev+2,off+len,
1738 "Grid section fourth flag byte has unknown bits (ignored)");
1739 psiconv_debug(lev+2,off+len,"Bits: %02x (%02x expected)",temp&0xfc,0x00);
1740 }
1741 len ++;
1742
1743 psiconv_progress(lev+2,off+len,"Going to read the first visible row");
1744 (*result)->first_row = psiconv_read_u32(buf,lev+2,off+len,&res);
1745 if (res)
1746 goto ERROR2;
1747 psiconv_debug(lev+2,off+len,"First row: %d",(*result)->first_row);
1748 len += 4;
1749
1750 psiconv_progress(lev+2,off+len,"Going to read the first visible column");
1751 (*result)->first_column = psiconv_read_u32(buf,lev+2,off+len,&res);
1752 if (res)
1753 goto ERROR2;
1754 psiconv_debug(lev+2,off+len,"First column: %d",(*result)->first_column);
1755 len += 4;
1756
1757 psiconv_progress(lev+2,off+len,"Going to read the last visible row");
1758 (*result)->last_row = psiconv_read_u32(buf,lev+2,off+len,&res);
1759 if (res)
1760 goto ERROR2;
1761 psiconv_debug(lev+2,off+len,"Last row: %d",(*result)->last_row);
1762 len += 4;
1763
1764 psiconv_progress(lev+2,off+len,"Going to read the last visible column");
1765 (*result)->last_column = psiconv_read_u32(buf,lev+2,off+len,&res);
1766 if (res)
1767 goto ERROR2;
1768 psiconv_debug(lev+2,off+len,"Last column: %d",(*result)->last_column);
1769 len += 4;
1770
1771 psiconv_progress(lev+2,off+len,"Going to read the default row height");
1772 (*result)->default_row_height = psiconv_read_length(buf,lev+2,off+len,
1773 &leng,&res);
1774 if (res)
1775 goto ERROR2;
1776 psiconv_debug(lev+2,off+len,"Default row height: %f",
1777 (*result)->default_row_height);
1778 len += leng;
1779
1780 psiconv_progress(lev+2,off+len,"Going to read the row heights list");
1781 if ((res = psiconv_parse_sheet_grid_size_list(buf,lev+2,off+len,&leng,
1782 &(*result)->row_heights)))
1783 goto ERROR2;
1784 len += leng;
1785
1786 psiconv_progress(lev+2,off+len,"Going to read the default column height");
1787 (*result)->default_column_width = psiconv_read_length(buf,lev+2,off+len,
1788 &leng,&res);
1789 if (res)
1790 goto ERROR3;
1791 psiconv_debug(lev+2,off+len,"Default column width: %f",
1792 (*result)->default_column_width);
1793 len += leng;
1794
1795 psiconv_progress(lev+2,off+len,"Going to read the column heights list");
1796 if ((res = psiconv_parse_sheet_grid_size_list(buf,lev+2,off+len,&leng,
1797 &(*result)->column_heights)))
1798 goto ERROR3;
1799 len += leng;
1800
1801 psiconv_progress(lev+2,off+len,
1802 "Going to read an unknown word (%04x expected)",0x00);
1803 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
1804 if (res)
1805 goto ERROR4;
1806 if (temp != 0x00) {
1807 psiconv_warn(lev+2,off+len,
1808 "Grid section unknown word has unknown value (ignored)");
1809 psiconv_debug(lev+2,off+len,"Value: %04x",temp);
1810 }
1811 len += 2;
1812
1813 psiconv_progress(lev+2,off+len,"Going to read the row breaks list");
1814 if ((res = psiconv_parse_sheet_grid_break_list(buf,lev+2,off+len,&leng,
1815 &(*result)->row_page_breaks)))
1816 goto ERROR4;
1817 len += leng;
1818
1819 psiconv_progress(lev+2,off+len,"Going to read the column breaks list");
1820 if ((res = psiconv_parse_sheet_grid_break_list(buf,lev+2,off+len,&leng,
1821 &(*result)->column_page_breaks)))
1822 goto ERROR5;
1823 len += leng;
1824
1825
1826 psiconv_progress(lev+2,off+len,
1827 "Going to read 22 unknown bytes (%02x expected)",0x00);
1828 for (i = 0; i < 22 ; i++) {
1829 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1830 if (res)
1831 goto ERROR6;
1832 if (temp != 0x00) {
1833 psiconv_warn(lev+2,off+len,
1834 "Grid section unknown byte %d has unknown value (ignored)",
1835 i);
1836 psiconv_debug(lev+2,off+len,"Value: %02x",temp);
1837 }
1838 len += 1;
1839 }
1840
1841 if ((*result)->freeze_rows || (*result)->freeze_columns) {
1842
1843 psiconv_progress(lev+2,off+len,"Going to read number of frozen rows");
1844 (*result)->frozen_rows = psiconv_read_u32(buf,lev+2,off+len, &res);
1845 if (res)
1846 goto ERROR6;
1847 psiconv_debug(lev+2,off+len,"Number of frozen rows: %d",
1848 (*result)->frozen_rows);
1849 len += leng;
1850
1851 psiconv_progress(lev+2,off+len,"Going to read number of frozen columns");
1852 (*result)->frozen_columns = psiconv_read_u32(buf,lev+2,off+len, &res);
1853 if (res)
1854 goto ERROR6;
1855 psiconv_debug(lev+2,off+len,"Number of frozen columns: %d",
1856 (*result)->frozen_columns);
1857 len += leng;
1858
1859 psiconv_progress(lev+2,off+len,"Going to read first unfrozen row");
1860 (*result)->first_unfrozen_row_displayed = psiconv_read_u32(buf,lev+2,
1861 off+len, &res);
1862 if (res)
1863 goto ERROR6;
1864 psiconv_debug(lev+2,off+len,"First row: %d",
1865 (*result)->first_unfrozen_row_displayed);
1866 len += leng;
1867
1868 psiconv_progress(lev+2,off+len,"Going to read first unfrozen column");
1869 (*result)->first_unfrozen_column_displayed = psiconv_read_u32(buf,lev+2,
1870 off+len,&res);
1871 if (res)
1872 goto ERROR6;
1873 psiconv_debug(lev+2,off+len,"First column: %d",
1874 (*result)->first_unfrozen_column_displayed);
1875 len += leng;
1876 } else
1877 (*result)->frozen_rows = (*result)->frozen_columns =
1878 (*result)->first_unfrozen_row_displayed =
1879 (*result)->first_unfrozen_column_displayed = 0;
1880
1881 psiconv_progress(lev+2,off+len,
1882 "Going to read 3 unknown bytes (%02x expected)",0xff);
1883 for (i = 0; i < 3 ; i++) {
1884 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1885 if (res)
1886 goto ERROR6;
1887 if (temp != 0xff) {
1888 psiconv_warn(lev+2,off+len,
1889 "Grid section unknown byte %d has unknown value (ignored)",
1890 i);
1891 psiconv_debug(lev+2,off+len,"Value: %02x",temp);
1892 }
1893 len ++;
1894 }
1895
1896 if (length)
1897 *length = len;
1898
1899 psiconv_progress(lev,off+len-1,
1900 "End of sheet grid section (total length: %08x)", len);
1901 return 0;
1902
1903ERROR6:
1904 psiconv_free_sheet_grid_break_list((*result)->column_page_breaks);
1905ERROR5:
1906 psiconv_free_sheet_grid_break_list((*result)->row_page_breaks);
1907ERROR4:
1908 psiconv_free_sheet_grid_size_list((*result)->column_heights);
1909ERROR3:
1910 psiconv_free_sheet_grid_size_list((*result)->row_heights);
1911ERROR2:
1912 free(*result);
1913ERROR1:
1914 psiconv_warn(lev+1,off,"Reading of Sheet Grid Section failed");
1915 if (length)
1916 *length = 0;
1917 if (!res)
1918 return -PSICONV_E_NOMEM;
1919 else
1920 return res;
1921}
1922
1923
1924int psiconv_parse_sheet_grid_size_list(const psiconv_buffer buf, int lev,
1925 psiconv_u32 off, int *length,
1926 psiconv_sheet_grid_size_list *result)
1927{
1928 int res=0;
1929 int len=0,i;
1930 int leng,listlen;
1931 psiconv_sheet_grid_size size;
1932
1933 psiconv_progress(lev+1,off,"Going to read a sheet grid size list");
1934 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_grid_size_s))))
1935 goto ERROR1;
1936
1937 psiconv_progress(lev+2,off+len,
1938 "Going to read the number of elements");
1939 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1940 if (res)
1941 goto ERROR2;
1942 psiconv_debug(lev+2,off+len,"Number of elements: %d",listlen);
1943 len += leng;
1944
1945 psiconv_progress(lev+2,off+len,"Going to read all elements");
1946 for (i = 0; i < listlen; i++) {
1947 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
1948 if ((res = psiconv_parse_sheet_grid_size(buf,lev+3,off+len,&leng,&size)))
1949 goto ERROR2;
1950 if ((res = psiconv_list_add(*result,size)))
1951 goto ERROR3;
1952 free(size);
1953 len += leng;
1954 }
1955
1956 if (length)
1957 *length = len;
1958
1959 psiconv_progress(lev,off+len-1,
1960 "End of sheet grid size list (total length: %08x)", len);
1961 return 0;
1962
1963ERROR3:
1964 psiconv_free_sheet_grid_size(size);
1965ERROR2:
1966 psiconv_list_free(*result);
1967ERROR1:
1968 psiconv_warn(lev+1,off,"Reading of Sheet Grid Size List failed");
1969 if (length)
1970 *length = 0;
1971 if (!res)
1972 return -PSICONV_E_NOMEM;
1973 else
1974 return res;
1975}
1976
1977int psiconv_parse_sheet_grid_size(const psiconv_buffer buf, int lev,
1978 psiconv_u32 off, int *length,
1979 psiconv_sheet_grid_size *result)
1980{
1981 int res=0;
1982 int len=0;
1983 int leng;
1984
1985 psiconv_progress(lev+1,off,"Going to read a sheet grid size");
1986
1987 if (!(*result = malloc(sizeof(**result))))
1988 goto ERROR1;
1989
1990 psiconv_progress(lev+2,off+len, "Going to read the row or column number");
1991 (*result)->line_number = psiconv_read_u32(buf,lev+2,off+len,&res);
1992 if (res)
1993 goto ERROR2;
1994 psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->line_number);
1995 len += 4;
1996
1997 psiconv_progress(lev+2,off+len, "Going to read the row or column height");
1998 (*result)->size = psiconv_read_length(buf,lev+2,off+len,&leng,&res);
1999 if (res)
2000 goto ERROR2;
2001 psiconv_debug(lev+2,off+len,"Size: %f\n",(*result)->size);
2002 len += leng;
2003
2004 if (length)
2005 *length = len;
2006
2007 psiconv_progress(lev,off+len-1,
2008 "End of sheet grid size(total length: %08x)", len);
2009 return 0;
2010
2011ERROR2:
2012 free (*result);
2013ERROR1:
2014 psiconv_warn(lev+1,off,"Reading of Sheet Grid Size failed");
2015 if (length)
2016 *length = 0;
2017 if (!res)
2018 return -PSICONV_E_NOMEM;
2019 else
2020 return res;
2021}
2022
2023
2024int psiconv_parse_sheet_grid_break_list(const psiconv_buffer buf, int lev,
2025 psiconv_u32 off, int *length,
2026 psiconv_sheet_grid_break_list *result)
2027{
2028 int res=0;
2029 int len=0,i;
2030 int leng,listlen;
2031 psiconv_u32 nr;
2032
2033 psiconv_progress(lev+1,off,"Going to read a sheet grid break list");
2034 if (!(*result = psiconv_list_new(sizeof(psiconv_u32))))
2035 goto ERROR1;
2036
2037 psiconv_progress(lev+2,off+len,
2038 "Going to read the number of elements");
2039 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
2040 if (res)
2041 goto ERROR2;
2042 psiconv_debug(lev+2,off+len,"Number of elements: %d",listlen);
2043 len += leng;
2044
2045 psiconv_progress(lev+2,off+len,"Going to read all elements");
2046 for (i = 0; i < listlen; i++) {
2047 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
2048 nr = psiconv_read_u32(buf,lev+3,off+len,&res);
2049 if (res)
2050 goto ERROR2;
2051 if ((res = psiconv_list_add(*result,&nr)))
2052 goto ERROR2;
2053 len += leng;
2054 }
2055
2056 if (length)
2057 *length = len;
2058
2059 psiconv_progress(lev,off+len-1,
2060 "End of sheet grid break list (total length: %08x)", len);
2061 return 0;
2062
2063ERROR2:
2064 psiconv_list_free(*result);
2065ERROR1:
2066 psiconv_warn(lev+1,off,"Reading of Sheet Grid break List failed");
2067 if (length)
2068 *length = 0;
2069 if (!res)
2070 return -PSICONV_E_NOMEM;
2071 else
2072 return res;
2073}
2074

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

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