/[public]/psiconv/trunk/lib/psiconv/parse_sheet.c
ViewVC logotype

Contents of /psiconv/trunk/lib/psiconv/parse_sheet.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 129 - (show annotations)
Tue Jul 24 20:32:51 2001 UTC (22 years, 9 months ago) by frodo
File MIME type: text/plain
File size: 52592 byte(s)
(Frodo) Many new sheet things

  * Variable section and formula variable parsing
  * Info section
  * Name section
  * Better parsing of vararg functions

1 /*
2 parse_sheet.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 2001 Frodo Looijaard <frodol@dds.nl>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "config.h"
21 #include "compat.h"
22
23 #include <stdlib.h>
24
25 #include "parse_routines.h"
26 #include "error.h"
27
28 static 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;
42 ERROR4:
43 psiconv_free_paragraph_layout(result->paragraph);
44 ERROR3:
45 psiconv_free_character_layout(result->character);
46 ERROR2:
47 free(result);
48 ERROR1:
49 return NULL;
50 }
51
52 static 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;
69 ERROR4:
70 psiconv_free_paragraph_layout(result->paragraph);
71 ERROR3:
72 psiconv_free_character_layout(result->character);
73 ERROR2:
74 free(result);
75 ERROR1:
76 return NULL;
77 }
78
79 static 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;
130 ERROR1:
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
139 static 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;
213 ERROR1:
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
222 int 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
331 ERROR1:
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 }
340
341 int psiconv_parse_sheet_status_section(const psiconv_buffer buf, int lev,
342 psiconv_u32 off, int *length,
343 psiconv_sheet_status_section *result)
344 {
345 int res=0;
346 int len=0;
347 psiconv_u32 temp;
348 int leng;
349
350 psiconv_progress(lev+1,off,"Going to read the sheet status section");
351 if (!(*result = malloc(sizeof(**result))))
352 goto ERROR1;
353
354 psiconv_progress(lev+2,off+len,
355 "Going to read the initial byte (%02x expected)",0x02);
356 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
357 if (res)
358 goto ERROR2;
359 if (temp != 0x02) {
360 psiconv_warn(lev+2,off+len,
361 "Sheet status section initial byte unknown value (ignored)");
362 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
363 }
364 len ++;
365
366 psiconv_progress(lev+2,off+len,
367 "Going to read the cursor row");
368 (*result)->cursor_row = psiconv_read_u32(buf,lev+2,off + len,&res);
369 if (res)
370 goto ERROR2;
371 psiconv_debug(lev+2,off+len,"Cursor row: %08x",
372 (*result)->cursor_row);
373 len += 0x04;
374
375 psiconv_progress(lev+2,off+len,
376 "Going to read the cursor column");
377 (*result)->cursor_column = psiconv_read_u32(buf,lev+2,off + len,&res);
378 if (res)
379 goto ERROR2;
380 psiconv_debug(lev+2,off+len,"Cursor column: %08x",
381 (*result)->cursor_column);
382 len += 0x04;
383
384 psiconv_progress(lev+2,off+len,"Going to read initially display graph");
385 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
386 &(*result)->show_graph)))
387 goto ERROR2;
388 len += leng;
389
390 psiconv_progress(lev+2,off+len,
391 "Going to read the toolbar status byte");
392 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
393 if (res)
394 goto ERROR2;
395
396 (*result)->show_side_sheet_toolbar = temp&0x01 ? psiconv_bool_true :
397 psiconv_bool_false;
398 psiconv_debug(lev+2,off+len,"Show side sheet toolbar: %02x",
399 (*result)->show_side_sheet_toolbar);
400 (*result)->show_top_sheet_toolbar = temp&0x02 ? psiconv_bool_true :
401 psiconv_bool_false;
402 psiconv_debug(lev+2,off+len,"Show top sheet toolbar: %02x",
403 (*result)->show_top_sheet_toolbar);
404 (*result)->show_side_graph_toolbar = temp&0x04 ? psiconv_bool_true :
405 psiconv_bool_false;
406 psiconv_debug(lev+2,off+len,"Show side graph toolbar: %02x",
407 (*result)->show_side_graph_toolbar);
408 (*result)->show_top_graph_toolbar = temp&0x08 ? psiconv_bool_true :
409 psiconv_bool_false;
410 psiconv_debug(lev+2,off+len,"Show top graph toolbar: %02x",
411 (*result)->show_top_graph_toolbar);
412 if (temp & 0xf0) {
413 psiconv_warn(lev+2,off+len,"Sheet status section toolbar byte "
414 "flags contains unknown flags (ignored)");
415 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0);
416 }
417 len ++;
418
419 psiconv_progress(lev+2,off+len,
420 "Going to read the scrollbar status byte");
421 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
422 if (res)
423 goto ERROR2;
424 if ((temp & 0x03) == 0x03) {
425 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte "
426 "flags contains unknown flags (ignored)");
427 psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x03);
428 }
429 (*result)->show_horizontal_scrollbar = (temp&0x03) == 1? psiconv_triple_off :
430 (temp&0x03) == 2? psiconv_triple_auto:
431 psiconv_triple_on;
432 psiconv_debug(lev+2,off+len,"Show horizontal scrollbar: %02x",
433 (*result)->show_horizontal_scrollbar);
434 if ((temp & 0x0c) == 0x0c) {
435 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte "
436 "flags contains unknown flags (ignored)");
437 psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x0c);
438 }
439 (*result)->show_vertical_scrollbar = (temp&0x0c) ==0x04? psiconv_triple_off:
440 (temp&0x0c) ==0x08? psiconv_triple_auto:
441 psiconv_triple_on;
442 psiconv_debug(lev+2,off+len,"Show vertical scrollbar: %02x",
443 (*result)->show_vertical_scrollbar);
444 if (temp & 0xf0) {
445 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte "
446 "flags contains unknown flags (ignored)");
447 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0);
448 }
449 len ++;
450
451 psiconv_progress(lev+2,off+len,
452 "Going to read an unknown byte (%02x expected)",0x00);
453 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
454 if (res)
455 goto ERROR2;
456 if (temp != 0x00) {
457 psiconv_warn(lev+2,off+len,
458 "Sheet status section unknown byte unknown value (ignored)");
459 psiconv_debug(lev+2,off+len,"Unknown byte: %02x",temp);
460 }
461 len ++;
462
463 psiconv_progress(lev+2,off+len,"Going to read sheet display size");
464 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res);
465 if (res)
466 goto ERROR2;
467 psiconv_debug(lev+2,off+len,"Sheet display size: %08x",
468 (*result)->sheet_display_size);
469 len += 0x04;
470
471 psiconv_progress(lev+2,off+len,"Going to read graph display size");
472 (*result)->graph_display_size = psiconv_read_u32(buf,lev+2,off + len,&res);
473 if (res)
474 goto ERROR2;
475 psiconv_debug(lev+2,off+len,"Graph display size: %08x",
476 (*result)->graph_display_size);
477 len += 0x04;
478
479 if (length)
480 *length = len;
481
482 psiconv_progress(lev,off+len-1,
483 "End of sheet status section (total length: %08x)", len);
484 return 0;
485
486 ERROR2:
487 free (*result);
488 ERROR1:
489 psiconv_warn(lev+1,off,"Reading of Sheet Status Section failed");
490 if (length)
491 *length = 0;
492 if (!res)
493 return -PSICONV_E_NOMEM;
494 else
495 return res;
496 }
497
498 int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev,
499 psiconv_u32 off, int *length,
500 psiconv_sheet_workbook_section *result)
501 {
502 int res=0,with_name;
503 psiconv_u32 temp,formulas_off,worksheets_off,info_off,var_off,name_off=0;
504 int len=0;
505
506 psiconv_progress(lev+1,off,"Going to read the sheet workbook section");
507 if (!(*result = malloc(sizeof(**result))))
508 goto ERROR1;
509
510 psiconv_progress(lev+2,off+len,
511 "Going to read the initial byte (%02x or $02xexpected)",
512 0x02,0x04);
513 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
514 if (res)
515 goto ERROR2;
516 if ((temp != 0x04) && temp !=0x02) {
517 psiconv_warn(lev+2,off+len,
518 "Sheet workbook section initial byte unknown value (ignored)");
519 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
520 }
521 with_name = temp ==0x04;
522 len ++;
523
524 psiconv_progress(lev+2,off+len,
525 "Going to read the offset of the sheet info Section");
526 info_off = psiconv_read_u32(buf,lev+2,off+len,&res);
527 if (res)
528 goto ERROR2;
529 psiconv_debug(lev+2,off+len,"Offset: %04x",info_off);
530 len += 4;
531
532 psiconv_progress(lev+2,off+len,
533 "Going to read the offset of the Formulas List");
534 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res);
535 if (res)
536 goto ERROR2;
537 psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off);
538 len += 4;
539
540 psiconv_progress(lev+2,off+len,
541 "Going to read the offset of the Worksheet List");
542 worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res);
543 if (res)
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;
553 psiconv_debug(lev+2,off+len,"Offset: %04x",var_off);
554 len += 4;
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
577 psiconv_progress(lev+2,off+len,"Going to read the formulas list");
578 if ((res = psiconv_parse_sheet_formula_list(buf,lev+2,formulas_off,NULL,
579 &(*result)->formulas)))
580 goto ERROR4;
581
582 psiconv_progress(lev+2,off+len,"Going to read the worksheet list");
583 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off,
584 NULL,&(*result)->worksheets)))
585 goto ERROR5;
586
587 if (with_name) {
588 psiconv_progress(lev+2,off+len,"Going to read the name section");
589
590 if ((res = psiconv_parse_sheet_name_section(buf,lev+2,name_off,NULL,
591 &(*result)->name)))
592 goto ERROR6;
593 } else
594 (*result)->name = NULL;
595
596 if (length)
597 *length = len;
598
599 psiconv_progress(lev,off+len-1,
600 "End of sheet workbook section (total length: %08x)", len);
601 return 0;
602
603 ERROR6:
604 psiconv_free_sheet_worksheet_list((*result)->worksheets);
605 ERROR5:
606 psiconv_free_formula_list((*result)->formulas);
607 ERROR4:
608 psiconv_free_sheet_variable_list((*result)->variables);
609 ERROR3:
610 psiconv_free_sheet_info_section((*result)->info);
611 ERROR2:
612 free (*result);
613 ERROR1:
614 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed");
615 if (length)
616 *length = 0;
617 if (!res)
618 return -PSICONV_E_NOMEM;
619 else
620 return res;
621 }
622
623 int psiconv_parse_sheet_name_section(const psiconv_buffer buf, int lev,
624 psiconv_u32 off, int *length,
625 psiconv_sheet_name_section *result)
626 {
627 int res=0;
628 psiconv_u32 temp;
629 int len=0,leng;
630
631 psiconv_progress(lev+1,off,"Going to read the sheet name section");
632 if (!(*result = malloc(sizeof(**result))))
633 goto ERROR1;
634
635 psiconv_progress(lev+2,off+len,
636 "Going to read the initial byte (%02x expected)",0x02);
637 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
638 if (res)
639 goto ERROR2;
640 if (temp != 0x02) {
641 psiconv_warn(lev+2,off+len,
642 "Sheet name section initial byte unknown value (ignored)");
643 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
644 }
645 len ++;
646
647 psiconv_progress(lev+2,off+len, "Going to read the sheet name");
648 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
649 if (res)
650 goto ERROR2;
651 len += leng;
652
653 if (length)
654 *length = len;
655
656 psiconv_progress(lev,off+len-1,
657 "End of sheet name section (total length: %08x)", len);
658 return 0;
659
660 ERROR2:
661 free(*result);
662 ERROR1:
663 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
664 if (length)
665 *length = 0;
666 if (!res)
667 return -PSICONV_E_NOMEM;
668 else
669 return res;
670 }
671
672 int psiconv_parse_sheet_info_section(const psiconv_buffer buf, int lev,
673 psiconv_u32 off, int *length,
674 psiconv_sheet_info_section *result)
675 {
676 int res=0;
677 psiconv_u32 temp;
678 int len=0;
679
680 psiconv_progress(lev+1,off,"Going to read the sheet info section");
681 if (!(*result = malloc(sizeof(**result))))
682 goto ERROR1;
683
684 psiconv_progress(lev+2,off+len,
685 "Going to read the initial byte (%02x expected)",0x02);
686 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
687 if (res)
688 goto ERROR2;
689 if (temp != 0x02) {
690 psiconv_warn(lev+2,off+len,
691 "Sheet info section initial byte unknown value (ignored)");
692 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
693 }
694 len ++;
695
696 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
697 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
698 if (res)
699 goto ERROR2;
700 (*result)->auto_recalc = temp & 0x01 ? psiconv_bool_true:psiconv_bool_false;
701 psiconv_debug(lev+2,off+len,"Auto recalculation: %02x",
702 (*result)->auto_recalc);
703 if ((temp & 0xfe) != 0x02) {
704 psiconv_warn(lev+2,off+len,"Sheet Info Section flags byte "
705 "contains unknown flags (ignored)");
706 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp &0xfe);
707 }
708
709 len ++;
710
711
712 if (length)
713 *length = len;
714
715 psiconv_progress(lev,off+len-1,
716 "End of sheet info section (total length: %08x)", len);
717 return 0;
718
719 ERROR2:
720 free(*result);
721 ERROR1:
722 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
723 if (length)
724 *length = 0;
725 if (!res)
726 return -PSICONV_E_NOMEM;
727 else
728 return res;
729 }
730
731 int psiconv_parse_sheet_formula_list(const psiconv_buffer buf, int lev,
732 psiconv_u32 off, int *length,
733 psiconv_formula_list *result)
734 {
735 int res=0;
736 int len=0;
737 psiconv_u32 temp;
738 psiconv_formula formula;
739 psiconv_u32 listlen,i;
740 int leng;
741
742 psiconv_progress(lev+1,off,"Going to read the sheet formula list");
743 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s))))
744 goto ERROR1;
745
746 psiconv_progress(lev+2,off+len,
747 "Going to read the initial byte (%02x expected)",0x02);
748 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
749 if (res)
750 goto ERROR2;
751 if (temp != 0x02) {
752 psiconv_warn(lev+2,off+len,
753 "Sheet formula list initial byte unknown value (ignored)");
754 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
755 }
756 len ++;
757
758 psiconv_progress(lev+2,off+len,
759 "Going to read the number of formulas");
760 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
761 if (res)
762 goto ERROR2;
763 psiconv_debug(lev+2,off+len,"Number of formulas: %d",listlen);
764 len += leng;
765
766 psiconv_progress(lev+2,off+len,"Going to read all formulas");
767 for (i = 0; i < listlen; i++) {
768 psiconv_progress(lev+3,off+len,"Going to read formula %d",i);
769 if ((res = psiconv_parse_formula(buf,lev+3,off+len,&leng,&formula)))
770 goto ERROR2;
771 if ((res = psiconv_list_add(*result,formula)))
772 goto ERROR3;
773 len += leng;
774 }
775
776 if (length)
777 *length = len;
778
779 psiconv_progress(lev,off+len-1,
780 "End of sheet formula list (total length: %08x)", len);
781 return 0;
782
783 ERROR3:
784 psiconv_free_formula(formula);
785 ERROR2:
786 psiconv_list_free(*result);
787 ERROR1:
788 psiconv_warn(lev+1,off,"Reading of Sheet Formula list failed");
789 if (length)
790 *length = 0;
791 if (!res)
792 return -PSICONV_E_NOMEM;
793 else
794 return res;
795 }
796
797 int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
798 psiconv_u32 off, int *length,
799 psiconv_sheet_cell *result,
800 const psiconv_sheet_cell_layout default_layout,
801 const psiconv_sheet_line_list row_default_layouts,
802 const psiconv_sheet_line_list col_default_layouts)
803 {
804 int res=0;
805 int len=0;
806 psiconv_u32 temp;
807 psiconv_bool_t has_layout;
808 int leng;
809
810 psiconv_progress(lev+1,off,"Going to read a sheet cell structure");
811 if (!(*result = malloc(sizeof(**result))))
812 goto ERROR1;
813
814 (*result)->layout = NULL;
815 (*result)->type = psiconv_cell_blank;
816
817 psiconv_progress(lev+2,off+len,"Going to read the cell position");
818 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
819 if (res)
820 goto ERROR2;
821 len ++;
822 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 8;
823 if (res)
824 goto ERROR2;
825 len ++;
826 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 16;
827 if (res)
828 goto ERROR2;
829 len ++;
830 (*result)->column = (temp >> 2) & 0xFF;
831 (*result)->row = (temp >> 10) & 0x3FFF;
832 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x",
833 (*result)->column,(*result)->row);
834 if (temp & 0x03) {
835 psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)");
836 psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03);
837 }
838
839 psiconv_progress(lev+2,off+len,"Going to read the cell type");
840 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
841 if (res)
842 goto ERROR2;
843 len ++;
844 (*result)->type = (temp >> 5) & 0x07;
845 (*result)->calculated = (temp & 0x08)?psiconv_bool_true:psiconv_bool_false;
846 has_layout = (temp & 0x10)?psiconv_bool_true:psiconv_bool_false;
847
848 psiconv_progress(lev+2,off+len,"Going to read the cell value");
849 if ((*result)->type == psiconv_cell_blank) {
850 psiconv_debug(lev+2,off+len,"Cell type is blank: no value given.");
851 } else if ((*result)->type == psiconv_cell_int) {
852 psiconv_progress(lev+2,off+len,"Going to read an integer");
853 (*result)->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
854 if (res)
855 goto ERROR2;
856 len += 4;
857 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
858
859 } else if ((*result)->type == psiconv_cell_bool) {
860 psiconv_progress(lev+2,off+len,"Going to read a boolean");
861 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
862 &(*result)->data.dat_bool)))
863 goto ERROR2;
864 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp);
865 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
866 len += leng;
867 } else if ((*result)->type == psiconv_cell_error) {
868 psiconv_progress(lev+2,off+len,"Going to read the error code");
869 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
870 if (res)
871 goto ERROR2;
872 if (temp == 0)
873 (*result)->data.dat_error = psiconv_sheet_error_none;
874 else if (temp == 1)
875 (*result)->data.dat_error = psiconv_sheet_error_null;
876 else if (temp == 2)
877 (*result)->data.dat_error = psiconv_sheet_error_divzero;
878 else if (temp == 3)
879 (*result)->data.dat_error = psiconv_sheet_error_value;
880 else if (temp == 4)
881 (*result)->data.dat_error = psiconv_sheet_error_reference;
882 else if (temp == 5)
883 (*result)->data.dat_error = psiconv_sheet_error_name;
884 else if (temp == 6)
885 (*result)->data.dat_error = psiconv_sheet_error_number;
886 else if (temp == 7)
887 (*result)->data.dat_error = psiconv_sheet_error_notavail;
888 else {
889 psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)");
890 psiconv_debug(lev+2,off+len,"Error code: %04x",temp);
891 (*result)->data.dat_error = psiconv_sheet_error_none;
892 }
893 psiconv_debug(lev+2,off+len,"Cell contents: %04x",
894 (*result)->data.dat_error);
895 len += 2;
896 } else if ((*result)->type == psiconv_cell_float) {
897 psiconv_progress(lev+2,off+len,"Going to read a float");
898 (*result)->data.dat_float =
899 psiconv_read_float(buf,lev+2,off+len,&leng,&res);
900 if (res)
901 goto ERROR2;
902 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
903 len += leng;
904 } else if ((*result)->type == psiconv_cell_string) {
905 psiconv_progress(lev+2,off+len,"Going to read a string");
906 (*result)->data.dat_string =
907 psiconv_read_string(buf,lev+2,off+len,&leng,&res);
908 if (res)
909 goto ERROR2;
910 psiconv_debug(lev+2,off+len,"Cell contents: `%s'",
911 (*result)->data.dat_string);
912 len += leng;
913 } else {
914 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
915 res = PSICONV_E_PARSE;
916 goto ERROR2;
917 }
918
919 if (!((*result)->layout = psiconv_clone_cell_layout(
920 psiconv_get_default_layout(row_default_layouts,
921 col_default_layouts,
922 default_layout,
923 (*result)->row,
924 (*result)->column))))
925 goto ERROR2;
926 if (has_layout) {
927 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
928 &leng,(*result)->layout)))
929 goto ERROR2;
930 len += leng;
931 }
932
933 if ((*result)->calculated) {
934 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference");
935 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
936 if (res)
937 goto ERROR2;
938 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp);
939 len += leng;
940 (*result)->ref_formula = temp;
941 }
942
943 if (length)
944 *length = len;
945
946 psiconv_progress(lev,off+len-1,
947 "End of sheet cell structure (total length: %08x)", len);
948 return 0;
949
950 ERROR2:
951 psiconv_free_sheet_cell(*result);
952 ERROR1:
953 psiconv_warn(lev+1,off,"Reading of Sheet Cell Structure failed");
954 if (length)
955 *length = 0;
956 if (!res)
957 return -PSICONV_E_NOMEM;
958 else
959 return res;
960 }
961
962 int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
963 psiconv_u32 off, int *length,
964 psiconv_sheet_cell_list *result,
965 const psiconv_sheet_cell_layout default_layout,
966 const psiconv_sheet_line_list row_default_layouts,
967 const psiconv_sheet_line_list col_default_layouts)
968 {
969 int res=0;
970 int len=0;
971 psiconv_u32 temp;
972 psiconv_sheet_cell cell;
973 psiconv_u32 listlen,i;
974 int leng;
975
976 psiconv_progress(lev+1,off,"Going to read the sheet cell list");
977 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
978 goto ERROR1;
979
980 psiconv_progress(lev+2,off+len,
981 "Going to read the initial byte (%02x expected)",0x02);
982 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
983 if (res)
984 goto ERROR2;
985 if (temp != 0x02) {
986 psiconv_warn(lev+2,off+len,
987 "Sheet cell list initial byte unknown value (ignored)");
988 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
989 }
990 len ++;
991
992 psiconv_progress(lev+2,off+len,
993 "Going to read the initial byte (%02x expected)",0x00);
994 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
995 if (res)
996 goto ERROR2;
997 if (temp != 0x00) {
998 psiconv_warn(lev+2,off+len,
999 "Sheet cell list initial byte unknown value (ignored)");
1000 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1001 }
1002 len ++;
1003
1004 psiconv_progress(lev+2,off+len,
1005 "Going to read the number of defined cells");
1006 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1007 if (res)
1008 goto ERROR2;
1009 psiconv_debug(lev+2,off+len,"Number of defined cells: %d",listlen);
1010 len += leng;
1011
1012 psiconv_progress(lev+2,off+len,"Going to read all cells");
1013 for (i = 0; i < listlen; i++) {
1014 psiconv_progress(lev+3,off+len,"Going to read cell %d",i);
1015 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
1016 default_layout,row_default_layouts,
1017 col_default_layouts)))
1018 goto ERROR2;
1019 if ((res = psiconv_list_add(*result,cell)))
1020 goto ERROR3;
1021 free(cell);
1022 len += leng;
1023 }
1024
1025 if (length)
1026 *length = len;
1027
1028 psiconv_progress(lev,off+len-1,
1029 "End of sheet cell list (total length: %08x)", len);
1030 return 0;
1031
1032 ERROR3:
1033 psiconv_free_sheet_cell(cell);
1034 ERROR2:
1035 psiconv_free_sheet_cell_list(*result);
1036 ERROR1:
1037 psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed");
1038 if (length)
1039 *length = 0;
1040 if (!res)
1041 return -PSICONV_E_NOMEM;
1042 else
1043 return res;
1044 }
1045
1046
1047 int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
1048 psiconv_u32 off, int *length,
1049 psiconv_sheet_worksheet_list *result)
1050 {
1051 psiconv_sheet_worksheet worksheet;
1052 int res=0;
1053 int len=0;
1054 psiconv_u8 temp;
1055 psiconv_u32 offset;
1056 int leng,i,nr;
1057
1058 psiconv_progress(lev+1,off,"Going to read the worksheet list");
1059 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
1060 goto ERROR1;
1061
1062 psiconv_progress(lev+2,off+len,
1063 "Going to read the initial bytes (%02x expected)",0x02);
1064 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1065 if (res)
1066 goto ERROR2;
1067 if (temp != 0x02) {
1068 psiconv_warn(lev+2,off+len,
1069 "Sheet worksheet list initial byte unknown value (ignored)");
1070 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1071 }
1072 len ++;
1073
1074 psiconv_progress(lev+2,off+len,"Going to read the list length");
1075 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1076 if (res)
1077 goto ERROR2;
1078 psiconv_debug(lev+2,off+len,"Length: %02x",nr);
1079 len += leng;
1080
1081 psiconv_progress(lev+2,off+len,"Going to read the list");
1082 for (i=0 ; i < nr; i++) {
1083 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
1084 psiconv_progress(lev+4,off+len,
1085 "Going to read the initial byte (%02x expected)",0x00);
1086 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
1087 if (res)
1088 goto ERROR2;
1089 if (temp != 0x00) {
1090 psiconv_warn(lev+4,off+len,
1091 "Sheet worksheet element initial byte unknown value (ignored)");
1092 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp);
1093 }
1094 len ++;
1095
1096 psiconv_progress(lev+4,off+len,"Going to read the worksheet offset");
1097 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
1098 if (res)
1099 goto ERROR2;
1100 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
1101 len += 4;
1102
1103 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
1104 &worksheet)))
1105 goto ERROR2;
1106 if ((res = psiconv_list_add(*result,worksheet)))
1107 goto ERROR3;
1108 free(worksheet);
1109 }
1110
1111 if (length)
1112 *length = len;
1113
1114 psiconv_progress(lev,off+len-1,
1115 "End of worksheet list (total length: %08x)", len);
1116
1117 return 0;
1118
1119 ERROR3:
1120 psiconv_free_sheet_worksheet(worksheet);
1121 ERROR2:
1122 psiconv_free_sheet_worksheet_list(*result);
1123 ERROR1:
1124 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
1125 if (length)
1126 *length = 0;
1127 if (!res)
1128 return -PSICONV_E_NOMEM;
1129 else
1130 return res;
1131 }
1132
1133 int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
1134 psiconv_u32 off, int *length,
1135 psiconv_sheet_cell_layout result)
1136
1137 {
1138 int res=0;
1139 int len=0;
1140 int leng;
1141 psiconv_u8 temp;
1142
1143 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
1144
1145 psiconv_progress(lev+2,off+len,
1146 "Going to read the first byte (%02x expected)",0x02);
1147 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1148 if (res)
1149 goto ERROR1;
1150 if (temp != 0x02) {
1151 psiconv_warn(lev+2,off+len,
1152 "Worksheet section initial byte unknown value (ignored)");
1153 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1154 }
1155 len ++;
1156
1157 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
1158 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1159 if (res)
1160 goto ERROR1;
1161 len ++;
1162
1163 if (temp & 0x01) {
1164 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
1165 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
1166 result->paragraph)))
1167 goto ERROR1;
1168 len += leng;
1169 }
1170
1171 if (temp & 0x02) {
1172 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
1173 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
1174 result->character)))
1175 goto ERROR1;
1176 len += leng;
1177 }
1178
1179 if (temp & 0x04) {
1180 psiconv_progress(lev+3,off+len, "Going to read the default number format");
1181 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
1182 result->numberformat);
1183 len += leng;
1184 }
1185
1186 if (length)
1187 *length = len;
1188
1189 psiconv_progress(lev,off+len-1,
1190 "End of sheet cell layout (total length: %08x)", len);
1191
1192 return 0;
1193
1194 ERROR1:
1195 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
1196 if (length)
1197 *length = 0;
1198 if (!res)
1199 return -PSICONV_E_NOMEM;
1200 else
1201 return res;
1202 }
1203
1204
1205 int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
1206 psiconv_u32 off, int *length,
1207 psiconv_sheet_worksheet *result)
1208 {
1209 int res=0;
1210 psiconv_u32 temp,cells_off,grid_off,rows_off,cols_off;
1211 int len=0;
1212 int leng;
1213
1214 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
1215 if (!(*result = malloc(sizeof(**result))))
1216 goto ERROR1;
1217
1218 psiconv_progress(lev+2,off+len,
1219 "Going to read the initial bytes (%02x expected)",0x04);
1220 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1221 if (res)
1222 goto ERROR2;
1223 if (temp != 0x04) {
1224 psiconv_warn(lev+2,off+len,
1225 "Worksheet section initial byte unknown value (ignored)");
1226 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1227 }
1228 len ++;
1229
1230 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
1231 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1232 if (res)
1233 goto ERROR2;
1234 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp);
1235 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
1236 if (temp & 0xfe) {
1237 psiconv_warn(lev+2,off+len,
1238 "Worksheet section flags byte unknown bits (ignored)");
1239 }
1240 len ++;
1241
1242 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
1243 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
1244 goto ERROR2;
1245 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
1246 (*result)->default_layout)))
1247 goto ERROR3;
1248 len += leng;
1249
1250 psiconv_progress(lev+2,off+len,
1251 "Going to read the offset of the row defaults Section");
1252 rows_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1253 if (res)
1254 goto ERROR3;
1255 psiconv_debug(lev+2,off+len,"Offset: %04x",rows_off);
1256 len += 4;
1257
1258 psiconv_progress(lev+2,off+len,
1259 "Going to read the offset of the column defaults Section");
1260 cols_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1261 if (res)
1262 goto ERROR3;
1263 psiconv_debug(lev+2,off+len,"Offset: %04x",cols_off);
1264 len += 4;
1265
1266 psiconv_progress(lev+2,off+len,
1267 "Going to read the offset of the Cells List");
1268 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1269 if (res)
1270 goto ERROR3;
1271 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
1272 len += 4;
1273
1274 psiconv_progress(lev+2,off+len,
1275 "Going to read the offset of the Grid Section");
1276 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1277 if (res)
1278 goto ERROR3;
1279 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
1280 len += 4;
1281
1282 psiconv_progress(lev+2,off+len,
1283 "Going to read the offset of the 3rd ??? Section");
1284 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
1285 if (res)
1286 goto ERROR3;
1287 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
1288 len += 4;
1289
1290 psiconv_progress(lev+2,off+len,"Going to read the row defaults");
1291 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,rows_off,NULL,
1292 &(*result)->row_default_layouts,
1293 (*result)->default_layout)))
1294 goto ERROR3;
1295
1296 psiconv_progress(lev+2,off+len,"Going to read the column defaults");
1297 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,cols_off,NULL,
1298 &(*result)->col_default_layouts,
1299 (*result)->default_layout)))
1300 goto ERROR4;
1301
1302 psiconv_progress(lev+2,off+len,"Going to read the cells list");
1303 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
1304 &(*result)->cells,
1305 (*result)->default_layout,
1306 (*result)->row_default_layouts,
1307 (*result)->col_default_layouts)))
1308 goto ERROR5;
1309
1310
1311 /* TODO: parse grid section */
1312
1313 if (length)
1314 *length = len;
1315
1316 psiconv_progress(lev,off+len-1,
1317 "End of sheet worksheet section (total length: %08x)", len);
1318 return 0;
1319
1320 ERROR5:
1321 psiconv_free_sheet_line_list((*result)->col_default_layouts);
1322 ERROR4:
1323 psiconv_free_sheet_line_list((*result)->row_default_layouts);
1324 ERROR3:
1325 psiconv_free_sheet_cell_layout((*result)->default_layout);
1326 ERROR2:
1327 free (*result);
1328 ERROR1:
1329 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed");
1330 if (length)
1331 *length = 0;
1332 if (!res)
1333 return -PSICONV_E_NOMEM;
1334 else
1335 return res;
1336 }
1337
1338 int psiconv_parse_sheet_line(const psiconv_buffer buf, int lev,
1339 psiconv_u32 off, int *length,
1340 psiconv_sheet_line *result,
1341 const psiconv_sheet_cell_layout default_layout)
1342 {
1343 int res=0;
1344 int len=0;
1345 int leng;
1346
1347
1348 psiconv_progress(lev+1,off,"Going to read a sheet line");
1349 if (!(*result = malloc(sizeof(**result))))
1350 goto ERROR1;
1351
1352 psiconv_progress(lev+2,off+len,"Going to read the line number");
1353 (*result)->position = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1354 if (res)
1355 goto ERROR2;
1356 psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->position);
1357 len += leng;
1358
1359 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
1360 goto ERROR2;
1361 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
1362 &leng,(*result)->layout)))
1363 goto ERROR3;
1364 len += leng;
1365
1366 if (length)
1367 *length = len;
1368
1369 psiconv_progress(lev,off+len-1,
1370 "End of the sheet line (total length: %08x)", len);
1371 return 0;
1372
1373 ERROR3:
1374 psiconv_free_sheet_cell_layout((*result)->layout);
1375 ERROR2:
1376 free (*result);
1377 ERROR1:
1378 psiconv_warn(lev+1,off,"Reading of the sheet line failed");
1379 if (length)
1380 *length = 0;
1381 if (!res)
1382 return -PSICONV_E_NOMEM;
1383 else
1384 return res;
1385 }
1386
1387
1388 int psiconv_parse_sheet_line_list(const psiconv_buffer buf, int lev,
1389 psiconv_u32 off, int *length,
1390 psiconv_sheet_line_list *result,
1391 const psiconv_sheet_cell_layout default_layout)
1392 {
1393 int res=0;
1394 int len=0;
1395 psiconv_u32 temp;
1396 psiconv_sheet_line line;
1397 psiconv_u32 listlen,i;
1398 int leng;
1399
1400 psiconv_progress(lev+1,off,"Going to read the sheet line list");
1401 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_line_s))))
1402 goto ERROR1;
1403
1404 psiconv_progress(lev+2,off+len,
1405 "Going to read the initial byte (%02x expected)",0x02);
1406 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1407 if (res)
1408 goto ERROR2;
1409 if (temp != 0x02) {
1410 psiconv_warn(lev+2,off+len,
1411 "Sheet line list initial byte unknown value (ignored)");
1412 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1413 }
1414 len ++;
1415
1416 psiconv_progress(lev+2,off+len,
1417 "Going to read the number of defined lines");
1418 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1419 if (res)
1420 goto ERROR2;
1421 psiconv_debug(lev+2,off+len,"Number of defined lines: %d",listlen);
1422 len += leng;
1423
1424 psiconv_progress(lev+2,off+len,"Going to read all lines");
1425 for (i = 0; i < listlen; i++) {
1426 psiconv_progress(lev+3,off+len,"Going to read line %d",i);
1427 if ((res = psiconv_parse_sheet_line(buf,lev+3,off+len,&leng,&line,
1428 default_layout)))
1429 goto ERROR2;
1430 if ((res = psiconv_list_add(*result,line)))
1431 goto ERROR3;
1432 free(line);
1433 len += leng;
1434 }
1435
1436 if (length)
1437 *length = len;
1438
1439 psiconv_progress(lev,off+len-1,
1440 "End of sheet line list (total length: %08x)", len);
1441 return 0;
1442
1443 ERROR3:
1444 psiconv_free_sheet_line(line);
1445 ERROR2:
1446 psiconv_free_sheet_line_list(*result);
1447 ERROR1:
1448 psiconv_warn(lev+1,off,"Reading of Sheet Line List failed");
1449 if (length)
1450 *length = 0;
1451 if (!res)
1452 return -PSICONV_E_NOMEM;
1453 else
1454 return res;
1455 }
1456
1457 int psiconv_parse_sheet_variable(const psiconv_buffer buf, int lev,
1458 psiconv_u32 off, int *length,
1459 psiconv_sheet_variable *result)
1460 {
1461 int res=0;
1462 int len=0;
1463 psiconv_u32 marker;
1464 int leng;
1465
1466 psiconv_progress(lev+1,off,"Going to read a sheet variable");
1467 if (!(*result = malloc(sizeof(**result))))
1468 goto ERROR1;
1469
1470 psiconv_progress(lev+2,off+len, "Going to read the variable name");
1471 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
1472 if (res)
1473 goto ERROR2;
1474 len += leng;
1475
1476 psiconv_progress(lev+2,off+len,"Going to read the type marker");
1477 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
1478 if (res)
1479 goto ERROR3;
1480 psiconv_debug(lev+2,off+len,"Marker: %02x",marker);
1481 len ++;
1482
1483 if (marker == 0x00) {
1484 (*result)->type = psiconv_var_int;
1485 psiconv_progress(lev+2,off+len,"Going to read a signed integer");
1486 (*result)->data.dat_int = psiconv_read_sint(buf,lev+2,off+len,&leng,&res);
1487 if (res)
1488 goto ERROR3;
1489 psiconv_debug(lev+2,off+len,"Value: %d",(*result)->data.dat_int);
1490 len += leng;
1491 } else if (marker == 0x01) {
1492 (*result)->type = psiconv_var_float;
1493 psiconv_progress(lev+2,off+len,"Going to read a floating point number");
1494 (*result)->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng,
1495 &res);
1496 if (res)
1497 goto ERROR3;
1498 psiconv_debug(lev+2,off+len,"Value: %f",(*result)->data.dat_float);
1499 len += leng;
1500 } else if (marker == 0x02) {
1501 (*result)->type = psiconv_var_string;
1502 psiconv_progress(lev+2,off+len,"Going to read a string");
1503 (*result)->data.dat_string = psiconv_read_string(buf,lev+2,off+len,&leng,
1504 &res);
1505 if (res)
1506 goto ERROR3;
1507 len += leng;
1508 } else if (marker == 0x03) {
1509 (*result)->type = psiconv_var_cellref;
1510 psiconv_progress(lev+2,off+len,"Going to read a cell reference");
1511 (*result)->data.dat_cellref = psiconv_read_var_cellref(buf,lev+2,off+len,
1512 &leng, &res);
1513 if (res)
1514 goto ERROR3;
1515 len += leng;
1516 } else if (marker == 0x04) {
1517 (*result)->type = psiconv_var_cellblock;
1518 psiconv_progress(lev+2,off+len,"Going to read a cell block reference");
1519 (*result)->data.dat_cellblock = psiconv_read_var_cellblock(buf,lev+2,
1520 off+len,
1521 &leng, &res);
1522 if (res)
1523 goto ERROR3;
1524 len += leng;
1525 } else {
1526 psiconv_warn(lev+2,off+len,"Sheet variable unknown type marker");
1527 res = -PSICONV_E_PARSE;
1528 goto ERROR3;
1529 }
1530
1531 psiconv_progress(lev+2,off+len,"Going to read the variable number");
1532 (*result)->number = psiconv_read_u32(buf,lev+2,off+len,&res);
1533 if (res)
1534 goto ERROR4;
1535 psiconv_debug(lev+2,off+len,"Number: %08x",(*result)->number);
1536 len += 4;
1537
1538 if (length)
1539 *length = len;
1540
1541 psiconv_progress(lev,off+len-1,
1542 "End of sheet variable (total length: %08x)", len);
1543 return 0;
1544
1545 ERROR4:
1546 if ((*result)->type == psiconv_var_string)
1547 free((*result)->data.dat_string);
1548 ERROR3:
1549 free((*result)->name);
1550 ERROR2:
1551 free (*result);
1552 ERROR1:
1553 psiconv_warn(lev+1,off,"Reading of Sheet Variable failed");
1554 if (length)
1555 *length = 0;
1556 if (!res)
1557 return -PSICONV_E_NOMEM;
1558 else
1559 return res;
1560 }
1561
1562
1563 int psiconv_parse_sheet_variable_list(const psiconv_buffer buf, int lev,
1564 psiconv_u32 off, int *length,
1565 psiconv_sheet_variable_list *result)
1566 {
1567 int res=0;
1568 int len=0;
1569 psiconv_u32 temp;
1570 psiconv_sheet_variable variable;
1571 psiconv_u32 listlen,i;
1572 int leng;
1573
1574 psiconv_progress(lev+1,off,"Going to read the sheet variable list");
1575 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_variable_s))))
1576 goto ERROR1;
1577
1578 psiconv_progress(lev+2,off+len,
1579 "Going to read the initial byte (%02x expected)",0x02);
1580 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1581 if (res)
1582 goto ERROR2;
1583 if (temp != 0x02) {
1584 psiconv_warn(lev+2,off+len,
1585 "Sheet variable list initial byte unknown value (ignored)");
1586 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1587 }
1588 len ++;
1589
1590 psiconv_progress(lev+2,off+len,
1591 "Going to read the number of variables");
1592 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1593 if (res)
1594 goto ERROR2;
1595 psiconv_debug(lev+2,off+len,"Number of variables: %d",listlen);
1596 len += leng;
1597
1598 psiconv_progress(lev+2,off+len,"Going to read all variables");
1599 for (i = 0; i < listlen; i++) {
1600 psiconv_progress(lev+3,off+len,"Going to read variable %d",i);
1601 if ((res = psiconv_parse_sheet_variable(buf,lev+3,off+len,&leng,&variable)))
1602 goto ERROR2;
1603 if ((res = psiconv_list_add(*result,variable)))
1604 goto ERROR3;
1605 len += leng;
1606 }
1607
1608 if (length)
1609 *length = len;
1610
1611 psiconv_progress(lev,off+len-1,
1612 "End of sheet variabels list (total length: %08x)", len);
1613 return 0;
1614
1615 ERROR3:
1616 psiconv_free_sheet_variable(variable);
1617 ERROR2:
1618 psiconv_list_free(*result);
1619 ERROR1:
1620 psiconv_warn(lev+1,off,"Reading of Sheet Variable list failed");
1621 if (length)
1622 *length = 0;
1623 if (!res)
1624 return -PSICONV_E_NOMEM;
1625 else
1626 return res;
1627 }

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