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 |
len += leng; |
670 |
} |
671 |
|
672 |
if (length) |
673 |
*length = len; |
674 |
|
675 |
psiconv_progress(lev,off+len-1, |
676 |
"End of sheet cell list (total length: %08x)", len); |
677 |
return 0; |
678 |
|
679 |
ERROR3: |
680 |
psiconv_free_sheet_cell(cell); |
681 |
ERROR2: |
682 |
psiconv_free_sheet_cell_list(*result); |
683 |
ERROR1: |
684 |
psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed"); |
685 |
if (length) |
686 |
*length = 0; |
687 |
if (!res) |
688 |
return -PSICONV_E_NOMEM; |
689 |
else |
690 |
return res; |
691 |
} |
692 |
|
693 |
|
694 |
int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev, |
695 |
psiconv_u32 off, int *length, |
696 |
psiconv_sheet_worksheet_list *result) |
697 |
{ |
698 |
psiconv_sheet_worksheet worksheet; |
699 |
int res=0; |
700 |
int len=0; |
701 |
psiconv_u8 temp; |
702 |
psiconv_u32 offset; |
703 |
int leng,i,nr; |
704 |
|
705 |
psiconv_progress(lev+1,off,"Going to read the worksheet list"); |
706 |
if (!(*result = psiconv_list_new(sizeof(*worksheet)))) |
707 |
goto ERROR1; |
708 |
|
709 |
psiconv_progress(lev+2,off+len, |
710 |
"Going to read the initial bytes (%02x expected)",0x02); |
711 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
712 |
if (res) |
713 |
goto ERROR2; |
714 |
if (temp != 0x02) { |
715 |
psiconv_warn(lev+2,off+len, |
716 |
"Sheet worksheet list initial byte unknown value (ignored)"); |
717 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
718 |
} |
719 |
len ++; |
720 |
|
721 |
psiconv_progress(lev+2,off+len,"Going to read the list length"); |
722 |
nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res); |
723 |
if (res) |
724 |
goto ERROR2; |
725 |
psiconv_debug(lev+2,off+len,"Length: %02x",nr); |
726 |
len += leng; |
727 |
|
728 |
psiconv_progress(lev+2,off+len,"Going to read the list"); |
729 |
for (i=0 ; i < nr; i++) { |
730 |
psiconv_progress(lev+3,off+len,"Going to read element %d",i); |
731 |
psiconv_progress(lev+4,off+len, |
732 |
"Going to read the initial byte (%02x expected)",0x00); |
733 |
temp = psiconv_read_u8(buf,lev+4,off+len,&res); |
734 |
if (res) |
735 |
goto ERROR2; |
736 |
if (temp != 0x00) { |
737 |
psiconv_warn(lev+4,off+len, |
738 |
"Sheet worksheet element initial byte unknown value (ignored)"); |
739 |
psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp); |
740 |
} |
741 |
len ++; |
742 |
|
743 |
psiconv_progress(lev+4,off+len,"Going to read the worksheet offset"); |
744 |
offset = psiconv_read_u32(buf,lev+2,off+len,&res); |
745 |
if (res) |
746 |
goto ERROR2; |
747 |
psiconv_debug(lev+4,off+len,"Offset: %08x",offset); |
748 |
len += 4; |
749 |
|
750 |
if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL, |
751 |
&worksheet))) |
752 |
goto ERROR2; |
753 |
if ((res = psiconv_list_add(*result,worksheet))) |
754 |
goto ERROR3; |
755 |
psiconv_free_sheet_worksheet(worksheet); |
756 |
} |
757 |
|
758 |
if (length) |
759 |
*length = len; |
760 |
|
761 |
psiconv_progress(lev,off+len-1, |
762 |
"End of worksheet list (total length: %08x)", len); |
763 |
|
764 |
return 0; |
765 |
|
766 |
ERROR3: |
767 |
psiconv_free_sheet_worksheet(worksheet); |
768 |
ERROR2: |
769 |
psiconv_free_sheet_worksheet_list(*result); |
770 |
ERROR1: |
771 |
psiconv_warn(lev+1,off,"Reading of worksheet list failed"); |
772 |
if (length) |
773 |
*length = 0; |
774 |
if (!res) |
775 |
return -PSICONV_E_NOMEM; |
776 |
else |
777 |
return res; |
778 |
} |
779 |
|
780 |
int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev, |
781 |
psiconv_u32 off, int *length, |
782 |
psiconv_sheet_cell_layout *result) |
783 |
|
784 |
{ |
785 |
int res=0; |
786 |
int len=0; |
787 |
int leng; |
788 |
psiconv_u8 temp; |
789 |
|
790 |
psiconv_progress(lev+1,off,"Going to read a sheet cell layout"); |
791 |
if (!(*result = malloc(sizeof(**result)))) |
792 |
goto ERROR1; |
793 |
|
794 |
(*result)->character = NULL; |
795 |
(*result)->paragraph = NULL; |
796 |
(*result)->numberformat = NULL; |
797 |
|
798 |
psiconv_progress(lev+2,off+len, |
799 |
"Going to read the first byte (%02x expected)",0x02); |
800 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
801 |
if (res) |
802 |
goto ERROR2; |
803 |
if (temp != 0x02) { |
804 |
psiconv_warn(lev+2,off+len, |
805 |
"Worksheet section initial byte unknown value (ignored)"); |
806 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
807 |
} |
808 |
len ++; |
809 |
|
810 |
psiconv_progress(lev+2,off+len,"Going to read the default formats flag"); |
811 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
812 |
if (res) |
813 |
goto ERROR2; |
814 |
len ++; |
815 |
|
816 |
if (temp & 0x01) { |
817 |
if (!((*result)->paragraph = psiconv_basic_paragraph_layout())) |
818 |
goto ERROR2; |
819 |
psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes"); |
820 |
if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, |
821 |
(*result)->paragraph))) |
822 |
goto ERROR2; |
823 |
len += leng; |
824 |
} |
825 |
|
826 |
if (temp & 0x02) { |
827 |
psiconv_progress(lev+3,off+len,"Going to read the default character codes"); |
828 |
if (!((*result)->character = psiconv_basic_character_layout())) |
829 |
goto ERROR2; |
830 |
if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, |
831 |
(*result)->character))) |
832 |
goto ERROR2; |
833 |
len += leng; |
834 |
} |
835 |
|
836 |
if (temp & 0x04) { |
837 |
psiconv_progress(lev+3,off+len, "Going to read the default number format"); |
838 |
psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng, |
839 |
&(*result)->numberformat); |
840 |
len += leng; |
841 |
} |
842 |
|
843 |
if (length) |
844 |
*length = len; |
845 |
|
846 |
psiconv_progress(lev,off+len-1, |
847 |
"End of sheet cell layout (total length: %08x)", len); |
848 |
|
849 |
return 0; |
850 |
|
851 |
ERROR2: |
852 |
psiconv_free_sheet_cell_layout(*result); |
853 |
ERROR1: |
854 |
psiconv_warn(lev+1,off,"Reading of sheet cell layout failed"); |
855 |
if (length) |
856 |
*length = 0; |
857 |
if (!res) |
858 |
return -PSICONV_E_NOMEM; |
859 |
else |
860 |
return res; |
861 |
} |
862 |
|
863 |
|
864 |
int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev, |
865 |
psiconv_u32 off, int *length, |
866 |
psiconv_sheet_worksheet *result) |
867 |
{ |
868 |
int res=0; |
869 |
psiconv_u32 temp,cells_off,grid_off; |
870 |
int len=0; |
871 |
int leng; |
872 |
|
873 |
psiconv_progress(lev+1,off,"Going to read the sheet worksheet section"); |
874 |
if (!(*result = malloc(sizeof(**result)))) |
875 |
goto ERROR1; |
876 |
|
877 |
psiconv_progress(lev+2,off+len, |
878 |
"Going to read the initial bytes (%02x expected)",0x04); |
879 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
880 |
if (res) |
881 |
goto ERROR2; |
882 |
if (temp != 0x04) { |
883 |
psiconv_warn(lev+2,off+len, |
884 |
"Worksheet section initial byte unknown value (ignored)"); |
885 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
886 |
} |
887 |
len ++; |
888 |
|
889 |
psiconv_progress(lev+2,off+len, "Going to read the flags byte"); |
890 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
891 |
if (res) |
892 |
goto ERROR2; |
893 |
psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp); |
894 |
(*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false; |
895 |
if (temp & 0xfe) { |
896 |
psiconv_warn(lev+2,off+len, |
897 |
"Worksheet section flags byte unknown bits (ignored)"); |
898 |
} |
899 |
len ++; |
900 |
|
901 |
psiconv_progress(lev+2,off+len,"Going to read the default cell layout"); |
902 |
if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng, |
903 |
&(*result)->default_layout))) |
904 |
goto ERROR2; |
905 |
len += leng; |
906 |
|
907 |
psiconv_progress(lev+2,off+len, |
908 |
"Going to read the offset of the 1st ??? Section"); |
909 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
910 |
if (res) |
911 |
goto ERROR3; |
912 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
913 |
len += 4; |
914 |
|
915 |
psiconv_progress(lev+2,off+len, |
916 |
"Going to read the offset of the 2nd ??? Section"); |
917 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
918 |
if (res) |
919 |
goto ERROR3; |
920 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
921 |
len += 4; |
922 |
|
923 |
psiconv_progress(lev+2,off+len, |
924 |
"Going to read the offset of the Cells List"); |
925 |
cells_off = psiconv_read_u32(buf,lev+2,off+len,&res); |
926 |
if (res) |
927 |
goto ERROR3; |
928 |
psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off); |
929 |
len += 4; |
930 |
|
931 |
psiconv_progress(lev+2,off+len, |
932 |
"Going to read the offset of the Grid Section"); |
933 |
grid_off = psiconv_read_u32(buf,lev+2,off+len,&res); |
934 |
if (res) |
935 |
goto ERROR3; |
936 |
psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off); |
937 |
len += 4; |
938 |
|
939 |
psiconv_progress(lev+2,off+len, |
940 |
"Going to read the offset of the 3rd ??? Section"); |
941 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
942 |
if (res) |
943 |
goto ERROR3; |
944 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
945 |
len += 4; |
946 |
|
947 |
psiconv_progress(lev+2,off+len,"Going to read the cells list"); |
948 |
if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL, |
949 |
&(*result)->cells))) |
950 |
goto ERROR3; |
951 |
|
952 |
|
953 |
/* TODO: parse grid section */ |
954 |
|
955 |
if (length) |
956 |
*length = len; |
957 |
|
958 |
psiconv_progress(lev,off+len-1, |
959 |
"End of sheet worksheet section (total length: %08x)", len); |
960 |
return 0; |
961 |
|
962 |
ERROR3: |
963 |
psiconv_free_sheet_cell_layout((*result)->default_layout); |
964 |
ERROR2: |
965 |
free (*result); |
966 |
ERROR1: |
967 |
psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed"); |
968 |
if (length) |
969 |
*length = 0; |
970 |
if (!res) |
971 |
return -PSICONV_E_NOMEM; |
972 |
else |
973 |
return res; |
974 |
} |
975 |
|
976 |
|