--- psiconv/trunk/lib/psiconv/parse_sheet.c 2001/07/24 22:50:33 133 +++ psiconv/trunk/lib/psiconv/parse_sheet.c 2001/07/25 11:49:59 134 @@ -776,6 +776,7 @@ goto ERROR2; if ((res = psiconv_list_add(*result,formula))) goto ERROR3; + free(formula); len += leng; } @@ -1327,7 +1328,12 @@ goto ERROR5; -/* TODO: parse grid section */ + psiconv_progress(lev+2,off+len,"Going to read the grid section"); + if ((res = psiconv_parse_sheet_grid_section(buf,lev+2,grid_off,NULL, + &(*result)->grid))) + goto ERROR6; + + if (length) *length = len; @@ -1336,6 +1342,8 @@ "End of sheet worksheet section (total length: %08x)", len); return 0; +ERROR6: + psiconv_free_sheet_cell_list((*result)->cells); ERROR5: psiconv_free_sheet_line_list((*result)->col_default_layouts); ERROR4: @@ -1644,3 +1652,423 @@ else return res; } + +int psiconv_parse_sheet_grid_section(const psiconv_buffer buf, int lev, + psiconv_u32 off, int *length, + psiconv_sheet_grid_section *result) +{ + int res=0,i; + int len=0,leng; + psiconv_u32 temp; + + psiconv_progress(lev+1,off,"Going to read the sheet grid section"); + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; + + psiconv_progress(lev+2,off+len, "Going to read the first flags byte"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + (*result)->show_column_titles = temp&0x01?psiconv_bool_true: + psiconv_bool_false; + psiconv_debug(lev+2,off+len, + "Show column titles: %s", + (*result)->show_column_titles?"true":"false"); + (*result)->show_row_titles = temp&0x02?psiconv_bool_true:psiconv_bool_false; + psiconv_debug(lev+2,off+len, + "Show row titles: %s", + (*result)->show_row_titles?"true":"false"); + (*result)->show_vertical_grid = temp&0x04?psiconv_bool_true: + psiconv_bool_false; + psiconv_debug(lev+2,off+len, + "Show vertical grid: %s", + (*result)->show_vertical_grid?"true":"false"); + (*result)->show_horizontal_grid = temp&0x07?psiconv_bool_true: + psiconv_bool_false; + psiconv_debug(lev+2,off+len, + "Show horizontal grid: %s", + (*result)->show_horizontal_grid?"true":"false"); + (*result)->freeze_rows = temp&0x80?psiconv_bool_true:psiconv_bool_false; + psiconv_debug(lev+2,off+len, + "Freeze rows: %s", + (*result)->freeze_rows?"true":"false"); + if ((temp & 0x70) != 0x30) { + psiconv_warn(lev+2,off+len, + "Grid section first flag byte has unknown bits (ignored)"); + psiconv_debug(lev+2,off+len,"Bits: %02x (%02x expected)",temp&0x70,0x30); + } + len ++; + + psiconv_progress(lev+2,off+len, "Going to read the second flags byte"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + (*result)->freeze_columns = temp&0x01?psiconv_bool_true:psiconv_bool_false; + psiconv_debug(lev+2,off+len, + "Freeze columns: %s", (*result)->freeze_columns?"true":"false"); + if ((temp & 0xfe) != 0x80) { + psiconv_warn(lev+2,off+len, + "Grid section second flag byte has unknown bits (ignored)"); + psiconv_debug(lev+2,off+len,"Bits: %02x (%02x expected)",temp&0xfe,0x80); + } + len ++; + + psiconv_progress(lev+2,off+len, + "Going to an unknown byte (%02x expected)",0x90); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + if (temp != 0x90) { + psiconv_warn(lev+2,off+len, + "Grid section third byte unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Value: %02x",temp); + } + len ++; + + psiconv_progress(lev+2,off+len, "Going to read the fourth flags byte"); + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + (*result)->show_page_breaks = temp&0x04?psiconv_bool_true:psiconv_bool_false; + psiconv_debug(lev+2,off+len, + "Show page breaks: %s", + (*result)->show_page_breaks?"true":"false"); + if ((temp & 0xfc) != 0x00) { + psiconv_warn(lev+2,off+len, + "Grid section fourth flag byte has unknown bits (ignored)"); + psiconv_debug(lev+2,off+len,"Bits: %02x (%02x expected)",temp&0xfc,0x00); + } + len ++; + + psiconv_progress(lev+2,off+len,"Going to read the first visible row"); + (*result)->first_row = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"First row: %d",(*result)->first_row); + len += 4; + + psiconv_progress(lev+2,off+len,"Going to read the first visible column"); + (*result)->first_column = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"First column: %d",(*result)->first_column); + len += 4; + + psiconv_progress(lev+2,off+len,"Going to read the last visible row"); + (*result)->last_row = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Last row: %d",(*result)->last_row); + len += 4; + + psiconv_progress(lev+2,off+len,"Going to read the last visible column"); + (*result)->last_column = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Last column: %d",(*result)->last_column); + len += 4; + + psiconv_progress(lev+2,off+len,"Going to read the default row height"); + (*result)->default_row_height = psiconv_read_length(buf,lev+2,off+len, + &leng,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Default row height: %f", + (*result)->default_row_height); + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read the row heights list"); + if ((res = psiconv_parse_sheet_grid_size_list(buf,lev+2,off+len,&leng, + &(*result)->row_heights))) + goto ERROR2; + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read the default column height"); + (*result)->default_column_width = psiconv_read_length(buf,lev+2,off+len, + &leng,&res); + if (res) + goto ERROR3; + psiconv_debug(lev+2,off+len,"Default column width: %f", + (*result)->default_column_width); + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read the column heights list"); + if ((res = psiconv_parse_sheet_grid_size_list(buf,lev+2,off+len,&leng, + &(*result)->column_heights))) + goto ERROR3; + len += leng; + + psiconv_progress(lev+2,off+len, + "Going to read an unknown word (%04x expected)",0x00); + temp = psiconv_read_u16(buf,lev+2,off+len,&res); + if (res) + goto ERROR4; + if (temp != 0x00) { + psiconv_warn(lev+2,off+len, + "Grid section unknown word has unknown value (ignored)"); + psiconv_debug(lev+2,off+len,"Value: %04x",temp); + } + len += 2; + + psiconv_progress(lev+2,off+len,"Going to read the row breaks list"); + if ((res = psiconv_parse_sheet_grid_break_list(buf,lev+2,off+len,&leng, + &(*result)->row_page_breaks))) + goto ERROR4; + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read the column breaks list"); + if ((res = psiconv_parse_sheet_grid_break_list(buf,lev+2,off+len,&leng, + &(*result)->column_page_breaks))) + goto ERROR5; + len += leng; + + + psiconv_progress(lev+2,off+len, + "Going to read 22 unknown bytes (%02x expected)",0x00); + for (i = 0; i < 22 ; i++) { + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR6; + if (temp != 0x00) { + psiconv_warn(lev+2,off+len, + "Grid section unknown byte %d has unknown value (ignored)", + i); + psiconv_debug(lev+2,off+len,"Value: %02x",temp); + } + len += 1; + } + + if ((*result)->freeze_rows || (*result)->freeze_columns) { + + psiconv_progress(lev+2,off+len,"Going to read number of frozen rows"); + (*result)->frozen_rows = psiconv_read_u32(buf,lev+2,off+len, &res); + if (res) + goto ERROR6; + psiconv_debug(lev+2,off+len,"Number of frozen rows: %d", + (*result)->frozen_rows); + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read number of frozen columns"); + (*result)->frozen_columns = psiconv_read_u32(buf,lev+2,off+len, &res); + if (res) + goto ERROR6; + psiconv_debug(lev+2,off+len,"Number of frozen columns: %d", + (*result)->frozen_columns); + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read first unfrozen row"); + (*result)->first_unfrozen_row_displayed = psiconv_read_u32(buf,lev+2, + off+len, &res); + if (res) + goto ERROR6; + psiconv_debug(lev+2,off+len,"First row: %d", + (*result)->first_unfrozen_row_displayed); + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read first unfrozen column"); + (*result)->first_unfrozen_column_displayed = psiconv_read_u32(buf,lev+2, + off+len,&res); + if (res) + goto ERROR6; + psiconv_debug(lev+2,off+len,"First column: %d", + (*result)->first_unfrozen_column_displayed); + len += leng; + } else + (*result)->frozen_rows = (*result)->frozen_columns = + (*result)->first_unfrozen_row_displayed = + (*result)->first_unfrozen_column_displayed = 0; + + psiconv_progress(lev+2,off+len, + "Going to read 3 unknown bytes (%02x expected)",0xff); + for (i = 0; i < 3 ; i++) { + temp = psiconv_read_u8(buf,lev+2,off+len,&res); + if (res) + goto ERROR6; + if (temp != 0xff) { + psiconv_warn(lev+2,off+len, + "Grid section unknown byte %d has unknown value (ignored)", + i); + psiconv_debug(lev+2,off+len,"Value: %02x",temp); + } + len ++; + } + + if (length) + *length = len; + + psiconv_progress(lev,off+len-1, + "End of sheet grid section (total length: %08x)", len); + return 0; + +ERROR6: + psiconv_free_sheet_grid_break_list((*result)->column_page_breaks); +ERROR5: + psiconv_free_sheet_grid_break_list((*result)->row_page_breaks); +ERROR4: + psiconv_free_sheet_grid_size_list((*result)->column_heights); +ERROR3: + psiconv_free_sheet_grid_size_list((*result)->row_heights); +ERROR2: + free(*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Sheet Grid Section failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; +} + + +int psiconv_parse_sheet_grid_size_list(const psiconv_buffer buf, int lev, + psiconv_u32 off, int *length, + psiconv_sheet_grid_size_list *result) +{ + int res=0; + int len=0,i; + int leng,listlen; + psiconv_sheet_grid_size size; + + psiconv_progress(lev+1,off,"Going to read a sheet grid size list"); + if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_grid_size_s)))) + goto ERROR1; + + psiconv_progress(lev+2,off+len, + "Going to read the number of elements"); + listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Number of elements: %d",listlen); + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read all elements"); + for (i = 0; i < listlen; i++) { + psiconv_progress(lev+3,off+len,"Going to read element %d",i); + if ((res = psiconv_parse_sheet_grid_size(buf,lev+3,off+len,&leng,&size))) + goto ERROR2; + if ((res = psiconv_list_add(*result,size))) + goto ERROR3; + free(size); + len += leng; + } + + if (length) + *length = len; + + psiconv_progress(lev,off+len-1, + "End of sheet grid size list (total length: %08x)", len); + return 0; + +ERROR3: + psiconv_free_sheet_grid_size(size); +ERROR2: + psiconv_list_free(*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Sheet Grid Size List failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; +} + +int psiconv_parse_sheet_grid_size(const psiconv_buffer buf, int lev, + psiconv_u32 off, int *length, + psiconv_sheet_grid_size *result) +{ + int res=0; + int len=0; + int leng; + + psiconv_progress(lev+1,off,"Going to read a sheet grid size"); + + if (!(*result = malloc(sizeof(**result)))) + goto ERROR1; + + psiconv_progress(lev+2,off+len, "Going to read the row or column number"); + (*result)->line_number = psiconv_read_u32(buf,lev+2,off+len,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->line_number); + len += 4; + + psiconv_progress(lev+2,off+len, "Going to read the row or column height"); + (*result)->size = psiconv_read_length(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Size: %f\n",(*result)->size); + len += leng; + + if (length) + *length = len; + + psiconv_progress(lev,off+len-1, + "End of sheet grid size(total length: %08x)", len); + return 0; + +ERROR2: + free (*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Sheet Grid Size failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; +} + + +int psiconv_parse_sheet_grid_break_list(const psiconv_buffer buf, int lev, + psiconv_u32 off, int *length, + psiconv_sheet_grid_break_list *result) +{ + int res=0; + int len=0,i; + int leng,listlen; + psiconv_u32 nr; + + psiconv_progress(lev+1,off,"Going to read a sheet grid break list"); + if (!(*result = psiconv_list_new(sizeof(psiconv_u32)))) + goto ERROR1; + + psiconv_progress(lev+2,off+len, + "Going to read the number of elements"); + listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res); + if (res) + goto ERROR2; + psiconv_debug(lev+2,off+len,"Number of elements: %d",listlen); + len += leng; + + psiconv_progress(lev+2,off+len,"Going to read all elements"); + for (i = 0; i < listlen; i++) { + psiconv_progress(lev+3,off+len,"Going to read element %d",i); + nr = psiconv_read_u32(buf,lev+3,off+len,&res); + if (res) + goto ERROR2; + if ((res = psiconv_list_add(*result,&nr))) + goto ERROR2; + len += leng; + } + + if (length) + *length = len; + + psiconv_progress(lev,off+len-1, + "End of sheet grid break list (total length: %08x)", len); + return 0; + +ERROR2: + psiconv_list_free(*result); +ERROR1: + psiconv_warn(lev+1,off,"Reading of Sheet Grid break List failed"); + if (length) + *length = 0; + if (!res) + return -PSICONV_E_NOMEM; + else + return res; +} +