--- psiconv/trunk/lib/psiconv/parse_sheet.c 2001/02/16 18:49:14 109 +++ psiconv/trunk/lib/psiconv/parse_sheet.c 2001/03/04 22:10:45 110 @@ -173,7 +173,7 @@ ERROR2: free (*result); ERROR1: - psiconv_warn(lev+1,off,"Reading of Sjeet Status Section failed"); + psiconv_warn(lev+1,off,"Reading of Sheet Status Section failed"); if (length) *length = 0; if (!res) @@ -187,7 +187,7 @@ psiconv_sheet_workbook_section *result) { int res=0; - psiconv_u32 temp,formulas_off; + psiconv_u32 temp,formulas_off,worksheets_off; int len=0; psiconv_progress(lev+1,off,"Going to read the sheet workbook section"); @@ -223,8 +223,8 @@ len += 4; psiconv_progress(lev+2,off+len, - "Going to read the offset of the 3rd ??? Section"); - temp = psiconv_read_u32(buf,lev+2,off+len,&res); + "Going to read the offset of the Worksheets Section"); + worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res); if (res) goto ERROR2; psiconv_debug(lev+2,off+len,"Offset: %04x",temp); @@ -243,7 +243,12 @@ &(*result)->formulas))) goto ERROR2; + psiconv_progress(lev+2,off+len,"Going to read the worksheets"); + if ((res = psiconv_parse_sheet_worksheet_section(buf,lev+2,worksheets_off, + NULL,&(*result)->worksheet))) + goto ERROR2; + if (length) *length = len; @@ -328,3 +333,467 @@ else return res; } + +int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, + psiconv_u32 off, int *length, + psiconv_sheet_cell *result) +{ + int res=0; + int len=0; + psiconv_u32 temp; + psiconv_bool_t has_layout; + int leng; + + psiconv_progress(lev+1,off,"Going to read a sheet cell structure"); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; + + psiconv_progress(lev+2,off+len,"Going to read the cell position"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len ++; + temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 8; + if (res) + goto ERROR2; + len ++; + temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 16; + if (res) + goto ERROR2; + len ++; + (*result)->column = (temp >> 2) & 0xFF; + (*result)->row = (temp >> 10) & 0x3FFF; + psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x", + (*result)->column,(*result)->row); + + psiconv_progress(lev+2,off+len,"Going to read the cell type"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len ++; + (*result)->type = (temp >> 5) & 0x07; + (*result)->calculated = (temp & 0x08)?psiconv_bool_true:psiconv_bool_false; + has_layout = (temp & 0x10)?psiconv_bool_true:psiconv_bool_false; + + psiconv_progress(lev+2,off+len,"Going to read the cell value"); + if ((*result)->type == psiconv_cell_blank) { + psiconv_debug(lev+2,off+len,"Cell type is blank: no value given."); + } else if ((*result)->type == psiconv_cell_int) { + psiconv_progress(lev+2,off+len,"Going to read an integer"); + (*result)->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len += 4; + psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int); + + } else if ((*result)->type == psiconv_cell_bool) { + psiconv_progress(lev+2,off+len,"Going to read a boolean"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len ++; + psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp); + (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false; + + } else if ((*result)->type == psiconv_cell_error) { + psiconv_progress(lev+2,off+len,"Going to read the error code"); + (*result)->data.dat_error = psiconv_read_u16(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len += 2; + psiconv_debug(lev+2,off+len,"Cell contents: %04x", + (*result)->data.dat_error); + + } else if ((*result)->type == psiconv_cell_float) { + psiconv_progress(lev+2,off+len,"Going to read a float"); + (*result)->data.dat_float = + psiconv_read_float(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; + len += leng; + psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float); + + } else if ((*result)->type == psiconv_cell_string) { + psiconv_progress(lev+2,off+len,"Going to read a string"); + (*result)->data.dat_string = + psiconv_read_short_string(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; + len += leng; + psiconv_debug(lev+2,off+len,"Cell contents: `%s'", + (*result)->data.dat_string); + } else { + psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); + } + + if (has_layout) { + psiconv_progress(lev+2,off+len,"Going to read the cell layout"); + + psiconv_progress(lev+2,off+len,"Going to read the cell layout flags"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len ++; + + if (temp & 0x01) { + if (!((*result)->paragraph = psiconv_basic_paragraph_layout())) + goto ERROR2; + psiconv_progress(lev+3,off+len,"Going to read the paragraph codes"); + if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, + (*result)->paragraph))) + goto ERROR2; + len += leng; + } + + if (temp & 0x02) { + psiconv_progress(lev+3,off+len,"Going to read the character codes"); + if (!((*result)->character = psiconv_basic_character_layout())) + goto ERROR2; + if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, + (*result)->character))) + goto ERROR2; + len += leng; + } + + if (temp & 0x04) { +/* TODO: default number format */ + psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len ++; + psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len ++; + psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len ++; + } + } + + if ((*result)->calculated) { + psiconv_progress(lev+2,off+len,"Going to read the cell formula reference"); + temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp); + len += leng; + (*result)->ref_formula = temp; + } + + if (length) + *length = len; + + psiconv_progress(lev,off+len-1, + "End of sheet cell structure (total length: %08x)", len); + return 0; + +ERROR2: + psiconv_free_sheet_cell(*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Sheet Cell Structure failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; +} + +int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, + psiconv_u32 off, int *length, + psiconv_sheet_cell_list *result) +{ + int res=0; + int len=0; + psiconv_u32 temp; + psiconv_sheet_cell cell; + psiconv_u32 listlen,i; + int leng; + + psiconv_progress(lev+1,off,"Going to read the sheet cells list"); + if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s)))) + goto ERROR1; + + psiconv_progress(lev+2,off+len, + "Going to read the initial byte (%02x expected)",0x02); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + if (temp != 0x02) { + psiconv_warn(lev+2,off+len, + "Sheet cell list initial byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len, + "Going to read the initial byte (%02x expected)",0x00); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + if (temp != 0x00) { + psiconv_warn(lev+2,off+len, + "Sheet cell list initial byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len, + "Going to read the number of defined cells"); + listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Number of defined cells: %d",listlen); + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read all cells"); + for (i = 0; i < listlen; i++) { + psiconv_progress(lev+3,off+len,"Going to read cell %d",i); + if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell))) + goto ERROR2; + if ((res = psiconv_list_add(*result,cell))) + goto ERROR3; + len += leng; + } + + if (length) + *length = len; + + psiconv_progress(lev,off+len-1, + "End of sheet cell list (total length: %08x)", len); + return 0; + +ERROR3: + psiconv_free_sheet_cell(cell); +ERROR2: + psiconv_free_sheet_cell_list(*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; +} + + +int psiconv_parse_sheet_worksheet_section(const psiconv_buffer buf, int lev, + psiconv_u32 off, int *length, + psiconv_sheet_worksheet_section *result) +{ + int res=0; + psiconv_u32 temp,cells_off,grid_off; + int len=0; + int leng; + + /* TODO: this is the section that might be an XListE instead... */ + psiconv_progress(lev+2,off+len, + "Going to read the initial bytes (%02x expected)",0x02); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR1; + if (temp != 0x04) { + psiconv_warn(lev+2,off+len, + "Sheet worksheet section initial byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len, + "Going to read the initial bytes (%02x expected)",0x02); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR1; + if (temp != 0x04) { + psiconv_warn(lev+2,off+len, + "Sheet worksheet section initial byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len, + "Going to read the initial bytes (%02x expected)",0x00); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR1; + if (temp != 0x04) { + psiconv_warn(lev+2,off+len, + "Sheet worksheet section initial byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len, + "Going to read the offset of the Worksheet Section"); + temp = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR1; + psiconv_debug(lev+2,off+len,"Offset: %04x",temp); + len += 4; + + len = 0; + off = temp; + + /* this is the real worksheet section from here */ + + psiconv_progress(lev+1,off,"Going to read the sheet worksheet section"); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; + + psiconv_progress(lev+2,off+len, + "Going to read the initial bytes (%02x expected)",0x04); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + if (temp != 0x04) { + psiconv_warn(lev+2,off+len, + "Worksheet section initial byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len, + "Going to read the initial bytes (%02x expected)",0x01); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + if (temp != 0x01) { + psiconv_warn(lev+2,off+len, + "Worksheet section initial byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len, + "Going to read the initial bytes (%02x expected)",0x02); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + if (temp != 0x02) { + psiconv_warn(lev+2,off+len, + "Worksheet section initial byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len,"Going to read the default formats flag"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + len ++; + + if (temp & 0x01) { + if (!((*result)->paragraph = psiconv_basic_paragraph_layout())) + goto ERROR2; + psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes"); + if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, + (*result)->paragraph))) + goto ERROR2_1; + len += leng; + } + + if (temp & 0x02) { + psiconv_progress(lev+3,off+len,"Going to read the default character codes"); + if (!((*result)->character = psiconv_basic_character_layout())) + goto ERROR2_1; + if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, + (*result)->character))) + goto ERROR2_2; + len += leng; + } + + if (temp & 0x04) { +/* TODO: default number format */ + psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2_3; + len ++; + psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2_3; + len ++; + psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2_3; + len ++; + } + + psiconv_progress(lev+2,off+len, + "Going to read the offset of the 1st ??? Section"); + temp = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Offset: %04x",temp); + len += 4; + + psiconv_progress(lev+2,off+len, + "Going to read the offset of the 2nd ??? Section"); + temp = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Offset: %04x",temp); + len += 4; + + psiconv_progress(lev+2,off+len, + "Going to read the offset of the Cells List"); + cells_off = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off); + len += 4; + + psiconv_progress(lev+2,off+len, + "Going to read the offset of the Grid Section"); + grid_off = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off); + len += 4; + + psiconv_progress(lev+2,off+len, + "Going to read the offset of the 3rd ??? Section"); + temp = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Offset: %04x",temp); + len += 4; + + psiconv_progress(lev+2,off+len,"Going to read the cells list"); + if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL, + &(*result)->cells))) + goto ERROR2; + + +/* TODO: parse grid section */ + + if (length) + *length = len; + + psiconv_progress(lev,off+len-1, + "End of sheet worksheet section (total length: %08x)", len); + return 0; + +ERROR2_3: + psiconv_free_numberformat((*result)->numberformat); +ERROR2_2: + psiconv_free_character_layout((*result)->character); +ERROR2_1: + psiconv_free_paragraph_layout((*result)->paragraph); +goto ERROR2; + +ERROR2: + free (*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; +} + +