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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 100 Revision 104
289 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 289 {psiconv_formula_unknown,0,"*UNKNOWN*"},
290 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 290 {psiconv_formula_unknown,0,"*UNKNOWN*"},
291 {psiconv_formula_unknown,0,"*UNKNOWN*"}, 291 {psiconv_formula_unknown,0,"*UNKNOWN*"},
292 {psiconv_formula_unknown,0,"*UNKNOWN*"}}; 292 {psiconv_formula_unknown,0,"*UNKNOWN*"}};
293 293
294static 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
338ERROR2:
339 free(result);
340ERROR1:
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
350static 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
375static 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;
408ERROR:
409 if (length)
410 *length = 0;
411 return res;
412}
413
414static 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;
437ERROR:
438 if (length)
439 *length = 0;
440 return res;
441}
442
294int psiconv_parse_formula(const psiconv_buffer buf, int lev, 443int psiconv_parse_formula(const psiconv_buffer buf, int lev,
295 psiconv_u32 off, int *length, 444 psiconv_u32 off, int *length,
296 psiconv_formula *result) 445 psiconv_formula *result)
297{ 446{
298 int res=0; 447 int res=0;
362 psiconv_debug(lev+3,off+len,"Value: %08x",formula->data.dat_int); 511 psiconv_debug(lev+3,off+len,"Value: %08x",formula->data.dat_int);
363 len += 4; 512 len += 4;
364 if ((res = psiconv_list_add(formula_stack,formula))) 513 if ((res = psiconv_list_add(formula_stack,formula)))
365 goto ERROR8; 514 goto ERROR8;
366 formula->type = psiconv_formula_unknown; 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;
367 } else if ((formula_elements[marker].formula_type == 563 } else if ((formula_elements[marker].formula_type ==
368 psiconv_formula_dat_float) ||
369 (formula_elements[marker].formula_type ==
370 psiconv_formula_dat_var) || 564 psiconv_formula_dat_var) ||
371 (formula_elements[marker].formula_type ==
372 psiconv_formula_dat_string) ||
373 (formula_elements[marker].formula_type ==
374 psiconv_formula_dat_cellref) ||
375 (formula_elements[marker].formula_type ==
376 psiconv_formula_dat_cellblock) ||
377 (formula_elements[marker].formula_type == 565 (formula_elements[marker].formula_type ==
378 psiconv_formula_dat_vcellblock) || 566 psiconv_formula_dat_vcellblock) ||
379 (formula_elements[marker].formula_type == 567 (formula_elements[marker].formula_type ==
380 psiconv_formula_mark_opsep) || 568 psiconv_formula_mark_opsep) ||
381 (formula_elements[marker].formula_type == 569 (formula_elements[marker].formula_type ==

Legend:
Removed from v.100  
changed lines
  Added in v.104

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