| 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_unknown,0,"*UNKNOWN*"}, |
| 52 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 53 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, /* 10 */ |
| 54 |
{psiconv_formula_op_con,2,"&"}, |
| 55 |
{psiconv_formula_op_bra,1,"{}"}, |
| 56 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 57 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 58 |
{psiconv_formula_mark_eof,0,"End of formula"}, |
| 59 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 60 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 61 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 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_dat_float,0,"Floating point number"}, |
| 69 |
{psiconv_formula_dat_int,0,"Signed integer number"}, /* 20 */ |
| 70 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 71 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 72 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 73 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 74 |
{psiconv_formula_dat_var,0,"Named variable"}, |
| 75 |
{psiconv_formula_dat_string,0,"String"}, |
| 76 |
{psiconv_formula_dat_cellref,0,"Cell reference"}, |
| 77 |
{psiconv_formula_dat_cellblock,0,"Cell block"}, |
| 78 |
{psiconv_formula_dat_vcellblock,0,"Cell block {varargs}"}, |
| 79 |
{psiconv_formula_mark_opsep,0,"Operand separator"}, |
| 80 |
{psiconv_formula_mark_opend,0,"Operand list end"}, |
| 81 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 82 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 83 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 84 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 85 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, /* 30 */ |
| 86 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 87 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 88 |
{psiconv_formula_fun_false,0,"FALSE"}, |
| 89 |
{psiconv_formula_fun_if,3,"IF"}, |
| 90 |
{psiconv_formula_fun_true,0,"TRUE"}, |
| 91 |
{psiconv_formula_fun_cell,2,"CELL"}, |
| 92 |
{psiconv_formula_fun_errortype,0,"ERRORTYPE"}, |
| 93 |
{psiconv_formula_fun_isblank,1,"ISBLANK"}, |
| 94 |
{psiconv_formula_fun_iserr,1,"ISERR"}, |
| 95 |
{psiconv_formula_fun_iserror,1,"ISERROR"}, |
| 96 |
{psiconv_formula_fun_islogical,1,"ISLOGICAL"}, |
| 97 |
{psiconv_formula_fun_isna,1,"ISNA"}, |
| 98 |
{psiconv_formula_fun_isnontext,1,"ISNONTEXT"}, |
| 99 |
{psiconv_formula_fun_isnumber,1,"ISNUMBER"}, |
| 100 |
{psiconv_formula_fun_istext,1,"ISTEXT"}, |
| 101 |
{psiconv_formula_fun_n,1,"N"}, /* 40 */ |
| 102 |
{psiconv_formula_fun_type,1,"TYPE"}, |
| 103 |
{psiconv_formula_fun_address,2,"ADDRESS"}, |
| 104 |
{psiconv_formula_fun_column,1,"COLUMN"}, |
| 105 |
{psiconv_formula_fun_columns,1,"COLUMNS"}, |
| 106 |
{psiconv_formula_fun_hlookup,3,"HLOOKUP"}, |
| 107 |
{psiconv_formula_fun_index,3,"INDEX"}, |
| 108 |
{psiconv_formula_fun_indirect,1,"INDIRECT"}, |
| 109 |
{psiconv_formula_fun_lookup,3,"LOOKUP"}, |
| 110 |
{psiconv_formula_fun_offset,3,"OFFSET"}, |
| 111 |
{psiconv_formula_fun_row,1,"ROW"}, |
| 112 |
{psiconv_formula_fun_rows,1,"ROWS"}, |
| 113 |
{psiconv_formula_fun_vlookup,3,"VLOOKUP"}, |
| 114 |
{psiconv_formula_fun_char,1,"CHAR"}, |
| 115 |
{psiconv_formula_fun_code,1,"CODE"}, |
| 116 |
{psiconv_formula_fun_exact,2,"EXACT"}, |
| 117 |
{psiconv_formula_fun_find,3,"FIND"}, /* 50 */ |
| 118 |
{psiconv_formula_fun_left,2,"LEFT"}, |
| 119 |
{psiconv_formula_fun_len,1,"LEN"}, |
| 120 |
{psiconv_formula_fun_lower,1,"LOWER"}, |
| 121 |
{psiconv_formula_fun_mid,3,"MID"}, |
| 122 |
{psiconv_formula_fun_proper,1,"PROPER"}, |
| 123 |
{psiconv_formula_fun_replace,4,"REPLACE"}, |
| 124 |
{psiconv_formula_fun_rept,2,"REPT"}, |
| 125 |
{psiconv_formula_fun_right,2,"RIGHT"}, |
| 126 |
{psiconv_formula_fun_string,2,"STRING"}, |
| 127 |
{psiconv_formula_fun_t,1,"T"}, |
| 128 |
{psiconv_formula_fun_trim,1,"TRIM"}, |
| 129 |
{psiconv_formula_fun_upper,1,"UPPER"}, |
| 130 |
{psiconv_formula_fun_value,1,"VALUE"}, |
| 131 |
{psiconv_formula_fun_date,3,"DATE"}, |
| 132 |
{psiconv_formula_fun_datevalue,1,"DATEVALUE"}, |
| 133 |
{psiconv_formula_fun_day,1,"DAY"}, /* 60 */ |
| 134 |
{psiconv_formula_fun_hour,1,"HOUR"}, |
| 135 |
{psiconv_formula_fun_minute,1,"MINUTE"}, |
| 136 |
{psiconv_formula_fun_month,1,"MONTH"}, |
| 137 |
{psiconv_formula_fun_now,0,"NOW"}, |
| 138 |
{psiconv_formula_fun_second,1,"SECOND"}, |
| 139 |
{psiconv_formula_fun_today,0,"TODAY"}, |
| 140 |
{psiconv_formula_fun_time,3,"TIME"}, |
| 141 |
{psiconv_formula_fun_timevalue,1,"TIMEVALUE"}, |
| 142 |
{psiconv_formula_fun_year,1,"YEAR"}, |
| 143 |
{psiconv_formula_fun_abs,1,"ABS"}, |
| 144 |
{psiconv_formula_fun_acos,1,"ACOS"}, |
| 145 |
{psiconv_formula_fun_asin,1,"ASIN"}, |
| 146 |
{psiconv_formula_fun_atan,1,"ATAN"}, |
| 147 |
{psiconv_formula_fun_atan2,2,"ATAN2"}, |
| 148 |
{psiconv_formula_fun_cos,1,"COS"}, |
| 149 |
{psiconv_formula_fun_degrees,0,"DEGREES"}, /* 70 */ |
| 150 |
{psiconv_formula_fun_exp,1,"EXP"}, |
| 151 |
{psiconv_formula_fun_fact,1,"FACT"}, |
| 152 |
{psiconv_formula_fun_int,1,"INT"}, |
| 153 |
{psiconv_formula_fun_ln,1,"LN"}, |
| 154 |
{psiconv_formula_fun_log10,1,"LOG10"}, |
| 155 |
{psiconv_formula_fun_mod,2,"MOD"}, |
| 156 |
{psiconv_formula_fun_pi,0,"PI"}, |
| 157 |
{psiconv_formula_fun_radians,1,"RADIANS"}, |
| 158 |
{psiconv_formula_fun_rand,0,"RAND"}, |
| 159 |
{psiconv_formula_fun_round,2,"ROUND"}, |
| 160 |
{psiconv_formula_fun_sign,1,"SIGN"}, |
| 161 |
{psiconv_formula_fun_sin,1,"SIN"}, |
| 162 |
{psiconv_formula_fun_sqrt,1,"SQRT"}, |
| 163 |
{psiconv_formula_fun_sumproduct,2,"SUMPRODUCT"}, |
| 164 |
{psiconv_formula_fun_tan,1,"TAN"}, |
| 165 |
{psiconv_formula_fun_trunc,1,"TRUNC"}, /* 80 */ |
| 166 |
{psiconv_formula_fun_cterm,3,"CTERM"}, |
| 167 |
{psiconv_formula_fun_ddb,4,"DDB"}, |
| 168 |
{psiconv_formula_fun_fv,3,"FV"}, |
| 169 |
{psiconv_formula_fun_irr,2,"IRR"}, |
| 170 |
{psiconv_formula_fun_npv,2,"NPV"}, |
| 171 |
{psiconv_formula_fun_pmt,3,"PMT"}, |
| 172 |
{psiconv_formula_fun_pv,3,"PV"}, |
| 173 |
{psiconv_formula_fun_rate,3,"RATE"}, |
| 174 |
{psiconv_formula_fun_sln,3,"SLN"}, |
| 175 |
{psiconv_formula_fun_syd,4,"SYD"}, |
| 176 |
{psiconv_formula_fun_term,3,"TERM"}, |
| 177 |
{psiconv_formula_fun_combin,2,"COMBIN"}, |
| 178 |
{psiconv_formula_fun_permut,2,"PERMUT"}, |
| 179 |
{psiconv_formula_vfn_average,-1,"AVERAGE"}, |
| 180 |
{psiconv_formula_vfn_choose,-1,"CHOOSE"}, |
| 181 |
{psiconv_formula_vfn_count,-1,"COUNT"}, /* 90 */ |
| 182 |
{psiconv_formula_vfn_counta,-1,"COUNTA"}, |
| 183 |
{psiconv_formula_vfn_countblank,-1,"COUNTBLANK"}, |
| 184 |
{psiconv_formula_vfn_max,-1,"MAX"}, |
| 185 |
{psiconv_formula_vfn_min,-1,"MIN"}, |
| 186 |
{psiconv_formula_vfn_product,-1,"PRODUCT"}, |
| 187 |
{psiconv_formula_vfn_stdevp,-1,"STDEVP"}, |
| 188 |
{psiconv_formula_vfn_stdev,-1,"STDEV"}, |
| 189 |
{psiconv_formula_vfn_sum,-1,"SUM"}, |
| 190 |
{psiconv_formula_vfn_sumsq,-1,"SUMSQ"}, |
| 191 |
{psiconv_formula_vfn_varp,-1,"VARP"}, |
| 192 |
{psiconv_formula_vfn_var,-1,"VAR"}, |
| 193 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 194 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 195 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 196 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, |
| 197 |
{psiconv_formula_unknown,0,"*UNKNOWN*"}, /* A0 */ |
| 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*"}, |
| 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*"}, /* B0 */ |
| 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*"}, |
| 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*"}, /* C0 */ |
| 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*"}, |
| 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*"}, /* D0 */ |
| 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*"}, |
| 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*"}, /* E0 */ |
| 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*"}, |
| 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*"}, /* F0 */ |
| 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*"}, |
| 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 |
int psiconv_parse_formula(const psiconv_buffer buf, int lev, |
| 444 |
psiconv_u32 off, int *length, |
| 445 |
psiconv_formula *result) |
| 446 |
{ |
| 447 |
int res=0; |
| 448 |
int len=0; |
| 449 |
int leng; |
| 450 |
int eof = 0; |
| 451 |
psiconv_u8 marker; |
| 452 |
psiconv_u32 bytelen; |
| 453 |
psiconv_formula_list formula_stack; |
| 454 |
psiconv_formula formula,subformula1,subformula2,subformula3,subformula4; |
| 455 |
|
| 456 |
psiconv_progress(lev+1,off,"Going to read a formula"); |
| 457 |
if (!(*result = malloc(sizeof(**result)))) |
| 458 |
goto ERROR1; |
| 459 |
if (!(formula_stack = psiconv_list_new(sizeof(struct psiconv_formula_s)))) |
| 460 |
goto ERROR2; |
| 461 |
if (!(formula = malloc(sizeof(*formula)))) |
| 462 |
goto ERROR3; |
| 463 |
/* By setting the type to unknown, we can safely call psiconv_free_formula */ |
| 464 |
formula->type = psiconv_formula_unknown; |
| 465 |
if (!(subformula1 = malloc(sizeof(*subformula1)))) |
| 466 |
goto ERROR4; |
| 467 |
subformula1->type = psiconv_formula_unknown; |
| 468 |
if (!(subformula2 = malloc(sizeof(*subformula2)))) |
| 469 |
goto ERROR5; |
| 470 |
subformula2->type = psiconv_formula_unknown; |
| 471 |
if (!(subformula3 = malloc(sizeof(*subformula3)))) |
| 472 |
goto ERROR6; |
| 473 |
subformula3->type = psiconv_formula_unknown; |
| 474 |
if (!(subformula4 = malloc(sizeof(*subformula4)))) |
| 475 |
goto ERROR7; |
| 476 |
subformula4->type = psiconv_formula_unknown; |
| 477 |
|
| 478 |
psiconv_progress(lev+2,off+len, |
| 479 |
"Going to read the formula byte length"); |
| 480 |
bytelen = psiconv_read_S(buf,lev+2,off+len,&leng,&res); |
| 481 |
if (res) |
| 482 |
goto ERROR8; |
| 483 |
psiconv_debug(lev+2,off+len,"Formula byte length: %d",bytelen); |
| 484 |
len += leng; |
| 485 |
bytelen += len; |
| 486 |
|
| 487 |
psiconv_progress(lev+2,off+len,"Going to read the formula items"); |
| 488 |
while (!eof && len < bytelen) { |
| 489 |
psiconv_progress(lev+3,off+len,"Going to read a formula item marker"); |
| 490 |
marker = psiconv_read_u8(buf,lev+2,off+len,&res); |
| 491 |
if (res) |
| 492 |
goto ERROR8; |
| 493 |
psiconv_debug(lev+3,off+len,"Marker: %02x (%s)",marker, |
| 494 |
formula_elements[marker].name); |
| 495 |
len ++; |
| 496 |
|
| 497 |
if (formula_elements[marker].formula_type == psiconv_formula_unknown) { |
| 498 |
psiconv_warn(lev+3,off+len,"Unknown formula marker found!"); |
| 499 |
goto ERROR8; |
| 500 |
} else if (formula_elements[marker].formula_type == |
| 501 |
psiconv_formula_mark_eof) { |
| 502 |
psiconv_progress(lev+3,off+len,"End of formula"); |
| 503 |
eof = 1; |
| 504 |
} else if (formula_elements[marker].formula_type == |
| 505 |
psiconv_formula_dat_int) { |
| 506 |
psiconv_progress(lev+3,off+len,"Next item: an integer"); |
| 507 |
formula->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); |
| 508 |
if (res) |
| 509 |
goto ERROR8; |
| 510 |
formula->type = formula_elements[marker].formula_type; |
| 511 |
psiconv_debug(lev+3,off+len,"Value: %08x",formula->data.dat_int); |
| 512 |
len += 4; |
| 513 |
if ((res = psiconv_list_add(formula_stack,formula))) |
| 514 |
goto ERROR8; |
| 515 |
formula->type = psiconv_formula_unknown; |
| 516 |
} else if (formula_elements[marker].formula_type == |
| 517 |
psiconv_formula_dat_float) { |
| 518 |
psiconv_progress(lev+3,off+len,"Next item: a float"); |
| 519 |
formula->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng, |
| 520 |
&res); |
| 521 |
if (res) |
| 522 |
goto ERROR8; |
| 523 |
formula->type = formula_elements[marker].formula_type; |
| 524 |
psiconv_debug(lev+3,off+len,"Value: %f",formula->data.dat_float); |
| 525 |
len += leng; |
| 526 |
if ((res = psiconv_list_add(formula_stack,formula))) |
| 527 |
goto ERROR8; |
| 528 |
formula->type = psiconv_formula_unknown; |
| 529 |
} else if (formula_elements[marker].formula_type == |
| 530 |
psiconv_formula_dat_cellref) { |
| 531 |
psiconv_progress(lev+3,off+len,"Next item: a cell reference"); |
| 532 |
if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, |
| 533 |
&formula->data.dat_cellref))) |
| 534 |
goto ERROR8; |
| 535 |
formula->type = formula_elements[marker].formula_type; |
| 536 |
len += leng; |
| 537 |
if ((res = psiconv_list_add(formula_stack,formula))) |
| 538 |
goto ERROR8; |
| 539 |
formula->type = psiconv_formula_unknown; |
| 540 |
} else if (formula_elements[marker].formula_type == |
| 541 |
psiconv_formula_dat_cellblock) { |
| 542 |
psiconv_progress(lev+3,off+len,"Next item: a cell block"); |
| 543 |
if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng, |
| 544 |
&formula->data.dat_cellblock))) |
| 545 |
goto ERROR8; |
| 546 |
formula->type = formula_elements[marker].formula_type; |
| 547 |
len += leng; |
| 548 |
if ((res = psiconv_list_add(formula_stack,formula))) |
| 549 |
goto ERROR8; |
| 550 |
formula->type = psiconv_formula_unknown; |
| 551 |
} else if (formula_elements[marker].formula_type == |
| 552 |
psiconv_formula_dat_string) { |
| 553 |
psiconv_progress(lev+3,off+len,"Next item: a string"); |
| 554 |
formula->data.dat_string = |
| 555 |
psiconv_read_sheet_string(buf,lev+2,off+len,&leng,&res); |
| 556 |
if (res) |
| 557 |
goto ERROR8; |
| 558 |
formula->type = formula_elements[marker].formula_type; |
| 559 |
len += leng; |
| 560 |
if ((res = psiconv_list_add(formula_stack,formula))) |
| 561 |
goto ERROR8; |
| 562 |
formula->type = psiconv_formula_unknown; |
| 563 |
} else if ((formula_elements[marker].formula_type == |
| 564 |
psiconv_formula_dat_var) || |
| 565 |
(formula_elements[marker].formula_type == |
| 566 |
psiconv_formula_dat_vcellblock) || |
| 567 |
(formula_elements[marker].formula_type == |
| 568 |
psiconv_formula_mark_opsep) || |
| 569 |
(formula_elements[marker].formula_type == |
| 570 |
psiconv_formula_mark_opend)) { |
| 571 |
psiconv_warn(lev+3,off+len,"Not yet supported formula mark!"); |
| 572 |
goto ERROR8; |
| 573 |
} else if (formula_elements[marker].number_of_args == -1) { |
| 574 |
psiconv_warn(lev+3,off+len,"Vararg functions not yet supported!"); |
| 575 |
goto ERROR8; |
| 576 |
} else { |
| 577 |
if (formula_elements[marker].number_of_args > 0) |
| 578 |
if ((res = psiconv_list_pop(formula_stack,subformula1))) |
| 579 |
goto ERROR8; |
| 580 |
if (formula_elements[marker].number_of_args > 1) |
| 581 |
if ((res = psiconv_list_pop(formula_stack,subformula2))) |
| 582 |
goto ERROR8; |
| 583 |
if (formula_elements[marker].number_of_args > 2) |
| 584 |
if ((res = psiconv_list_pop(formula_stack,subformula3))) |
| 585 |
goto ERROR8; |
| 586 |
if (formula_elements[marker].number_of_args > 3) |
| 587 |
if ((res = psiconv_list_pop(formula_stack,subformula4))) |
| 588 |
goto ERROR8; |
| 589 |
if (!(formula->data.fun_operands = |
| 590 |
psiconv_list_new(sizeof(*formula)))) |
| 591 |
goto ERROR8; |
| 592 |
formula->type = formula_elements[marker].formula_type; |
| 593 |
if (formula_elements[marker].number_of_args > 3) |
| 594 |
if ((res = psiconv_list_add(formula->data.fun_operands,subformula4))) |
| 595 |
goto ERROR8; |
| 596 |
if (formula_elements[marker].number_of_args > 2) |
| 597 |
if ((res = psiconv_list_add(formula->data.fun_operands,subformula3))) |
| 598 |
goto ERROR8; |
| 599 |
if (formula_elements[marker].number_of_args > 1) |
| 600 |
if ((res = psiconv_list_add(formula->data.fun_operands,subformula2))) |
| 601 |
goto ERROR8; |
| 602 |
if (formula_elements[marker].number_of_args > 0) |
| 603 |
if ((res = psiconv_list_add(formula->data.fun_operands,subformula1))) |
| 604 |
goto ERROR8; |
| 605 |
if ((res = psiconv_list_add(formula_stack,formula))) |
| 606 |
goto ERROR8; |
| 607 |
subformula4->type = subformula3->type = subformula2->type = |
| 608 |
subformula1->type = formula->type = psiconv_formula_unknown; |
| 609 |
} |
| 610 |
} |
| 611 |
if ((len != bytelen) || !eof) { |
| 612 |
psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
| 613 |
psiconv_debug(lev+2,off+len,"Expected end: %04x, found end: %04x", |
| 614 |
bytelen,len); |
| 615 |
goto ERROR8; |
| 616 |
} |
| 617 |
if ((psiconv_list_length(formula_stack)) != 1) { |
| 618 |
psiconv_warn(lev+2,off+len,"Formula corrupted!"); |
| 619 |
psiconv_debug(lev+2,off+len,"More than one item left on the stack (%d)", |
| 620 |
psiconv_list_length(formula_stack)); |
| 621 |
goto ERROR8; |
| 622 |
} |
| 623 |
if ((res = psiconv_list_pop(formula_stack,*result))) |
| 624 |
goto ERROR8; |
| 625 |
psiconv_list_free(formula_stack); |
| 626 |
free(formula); |
| 627 |
|
| 628 |
if (length) |
| 629 |
*length = len; |
| 630 |
|
| 631 |
psiconv_progress(lev,off+len-1, |
| 632 |
"End of formula (total length: %08x)", len); |
| 633 |
return 0; |
| 634 |
|
| 635 |
ERROR8: |
| 636 |
psiconv_free_formula(subformula4); |
| 637 |
ERROR7: |
| 638 |
psiconv_free_formula(subformula3); |
| 639 |
ERROR6: |
| 640 |
psiconv_free_formula(subformula2); |
| 641 |
ERROR5: |
| 642 |
psiconv_free_formula(subformula1); |
| 643 |
ERROR4: |
| 644 |
psiconv_free_formula(formula); |
| 645 |
ERROR3: |
| 646 |
psiconv_free_formula_list(formula_stack); |
| 647 |
ERROR2: |
| 648 |
free (*result); |
| 649 |
ERROR1: |
| 650 |
psiconv_warn(lev+1,off,"Reading of formula failed"); |
| 651 |
if (length) |
| 652 |
*length = 0; |
| 653 |
if (!res) |
| 654 |
return -PSICONV_E_NOMEM; |
| 655 |
else |
| 656 |
return res; |
| 657 |
} |
| 658 |
|