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