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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 142 - (show annotations)
Tue Jan 29 18:38:38 2002 UTC (22 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 29418 byte(s)
(Frodo) DMALLOC support

1 /*
2 parse_formula.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 #ifdef DMALLOC
29 #include <dmalloc.h>
30 #endif
31
32
33 struct formula_element
34 {
35 psiconv_formula_type_t formula_type;
36 int number_of_args;
37 const char* name;
38 };
39
40 static struct formula_element formula_elements[256] =
41 {
42 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* 0 */
43 {psiconv_formula_op_lt,2,"<"},
44 {psiconv_formula_op_le,2,"<="},
45 {psiconv_formula_op_gt,2,">"},
46 {psiconv_formula_op_ge,2,">="},
47 {psiconv_formula_op_ne,2,"<>"},
48 {psiconv_formula_op_eq,2,"="},
49 {psiconv_formula_op_add,2,"+"},
50 {psiconv_formula_op_sub,2,"-"},
51 {psiconv_formula_op_mul,2,"*"},
52 {psiconv_formula_op_div,2,"/"},
53 {psiconv_formula_op_pow,2,"^"},
54 {psiconv_formula_op_pos,1,"+"},
55 {psiconv_formula_op_neg,1,"-"},
56 {psiconv_formula_op_not,1,"NOT"},
57 {psiconv_formula_op_and,2,"AND"},
58 {psiconv_formula_op_or,2,"OR"}, /* 10 */
59 {psiconv_formula_op_con,2,"&"},
60 {psiconv_formula_op_bra,1,"()"},
61 {psiconv_formula_unknown,0,"*UNKNOWN*"},
62 {psiconv_formula_unknown,0,"*UNKNOWN*"},
63 {psiconv_formula_mark_eof,0,"End of formula"},
64 {psiconv_formula_unknown,0,"*UNKNOWN*"},
65 {psiconv_formula_unknown,0,"*UNKNOWN*"},
66 {psiconv_formula_unknown,0,"*UNKNOWN*"},
67 {psiconv_formula_unknown,0,"*UNKNOWN*"},
68 {psiconv_formula_unknown,0,"*UNKNOWN*"},
69 {psiconv_formula_unknown,0,"*UNKNOWN*"},
70 {psiconv_formula_unknown,0,"*UNKNOWN*"},
71 {psiconv_formula_unknown,0,"*UNKNOWN*"},
72 {psiconv_formula_unknown,0,"*UNKNOWN*"},
73 {psiconv_formula_dat_float,0,"Floating point number"},
74 {psiconv_formula_dat_int,0,"Signed integer number"}, /* 20 */
75 {psiconv_formula_unknown,0,"*UNKNOWN*"},
76 {psiconv_formula_unknown,0,"*UNKNOWN*"},
77 {psiconv_formula_unknown,0,"*UNKNOWN*"},
78 {psiconv_formula_unknown,0,"*UNKNOWN*"},
79 {psiconv_formula_dat_var,0,"Named variable"},
80 {psiconv_formula_dat_string,0,"String"},
81 {psiconv_formula_dat_cellref,0,"Cell reference"},
82 {psiconv_formula_dat_cellblock,0,"Cell block"},
83 {psiconv_formula_dat_vcellblock,0,"Cell block {varargs}"},
84 {psiconv_formula_mark_opsep,0,"Operand separator"},
85 {psiconv_formula_mark_opend,0,"Operand list end"},
86 {psiconv_formula_unknown,0,"*UNKNOWN*"},
87 {psiconv_formula_unknown,0,"*UNKNOWN*"},
88 {psiconv_formula_unknown,0,"*UNKNOWN*"},
89 {psiconv_formula_unknown,0,"*UNKNOWN*"},
90 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* 30 */
91 {psiconv_formula_unknown,0,"*UNKNOWN*"},
92 {psiconv_formula_unknown,0,"*UNKNOWN*"},
93 {psiconv_formula_fun_false,0,"FALSE"},
94 {psiconv_formula_fun_if,3,"IF"},
95 {psiconv_formula_fun_true,0,"TRUE"},
96 {psiconv_formula_fun_cell,2,"CELL"},
97 {psiconv_formula_fun_errortype,0,"ERRORTYPE"},
98 {psiconv_formula_fun_isblank,1,"ISBLANK"},
99 {psiconv_formula_fun_iserr,1,"ISERR"},
100 {psiconv_formula_fun_iserror,1,"ISERROR"},
101 {psiconv_formula_fun_islogical,1,"ISLOGICAL"},
102 {psiconv_formula_fun_isna,1,"ISNA"},
103 {psiconv_formula_fun_isnontext,1,"ISNONTEXT"},
104 {psiconv_formula_fun_isnumber,1,"ISNUMBER"},
105 {psiconv_formula_fun_istext,1,"ISTEXT"},
106 {psiconv_formula_fun_n,1,"N"}, /* 40 */
107 {psiconv_formula_fun_type,1,"TYPE"},
108 {psiconv_formula_fun_address,2,"ADDRESS"},
109 {psiconv_formula_fun_column,1,"COLUMN"},
110 {psiconv_formula_fun_columns,1,"COLUMNS"},
111 {psiconv_formula_fun_hlookup,3,"HLOOKUP"},
112 {psiconv_formula_fun_index,3,"INDEX"},
113 {psiconv_formula_fun_indirect,1,"INDIRECT"},
114 {psiconv_formula_fun_lookup,3,"LOOKUP"},
115 {psiconv_formula_fun_offset,3,"OFFSET"},
116 {psiconv_formula_fun_row,1,"ROW"},
117 {psiconv_formula_fun_rows,1,"ROWS"},
118 {psiconv_formula_fun_vlookup,3,"VLOOKUP"},
119 {psiconv_formula_fun_char,1,"CHAR"},
120 {psiconv_formula_fun_code,1,"CODE"},
121 {psiconv_formula_fun_exact,2,"EXACT"},
122 {psiconv_formula_fun_find,3,"FIND"}, /* 50 */
123 {psiconv_formula_fun_left,2,"LEFT"},
124 {psiconv_formula_fun_len,1,"LEN"},
125 {psiconv_formula_fun_lower,1,"LOWER"},
126 {psiconv_formula_fun_mid,3,"MID"},
127 {psiconv_formula_fun_proper,1,"PROPER"},
128 {psiconv_formula_fun_replace,4,"REPLACE"},
129 {psiconv_formula_fun_rept,2,"REPT"},
130 {psiconv_formula_fun_right,2,"RIGHT"},
131 {psiconv_formula_fun_string,2,"STRING"},
132 {psiconv_formula_fun_t,1,"T"},
133 {psiconv_formula_fun_trim,1,"TRIM"},
134 {psiconv_formula_fun_upper,1,"UPPER"},
135 {psiconv_formula_fun_value,1,"VALUE"},
136 {psiconv_formula_fun_date,3,"DATE"},
137 {psiconv_formula_fun_datevalue,1,"DATEVALUE"},
138 {psiconv_formula_fun_day,1,"DAY"}, /* 60 */
139 {psiconv_formula_fun_hour,1,"HOUR"},
140 {psiconv_formula_fun_minute,1,"MINUTE"},
141 {psiconv_formula_fun_month,1,"MONTH"},
142 {psiconv_formula_fun_now,0,"NOW"},
143 {psiconv_formula_fun_second,1,"SECOND"},
144 {psiconv_formula_fun_today,0,"TODAY"},
145 {psiconv_formula_fun_time,3,"TIME"},
146 {psiconv_formula_fun_timevalue,1,"TIMEVALUE"},
147 {psiconv_formula_fun_year,1,"YEAR"},
148 {psiconv_formula_fun_abs,1,"ABS"},
149 {psiconv_formula_fun_acos,1,"ACOS"},
150 {psiconv_formula_fun_asin,1,"ASIN"},
151 {psiconv_formula_fun_atan,1,"ATAN"},
152 {psiconv_formula_fun_atan2,2,"ATAN2"},
153 {psiconv_formula_fun_cos,1,"COS"},
154 {psiconv_formula_fun_degrees,0,"DEGREES"}, /* 70 */
155 {psiconv_formula_fun_exp,1,"EXP"},
156 {psiconv_formula_fun_fact,1,"FACT"},
157 {psiconv_formula_fun_int,1,"INT"},
158 {psiconv_formula_fun_ln,1,"LN"},
159 {psiconv_formula_fun_log10,1,"LOG10"},
160 {psiconv_formula_fun_mod,2,"MOD"},
161 {psiconv_formula_fun_pi,0,"PI"},
162 {psiconv_formula_fun_radians,1,"RADIANS"},
163 {psiconv_formula_fun_rand,0,"RAND"},
164 {psiconv_formula_fun_round,2,"ROUND"},
165 {psiconv_formula_fun_sign,1,"SIGN"},
166 {psiconv_formula_fun_sin,1,"SIN"},
167 {psiconv_formula_fun_sqrt,1,"SQRT"},
168 {psiconv_formula_fun_sumproduct,2,"SUMPRODUCT"},
169 {psiconv_formula_fun_tan,1,"TAN"},
170 {psiconv_formula_fun_trunc,1,"TRUNC"}, /* 80 */
171 {psiconv_formula_fun_cterm,3,"CTERM"},
172 {psiconv_formula_fun_ddb,4,"DDB"},
173 {psiconv_formula_fun_fv,3,"FV"},
174 {psiconv_formula_fun_irr,2,"IRR"},
175 {psiconv_formula_fun_npv,2,"NPV"},
176 {psiconv_formula_fun_pmt,3,"PMT"},
177 {psiconv_formula_fun_pv,3,"PV"},
178 {psiconv_formula_fun_rate,3,"RATE"},
179 {psiconv_formula_fun_sln,3,"SLN"},
180 {psiconv_formula_fun_syd,4,"SYD"},
181 {psiconv_formula_fun_term,3,"TERM"},
182 {psiconv_formula_fun_combin,2,"COMBIN"},
183 {psiconv_formula_fun_permut,2,"PERMUT"},
184 {psiconv_formula_vfn_average,-1,"AVERAGE"},
185 {psiconv_formula_vfn_choose,-1,"CHOOSE"},
186 {psiconv_formula_vfn_count,-1,"COUNT"}, /* 90 */
187 {psiconv_formula_vfn_counta,-1,"COUNTA"},
188 {psiconv_formula_vfn_countblank,-1,"COUNTBLANK"},
189 {psiconv_formula_vfn_max,-1,"MAX"},
190 {psiconv_formula_vfn_min,-1,"MIN"},
191 {psiconv_formula_vfn_product,-1,"PRODUCT"},
192 {psiconv_formula_vfn_stdevp,-1,"STDEVP"},
193 {psiconv_formula_vfn_stdev,-1,"STDEV"},
194 {psiconv_formula_vfn_sum,-1,"SUM"},
195 {psiconv_formula_vfn_sumsq,-1,"SUMSQ"},
196 {psiconv_formula_vfn_varp,-1,"VARP"},
197 {psiconv_formula_vfn_var,-1,"VAR"},
198 {psiconv_formula_unknown,0,"*UNKNOWN*"},
199 {psiconv_formula_unknown,0,"*UNKNOWN*"},
200 {psiconv_formula_unknown,0,"*UNKNOWN*"},
201 {psiconv_formula_unknown,0,"*UNKNOWN*"},
202 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* A0 */
203 {psiconv_formula_unknown,0,"*UNKNOWN*"},
204 {psiconv_formula_unknown,0,"*UNKNOWN*"},
205 {psiconv_formula_unknown,0,"*UNKNOWN*"},
206 {psiconv_formula_unknown,0,"*UNKNOWN*"},
207 {psiconv_formula_unknown,0,"*UNKNOWN*"},
208 {psiconv_formula_unknown,0,"*UNKNOWN*"},
209 {psiconv_formula_unknown,0,"*UNKNOWN*"},
210 {psiconv_formula_unknown,0,"*UNKNOWN*"},
211 {psiconv_formula_unknown,0,"*UNKNOWN*"},
212 {psiconv_formula_unknown,0,"*UNKNOWN*"},
213 {psiconv_formula_unknown,0,"*UNKNOWN*"},
214 {psiconv_formula_unknown,0,"*UNKNOWN*"},
215 {psiconv_formula_unknown,0,"*UNKNOWN*"},
216 {psiconv_formula_unknown,0,"*UNKNOWN*"},
217 {psiconv_formula_unknown,0,"*UNKNOWN*"},
218 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* B0 */
219 {psiconv_formula_unknown,0,"*UNKNOWN*"},
220 {psiconv_formula_unknown,0,"*UNKNOWN*"},
221 {psiconv_formula_unknown,0,"*UNKNOWN*"},
222 {psiconv_formula_unknown,0,"*UNKNOWN*"},
223 {psiconv_formula_unknown,0,"*UNKNOWN*"},
224 {psiconv_formula_unknown,0,"*UNKNOWN*"},
225 {psiconv_formula_unknown,0,"*UNKNOWN*"},
226 {psiconv_formula_unknown,0,"*UNKNOWN*"},
227 {psiconv_formula_unknown,0,"*UNKNOWN*"},
228 {psiconv_formula_unknown,0,"*UNKNOWN*"},
229 {psiconv_formula_unknown,0,"*UNKNOWN*"},
230 {psiconv_formula_unknown,0,"*UNKNOWN*"},
231 {psiconv_formula_unknown,0,"*UNKNOWN*"},
232 {psiconv_formula_unknown,0,"*UNKNOWN*"},
233 {psiconv_formula_unknown,0,"*UNKNOWN*"},
234 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* C0 */
235 {psiconv_formula_unknown,0,"*UNKNOWN*"},
236 {psiconv_formula_unknown,0,"*UNKNOWN*"},
237 {psiconv_formula_unknown,0,"*UNKNOWN*"},
238 {psiconv_formula_unknown,0,"*UNKNOWN*"},
239 {psiconv_formula_unknown,0,"*UNKNOWN*"},
240 {psiconv_formula_unknown,0,"*UNKNOWN*"},
241 {psiconv_formula_unknown,0,"*UNKNOWN*"},
242 {psiconv_formula_unknown,0,"*UNKNOWN*"},
243 {psiconv_formula_unknown,0,"*UNKNOWN*"},
244 {psiconv_formula_unknown,0,"*UNKNOWN*"},
245 {psiconv_formula_unknown,0,"*UNKNOWN*"},
246 {psiconv_formula_unknown,0,"*UNKNOWN*"},
247 {psiconv_formula_unknown,0,"*UNKNOWN*"},
248 {psiconv_formula_unknown,0,"*UNKNOWN*"},
249 {psiconv_formula_unknown,0,"*UNKNOWN*"},
250 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* D0 */
251 {psiconv_formula_unknown,0,"*UNKNOWN*"},
252 {psiconv_formula_unknown,0,"*UNKNOWN*"},
253 {psiconv_formula_unknown,0,"*UNKNOWN*"},
254 {psiconv_formula_unknown,0,"*UNKNOWN*"},
255 {psiconv_formula_unknown,0,"*UNKNOWN*"},
256 {psiconv_formula_unknown,0,"*UNKNOWN*"},
257 {psiconv_formula_unknown,0,"*UNKNOWN*"},
258 {psiconv_formula_unknown,0,"*UNKNOWN*"},
259 {psiconv_formula_unknown,0,"*UNKNOWN*"},
260 {psiconv_formula_unknown,0,"*UNKNOWN*"},
261 {psiconv_formula_unknown,0,"*UNKNOWN*"},
262 {psiconv_formula_unknown,0,"*UNKNOWN*"},
263 {psiconv_formula_unknown,0,"*UNKNOWN*"},
264 {psiconv_formula_unknown,0,"*UNKNOWN*"},
265 {psiconv_formula_unknown,0,"*UNKNOWN*"},
266 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* E0 */
267 {psiconv_formula_unknown,0,"*UNKNOWN*"},
268 {psiconv_formula_unknown,0,"*UNKNOWN*"},
269 {psiconv_formula_unknown,0,"*UNKNOWN*"},
270 {psiconv_formula_unknown,0,"*UNKNOWN*"},
271 {psiconv_formula_unknown,0,"*UNKNOWN*"},
272 {psiconv_formula_unknown,0,"*UNKNOWN*"},
273 {psiconv_formula_unknown,0,"*UNKNOWN*"},
274 {psiconv_formula_unknown,0,"*UNKNOWN*"},
275 {psiconv_formula_unknown,0,"*UNKNOWN*"},
276 {psiconv_formula_unknown,0,"*UNKNOWN*"},
277 {psiconv_formula_unknown,0,"*UNKNOWN*"},
278 {psiconv_formula_unknown,0,"*UNKNOWN*"},
279 {psiconv_formula_unknown,0,"*UNKNOWN*"},
280 {psiconv_formula_unknown,0,"*UNKNOWN*"},
281 {psiconv_formula_unknown,0,"*UNKNOWN*"},
282 {psiconv_formula_unknown,0,"*UNKNOWN*"}, /* F0 */
283 {psiconv_formula_unknown,0,"*UNKNOWN*"},
284 {psiconv_formula_unknown,0,"*UNKNOWN*"},
285 {psiconv_formula_unknown,0,"*UNKNOWN*"},
286 {psiconv_formula_unknown,0,"*UNKNOWN*"},
287 {psiconv_formula_unknown,0,"*UNKNOWN*"},
288 {psiconv_formula_unknown,0,"*UNKNOWN*"},
289 {psiconv_formula_unknown,0,"*UNKNOWN*"},
290 {psiconv_formula_unknown,0,"*UNKNOWN*"},
291 {psiconv_formula_unknown,0,"*UNKNOWN*"},
292 {psiconv_formula_unknown,0,"*UNKNOWN*"},
293 {psiconv_formula_unknown,0,"*UNKNOWN*"},
294 {psiconv_formula_unknown,0,"*UNKNOWN*"}};
295
296 static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev,
297 psiconv_u32 off, int *length,
298 psiconv_sheet_ref_t *result)
299 {
300 int res;
301 psiconv_u16 temp;
302
303 psiconv_progress(lev+1,off,"Going to read a sheet ref");
304 psiconv_progress(lev+2,off,"Going to read the offset encoding");
305 temp = psiconv_read_u16(buf,lev+2,off,&res);
306 if (res) {
307 if (length)
308 *length = 0;
309 return res;
310 }
311 psiconv_debug(lev+2,off,"Encoded word: %04x",temp);
312 result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false;
313 result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1);
314 psiconv_debug(lev+2,off,"Reference: %s offset %d",
315 result->absolute?"absolute":"relative",result->offset);
316 if (length)
317 *length = 2;
318 return 0;
319 }
320
321 static int psiconv_parse_sheet_cell_reference(const psiconv_buffer buf,int lev,
322 psiconv_u32 off, int *length,
323 psiconv_sheet_cell_reference_t *result)
324 {
325 int len = 0;
326 int leng,res;
327 psiconv_u8 temp;
328
329 psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference");
330 psiconv_progress(lev+2,off+len,"Going to read the row reference");
331 if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->row)))
332 goto ERROR;
333 len += leng;
334 psiconv_progress(lev+2,off+len,"Going to read the column reference");
335 if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->column)))
336 goto ERROR;
337 len += leng;
338
339 psiconv_progress(lev+2,off+len,
340 "Going to read the trailing byte (%02x expected)",0);
341 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
342 if (res)
343 goto ERROR;
344 if (temp != 0) {
345 psiconv_warn(lev+2,off+len,"Unknown byte in cell reference (ignored");
346 psiconv_debug(lev+2,off+len,"Trailing byte: %02x",temp);
347 }
348 len ++;
349 psiconv_progress(lev,off+len-1,
350 "End of cell reference (total length: %08x)", len);
351 if (length)
352 *length = len;
353 return 0;
354 ERROR:
355 if (length)
356 *length = 0;
357 return res;
358 }
359
360 static int psiconv_parse_sheet_cell_block(const psiconv_buffer buf,int lev,
361 psiconv_u32 off, int *length,
362 psiconv_sheet_cell_block_t *result)
363 {
364 int len = 0;
365 int leng,res;
366
367 psiconv_progress(lev+1,off+len,"Going to read a sheet cell block");
368 psiconv_progress(lev+2,off+len,"Going to read the first cell");
369 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
370 &result->first)))
371 goto ERROR;
372 len += leng;
373 psiconv_progress(lev+2,off+len,"Going to read the last cell");
374 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
375 &result->last)))
376 goto ERROR;
377 len += leng;
378 psiconv_progress(lev,off+len-1,
379 "End of cell block (total length: %08x)", len);
380 if (length)
381 *length = len;
382 return 0;
383 ERROR:
384 if (length)
385 *length = 0;
386 return res;
387 }
388
389 static int psiconv_parse_formula_element_list(const psiconv_buffer buf, int lev,
390 psiconv_u32 off, int *length,
391 psiconv_formula *result,
392 psiconv_u32 maxlen)
393 {
394 int res=0;
395 int len=0;
396 int leng;
397 int eof = 0;
398 psiconv_u8 marker,submarker,submarker2;
399 psiconv_formula_list formula_stack;
400 psiconv_formula formula,subformula,subformula1,subformula2,
401 subformula3,subformula4;
402 psiconv_u16 temp,nr_of_subs;
403
404 psiconv_progress(lev+1,off,"Going to read a formula element list");
405 if (!(*result = malloc(sizeof(**result))))
406 goto ERROR1;
407 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
408 goto ERROR2;
409 if (!(formula = malloc(sizeof(*formula))))
410 goto ERROR3;
411 /* By setting the type to unknown, we can safely call psiconv_free_formula */
412 formula->type = psiconv_formula_unknown;
413 if (!(subformula1 = malloc(sizeof(*subformula1))))
414 goto ERROR4;
415 subformula1->type = psiconv_formula_unknown;
416 if (!(subformula2 = malloc(sizeof(*subformula2))))
417 goto ERROR5;
418 subformula2->type = psiconv_formula_unknown;
419 if (!(subformula3 = malloc(sizeof(*subformula3))))
420 goto ERROR6;
421 subformula3->type = psiconv_formula_unknown;
422 if (!(subformula4 = malloc(sizeof(*subformula4))))
423 goto ERROR7;
424 subformula4->type = psiconv_formula_unknown;
425
426 while (!eof && len+off < maxlen) {
427 psiconv_progress(lev+3,off+len,"Going to read a formula item marker");
428 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
429 if (res)
430 goto ERROR8;
431 psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker,
432 formula_elements[marker].name);
433 len ++;
434
435 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
436 psiconv_warn(lev+3,off+len,"Unknown formula marker found!");
437 goto ERROR8;
438 } else if ((formula_elements[marker].formula_type ==
439 psiconv_formula_mark_eof) ||
440 (formula_elements[marker].formula_type ==
441 psiconv_formula_mark_opend) ||
442 (formula_elements[marker].formula_type ==
443 psiconv_formula_mark_opsep)) {
444 len--;
445 psiconv_progress(lev+3,off+len,"End of this formula list");
446 eof = 1;
447 } else if (formula_elements[marker].formula_type ==
448 psiconv_formula_dat_int) {
449 psiconv_progress(lev+3,off+len,"Next item: an integer");
450 formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res);
451 if (res)
452 goto ERROR8;
453 formula->type = formula_elements[marker].formula_type;
454 psiconv_debug(lev+3,off+len,"Value: %08x",formula->data.dat_int);
455 len += 4;
456 if ((res = psiconv_list_add(formula_stack,formula)))
457 goto ERROR8;
458 formula->type = psiconv_formula_unknown;
459 } else if (formula_elements[marker].formula_type ==
460 psiconv_formula_dat_float) {
461 psiconv_progress(lev+3,off+len,"Next item: a float");
462 formula->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng,
463 &res);
464 if (res)
465 goto ERROR8;
466 formula->type = formula_elements[marker].formula_type;
467 psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float);
468 len += leng;
469 if ((res = psiconv_list_add(formula_stack,formula)))
470 goto ERROR8;
471 formula->type = psiconv_formula_unknown;
472 } else if (formula_elements[marker].formula_type ==
473 psiconv_formula_dat_cellref) {
474 psiconv_progress(lev+3,off+len,"Next item: a cell reference");
475 if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng,
476 &formula->data.dat_cellref)))
477 goto ERROR8;
478 formula->type = formula_elements[marker].formula_type;
479 len += leng;
480 if ((res = psiconv_list_add(formula_stack,formula)))
481 goto ERROR8;
482 formula->type = psiconv_formula_unknown;
483 } else if ((formula_elements[marker].formula_type ==
484 psiconv_formula_dat_cellblock) ||
485 (formula_elements[marker].formula_type ==
486 psiconv_formula_dat_vcellblock)) {
487 psiconv_progress(lev+3,off+len,"Next item: a cell block");
488 if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng,
489 &formula->data.dat_cellblock)))
490 goto ERROR8;
491 formula->type = formula_elements[marker].formula_type;
492 len += leng;
493 if ((res = psiconv_list_add(formula_stack,formula)))
494 goto ERROR8;
495 formula->type = psiconv_formula_unknown;
496 } else if (formula_elements[marker].formula_type ==
497 psiconv_formula_dat_string) {
498 psiconv_progress(lev+3,off+len,"Next item: a string");
499 formula->data.dat_string =
500 psiconv_read_short_string(buf,lev+2,off+len,&leng,&res);
501 if (res)
502 goto ERROR8;
503 formula->type = formula_elements[marker].formula_type;
504 len += leng;
505 if ((res = psiconv_list_add(formula_stack,formula)))
506 goto ERROR8;
507 formula->type = psiconv_formula_unknown;
508 } else if ((formula_elements[marker].formula_type ==
509 psiconv_formula_dat_var)) {
510 psiconv_progress(lev+3,off+len,"Next item: a variable reference");
511 formula->data.dat_variable = psiconv_read_u32(buf,lev+2,off+len,&res);
512 if (res)
513 goto ERROR8;
514 formula->type = formula_elements[marker].formula_type;
515 len += 4;
516 if ((res = psiconv_list_add(formula_stack,formula)))
517 goto ERROR8;
518 formula->type = psiconv_formula_unknown;
519 } else if (formula_elements[marker].number_of_args == -1) {
520 psiconv_progress(lev+3,off+len,"Going to parse a vararg function");
521 if (!(formula->data.fun_operands =
522 psiconv_list_new(sizeof(*formula))))
523 goto ERROR8;
524 formula->type = formula_elements[marker].formula_type;
525 nr_of_subs = 0;
526 do {
527 nr_of_subs ++;
528 psiconv_progress(lev+4,off+len,"Going to read vararg argument %d",
529 nr_of_subs);
530 if ((res = psiconv_parse_formula_element_list(buf,lev+4,off+len,&leng,
531 &subformula,maxlen)))
532 goto ERROR8;
533 len += leng;
534 if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) {
535 psiconv_free_formula(subformula);
536 goto ERROR8;
537 }
538 free(subformula);
539 psiconv_progress(lev+4,off+len,"Going to read the next marker");
540 submarker = psiconv_read_u8(buf,lev+4,off+len,&res);
541 len ++;
542 if (res)
543 goto ERROR8;
544 submarker2 = psiconv_read_u8(buf,lev+4,off+len,&res);
545 if (res)
546 goto ERROR8;
547 } while ((formula_elements[submarker].formula_type
548 == psiconv_formula_mark_opsep) &&
549 (formula_elements[submarker2].formula_type
550 != psiconv_formula_mark_opend));
551 if ((formula_elements[submarker].formula_type ==
552 psiconv_formula_mark_opsep) &&
553 (formula_elements[submarker2].formula_type ==
554 psiconv_formula_mark_opend)) {
555 submarker=submarker2;
556 len++;
557 }
558 if (formula_elements[submarker].formula_type
559 != psiconv_formula_mark_opend) {
560 psiconv_warn(lev+3,off+len,"Formula corrupted!");
561 psiconv_debug(lev+3,off+len,"Found unexpected marker %02x",submarker);
562 goto ERROR8;
563 }
564 psiconv_progress(lev+3,off+len,"Going to read the repeated marker %02x",
565 marker);
566 submarker = psiconv_read_u8(buf,lev+3,off+len,&res);
567 if (res)
568 goto ERROR8;
569 if (submarker != marker) {
570 psiconv_warn(lev+3,off+len,"Formula corrupted!");
571 psiconv_debug(lev+3,off+len,"Expected marker %02x, found %02x",
572 marker,submarker);
573 goto ERROR8;
574 }
575 len++;
576 psiconv_progress(lev+3,off+len,
577 "Going to read the number of arguments (%d expected)",
578 nr_of_subs);
579 temp = psiconv_read_u16(buf,lev+3,off+len,&res);
580 if (res)
581 goto ERROR8;
582 if (temp != nr_of_subs) {
583 psiconv_warn(lev+3,off+len,"Formula corrupted!");
584 psiconv_debug(lev+3,off+len,
585 "Read %d arguments, but formula says there are %d",
586 nr_of_subs,temp);
587 goto ERROR8;
588 }
589 len += 2;
590 if ((res = psiconv_list_add(formula_stack,formula)))
591 goto ERROR8;
592 formula->type = psiconv_formula_unknown;
593 } else {
594 if (formula_elements[marker].number_of_args > 0)
595 if ((res = psiconv_list_pop(formula_stack,subformula1)))
596 goto ERROR8;
597 if (formula_elements[marker].number_of_args > 1)
598 if ((res = psiconv_list_pop(formula_stack,subformula2)))
599 goto ERROR8;
600 if (formula_elements[marker].number_of_args > 2)
601 if ((res = psiconv_list_pop(formula_stack,subformula3)))
602 goto ERROR8;
603 if (formula_elements[marker].number_of_args > 3)
604 if ((res = psiconv_list_pop(formula_stack,subformula4)))
605 goto ERROR8;
606 if (!(formula->data.fun_operands =
607 psiconv_list_new(sizeof(*formula))))
608 goto ERROR8;
609 formula->type = formula_elements[marker].formula_type;
610 if (formula_elements[marker].number_of_args > 3)
611 if ((res = psiconv_list_add(formula->data.fun_operands,subformula4)))
612 goto ERROR8;
613 if (formula_elements[marker].number_of_args > 2)
614 if ((res = psiconv_list_add(formula->data.fun_operands,subformula3)))
615 goto ERROR8;
616 if (formula_elements[marker].number_of_args > 1)
617 if ((res = psiconv_list_add(formula->data.fun_operands,subformula2)))
618 goto ERROR8;
619 if (formula_elements[marker].number_of_args > 0)
620 if ((res = psiconv_list_add(formula->data.fun_operands,subformula1)))
621 goto ERROR8;
622 if ((res = psiconv_list_add(formula_stack,formula)))
623 goto ERROR8;
624 subformula4->type = subformula3->type = subformula2->type =
625 subformula1->type = formula->type = psiconv_formula_unknown;
626 }
627 }
628 if ((len+off > maxlen) || !eof) {
629 psiconv_warn(lev+2,off+len,"Formula corrupted!");
630 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
631 maxlen,len+off);
632 goto ERROR8;
633 }
634 if ((psiconv_list_length(formula_stack)) != 1) {
635 psiconv_warn(lev+2,off+len,"Formula corrupted!");
636 psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)",
637 psiconv_list_length(formula_stack));
638 goto ERROR8;
639 }
640 if ((res = psiconv_list_pop(formula_stack,*result)))
641 goto ERROR8;
642 psiconv_list_free(formula_stack);
643 free(formula);
644
645 if (length)
646 *length = len;
647
648 psiconv_progress(lev,off+len-1,
649 "End of formula element list (total length: %08x)", len);
650 return 0;
651
652 ERROR8:
653 psiconv_free_formula(subformula4);
654 ERROR7:
655 psiconv_free_formula(subformula3);
656 ERROR6:
657 psiconv_free_formula(subformula2);
658 ERROR5:
659 psiconv_free_formula(subformula1);
660 ERROR4:
661 psiconv_free_formula(formula);
662 ERROR3:
663 psiconv_free_formula_list(formula_stack);
664 ERROR2:
665 free (*result);
666 ERROR1:
667 psiconv_warn(lev+1,off,"Reading of formula element list failed");
668 if (length)
669 *length = 0;
670 if (!res)
671 return -PSICONV_E_NOMEM;
672 else
673 return res;
674 }
675
676
677
678
679 int psiconv_parse_formula(const psiconv_buffer buf, int lev,
680 psiconv_u32 off, int *length,
681 psiconv_formula *result)
682 {
683 int res=0;
684 int len=0;
685 int leng;
686 psiconv_u32 bytelen,formula_end;
687 psiconv_u8 temp;
688
689 psiconv_progress(lev+1,off,"Going to read a formula");
690
691 psiconv_progress(lev+2,off+len,
692 "Going to read the formula byte length");
693 bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res);
694 if (res)
695 goto ERROR1;
696 psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen);
697 len += leng;
698 bytelen += len;
699 formula_end = off + bytelen;
700
701 psiconv_progress(lev+2,off+len,"Going to read the formula elements list");
702 if ((res = psiconv_parse_formula_element_list(buf,lev+2,off+len,&leng,
703 result,formula_end)))
704 goto ERROR1;
705 len += leng;
706
707 psiconv_progress(lev+2,off+len,"Going to read the eof marker");
708 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
709 if (res)
710 goto ERROR2;
711 if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) {
712 psiconv_warn(lev+2,off+len,"Formula corrupted!");
713 psiconv_debug(lev+2,off+len,"Expected marker: %02x, found byte: %02x",
714 0x15,temp);
715 goto ERROR2;
716 }
717 len ++;
718
719 if (off+len != formula_end) {
720 psiconv_warn(lev+2,off+len,"Formula corrupted!");
721 psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x",
722 formula_end,len+off);
723 goto ERROR2;
724 }
725
726 if (length)
727 *length = len;
728
729 psiconv_progress(lev,off+len-1,
730 "End of formula (total length: %08x)", len);
731 return 0;
732
733 ERROR2:
734 psiconv_free_formula(*result);
735 ERROR1:
736 psiconv_warn(lev+1,off,"Reading of formula failed");
737 if (length)
738 *length = 0;
739 if (!res)
740 return -PSICONV_E_NOMEM;
741 else
742 return res;
743 }
744
745

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