/[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 120 - (show annotations)
Sun Jul 1 12:01:59 2001 UTC (19 years, 1 month ago) by frodo
File MIME type: text/plain
File size: 32133 byte(s)
(Frodo) Fixed segfault

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 int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev,
29 psiconv_u32 off, int *length,
30 psiconv_sheet_numberformat *result)
31 {
32 int res=0;
33 int len=0;
34 psiconv_u8 temp;
35
36 psiconv_progress(lev+1,off,"Going to read a sheet numberformat");
37 if (!(*result = malloc(sizeof(**result))))
38 goto ERROR1;
39
40 psiconv_progress(lev+2,off+len,
41 "Going to read the initial byte (%02x expected)",0x02);
42 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
43 if (res)
44 goto ERROR2;
45 if (temp != 0x02) {
46 psiconv_warn(lev+2,off+len,
47 "Sheet numberformat initial byte unknown value (ignored)");
48 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
49 }
50 len ++;
51
52 psiconv_progress(lev+2,off+len, "Going to read the code byte");
53 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
54 if (res)
55 goto ERROR2;
56 psiconv_debug(lev+2,off+len,"Code: %02x",temp);
57 if (temp == 0x00)
58 (*result)->code = psiconv_numberformat_general;
59 else if (temp == 0x02)
60 (*result)->code = psiconv_numberformat_fixeddecimal;
61 else if (temp == 0x04)
62 (*result)->code = psiconv_numberformat_scientific;
63 else if (temp == 0x06)
64 (*result)->code = psiconv_numberformat_currency;
65 else if (temp == 0x08)
66 (*result)->code = psiconv_numberformat_percent;
67 else if (temp == 0x0A)
68 (*result)->code = psiconv_numberformat_triads;
69 else if (temp == 0x0C)
70 (*result)->code = psiconv_numberformat_boolean;
71 else if (temp == 0x0E)
72 (*result)->code = psiconv_numberformat_text;
73 else if (temp == 0x10)
74 (*result)->code = psiconv_numberformat_date_ddmm;
75 else if (temp == 0x12)
76 (*result)->code = psiconv_numberformat_date_mmdd;
77 else if (temp == 0x14)
78 (*result)->code = psiconv_numberformat_date_ddmmyy;
79 else if (temp == 0x16)
80 (*result)->code = psiconv_numberformat_date_mmddyy;
81 else if (temp == 0x18)
82 (*result)->code = psiconv_numberformat_date_yymmdd;
83 else if (temp == 0x1A)
84 (*result)->code = psiconv_numberformat_date_ddmmm;
85 else if (temp == 0x1C)
86 (*result)->code = psiconv_numberformat_date_ddmmmyy;
87 else if (temp == 0x1E)
88 (*result)->code = psiconv_numberformat_date_ddmmmyyyy;
89 else if (temp == 0x20)
90 (*result)->code = psiconv_numberformat_date_mmm;
91 else if (temp == 0x22)
92 (*result)->code = psiconv_numberformat_date_monthname;
93 else if (temp == 0x24)
94 (*result)->code = psiconv_numberformat_date_mmmyy;
95 else if (temp == 0x26)
96 (*result)->code = psiconv_numberformat_date_monthnameyy;
97 else if (temp == 0x28)
98 (*result)->code = psiconv_numberformat_date_monthnameddyyyy;
99 else if (temp == 0x2A)
100 (*result)->code = psiconv_numberformat_datetime_ddmmyyyyhhii;
101 else if (temp == 0x2C)
102 (*result)->code = psiconv_numberformat_datetime_ddmmyyyyHHii;
103 else if (temp == 0x2E)
104 (*result)->code = psiconv_numberformat_datetime_mmddyyyyhhii;
105 else if (temp == 0x30)
106 (*result)->code = psiconv_numberformat_datetime_mmddyyyyHHii;
107 else if (temp == 0x32)
108 (*result)->code = psiconv_numberformat_datetime_yyyymmddhhii;
109 else if (temp == 0x34)
110 (*result)->code = psiconv_numberformat_datetime_yyyymmddHHii;
111 else if (temp == 0x36)
112 (*result)->code = psiconv_numberformat_time_hhii;
113 else if (temp == 0x38)
114 (*result)->code = psiconv_numberformat_time_hhiiss;
115 else if (temp == 0x3A)
116 (*result)->code = psiconv_numberformat_time_HHii;
117 else if (temp == 0x3C)
118 (*result)->code = psiconv_numberformat_time_HHiiss;
119 else {
120 psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)");
121 (*result)->code = psiconv_numberformat_general;
122 }
123 len ++;
124
125 psiconv_progress(lev+2,off+len, "Going to read the number of decimals");
126 (*result)->decimal = psiconv_read_u8(buf,lev+2,off+len,&res);
127 if (res)
128 goto ERROR2;
129 psiconv_debug(lev+2,off+len,"Decimals: %d",(*result)->decimal);
130 len ++;
131
132 if (length)
133 *length = len;
134
135 psiconv_progress(lev,off+len-1,
136 "End of sheet number format (total length: %08x)", len);
137 return 0;
138
139 ERROR2:
140 free (*result);
141 ERROR1:
142 psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed");
143 if (length)
144 *length = 0;
145 if (!res)
146 return -PSICONV_E_NOMEM;
147 else
148 return res;
149 }
150
151 int psiconv_parse_sheet_status_section(const psiconv_buffer buf, int lev,
152 psiconv_u32 off, int *length,
153 psiconv_sheet_status_section *result)
154 {
155 int res=0;
156 int len=0;
157 psiconv_u32 temp;
158 int leng;
159
160 psiconv_progress(lev+1,off,"Going to read the sheet status section");
161 if (!(*result = malloc(sizeof(**result))))
162 goto ERROR1;
163
164 psiconv_progress(lev+2,off+len,
165 "Going to read the initial byte (%02x expected)",0x02);
166 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
167 if (res)
168 goto ERROR2;
169 if (temp != 0x02) {
170 psiconv_warn(lev+2,off+len,
171 "Sheet status section initial byte unknown value (ignored)");
172 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
173 }
174 len ++;
175
176 psiconv_progress(lev+2,off+len,
177 "Going to read the cursor row");
178 (*result)->cursor_row = psiconv_read_u32(buf,lev+2,off + len,&res);
179 if (res)
180 goto ERROR2;
181 psiconv_debug(lev+2,off+len,"Cursor row: %08x",
182 (*result)->cursor_row);
183 len += 0x04;
184
185 psiconv_progress(lev+2,off+len,
186 "Going to read the cursor column");
187 (*result)->cursor_column = psiconv_read_u32(buf,lev+2,off + len,&res);
188 if (res)
189 goto ERROR2;
190 psiconv_debug(lev+2,off+len,"Cursor column: %08x",
191 (*result)->cursor_column);
192 len += 0x04;
193
194 psiconv_progress(lev+2,off+len,"Going to read initially display graph");
195 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
196 &(*result)->show_graph)))
197 goto ERROR2;
198 len += leng;
199
200 psiconv_progress(lev+2,off+len,
201 "Going to read the toolbar status byte");
202 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
203 if (res)
204 goto ERROR2;
205
206 (*result)->show_side_sheet_toolbar = temp&0x01 ? psiconv_bool_true :
207 psiconv_bool_false;
208 psiconv_debug(lev+2,off+len,"Show side sheet toolbar: %02x",
209 (*result)->show_side_sheet_toolbar);
210 (*result)->show_top_sheet_toolbar = temp&0x02 ? psiconv_bool_true :
211 psiconv_bool_false;
212 psiconv_debug(lev+2,off+len,"Show top sheet toolbar: %02x",
213 (*result)->show_top_sheet_toolbar);
214 (*result)->show_side_graph_toolbar = temp&0x04 ? psiconv_bool_true :
215 psiconv_bool_false;
216 psiconv_debug(lev+2,off+len,"Show side graph toolbar: %02x",
217 (*result)->show_side_graph_toolbar);
218 (*result)->show_top_graph_toolbar = temp&0x08 ? psiconv_bool_true :
219 psiconv_bool_false;
220 psiconv_debug(lev+2,off+len,"Show top graph toolbar: %02x",
221 (*result)->show_top_graph_toolbar);
222 if (temp & 0xf0) {
223 psiconv_warn(lev+2,off+len,"Sheet status section toolbar byte "
224 "flags contains unknown flags (ignored)");
225 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0);
226 }
227 len ++;
228
229 psiconv_progress(lev+2,off+len,
230 "Going to read the scrollbar status byte");
231 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
232 if (res)
233 goto ERROR2;
234 if ((temp & 0x03) == 0x03) {
235 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte "
236 "flags contains unknown flags (ignored)");
237 psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x03);
238 }
239 (*result)->show_horizontal_scrollbar = (temp&0x03) == 1? psiconv_triple_off :
240 (temp&0x03) == 2? psiconv_triple_auto:
241 psiconv_triple_on;
242 psiconv_debug(lev+2,off+len,"Show horizontal scrollbar: %02x",
243 (*result)->show_horizontal_scrollbar);
244 if ((temp & 0x0c) == 0x0c) {
245 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte "
246 "flags contains unknown flags (ignored)");
247 psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x0c);
248 }
249 (*result)->show_vertical_scrollbar = (temp&0x0c) ==0x04? psiconv_triple_off:
250 (temp&0x0c) ==0x08? psiconv_triple_auto:
251 psiconv_triple_on;
252 psiconv_debug(lev+2,off+len,"Show vertical scrollbar: %02x",
253 (*result)->show_vertical_scrollbar);
254 if (temp & 0xf0) {
255 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte "
256 "flags contains unknown flags (ignored)");
257 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0);
258 }
259 len ++;
260
261 psiconv_progress(lev+2,off+len,
262 "Going to read an unknown byte (%02x expected)",0x00);
263 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
264 if (res)
265 goto ERROR2;
266 if (temp != 0x00) {
267 psiconv_warn(lev+2,off+len,
268 "Sheet status section unknown byte unknown value (ignored)");
269 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
270 }
271 len ++;
272
273 psiconv_progress(lev+2,off+len,"Going to read sheet display size");
274 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res);
275 if (res)
276 goto ERROR2;
277 psiconv_debug(lev+2,off+len,"Sheet display size: %08x",
278 (*result)->sheet_display_size);
279 len += 0x04;
280
281 psiconv_progress(lev+2,off+len,"Going to read graph display size");
282 (*result)->graph_display_size = psiconv_read_u32(buf,lev+2,off + len,&res);
283 if (res)
284 goto ERROR2;
285 psiconv_debug(lev+2,off+len,"Graph display size: %08x",
286 (*result)->graph_display_size);
287 len += 0x04;
288
289 if (length)
290 *length = len;
291
292 psiconv_progress(lev,off+len-1,
293 "End of sheet status section (total length: %08x)", len);
294 return 0;
295
296 ERROR2:
297 free (*result);
298 ERROR1:
299 psiconv_warn(lev+1,off,"Reading of Sheet Status Section failed");
300 if (length)
301 *length = 0;
302 if (!res)
303 return -PSICONV_E_NOMEM;
304 else
305 return res;
306 }
307
308 int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev,
309 psiconv_u32 off, int *length,
310 psiconv_sheet_workbook_section *result)
311 {
312 int res=0;
313 psiconv_u32 temp,formulas_off,worksheets_off;
314 int len=0;
315
316 psiconv_progress(lev+1,off,"Going to read the sheet workbook section");
317 if (!(*result = malloc(sizeof(**result))))
318 goto ERROR1;
319
320 psiconv_progress(lev+2,off+len,
321 "Going to read the initial byte (%02x expected)",0x04);
322 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
323 if (res)
324 goto ERROR2;
325 if (temp != 0x04) {
326 psiconv_warn(lev+2,off+len,
327 "Sheet workbook section initial byte unknown value (ignored)");
328 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
329 }
330 len ++;
331
332 psiconv_progress(lev+2,off+len,
333 "Going to read the offset of the 1st ??? Section");
334 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
335 if (res)
336 goto ERROR2;
337 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
338 len += 4;
339
340 psiconv_progress(lev+2,off+len,
341 "Going to read the offset of the Formulas List");
342 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res);
343 if (res)
344 goto ERROR2;
345 psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off);
346 len += 4;
347
348 psiconv_progress(lev+2,off+len,
349 "Going to read the offset of the Worksheet List");
350 worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res);
351 if (res)
352 goto ERROR2;
353 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off);
354 len += 4;
355
356 psiconv_progress(lev+2,off+len,
357 "Going to read the offset of the 4th ??? Section");
358 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
359 if (res)
360 goto ERROR2;
361 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
362 len += 4;
363
364 psiconv_progress(lev+2,off+len,"Going to read the formulas list");
365 if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL,
366 &(*result)->formulas)))
367 goto ERROR2;
368
369 psiconv_progress(lev+2,off+len,"Going to read the worksheet list");
370 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off,
371 NULL,&(*result)->worksheets)))
372 goto ERROR2;
373
374
375 if (length)
376 *length = len;
377
378 psiconv_progress(lev,off+len-1,
379 "End of sheet workbook section (total length: %08x)", len);
380 return 0;
381
382 ERROR2:
383 free (*result);
384 ERROR1:
385 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed");
386 if (length)
387 *length = 0;
388 if (!res)
389 return -PSICONV_E_NOMEM;
390 else
391 return res;
392 }
393
394 int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev,
395 psiconv_u32 off, int *length,
396 psiconv_formula_list *result)
397 {
398 int res=0;
399 int len=0;
400 psiconv_u32 temp;
401 psiconv_formula formula;
402 psiconv_u32 listlen,i;
403 int leng;
404
405 psiconv_progress(lev+1,off,"Going to read the sheet formula table");
406 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s))))
407 goto ERROR1;
408
409 psiconv_progress(lev+2,off+len,
410 "Going to read the initial byte (%02x expected)",0x02);
411 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
412 if (res)
413 goto ERROR2;
414 if (temp != 0x02) {
415 psiconv_warn(lev+2,off+len,
416 "Sheet formula table initial byte unknown value (ignored)");
417 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
418 }
419 len ++;
420
421 psiconv_progress(lev+2,off+len,
422 "Going to read the number of formulas");
423 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
424 if (res)
425 goto ERROR2;
426 psiconv_debug(lev+2,off+len,"Number of formulas: %d",listlen);
427 len += leng;
428
429 psiconv_progress(lev+2,off+len,"Going to read all formulas");
430 for (i = 0; i < listlen; i++) {
431 psiconv_progress(lev+3,off+len,"Going to read formula %d",i);
432 if ((res = psiconv_parse_formula(buf,lev+3,off+len,&leng,&formula)))
433 goto ERROR2;
434 if ((res = psiconv_list_add(*result,formula)))
435 goto ERROR3;
436 len += leng;
437 }
438
439 if (length)
440 *length = len;
441
442 psiconv_progress(lev,off+len-1,
443 "End of sheet formula table (total length: %08x)", len);
444 return 0;
445
446 ERROR3:
447 psiconv_free_formula(formula);
448 ERROR2:
449 psiconv_list_free(*result);
450 ERROR1:
451 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed");
452 if (length)
453 *length = 0;
454 if (!res)
455 return -PSICONV_E_NOMEM;
456 else
457 return res;
458 }
459
460 int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
461 psiconv_u32 off, int *length,
462 psiconv_sheet_cell *result)
463 {
464 int res=0;
465 int len=0;
466 psiconv_u32 temp;
467 psiconv_bool_t has_layout;
468 int leng;
469
470 psiconv_progress(lev+1,off,"Going to read a sheet cell structure");
471 if (!(*result = malloc(sizeof(**result))))
472 goto ERROR1;
473
474 (*result)->layout = NULL;
475 (*result)->type = psiconv_cell_blank;
476
477 psiconv_progress(lev+2,off+len,"Going to read the cell position");
478 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
479 if (res)
480 goto ERROR2;
481 len ++;
482 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 8;
483 if (res)
484 goto ERROR2;
485 len ++;
486 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 16;
487 if (res)
488 goto ERROR2;
489 len ++;
490 (*result)->column = (temp >> 2) & 0xFF;
491 (*result)->row = (temp >> 10) & 0x3FFF;
492 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x",
493 (*result)->column,(*result)->row);
494 if (temp & 0x03) {
495 psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)");
496 psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03);
497 }
498
499 psiconv_progress(lev+2,off+len,"Going to read the cell type");
500 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
501 if (res)
502 goto ERROR2;
503 len ++;
504 (*result)->type = (temp >> 5) & 0x07;
505 (*result)->calculated = (temp & 0x08)?psiconv_bool_true:psiconv_bool_false;
506 has_layout = (temp & 0x10)?psiconv_bool_true:psiconv_bool_false;
507
508 psiconv_progress(lev+2,off+len,"Going to read the cell value");
509 if ((*result)->type == psiconv_cell_blank) {
510 psiconv_debug(lev+2,off+len,"Cell type is blank: no value given.");
511 } else if ((*result)->type == psiconv_cell_int) {
512 psiconv_progress(lev+2,off+len,"Going to read an integer");
513 (*result)->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
514 if (res)
515 goto ERROR2;
516 len += 4;
517 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
518
519 } else if ((*result)->type == psiconv_cell_bool) {
520 psiconv_progress(lev+2,off+len,"Going to read a boolean");
521 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng,
522 &(*result)->data.dat_bool)))
523 goto ERROR2;
524 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp);
525 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
526 len += leng;
527 } else if ((*result)->type == psiconv_cell_error) {
528 psiconv_progress(lev+2,off+len,"Going to read the error code");
529 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
530 if (res)
531 goto ERROR2;
532 if (temp == 0)
533 (*result)->data.dat_error = psiconv_sheet_error_none;
534 else if (temp == 1)
535 (*result)->data.dat_error = psiconv_sheet_error_null;
536 else if (temp == 2)
537 (*result)->data.dat_error = psiconv_sheet_error_divzero;
538 else if (temp == 3)
539 (*result)->data.dat_error = psiconv_sheet_error_value;
540 else if (temp == 4)
541 (*result)->data.dat_error = psiconv_sheet_error_reference;
542 else if (temp == 5)
543 (*result)->data.dat_error = psiconv_sheet_error_name;
544 else if (temp == 6)
545 (*result)->data.dat_error = psiconv_sheet_error_number;
546 else if (temp == 7)
547 (*result)->data.dat_error = psiconv_sheet_error_notavail;
548 else {
549 psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)");
550 psiconv_debug(lev+2,off+len,"Error code: %04x",temp);
551 (*result)->data.dat_error = psiconv_sheet_error_none;
552 }
553 psiconv_debug(lev+2,off+len,"Cell contents: %04x",
554 (*result)->data.dat_error);
555 len += 2;
556 } else if ((*result)->type == psiconv_cell_float) {
557 psiconv_progress(lev+2,off+len,"Going to read a float");
558 (*result)->data.dat_float =
559 psiconv_read_float(buf,lev+2,off+len,&leng,&res);
560 if (res)
561 goto ERROR2;
562 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
563 len += leng;
564 } else if ((*result)->type == psiconv_cell_string) {
565 psiconv_progress(lev+2,off+len,"Going to read a string");
566 (*result)->data.dat_string =
567 psiconv_read_string(buf,lev+2,off+len,&leng,&res);
568 if (res)
569 goto ERROR2;
570 psiconv_debug(lev+2,off+len,"Cell contents: `%s'",
571 (*result)->data.dat_string);
572 len += leng;
573 } else {
574 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
575 res = PSICONV_E_PARSE;
576 goto ERROR2;
577 }
578
579 if (has_layout) {
580 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
581 &leng,&(*result)->layout)))
582 goto ERROR2;
583 len += leng;
584 }
585
586 if ((*result)->calculated) {
587 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference");
588 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
589 if (res)
590 goto ERROR2;
591 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp);
592 len += leng;
593 (*result)->ref_formula = temp;
594 }
595
596 if (length)
597 *length = len;
598
599 psiconv_progress(lev,off+len-1,
600 "End of sheet cell structure (total length: %08x)", len);
601 return 0;
602
603 ERROR2:
604 psiconv_free_sheet_cell(*result);
605 ERROR1:
606 psiconv_warn(lev+1,off,"Reading of Sheet Cell Structure failed");
607 if (length)
608 *length = 0;
609 if (!res)
610 return -PSICONV_E_NOMEM;
611 else
612 return res;
613 }
614
615 int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
616 psiconv_u32 off, int *length,
617 psiconv_sheet_cell_list *result)
618 {
619 int res=0;
620 int len=0;
621 psiconv_u32 temp;
622 psiconv_sheet_cell cell;
623 psiconv_u32 listlen,i;
624 int leng;
625
626 psiconv_progress(lev+1,off,"Going to read the sheet cell list");
627 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
628 goto ERROR1;
629
630 psiconv_progress(lev+2,off+len,
631 "Going to read the initial byte (%02x expected)",0x02);
632 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
633 if (res)
634 goto ERROR2;
635 if (temp != 0x02) {
636 psiconv_warn(lev+2,off+len,
637 "Sheet cell list initial byte unknown value (ignored)");
638 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
639 }
640 len ++;
641
642 psiconv_progress(lev+2,off+len,
643 "Going to read the initial byte (%02x expected)",0x00);
644 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
645 if (res)
646 goto ERROR2;
647 if (temp != 0x00) {
648 psiconv_warn(lev+2,off+len,
649 "Sheet cell list initial byte unknown value (ignored)");
650 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
651 }
652 len ++;
653
654 psiconv_progress(lev+2,off+len,
655 "Going to read the number of defined cells");
656 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
657 if (res)
658 goto ERROR2;
659 psiconv_debug(lev+2,off+len,"Number of defined cells: %d",listlen);
660 len += leng;
661
662 psiconv_progress(lev+2,off+len,"Going to read all cells");
663 for (i = 0; i < listlen; i++) {
664 psiconv_progress(lev+3,off+len,"Going to read cell %d",i);
665 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell)))
666 goto ERROR2;
667 if ((res = psiconv_list_add(*result,cell)))
668 goto ERROR3;
669 free(cell);
670 len += leng;
671 }
672
673 if (length)
674 *length = len;
675
676 psiconv_progress(lev,off+len-1,
677 "End of sheet cell list (total length: %08x)", len);
678 return 0;
679
680 ERROR3:
681 psiconv_free_sheet_cell(cell);
682 ERROR2:
683 psiconv_free_sheet_cell_list(*result);
684 ERROR1:
685 psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed");
686 if (length)
687 *length = 0;
688 if (!res)
689 return -PSICONV_E_NOMEM;
690 else
691 return res;
692 }
693
694
695 int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev,
696 psiconv_u32 off, int *length,
697 psiconv_sheet_worksheet_list *result)
698 {
699 psiconv_sheet_worksheet worksheet;
700 int res=0;
701 int len=0;
702 psiconv_u8 temp;
703 psiconv_u32 offset;
704 int leng,i,nr;
705
706 psiconv_progress(lev+1,off,"Going to read the worksheet list");
707 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
708 goto ERROR1;
709
710 psiconv_progress(lev+2,off+len,
711 "Going to read the initial bytes (%02x expected)",0x02);
712 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
713 if (res)
714 goto ERROR2;
715 if (temp != 0x02) {
716 psiconv_warn(lev+2,off+len,
717 "Sheet worksheet list initial byte unknown value (ignored)");
718 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
719 }
720 len ++;
721
722 psiconv_progress(lev+2,off+len,"Going to read the list length");
723 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
724 if (res)
725 goto ERROR2;
726 psiconv_debug(lev+2,off+len,"Length: %02x",nr);
727 len += leng;
728
729 psiconv_progress(lev+2,off+len,"Going to read the list");
730 for (i=0 ; i < nr; i++) {
731 psiconv_progress(lev+3,off+len,"Going to read element %d",i);
732 psiconv_progress(lev+4,off+len,
733 "Going to read the initial byte (%02x expected)",0x00);
734 temp = psiconv_read_u8(buf,lev+4,off+len,&res);
735 if (res)
736 goto ERROR2;
737 if (temp != 0x00) {
738 psiconv_warn(lev+4,off+len,
739 "Sheet worksheet element initial byte unknown value (ignored)");
740 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp);
741 }
742 len ++;
743
744 psiconv_progress(lev+4,off+len,"Going to read the worksheet offset");
745 offset = psiconv_read_u32(buf,lev+2,off+len,&res);
746 if (res)
747 goto ERROR2;
748 psiconv_debug(lev+4,off+len,"Offset: %08x",offset);
749 len += 4;
750
751 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL,
752 &worksheet)))
753 goto ERROR2;
754 if ((res = psiconv_list_add(*result,worksheet)))
755 goto ERROR3;
756 free(worksheet);
757 }
758
759 if (length)
760 *length = len;
761
762 psiconv_progress(lev,off+len-1,
763 "End of worksheet list (total length: %08x)", len);
764
765 return 0;
766
767 ERROR3:
768 psiconv_free_sheet_worksheet(worksheet);
769 ERROR2:
770 psiconv_free_sheet_worksheet_list(*result);
771 ERROR1:
772 psiconv_warn(lev+1,off,"Reading of worksheet list failed");
773 if (length)
774 *length = 0;
775 if (!res)
776 return -PSICONV_E_NOMEM;
777 else
778 return res;
779 }
780
781 int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
782 psiconv_u32 off, int *length,
783 psiconv_sheet_cell_layout *result)
784
785 {
786 int res=0;
787 int len=0;
788 int leng;
789 psiconv_u8 temp;
790
791 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
792 if (!(*result = malloc(sizeof(**result))))
793 goto ERROR1;
794
795 (*result)->character = NULL;
796 (*result)->paragraph = NULL;
797 (*result)->numberformat = NULL;
798
799 psiconv_progress(lev+2,off+len,
800 "Going to read the first byte (%02x expected)",0x02);
801 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
802 if (res)
803 goto ERROR2;
804 if (temp != 0x02) {
805 psiconv_warn(lev+2,off+len,
806 "Worksheet section initial byte unknown value (ignored)");
807 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
808 }
809 len ++;
810
811 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
812 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
813 if (res)
814 goto ERROR2;
815 len ++;
816
817 if (temp & 0x01) {
818 if (!((*result)->paragraph = psiconv_basic_paragraph_layout()))
819 goto ERROR2;
820 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
821 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
822 (*result)->paragraph)))
823 goto ERROR2;
824 len += leng;
825 }
826
827 if (temp & 0x02) {
828 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
829 if (!((*result)->character = psiconv_basic_character_layout()))
830 goto ERROR2;
831 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
832 (*result)->character)))
833 goto ERROR2;
834 len += leng;
835 }
836
837 if (temp & 0x04) {
838 psiconv_progress(lev+3,off+len, "Going to read the default number format");
839 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
840 &(*result)->numberformat);
841 len += leng;
842 }
843
844 if (length)
845 *length = len;
846
847 psiconv_progress(lev,off+len-1,
848 "End of sheet cell layout (total length: %08x)", len);
849
850 return 0;
851
852 ERROR2:
853 psiconv_free_sheet_cell_layout(*result);
854 ERROR1:
855 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
856 if (length)
857 *length = 0;
858 if (!res)
859 return -PSICONV_E_NOMEM;
860 else
861 return res;
862 }
863
864
865 int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
866 psiconv_u32 off, int *length,
867 psiconv_sheet_worksheet *result)
868 {
869 int res=0;
870 psiconv_u32 temp,cells_off,grid_off;
871 int len=0;
872 int leng;
873
874 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
875 if (!(*result = malloc(sizeof(**result))))
876 goto ERROR1;
877
878 psiconv_progress(lev+2,off+len,
879 "Going to read the initial bytes (%02x expected)",0x04);
880 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
881 if (res)
882 goto ERROR2;
883 if (temp != 0x04) {
884 psiconv_warn(lev+2,off+len,
885 "Worksheet section initial byte unknown value (ignored)");
886 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
887 }
888 len ++;
889
890 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
891 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
892 if (res)
893 goto ERROR2;
894 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp);
895 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
896 if (temp & 0xfe) {
897 psiconv_warn(lev+2,off+len,
898 "Worksheet section flags byte unknown bits (ignored)");
899 }
900 len ++;
901
902 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
903 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
904 &(*result)->default_layout)))
905 goto ERROR2;
906 len += leng;
907
908 psiconv_progress(lev+2,off+len,
909 "Going to read the offset of the 1st ??? Section");
910 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
911 if (res)
912 goto ERROR3;
913 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
914 len += 4;
915
916 psiconv_progress(lev+2,off+len,
917 "Going to read the offset of the 2nd ??? Section");
918 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
919 if (res)
920 goto ERROR3;
921 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
922 len += 4;
923
924 psiconv_progress(lev+2,off+len,
925 "Going to read the offset of the Cells List");
926 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
927 if (res)
928 goto ERROR3;
929 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
930 len += 4;
931
932 psiconv_progress(lev+2,off+len,
933 "Going to read the offset of the Grid Section");
934 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
935 if (res)
936 goto ERROR3;
937 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
938 len += 4;
939
940 psiconv_progress(lev+2,off+len,
941 "Going to read the offset of the 3rd ??? Section");
942 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
943 if (res)
944 goto ERROR3;
945 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
946 len += 4;
947
948 psiconv_progress(lev+2,off+len,"Going to read the cells list");
949 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
950 &(*result)->cells)))
951 goto ERROR3;
952
953
954 /* TODO: parse grid section */
955
956 if (length)
957 *length = len;
958
959 psiconv_progress(lev,off+len-1,
960 "End of sheet worksheet section (total length: %08x)", len);
961 return 0;
962
963 ERROR3:
964 psiconv_free_sheet_cell_layout((*result)->default_layout);
965 ERROR2:
966 free (*result);
967 ERROR1:
968 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed");
969 if (length)
970 *length = 0;
971 if (!res)
972 return -PSICONV_E_NOMEM;
973 else
974 return res;
975 }
976
977

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