--- psiconv/trunk/lib/psiconv/parse_formula.c 2001/01/30 23:57:28 101 +++ psiconv/trunk/lib/psiconv/parse_formula.c 2001/01/31 00:35:06 102 @@ -291,6 +291,100 @@ {psiconv_formula_unknown,0,"*UNKNOWN*"}, {psiconv_formula_unknown,0,"*UNKNOWN*"}}; + +static int psiconv_parse_sheet_ref(const psiconv_buffer buf,int lev, + psiconv_u32 off, int *length, + psiconv_sheet_ref_t *result) +{ + int res; + psiconv_u16 temp; + + psiconv_progress(lev+1,off,"Going to read a sheet ref"); + psiconv_progress(lev+2,off,"Going to read the offset encoding"); + temp = psiconv_read_u16(buf,lev+2,off,&res); + if (res) { + if (length) + *length = 0; + return res; + } + psiconv_debug(lev+2,off,"Encoded word: %04x",temp); + result->absolute = (temp & 0x4000)?psiconv_bool_true:psiconv_bool_false; + result->offset = (temp & 0x3fff) * ((temp & 0x8000)?-1:1); + psiconv_debug(lev+2,off,"Reference: %s offset %d", + result->absolute?"absolute":"relative",result->offset); + if (length) + *length = 2; + return 0; +} + +static int psiconv_parse_sheet_cell_reference(const psiconv_buffer buf,int lev, + psiconv_u32 off, int *length, + psiconv_sheet_cell_reference_t *result) +{ + int len = 0; + int leng,res; + psiconv_u8 temp; + + psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference"); + psiconv_progress(lev+2,off+len,"Going to read the row reference"); + if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->row))) + goto ERROR; + len += leng; + psiconv_progress(lev+2,off+len,"Going to read the column reference"); + if ((res = psiconv_parse_sheet_ref(buf,lev+2,off+len,&leng,&result->column))) + goto ERROR; + len += leng; + + psiconv_progress(lev+2,off+len, + "Going to read the trailing byte (%02x expected)",0); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR; + if (temp != 0) { + psiconv_warn(lev+2,off+len,"Unknown byte in cell reference (ignored"); + psiconv_debug(lev+2,off+len,"Trailing byte: %02x",temp); + } + len ++; + psiconv_progress(lev,off+len-1, + "End of cell reference (total length: %08x)", len); + if (length) + *length = len; + return 0; +ERROR: + if (length) + *length = 0; + return res; +} + +static int psiconv_parse_sheet_cell_block(const psiconv_buffer buf,int lev, + psiconv_u32 off, int *length, + psiconv_sheet_cell_block_t *result) +{ + int len = 0; + int leng,res; + + psiconv_progress(lev+1,off+len,"Going to read a sheet cell block"); + psiconv_progress(lev+2,off+len,"Going to read the first cell"); + if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, + &result->first))) + goto ERROR; + len += leng; + psiconv_progress(lev+2,off+len,"Going to read the last cell"); + if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, + &result->last))) + goto ERROR; + len += leng; + psiconv_progress(lev,off+len-1, + "End of cell block (total length: %08x)", len); + if (length) + *length = len; + return 0; +ERROR: + if (length) + *length = 0; + return res; +} + int psiconv_parse_formula(const psiconv_buffer buf, int lev, psiconv_u32 off, int *length, psiconv_formula *result) @@ -377,16 +471,33 @@ if ((res = psiconv_list_add(formula_stack,formula))) goto ERROR8; formula->type = psiconv_formula_unknown; - + } else if (formula_elements[marker].formula_type == + psiconv_formula_dat_cellref) { + psiconv_progress(lev+3,off+len,"Next item: a cell reference"); + if ((res = psiconv_parse_sheet_cell_reference(buf,lev+2,off+len,&leng, + &formula->data.dat_cellref))) + goto ERROR8; + formula->type = formula_elements[marker].formula_type; + len += leng; + if ((res = psiconv_list_add(formula_stack,formula))) + goto ERROR8; + formula->type = psiconv_formula_unknown; + } else if (formula_elements[marker].formula_type == + psiconv_formula_dat_cellblock) { + psiconv_progress(lev+3,off+len,"Next item: a cell block"); + if ((res = psiconv_parse_sheet_cell_block(buf,lev+2,off+len,&leng, + &formula->data.dat_cellblock))) + goto ERROR8; + formula->type = formula_elements[marker].formula_type; + len += leng; + if ((res = psiconv_list_add(formula_stack,formula))) + goto ERROR8; + formula->type = psiconv_formula_unknown; } else if ((formula_elements[marker].formula_type == psiconv_formula_dat_var) || (formula_elements[marker].formula_type == psiconv_formula_dat_string) || (formula_elements[marker].formula_type == - psiconv_formula_dat_cellref) || - (formula_elements[marker].formula_type == - psiconv_formula_dat_cellblock) || - (formula_elements[marker].formula_type == psiconv_formula_dat_vcellblock) || (formula_elements[marker].formula_type == psiconv_formula_mark_opsep) ||