/[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 183 - (show annotations)
Sun Jan 4 22:20:43 2004 UTC (20 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 30281 byte(s)
(Frodo) More preparations for the Unicode change. Use psiconv_string everywhere
        possible (no more char * stuff)

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_config config,
297 const psiconv_buffer buf,int lev,
298 psiconv_u32 off, int *length,
299 psiconv_sheet_ref_t *result)
300 {
301 int res;
302 psiconv_u16 temp;
303
304 psiconv_progress(config,lev+1,off,"Going to read a sheet ref");
305 psiconv_progress(config,lev+2,off,"Going to read the offset encoding");
306 temp = psiconv_read_u16(config,buf,lev+2,off,&res);
307 if (res) {
308 if (length)
309 *length = 0;
310 return res;
311 }
312 psiconv_debug(config,lev+2,off,"Encoded word: %04x",temp);
313 result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false;
314 result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1);
315 psiconv_debug(config,lev+2,off,"Reference: %s offset %d",
316 result->absolute?"absolute":"relative",result->offset);
317 if (length)
318 *length = 2;
319 return 0;
320 }
321
322 static int psiconv_parse_sheet_cell_reference(const psiconv_config config,
323 const psiconv_buffer buf,int lev,
324 psiconv_u32 off, int *length,
325 psiconv_sheet_cell_reference_t *result)
326 {
327 int len = 0;
328 int leng,res;
329 psiconv_u8 temp;
330
331 psiconv_progress(config,lev+1,off+len,"Going to read a sheet cell reference");
332 psiconv_progress(config,lev+2,off+len,"Going to read the row reference");
333 if ((res = psiconv_parse_sheet_ref(config,buf,lev+2,off+len,&leng,&result->row)))
334 goto ERROR;
335 len += leng;
336 psiconv_progress(config,lev+2,off+len,"Going to read the column reference");
337 if ((res = psiconv_parse_sheet_ref(config,buf,lev+2,off+len,&leng,&result->column)))
338 goto ERROR;
339 len += leng;
340
341 psiconv_progress(config,lev+2,off+len,
342 "Going to read the trailing byte (%02x expected)",0);
343 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
344 if (res)
345 goto ERROR;
346 if (temp != 0) {
347 psiconv_warn(config,lev+2,off+len,"Unknown byte in cell reference (ignored");
348 psiconv_debug(config,lev+2,off+len,"Trailing byte: %02x",temp);
349 }
350 len ++;
351 psiconv_progress(config,lev,off+len-1,
352 "End of cell reference (total length: %08x)", len);
353 if (length)
354 *length = len;
355 return 0;
356 ERROR:
357 if (length)
358 *length = 0;
359 return res;
360 }
361
362 static int psiconv_parse_sheet_cell_block(const psiconv_config config,
363 const psiconv_buffer buf,int lev,
364 psiconv_u32 off, int *length,
365 psiconv_sheet_cell_block_t *result)
366 {
367 int len = 0;
368 int leng,res;
369
370 psiconv_progress(config,lev+1,off+len,"Going to read a sheet cell block");
371 psiconv_progress(config,lev+2,off+len,"Going to read the first cell");
372 if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng,
373 &result->first)))
374 goto ERROR;
375 len += leng;
376 psiconv_progress(config,lev+2,off+len,"Going to read the last cell");
377 if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng,
378 &result->last)))
379 goto ERROR;
380 len += leng;
381 psiconv_progress(config,lev,off+len-1,
382 "End of cell block (total length: %08x)", len);
383 if (length)
384 *length = len;
385 return 0;
386 ERROR:
387 if (length)
388 *length = 0;
389 return res;
390 }
391
392 static int psiconv_parse_formula_element_list(const psiconv_config config,
393 const psiconv_buffer buf, int lev,
394 psiconv_u32 off, int *length,
395 psiconv_formula *result,
396 psiconv_u32 maxlen)
397 {
398 int res=0;
399 int len=0;
400 int leng;
401 int eof = 0;
402 psiconv_u8 marker,submarker,submarker2;
403 psiconv_formula_list formula_stack;
404 psiconv_formula formula,subformula,subformula1,subformula2,
405 subformula3,subformula4;
406 psiconv_u16 temp,nr_of_subs;
407
408 psiconv_progress(config,lev+1,off,"Going to read a formula element list");
409 if (!(*result = malloc(sizeof(**result))))
410 goto ERROR1;
411 if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s))))
412 goto ERROR2;
413 if (!(formula = malloc(sizeof(*formula))))
414 goto ERROR3;
415 /* By setting the type to unknown, we can safely call psiconv_free_formula */
416 formula->type = psiconv_formula_unknown;
417 if (!(subformula1 = malloc(sizeof(*subformula1))))
418 goto ERROR4;
419 subformula1->type = psiconv_formula_unknown;
420 if (!(subformula2 = malloc(sizeof(*subformula2))))
421 goto ERROR5;
422 subformula2->type = psiconv_formula_unknown;
423 if (!(subformula3 = malloc(sizeof(*subformula3))))
424 goto ERROR6;
425 subformula3->type = psiconv_formula_unknown;
426 if (!(subformula4 = malloc(sizeof(*subformula4))))
427 goto ERROR7;
428 subformula4->type = psiconv_formula_unknown;
429
430 while (!eof && len+off < maxlen) {
431 psiconv_progress(config,lev+3,off+len,"Going to read a formula item marker");
432 marker = psiconv_read_u8(config,buf,lev+2,off+len,&res);
433 if (res)
434 goto ERROR8;
435 psiconv_debug(config,lev+3,off+len,"Marker: %02x (%s)",marker,
436 formula_elements[marker].name);
437 len ++;
438
439 if (formula_elements[marker].formula_type == psiconv_formula_unknown) {
440 psiconv_warn(config,lev+3,off+len,"Unknown formula marker found!");
441 goto ERROR8;
442 } else if ((formula_elements[marker].formula_type ==
443 psiconv_formula_mark_eof) ||
444 (formula_elements[marker].formula_type ==
445 psiconv_formula_mark_opend) ||
446 (formula_elements[marker].formula_type ==
447 psiconv_formula_mark_opsep)) {
448 len--;
449 psiconv_progress(config,lev+3,off+len,"End of this formula list");
450 eof = 1;
451 } else if (formula_elements[marker].formula_type ==
452 psiconv_formula_dat_int) {
453 psiconv_progress(config,lev+3,off+len,"Next item: an integer");
454 formula->data.dat_int = psiconv_read_u32(config,buf,lev+2,off+len,&res);
455 if (res)
456 goto ERROR8;
457 formula->type = formula_elements[marker].formula_type;
458 psiconv_debug(config,lev+3,off+len,"Value: %08x",formula->data.dat_int);
459 len += 4;
460 if ((res = psiconv_list_add(formula_stack,formula)))
461 goto ERROR8;
462 formula->type = psiconv_formula_unknown;
463 } else if (formula_elements[marker].formula_type ==
464 psiconv_formula_dat_float) {
465 psiconv_progress(config,lev+3,off+len,"Next item: a float");
466 formula->data.dat_float = psiconv_read_float(config,buf,lev+2,off+len,&leng,
467 &res);
468 if (res)
469 goto ERROR8;
470 formula->type = formula_elements[marker].formula_type;
471 psiconv_debug(config,lev+3,off+len,"Value: %f",formula->data.dat_float);
472 len += leng;
473 if ((res = psiconv_list_add(formula_stack,formula)))
474 goto ERROR8;
475 formula->type = psiconv_formula_unknown;
476 } else if (formula_elements[marker].formula_type ==
477 psiconv_formula_dat_cellref) {
478 psiconv_progress(config,lev+3,off+len,"Next item: a cell reference");
479 if ((res = psiconv_parse_sheet_cell_reference(config,buf,lev+2,off+len,&leng,
480 &formula->data.dat_cellref)))
481 goto ERROR8;
482 formula->type = formula_elements[marker].formula_type;
483 len += leng;
484 if ((res = psiconv_list_add(formula_stack,formula)))
485 goto ERROR8;
486 formula->type = psiconv_formula_unknown;
487 } else if ((formula_elements[marker].formula_type ==
488 psiconv_formula_dat_cellblock) ||
489 (formula_elements[marker].formula_type ==
490 psiconv_formula_dat_vcellblock)) {
491 psiconv_progress(config,lev+3,off+len,"Next item: a cell block");
492 if ((res = psiconv_parse_sheet_cell_block(config,buf,lev+2,off+len,&leng,
493 &formula->data.dat_cellblock)))
494 goto ERROR8;
495 formula->type = formula_elements[marker].formula_type;
496 len += leng;
497 if ((res = psiconv_list_add(formula_stack,formula)))
498 goto ERROR8;
499 formula->type = psiconv_formula_unknown;
500 } else if (formula_elements[marker].formula_type ==
501 psiconv_formula_dat_string) {
502 psiconv_progress(config,lev+3,off+len,"Next item: a string");
503 formula->data.dat_string =
504 psiconv_read_short_string(config,buf,lev+2,off+len,&leng,&res);
505 if (res)
506 goto ERROR8;
507 formula->type = formula_elements[marker].formula_type;
508 len += leng;
509 if ((res = psiconv_list_add(formula_stack,formula)))
510 goto ERROR8;
511 formula->type = psiconv_formula_unknown;
512 } else if ((formula_elements[marker].formula_type ==
513 psiconv_formula_dat_var)) {
514 psiconv_progress(config,lev+3,off+len,"Next item: a variable reference");
515 formula->data.dat_variable = psiconv_read_u32(config,buf,lev+2,off+len,&res);
516 if (res)
517 goto ERROR8;
518 formula->type = formula_elements[marker].formula_type;
519 len += 4;
520 if ((res = psiconv_list_add(formula_stack,formula)))
521 goto ERROR8;
522 formula->type = psiconv_formula_unknown;
523 } else if (formula_elements[marker].number_of_args == -1) {
524 psiconv_progress(config,lev+3,off+len,"Going to parse a vararg function");
525 if (!(formula->data.fun_operands =
526 psiconv_list_new(sizeof(*formula))))
527 goto ERROR8;
528 formula->type = formula_elements[marker].formula_type;
529 nr_of_subs = 0;
530 do {
531 nr_of_subs ++;
532 psiconv_progress(config,lev+4,off+len,"Going to read vararg argument %d",
533 nr_of_subs);
534 if ((res = psiconv_parse_formula_element_list(config,buf,lev+4,off+len,&leng,
535 &subformula,maxlen)))
536 goto ERROR8;
537 len += leng;
538 if ((res = psiconv_list_add(formula->data.fun_operands,subformula))) {
539 psiconv_free_formula(subformula);
540 goto ERROR8;
541 }
542 free(subformula);
543 psiconv_progress(config,lev+4,off+len,"Going to read the next marker");
544 submarker = psiconv_read_u8(config,buf,lev+4,off+len,&res);
545 len ++;
546 if (res)
547 goto ERROR8;
548 submarker2 = psiconv_read_u8(config,buf,lev+4,off+len,&res);
549 if (res)
550 goto ERROR8;
551 } while ((formula_elements[submarker].formula_type
552 == psiconv_formula_mark_opsep) &&
553 (formula_elements[submarker2].formula_type
554 != psiconv_formula_mark_opend));
555 if ((formula_elements[submarker].formula_type ==
556 psiconv_formula_mark_opsep) &&
557 (formula_elements[submarker2].formula_type ==
558 psiconv_formula_mark_opend)) {
559 submarker=submarker2;
560 len++;
561 }
562 if (formula_elements[submarker].formula_type
563 != psiconv_formula_mark_opend) {
564 psiconv_warn(config,lev+3,off+len,"Formula corrupted!");
565 psiconv_debug(config,lev+3,off+len,"Found unexpected marker %02x",submarker);
566 goto ERROR8;
567 }
568 psiconv_progress(config,lev+3,off+len,"Going to read the repeated marker %02x",
569 marker);
570 submarker = psiconv_read_u8(config,buf,lev+3,off+len,&res);
571 if (res)
572 goto ERROR8;
573 if (submarker != marker) {
574 psiconv_warn(config,lev+3,off+len,"Formula corrupted!");
575 psiconv_debug(config,lev+3,off+len,"Expected marker %02x, found %02x",
576 marker,submarker);
577 goto ERROR8;
578 }
579 len++;
580 psiconv_progress(config,lev+3,off+len,
581 "Going to read the number of arguments (%d expected)",
582 nr_of_subs);
583 temp = psiconv_read_u16(config,buf,lev+3,off+len,&res);
584 if (res)
585 goto ERROR8;
586 if (temp != nr_of_subs) {
587 psiconv_warn(config,lev+3,off+len,"Formula corrupted!");
588 psiconv_debug(config,lev+3,off+len,
589 "Read %d arguments, but formula says there are %d",
590 nr_of_subs,temp);
591 goto ERROR8;
592 }
593 len += 2;
594 if ((res = psiconv_list_add(formula_stack,formula)))
595 goto ERROR8;
596 formula->type = psiconv_formula_unknown;
597 } else {
598 if (formula_elements[marker].number_of_args > 0)
599 if ((res = psiconv_list_pop(formula_stack,subformula1)))
600 goto ERROR8;
601 if (formula_elements[marker].number_of_args > 1)
602 if ((res = psiconv_list_pop(formula_stack,subformula2)))
603 goto ERROR8;
604 if (formula_elements[marker].number_of_args > 2)
605 if ((res = psiconv_list_pop(formula_stack,subformula3)))
606 goto ERROR8;
607 if (formula_elements[marker].number_of_args > 3)
608 if ((res = psiconv_list_pop(formula_stack,subformula4)))
609 goto ERROR8;
610 if (!(formula->data.fun_operands =
611 psiconv_list_new(sizeof(*formula))))
612 goto ERROR8;
613 formula->type = formula_elements[marker].formula_type;
614 if (formula_elements[marker].number_of_args > 3)
615 if ((res = psiconv_list_add(formula->data.fun_operands,subformula4)))
616 goto ERROR8;
617 if (formula_elements[marker].number_of_args > 2)
618 if ((res = psiconv_list_add(formula->data.fun_operands,subformula3)))
619 goto ERROR8;
620 if (formula_elements[marker].number_of_args > 1)
621 if ((res = psiconv_list_add(formula->data.fun_operands,subformula2)))
622 goto ERROR8;
623 if (formula_elements[marker].number_of_args > 0)
624 if ((res = psiconv_list_add(formula->data.fun_operands,subformula1)))
625 goto ERROR8;
626 if ((res = psiconv_list_add(formula_stack,formula)))
627 goto ERROR8;
628 subformula4->type = subformula3->type = subformula2->type =
629 subformula1->type = formula->type = psiconv_formula_unknown;
630 }
631 }
632 if ((len+off > maxlen) || !eof) {
633 psiconv_warn(config,lev+2,off+len,"Formula corrupted!");
634 psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x",
635 maxlen,len+off);
636 goto ERROR8;
637 }
638 if ((psiconv_list_length(formula_stack)) != 1) {
639 psiconv_warn(config,lev+2,off+len,"Formula corrupted!");
640 psiconv_debug(config,lev+2,off+len,"More than one item left on the stack (%d)",
641 psiconv_list_length(formula_stack));
642 goto ERROR8;
643 }
644 if ((res = psiconv_list_pop(formula_stack,*result)))
645 goto ERROR8;
646 psiconv_list_free(formula_stack);
647 free(formula);
648
649 if (length)
650 *length = len;
651
652 psiconv_progress(config,lev,off+len-1,
653 "End of formula element list (total length: %08x)", len);
654 return 0;
655
656 ERROR8:
657 psiconv_free_formula(subformula4);
658 ERROR7:
659 psiconv_free_formula(subformula3);
660 ERROR6:
661 psiconv_free_formula(subformula2);
662 ERROR5:
663 psiconv_free_formula(subformula1);
664 ERROR4:
665 psiconv_free_formula(formula);
666 ERROR3:
667 psiconv_free_formula_list(formula_stack);
668 ERROR2:
669 free (*result);
670 ERROR1:
671 psiconv_warn(config,lev+1,off,"Reading of formula element list failed");
672 if (length)
673 *length = 0;
674 if (!res)
675 return -PSICONV_E_NOMEM;
676 else
677 return res;
678 }
679
680
681
682
683 int psiconv_parse_formula(const psiconv_config config,
684 const psiconv_buffer buf, int lev,
685 psiconv_u32 off, int *length,
686 psiconv_formula *result)
687 {
688 int res=0;
689 int len=0;
690 int leng;
691 psiconv_u32 bytelen,formula_end;
692 psiconv_u8 temp;
693
694 psiconv_progress(config,lev+1,off,"Going to read a formula");
695
696 psiconv_progress(config,lev+2,off+len,
697 "Going to read the formula byte length");
698 bytelen = psiconv_read_S(config,buf,lev+2,off+len,&leng,&res);
699 if (res)
700 goto ERROR1;
701 psiconv_debug(config,lev+2,off+len,"Formula byte length: %d",bytelen);
702 len += leng;
703 bytelen += len;
704 formula_end = off + bytelen;
705
706 psiconv_progress(config,lev+2,off+len,"Going to read the formula elements list");
707 if ((res = psiconv_parse_formula_element_list(config,buf,lev+2,off+len,&leng,
708 result,formula_end)))
709 goto ERROR1;
710 len += leng;
711
712 psiconv_progress(config,lev+2,off+len,"Going to read the eof marker");
713 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
714 if (res)
715 goto ERROR2;
716 if (formula_elements[temp].formula_type != psiconv_formula_mark_eof) {
717 psiconv_warn(config,lev+2,off+len,"Formula corrupted!");
718 psiconv_debug(config,lev+2,off+len,"Expected marker: %02x, found byte: %02x",
719 0x15,temp);
720 goto ERROR2;
721 }
722 len ++;
723
724 if (off+len != formula_end) {
725 psiconv_warn(config,lev+2,off+len,"Formula corrupted!");
726 psiconv_debug(config,lev+2,off+len,"Expected end: %04x, found end: %04x",
727 formula_end,len+off);
728 goto ERROR2;
729 }
730
731 if (length)
732 *length = len;
733
734 psiconv_progress(config,lev,off+len-1,
735 "End of formula (total length: %08x)", len);
736 return 0;
737
738 ERROR2:
739 psiconv_free_formula(*result);
740 ERROR1:
741 psiconv_warn(config,lev+1,off,"Reading of formula failed");
742 if (length)
743 *length = 0;
744 if (!res)
745 return -PSICONV_E_NOMEM;
746 else
747 return res;
748 }
749
750

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