/[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 107 - (show annotations)
Thu Feb 8 20:03:49 2001 UTC (23 years, 1 month ago) by frodo
File MIME type: text/plain
File size: 30051 byte(s)
(Frodo) Latest formula additions of Decsi

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

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