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

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

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

Revision 120 Revision 121
23#include <stdlib.h> 23#include <stdlib.h>
24 24
25#include "parse_routines.h" 25#include "parse_routines.h"
26#include "error.h" 26#include "error.h"
27 27
28static psiconv_sheet_cell_layout psiconv_basic_cell_layout(void)
29{
30 psiconv_sheet_cell_layout result;
31 if (!(result = malloc(sizeof(*result))))
32 goto ERROR1;
33 if (!(result->character = psiconv_basic_character_layout()))
34 goto ERROR2;
35 if (!(result->paragraph = psiconv_basic_paragraph_layout()))
36 goto ERROR3;
37 if (!(result->numberformat = malloc(sizeof(*result->numberformat))))
38 goto ERROR4;
39 result->numberformat->code = psiconv_numberformat_general;
40 result->numberformat->decimal = 2;
41 return result;
42ERROR4:
43 psiconv_free_paragraph_layout(result->paragraph);
44ERROR3:
45 psiconv_free_character_layout(result->character);
46ERROR2:
47 free(result);
48ERROR1:
49 return NULL;
50}
51
52static psiconv_sheet_cell_layout psiconv_clone_cell_layout
53 (psiconv_sheet_cell_layout original)
54{
55 psiconv_sheet_cell_layout result;
56 if (!(result = malloc(sizeof(*result))))
57 goto ERROR1;
58 if (!(result->character =
59 psiconv_clone_character_layout(original->character)))
60 goto ERROR2;
61 if (!(result->paragraph =
62 psiconv_clone_paragraph_layout(original->paragraph)))
63 goto ERROR3;
64 if (!(result->numberformat = malloc(sizeof(*result->numberformat))))
65 goto ERROR4;
66 result->numberformat->code = original->numberformat->code;
67 result->numberformat->decimal = original->numberformat->decimal;
68 return result;
69ERROR4:
70 psiconv_free_paragraph_layout(result->paragraph);
71ERROR3:
72 psiconv_free_character_layout(result->character);
73ERROR2:
74 free(result);
75ERROR1:
76 return NULL;
77}
78
28int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev, 79int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev,
29 psiconv_u32 off, int *length, 80 psiconv_u32 off, int *length,
30 psiconv_sheet_numberformat *result) 81 psiconv_sheet_numberformat result)
31{ 82{
32 int res=0; 83 int res=0;
33 int len=0; 84 int len=0;
34 psiconv_u8 temp; 85 psiconv_u8 temp;
35 86
36 psiconv_progress(lev+1,off,"Going to read a sheet numberformat"); 87 psiconv_progress(lev+1,off,"Going to read a sheet numberformat");
37 if (!(*result = malloc(sizeof(**result))))
38 goto ERROR1;
39 88
40 psiconv_progress(lev+2,off+len, 89 psiconv_progress(lev+2,off+len,
41 "Going to read the initial byte (%02x expected)",0x02); 90 "Going to read the initial byte (%02x expected)",0x02);
42 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 91 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
43 if (res) 92 if (res)
44 goto ERROR2; 93 goto ERROR1;
45 if (temp != 0x02) { 94 if (temp != 0x02) {
46 psiconv_warn(lev+2,off+len, 95 psiconv_warn(lev+2,off+len,
47 "Sheet numberformat initial byte unknown value (ignored)"); 96 "Sheet numberformat initial byte unknown value (ignored)");
48 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 97 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
49 } 98 }
50 len ++; 99 len ++;
51 100
52 psiconv_progress(lev+2,off+len, "Going to read the code byte"); 101 psiconv_progress(lev+2,off+len, "Going to read the code byte");
53 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 102 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
54 if (res) 103 if (res)
55 goto ERROR2; 104 goto ERROR1;
56 psiconv_debug(lev+2,off+len,"Code: %02x",temp); 105 psiconv_debug(lev+2,off+len,"Code: %02x",temp);
57 if (temp == 0x00) 106 if (temp == 0x00)
58 (*result)->code = psiconv_numberformat_general; 107 result->code = psiconv_numberformat_general;
59 else if (temp == 0x02) 108 else if (temp == 0x02)
60 (*result)->code = psiconv_numberformat_fixeddecimal; 109 result->code = psiconv_numberformat_fixeddecimal;
61 else if (temp == 0x04) 110 else if (temp == 0x04)
62 (*result)->code = psiconv_numberformat_scientific; 111 result->code = psiconv_numberformat_scientific;
63 else if (temp == 0x06) 112 else if (temp == 0x06)
64 (*result)->code = psiconv_numberformat_currency; 113 result->code = psiconv_numberformat_currency;
65 else if (temp == 0x08) 114 else if (temp == 0x08)
66 (*result)->code = psiconv_numberformat_percent; 115 result->code = psiconv_numberformat_percent;
67 else if (temp == 0x0A) 116 else if (temp == 0x0A)
68 (*result)->code = psiconv_numberformat_triads; 117 result->code = psiconv_numberformat_triads;
69 else if (temp == 0x0C) 118 else if (temp == 0x0C)
70 (*result)->code = psiconv_numberformat_boolean; 119 result->code = psiconv_numberformat_boolean;
71 else if (temp == 0x0E) 120 else if (temp == 0x0E)
72 (*result)->code = psiconv_numberformat_text; 121 result->code = psiconv_numberformat_text;
73 else if (temp == 0x10) 122 else if (temp == 0x10)
74 (*result)->code = psiconv_numberformat_date_ddmm; 123 result->code = psiconv_numberformat_date_ddmm;
75 else if (temp == 0x12) 124 else if (temp == 0x12)
76 (*result)->code = psiconv_numberformat_date_mmdd; 125 result->code = psiconv_numberformat_date_mmdd;
77 else if (temp == 0x14) 126 else if (temp == 0x14)
78 (*result)->code = psiconv_numberformat_date_ddmmyy; 127 result->code = psiconv_numberformat_date_ddmmyy;
79 else if (temp == 0x16) 128 else if (temp == 0x16)
80 (*result)->code = psiconv_numberformat_date_mmddyy; 129 result->code = psiconv_numberformat_date_mmddyy;
81 else if (temp == 0x18) 130 else if (temp == 0x18)
82 (*result)->code = psiconv_numberformat_date_yymmdd; 131 result->code = psiconv_numberformat_date_yymmdd;
83 else if (temp == 0x1A) 132 else if (temp == 0x1A)
84 (*result)->code = psiconv_numberformat_date_ddmmm; 133 result->code = psiconv_numberformat_date_ddmmm;
85 else if (temp == 0x1C) 134 else if (temp == 0x1C)
86 (*result)->code = psiconv_numberformat_date_ddmmmyy; 135 result->code = psiconv_numberformat_date_ddmmmyy;
87 else if (temp == 0x1E) 136 else if (temp == 0x1E)
88 (*result)->code = psiconv_numberformat_date_ddmmmyyyy; 137 result->code = psiconv_numberformat_date_ddmmmyyyy;
89 else if (temp == 0x20) 138 else if (temp == 0x20)
90 (*result)->code = psiconv_numberformat_date_mmm; 139 result->code = psiconv_numberformat_date_mmm;
91 else if (temp == 0x22) 140 else if (temp == 0x22)
92 (*result)->code = psiconv_numberformat_date_monthname; 141 result->code = psiconv_numberformat_date_monthname;
93 else if (temp == 0x24) 142 else if (temp == 0x24)
94 (*result)->code = psiconv_numberformat_date_mmmyy; 143 result->code = psiconv_numberformat_date_mmmyy;
95 else if (temp == 0x26) 144 else if (temp == 0x26)
96 (*result)->code = psiconv_numberformat_date_monthnameyy; 145 result->code = psiconv_numberformat_date_monthnameyy;
97 else if (temp == 0x28) 146 else if (temp == 0x28)
98 (*result)->code = psiconv_numberformat_date_monthnameddyyyy; 147 result->code = psiconv_numberformat_date_monthnameddyyyy;
99 else if (temp == 0x2A) 148 else if (temp == 0x2A)
100 (*result)->code = psiconv_numberformat_datetime_ddmmyyyyhhii; 149 result->code = psiconv_numberformat_datetime_ddmmyyyyhhii;
101 else if (temp == 0x2C) 150 else if (temp == 0x2C)
102 (*result)->code = psiconv_numberformat_datetime_ddmmyyyyHHii; 151 result->code = psiconv_numberformat_datetime_ddmmyyyyHHii;
103 else if (temp == 0x2E) 152 else if (temp == 0x2E)
104 (*result)->code = psiconv_numberformat_datetime_mmddyyyyhhii; 153 result->code = psiconv_numberformat_datetime_mmddyyyyhhii;
105 else if (temp == 0x30) 154 else if (temp == 0x30)
106 (*result)->code = psiconv_numberformat_datetime_mmddyyyyHHii; 155 result->code = psiconv_numberformat_datetime_mmddyyyyHHii;
107 else if (temp == 0x32) 156 else if (temp == 0x32)
108 (*result)->code = psiconv_numberformat_datetime_yyyymmddhhii; 157 result->code = psiconv_numberformat_datetime_yyyymmddhhii;
109 else if (temp == 0x34) 158 else if (temp == 0x34)
110 (*result)->code = psiconv_numberformat_datetime_yyyymmddHHii; 159 result->code = psiconv_numberformat_datetime_yyyymmddHHii;
111 else if (temp == 0x36) 160 else if (temp == 0x36)
112 (*result)->code = psiconv_numberformat_time_hhii; 161 result->code = psiconv_numberformat_time_hhii;
113 else if (temp == 0x38) 162 else if (temp == 0x38)
114 (*result)->code = psiconv_numberformat_time_hhiiss; 163 result->code = psiconv_numberformat_time_hhiiss;
115 else if (temp == 0x3A) 164 else if (temp == 0x3A)
116 (*result)->code = psiconv_numberformat_time_HHii; 165 result->code = psiconv_numberformat_time_HHii;
117 else if (temp == 0x3C) 166 else if (temp == 0x3C)
118 (*result)->code = psiconv_numberformat_time_HHiiss; 167 result->code = psiconv_numberformat_time_HHiiss;
119 else { 168 else {
120 psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)"); 169 psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)");
121 (*result)->code = psiconv_numberformat_general; 170 result->code = psiconv_numberformat_general;
122 } 171 }
123 len ++; 172 len ++;
124 173
125 psiconv_progress(lev+2,off+len, "Going to read the number of decimals"); 174 psiconv_progress(lev+2,off+len, "Going to read the number of decimals");
126 (*result)->decimal = psiconv_read_u8(buf,lev+2,off+len,&res); 175 result->decimal = psiconv_read_u8(buf,lev+2,off+len,&res);
127 if (res) 176 if (res)
128 goto ERROR2; 177 goto ERROR1;
129 psiconv_debug(lev+2,off+len,"Decimals: %d",(*result)->decimal); 178 psiconv_debug(lev+2,off+len,"Decimals: %d",result->decimal);
130 len ++; 179 len ++;
131 180
132 if (length) 181 if (length)
133 *length = len; 182 *length = len;
134 183
135 psiconv_progress(lev,off+len-1, 184 psiconv_progress(lev,off+len-1,
136 "End of sheet number format (total length: %08x)", len); 185 "End of sheet number format (total length: %08x)", len);
137 return 0; 186 return 0;
138 187
139ERROR2:
140 free (*result);
141ERROR1: 188ERROR1:
142 psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed"); 189 psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed");
143 if (length) 190 if (length)
144 *length = 0; 191 *length = 0;
145 if (!res) 192 if (!res)
456 else 503 else
457 return res; 504 return res;
458} 505}
459 506
460int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, 507int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
461 psiconv_u32 off, int *length, 508 psiconv_u32 off, int *length,
462 psiconv_sheet_cell *result) 509 psiconv_sheet_cell *result,
510 const psiconv_sheet_cell_layout default_layout)
463{ 511{
464 int res=0; 512 int res=0;
465 int len=0; 513 int len=0;
466 psiconv_u32 temp; 514 psiconv_u32 temp;
467 psiconv_bool_t has_layout; 515 psiconv_bool_t has_layout;
574 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); 622 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
575 res = PSICONV_E_PARSE; 623 res = PSICONV_E_PARSE;
576 goto ERROR2; 624 goto ERROR2;
577 } 625 }
578 626
627 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
628 goto ERROR2;
579 if (has_layout) { 629 if (has_layout) {
580 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len, 630 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
581 &leng,&(*result)->layout))) 631 &leng,(*result)->layout)))
582 goto ERROR2; 632 goto ERROR2;
583 len += leng; 633 len += leng;
584 } 634 }
585 635
586 if ((*result)->calculated) { 636 if ((*result)->calculated) {
611 else 661 else
612 return res; 662 return res;
613} 663}
614 664
615int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, 665int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
616 psiconv_u32 off, int *length, 666 psiconv_u32 off, int *length,
617 psiconv_sheet_cell_list *result) 667 psiconv_sheet_cell_list *result,
668 const psiconv_sheet_cell_layout default_layout)
618{ 669{
619 int res=0; 670 int res=0;
620 int len=0; 671 int len=0;
621 psiconv_u32 temp; 672 psiconv_u32 temp;
622 psiconv_sheet_cell cell; 673 psiconv_sheet_cell cell;
660 len += leng; 711 len += leng;
661 712
662 psiconv_progress(lev+2,off+len,"Going to read all cells"); 713 psiconv_progress(lev+2,off+len,"Going to read all cells");
663 for (i = 0; i < listlen; i++) { 714 for (i = 0; i < listlen; i++) {
664 psiconv_progress(lev+3,off+len,"Going to read cell %d",i); 715 psiconv_progress(lev+3,off+len,"Going to read cell %d",i);
665 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell))) 716 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
717 default_layout)))
666 goto ERROR2; 718 goto ERROR2;
667 if ((res = psiconv_list_add(*result,cell))) 719 if ((res = psiconv_list_add(*result,cell)))
668 goto ERROR3; 720 goto ERROR3;
669 free(cell); 721 free(cell);
670 len += leng; 722 len += leng;
778 return res; 830 return res;
779} 831}
780 832
781int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev, 833int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
782 psiconv_u32 off, int *length, 834 psiconv_u32 off, int *length,
783 psiconv_sheet_cell_layout *result) 835 psiconv_sheet_cell_layout result)
784 836
785{ 837{
786 int res=0; 838 int res=0;
787 int len=0; 839 int len=0;
788 int leng; 840 int leng;
789 psiconv_u8 temp; 841 psiconv_u8 temp;
790 842
791 psiconv_progress(lev+1,off,"Going to read a sheet cell layout"); 843 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
792 if (!(*result = malloc(sizeof(**result))))
793 goto ERROR1;
794 844
795 (*result)->character = NULL;
796 (*result)->paragraph = NULL;
797 (*result)->numberformat = NULL;
798
799 psiconv_progress(lev+2,off+len, 845 psiconv_progress(lev+2,off+len,
800 "Going to read the first byte (%02x expected)",0x02); 846 "Going to read the first byte (%02x expected)",0x02);
801 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 847 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
802 if (res) 848 if (res)
803 goto ERROR2; 849 goto ERROR1;
804 if (temp != 0x02) { 850 if (temp != 0x02) {
805 psiconv_warn(lev+2,off+len, 851 psiconv_warn(lev+2,off+len,
806 "Worksheet section initial byte unknown value (ignored)"); 852 "Worksheet section initial byte unknown value (ignored)");
807 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 853 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
808 } 854 }
809 len ++; 855 len ++;
810 856
811 psiconv_progress(lev+2,off+len,"Going to read the default formats flag"); 857 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
812 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 858 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
813 if (res) 859 if (res)
814 goto ERROR2; 860 goto ERROR1;
815 len ++; 861 len ++;
816 862
817 if (temp & 0x01) { 863 if (temp & 0x01) {
818 if (!((*result)->paragraph = psiconv_basic_paragraph_layout()))
819 goto ERROR2;
820 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes"); 864 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes");
821 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, 865 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
822 (*result)->paragraph))) 866 result->paragraph)))
823 goto ERROR2; 867 goto ERROR1;
824 len += leng; 868 len += leng;
825 } 869 }
826 870
827 if (temp & 0x02) { 871 if (temp & 0x02) {
828 psiconv_progress(lev+3,off+len,"Going to read the default character codes"); 872 psiconv_progress(lev+3,off+len,"Going to read the default character codes");
829 if (!((*result)->character = psiconv_basic_character_layout()))
830 goto ERROR2;
831 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, 873 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
832 (*result)->character))) 874 result->character)))
833 goto ERROR2; 875 goto ERROR1;
834 len += leng; 876 len += leng;
835 } 877 }
836 878
837 if (temp & 0x04) { 879 if (temp & 0x04) {
838 psiconv_progress(lev+3,off+len, "Going to read the default number format"); 880 psiconv_progress(lev+3,off+len, "Going to read the default number format");
839 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng, 881 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
840 &(*result)->numberformat); 882 result->numberformat);
841 len += leng; 883 len += leng;
842 } 884 }
843 885
844 if (length) 886 if (length)
845 *length = len; 887 *length = len;
847 psiconv_progress(lev,off+len-1, 889 psiconv_progress(lev,off+len-1,
848 "End of sheet cell layout (total length: %08x)", len); 890 "End of sheet cell layout (total length: %08x)", len);
849 891
850 return 0; 892 return 0;
851 893
852ERROR2:
853 psiconv_free_sheet_cell_layout(*result);
854ERROR1: 894ERROR1:
855 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed"); 895 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
856 if (length) 896 if (length)
857 *length = 0; 897 *length = 0;
858 if (!res) 898 if (!res)
898 "Worksheet section flags byte unknown bits (ignored)"); 938 "Worksheet section flags byte unknown bits (ignored)");
899 } 939 }
900 len ++; 940 len ++;
901 941
902 psiconv_progress(lev+2,off+len,"Going to read the default cell layout"); 942 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
943 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
944 goto ERROR2;
903 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng, 945 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
904 &(*result)->default_layout))) 946 (*result)->default_layout)))
905 goto ERROR2; 947 goto ERROR3;
906 len += leng; 948 len += leng;
907 949
908 psiconv_progress(lev+2,off+len, 950 psiconv_progress(lev+2,off+len,
909 "Going to read the offset of the 1st ??? Section"); 951 "Going to read the offset of the 1st ??? Section");
910 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 952 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
945 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 987 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
946 len += 4; 988 len += 4;
947 989
948 psiconv_progress(lev+2,off+len,"Going to read the cells list"); 990 psiconv_progress(lev+2,off+len,"Going to read the cells list");
949 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL, 991 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
950 &(*result)->cells))) 992 &(*result)->cells,
993 (*result)->default_layout)))
951 goto ERROR3; 994 goto ERROR3;
952 995
953 996
954/* TODO: parse grid section */ 997/* TODO: parse grid section */
955 998

Legend:
Removed from v.120  
changed lines
  Added in v.121

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