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_status_section(const psiconv_buffer buf, int lev, |
29 |
psiconv_u32 off, int *length, |
30 |
psiconv_sheet_status_section *result) |
31 |
{ |
32 |
int res=0; |
33 |
int len=0; |
34 |
psiconv_u32 temp; |
35 |
int leng; |
36 |
|
37 |
psiconv_progress(lev+1,off,"Going to read the sheet status section"); |
38 |
if (!(*result = malloc(sizeof(**result)))) |
39 |
goto ERROR1; |
40 |
|
41 |
psiconv_progress(lev+2,off+len, |
42 |
"Going to read the initial byte (%02x expected)",0x02); |
43 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
44 |
if (res) |
45 |
goto ERROR2; |
46 |
if (temp != 0x02) { |
47 |
psiconv_warn(lev+2,off+len, |
48 |
"Sheet status section initial byte unknown value (ignored)"); |
49 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
50 |
} |
51 |
len ++; |
52 |
|
53 |
psiconv_progress(lev+2,off+len, |
54 |
"Going to read the cursor row"); |
55 |
(*result)->cursor_row = psiconv_read_u32(buf,lev+2,off + len,&res); |
56 |
if (res) |
57 |
goto ERROR2; |
58 |
psiconv_debug(lev+2,off+len,"Cursor row: %08x", |
59 |
(*result)->cursor_row); |
60 |
len += 0x04; |
61 |
|
62 |
psiconv_progress(lev+2,off+len, |
63 |
"Going to read the cursor column"); |
64 |
(*result)->cursor_column = psiconv_read_u32(buf,lev+2,off + len,&res); |
65 |
if (res) |
66 |
goto ERROR2; |
67 |
psiconv_debug(lev+2,off+len,"Cursor column: %08x", |
68 |
(*result)->cursor_column); |
69 |
len += 0x04; |
70 |
|
71 |
psiconv_progress(lev+2,off+len,"Going to read initially display graph"); |
72 |
if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, |
73 |
&(*result)->show_graph))) |
74 |
goto ERROR2; |
75 |
len += leng; |
76 |
|
77 |
psiconv_progress(lev+2,off+len, |
78 |
"Going to read the toolbar status byte"); |
79 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
80 |
if (res) |
81 |
goto ERROR2; |
82 |
|
83 |
(*result)->show_side_sheet_toolbar = temp&0x01 ? psiconv_bool_true : |
84 |
psiconv_bool_false; |
85 |
psiconv_debug(lev+2,off+len,"Show side sheet toolbar: %02x", |
86 |
(*result)->show_side_sheet_toolbar); |
87 |
(*result)->show_top_sheet_toolbar = temp&0x02 ? psiconv_bool_true : |
88 |
psiconv_bool_false; |
89 |
psiconv_debug(lev+2,off+len,"Show top sheet toolbar: %02x", |
90 |
(*result)->show_top_sheet_toolbar); |
91 |
(*result)->show_side_graph_toolbar = temp&0x04 ? psiconv_bool_true : |
92 |
psiconv_bool_false; |
93 |
psiconv_debug(lev+2,off+len,"Show side graph toolbar: %02x", |
94 |
(*result)->show_side_graph_toolbar); |
95 |
(*result)->show_top_graph_toolbar = temp&0x08 ? psiconv_bool_true : |
96 |
psiconv_bool_false; |
97 |
psiconv_debug(lev+2,off+len,"Show top graph toolbar: %02x", |
98 |
(*result)->show_top_graph_toolbar); |
99 |
if (temp & 0xf0) { |
100 |
psiconv_warn(lev+2,off+len,"Sheet status section toolbar byte " |
101 |
"flags contains unknown flags (ignored)"); |
102 |
psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0); |
103 |
} |
104 |
len ++; |
105 |
|
106 |
psiconv_progress(lev+2,off+len, |
107 |
"Going to read the scrollbar status byte"); |
108 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
109 |
if (res) |
110 |
goto ERROR2; |
111 |
if ((temp & 0x03) == 0x03) { |
112 |
psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " |
113 |
"flags contains unknown flags (ignored)"); |
114 |
psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x03); |
115 |
} |
116 |
(*result)->show_horizontal_scrollbar = (temp&0x03) == 1? psiconv_triple_off : |
117 |
(temp&0x03) == 2? psiconv_triple_auto: |
118 |
psiconv_triple_on; |
119 |
psiconv_debug(lev+2,off+len,"Show horizontal scrollbar: %02x", |
120 |
(*result)->show_horizontal_scrollbar); |
121 |
if ((temp & 0x0c) == 0x0c) { |
122 |
psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " |
123 |
"flags contains unknown flags (ignored)"); |
124 |
psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x0c); |
125 |
} |
126 |
(*result)->show_vertical_scrollbar = (temp&0x0c) ==0x04? psiconv_triple_off: |
127 |
(temp&0x0c) ==0x08? psiconv_triple_auto: |
128 |
psiconv_triple_on; |
129 |
psiconv_debug(lev+2,off+len,"Show vertical scrollbar: %02x", |
130 |
(*result)->show_vertical_scrollbar); |
131 |
if (temp & 0xf0) { |
132 |
psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " |
133 |
"flags contains unknown flags (ignored)"); |
134 |
psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0); |
135 |
} |
136 |
len ++; |
137 |
|
138 |
psiconv_progress(lev+2,off+len, |
139 |
"Going to read an unknown byte (%02x expected)",0x00); |
140 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
141 |
if (res) |
142 |
goto ERROR2; |
143 |
if (temp != 0x00) { |
144 |
psiconv_warn(lev+2,off+len, |
145 |
"Sheet status section unknown byte unknown value (ignored)"); |
146 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
147 |
} |
148 |
len ++; |
149 |
|
150 |
psiconv_progress(lev+2,off+len,"Going to read sheet display size"); |
151 |
(*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); |
152 |
if (res) |
153 |
goto ERROR2; |
154 |
psiconv_debug(lev+2,off+len,"Sheet display size: %08x", |
155 |
(*result)->sheet_display_size); |
156 |
len += 0x04; |
157 |
|
158 |
psiconv_progress(lev+2,off+len,"Going to read graph display size"); |
159 |
(*result)->graph_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); |
160 |
if (res) |
161 |
goto ERROR2; |
162 |
psiconv_debug(lev+2,off+len,"Graph display size: %08x", |
163 |
(*result)->graph_display_size); |
164 |
len += 0x04; |
165 |
|
166 |
if (length) |
167 |
*length = len; |
168 |
|
169 |
psiconv_progress(lev,off+len-1, |
170 |
"End of sheet status section (total length: %08x)", len); |
171 |
return 0; |
172 |
|
173 |
ERROR2: |
174 |
free (*result); |
175 |
ERROR1: |
176 |
psiconv_warn(lev+1,off,"Reading of Sjeet Status Section failed"); |
177 |
if (length) |
178 |
*length = 0; |
179 |
if (!res) |
180 |
return -PSICONV_E_NOMEM; |
181 |
else |
182 |
return res; |
183 |
} |
184 |
|
185 |
int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev, |
186 |
psiconv_u32 off, int *length, |
187 |
psiconv_sheet_workbook_section *result) |
188 |
{ |
189 |
int res=0; |
190 |
psiconv_u32 temp,formulas_off; |
191 |
int len=0; |
192 |
|
193 |
psiconv_progress(lev+1,off,"Going to read the sheet workbook section"); |
194 |
if (!(*result = malloc(sizeof(**result)))) |
195 |
goto ERROR1; |
196 |
|
197 |
psiconv_progress(lev+2,off+len, |
198 |
"Going to read the initial byte (%02x expected)",0x04); |
199 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
200 |
if (res) |
201 |
goto ERROR2; |
202 |
if (temp != 0x04) { |
203 |
psiconv_warn(lev+2,off+len, |
204 |
"Sheet workbook section initial byte unknown value (ignored)"); |
205 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
206 |
} |
207 |
len ++; |
208 |
|
209 |
psiconv_progress(lev+2,off+len, |
210 |
"Going to read the offset of the 1st ??? Section"); |
211 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
212 |
if (res) |
213 |
goto ERROR2; |
214 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
215 |
len += 4; |
216 |
|
217 |
psiconv_progress(lev+2,off+len, |
218 |
"Going to read the offset of the Formulas List"); |
219 |
formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res); |
220 |
if (res) |
221 |
goto ERROR2; |
222 |
psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off); |
223 |
len += 4; |
224 |
|
225 |
psiconv_progress(lev+2,off+len, |
226 |
"Going to read the offset of the 3rd ??? Section"); |
227 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
228 |
if (res) |
229 |
goto ERROR2; |
230 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
231 |
len += 4; |
232 |
|
233 |
psiconv_progress(lev+2,off+len, |
234 |
"Going to read the offset of the 4th ??? Section"); |
235 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
236 |
if (res) |
237 |
goto ERROR2; |
238 |
psiconv_debug(lev+2,off+len,"Offset: %04x",temp); |
239 |
len += 4; |
240 |
|
241 |
psiconv_progress(lev+2,off+len,"Going to read the formulas list"); |
242 |
if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL, |
243 |
&(*result)->formulas))) |
244 |
goto ERROR2; |
245 |
|
246 |
|
247 |
if (length) |
248 |
*length = len; |
249 |
|
250 |
psiconv_progress(lev,off+len-1, |
251 |
"End of sheet workbook section (total length: %08x)", len); |
252 |
return 0; |
253 |
|
254 |
ERROR2: |
255 |
free (*result); |
256 |
ERROR1: |
257 |
psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed"); |
258 |
if (length) |
259 |
*length = 0; |
260 |
if (!res) |
261 |
return -PSICONV_E_NOMEM; |
262 |
else |
263 |
return res; |
264 |
} |
265 |
|
266 |
int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev, |
267 |
psiconv_u32 off, int *length, |
268 |
psiconv_formula_list *result) |
269 |
{ |
270 |
int res=0; |
271 |
int len=0; |
272 |
psiconv_u32 temp; |
273 |
psiconv_formula formula; |
274 |
psiconv_u32 listlen,i; |
275 |
int leng; |
276 |
|
277 |
psiconv_progress(lev+1,off,"Going to read the sheet formula table"); |
278 |
if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
279 |
goto ERROR1; |
280 |
|
281 |
psiconv_progress(lev+2,off+len, |
282 |
"Going to read the initial byte (%02x expected)",0x02); |
283 |
temp = psiconv_read_u8(buf,lev+2,off+len,&res); |
284 |
if (res) |
285 |
goto ERROR2; |
286 |
if (temp != 0x02) { |
287 |
psiconv_warn(lev+2,off+len, |
288 |
"Sheet formula table initial byte unknown value (ignored)"); |
289 |
psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); |
290 |
} |
291 |
len ++; |
292 |
|
293 |
psiconv_progress(lev+2,off+len, |
294 |
"Going to read the number of formulas"); |
295 |
listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res); |
296 |
if (res) |
297 |
goto ERROR2; |
298 |
psiconv_debug(lev+2,off+len,"Number of formulas: %d",listlen); |
299 |
len += leng; |
300 |
|
301 |
psiconv_progress(lev+2,off+len,"Going to read all formulas"); |
302 |
for (i = 0; i < listlen; i++) { |
303 |
psiconv_progress(lev+3,off+len,"Going to read formula %d",i); |
304 |
if ((res = psiconv_parse_formula(buf,lev+3,off+len,&leng,&formula))) |
305 |
goto ERROR2; |
306 |
if ((res = psiconv_list_add(*result,formula))) |
307 |
goto ERROR3; |
308 |
len += leng; |
309 |
} |
310 |
|
311 |
if (length) |
312 |
*length = len; |
313 |
|
314 |
psiconv_progress(lev,off+len-1, |
315 |
"End of sheet formula table (total length: %08x)", len); |
316 |
return 0; |
317 |
|
318 |
ERROR3: |
319 |
psiconv_free_formula(formula); |
320 |
ERROR2: |
321 |
psiconv_list_free(*result); |
322 |
ERROR1: |
323 |
psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); |
324 |
if (length) |
325 |
*length = 0; |
326 |
if (!res) |
327 |
return -PSICONV_E_NOMEM; |
328 |
else |
329 |
return res; |
330 |
} |