/[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 129
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
79static psiconv_sheet_cell_reference_t
80 psiconv_read_var_cellref (const psiconv_buffer buf, int lev,
81 psiconv_u32 off, int *length,
82 int *status)
83{
84 int len=0;
85 int res;
86 psiconv_sheet_cell_reference_t result;
87 psiconv_u32 temp;
88
89 psiconv_progress(lev+1,off+len,"Going to read a sheet cell reference");
90 psiconv_progress(lev+2,off+len,
91 "Going to read the initial byte (%02x expected)",0x00);
92 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
93 if (res)
94 goto ERROR1;
95 if (temp != 0x00) {
96 psiconv_warn(lev+2,off+len,
97 "Sheet cell reference initial byte unknown value (ignored)");
98 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
99 }
100 len ++;
101
102 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
103 if (res)
104 goto ERROR1;
105 if (temp & 0xffff0000) {
106 psiconv_warn(lev+2,off+len,
107 "Sheet cell row reference to unknown row (reset)");
108 }
109 result.row.offset = temp;
110 result.row.absolute = psiconv_bool_true;
111 len += 4;
112
113 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
114 if (res)
115 goto ERROR1;
116 if (temp & 0xffff0000) {
117 psiconv_warn(lev+2,off+len,
118 "Sheet cell column reference to unknown row (reset)");
119 }
120 result.column.offset = temp;
121 result.column.absolute = psiconv_bool_true;
122 len += 4;
123
124 if (length)
125 *length = len;
126
127 psiconv_progress(lev,off+len-1,
128 "End of sheet column reference (total length: %08x)", len);
129 return result;
130ERROR1:
131 psiconv_warn(lev+1,off,"Reading of Sheet Column Reference failed");
132 if (length)
133 *length = 0;
134 if (status)
135 *status = res?res:-PSICONV_E_NOMEM;
136 return result;
137}
138
139static psiconv_sheet_cell_block_t
140 psiconv_read_var_cellblock (const psiconv_buffer buf, int lev,
141 psiconv_u32 off, int *length,
142 int *status)
143{
144 int len=0;
145 int res;
146 psiconv_sheet_cell_block_t result;
147 psiconv_u32 temp;
148
149 psiconv_progress(lev+1,off+len,"Going to read a sheet cell block reference");
150 psiconv_progress(lev+2,off+len,
151 "Going to read the initial byte (%02x expected)",0x00);
152 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
153 if (res)
154 goto ERROR1;
155 if (temp != 0x00) {
156 psiconv_warn(lev+2,off+len,
157 "Sheet cell reference initial byte unknown value (ignored)");
158 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
159 }
160 len ++;
161
162 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
163 if (res)
164 goto ERROR1;
165 if (temp & 0xffff0000) {
166 psiconv_warn(lev+2,off+len,
167 "Sheet block initial row reference to unknown row (reset)");
168 }
169 result.first.row.offset = temp;
170 result.first.row.absolute = psiconv_bool_true;
171 len += 4;
172
173 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
174 if (res)
175 goto ERROR1;
176 if (temp & 0xffff0000) {
177 psiconv_warn(lev+2,off+len,
178 "Sheet block initial column reference to unknown row (reset)");
179 }
180 result.first.column.offset = temp;
181 result.first.column.absolute = psiconv_bool_true;
182 len += 4;
183
184 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
185 if (res)
186 goto ERROR1;
187 if (temp & 0xffff0000) {
188 psiconv_warn(lev+2,off+len,
189 "Sheet block final row reference to unknown row (reset)");
190 }
191 result.last.row.offset = temp;
192 result.last.row.absolute = psiconv_bool_true;
193 len += 4;
194
195 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
196 if (res)
197 goto ERROR1;
198 if (temp & 0xffff0000) {
199 psiconv_warn(lev+2,off+len,
200 "Sheet block final column reference to unknown row (reset)");
201 }
202 result.last.column.offset = temp;
203 result.last.column.absolute = psiconv_bool_true;
204 len += 4;
205
206 if (length)
207 *length = len;
208
209 psiconv_progress(lev,off+len-1,
210 "End of sheet cell block reference (total length: %08x)",
211 len);
212 return result;
213ERROR1:
214 psiconv_warn(lev+1,off,"Reading of Sheet Cell Block Reference failed");
215 if (length)
216 *length = 0;
217 if (status)
218 *status = res?res:-PSICONV_E_NOMEM;
219 return result;
220}
221
28int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev, 222int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev,
29 psiconv_u32 off, int *length, 223 psiconv_u32 off, int *length,
30 psiconv_sheet_numberformat *result) 224 psiconv_sheet_numberformat result)
31{ 225{
32 int res=0; 226 int res=0;
33 int len=0; 227 int len=0;
34 psiconv_u8 temp; 228 psiconv_u8 temp;
35 229
36 psiconv_progress(lev+1,off,"Going to read a sheet numberformat"); 230 psiconv_progress(lev+1,off,"Going to read a sheet numberformat");
37 if (!(*result = malloc(sizeof(**result))))
38 goto ERROR1;
39 231
40 psiconv_progress(lev+2,off+len, 232 psiconv_progress(lev+2,off+len,
41 "Going to read the initial byte (%02x expected)",0x02); 233 "Going to read the initial byte (%02x expected)",0x02);
42 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 234 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
43 if (res) 235 if (res)
44 goto ERROR2; 236 goto ERROR1;
45 if (temp != 0x02) { 237 if (temp != 0x02) {
46 psiconv_warn(lev+2,off+len, 238 psiconv_warn(lev+2,off+len,
47 "Sheet numberformat initial byte unknown value (ignored)"); 239 "Sheet numberformat initial byte unknown value (ignored)");
48 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 240 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
49 } 241 }
50 len ++; 242 len ++;
51 243
52 psiconv_progress(lev+2,off+len, "Going to read the code byte"); 244 psiconv_progress(lev+2,off+len, "Going to read the code byte");
53 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 245 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
54 if (res) 246 if (res)
55 goto ERROR2; 247 goto ERROR1;
56 psiconv_debug(lev+2,off+len,"Code: %02x",temp); 248 psiconv_debug(lev+2,off+len,"Code: %02x",temp);
57 if (temp == 0x00) 249 if (temp == 0x00)
58 (*result)->code = psiconv_numberformat_general; 250 result->code = psiconv_numberformat_general;
59 else if (temp == 0x02) 251 else if (temp == 0x02)
60 (*result)->code = psiconv_numberformat_fixeddecimal; 252 result->code = psiconv_numberformat_fixeddecimal;
61 else if (temp == 0x04) 253 else if (temp == 0x04)
62 (*result)->code = psiconv_numberformat_scientific; 254 result->code = psiconv_numberformat_scientific;
63 else if (temp == 0x06) 255 else if (temp == 0x06)
64 (*result)->code = psiconv_numberformat_currency; 256 result->code = psiconv_numberformat_currency;
65 else if (temp == 0x08) 257 else if (temp == 0x08)
66 (*result)->code = psiconv_numberformat_percent; 258 result->code = psiconv_numberformat_percent;
67 else if (temp == 0x0A) 259 else if (temp == 0x0A)
68 (*result)->code = psiconv_numberformat_triads; 260 result->code = psiconv_numberformat_triads;
69 else if (temp == 0x0C) 261 else if (temp == 0x0C)
70 (*result)->code = psiconv_numberformat_boolean; 262 result->code = psiconv_numberformat_boolean;
71 else if (temp == 0x0E) 263 else if (temp == 0x0E)
72 (*result)->code = psiconv_numberformat_text; 264 result->code = psiconv_numberformat_text;
73 else if (temp == 0x10) 265 else if (temp == 0x10)
74 (*result)->code = psiconv_numberformat_date_ddmm; 266 result->code = psiconv_numberformat_date_dmm;
75 else if (temp == 0x12) 267 else if (temp == 0x12)
76 (*result)->code = psiconv_numberformat_date_mmdd; 268 result->code = psiconv_numberformat_date_mmd;
77 else if (temp == 0x14) 269 else if (temp == 0x14)
78 (*result)->code = psiconv_numberformat_date_ddmmyy; 270 result->code = psiconv_numberformat_date_ddmmyy;
79 else if (temp == 0x16) 271 else if (temp == 0x16)
80 (*result)->code = psiconv_numberformat_date_mmddyy; 272 result->code = psiconv_numberformat_date_mmddyy;
81 else if (temp == 0x18) 273 else if (temp == 0x18)
82 (*result)->code = psiconv_numberformat_date_yymmdd; 274 result->code = psiconv_numberformat_date_yymmdd;
83 else if (temp == 0x1A) 275 else if (temp == 0x1A)
84 (*result)->code = psiconv_numberformat_date_ddmmm; 276 result->code = psiconv_numberformat_date_dmmm;
85 else if (temp == 0x1C) 277 else if (temp == 0x1C)
86 (*result)->code = psiconv_numberformat_date_ddmmmyy; 278 result->code = psiconv_numberformat_date_dmmmyy;
87 else if (temp == 0x1E) 279 else if (temp == 0x1E)
88 (*result)->code = psiconv_numberformat_date_ddmmmyyyy; 280 result->code = psiconv_numberformat_date_ddmmmyy;
89 else if (temp == 0x20) 281 else if (temp == 0x20)
90 (*result)->code = psiconv_numberformat_date_mmm; 282 result->code = psiconv_numberformat_date_mmm;
91 else if (temp == 0x22) 283 else if (temp == 0x22)
92 (*result)->code = psiconv_numberformat_date_monthname; 284 result->code = psiconv_numberformat_date_monthname;
93 else if (temp == 0x24) 285 else if (temp == 0x24)
94 (*result)->code = psiconv_numberformat_date_mmmyy; 286 result->code = psiconv_numberformat_date_mmmyy;
95 else if (temp == 0x26) 287 else if (temp == 0x26)
96 (*result)->code = psiconv_numberformat_date_monthnameyy; 288 result->code = psiconv_numberformat_date_monthnameyy;
97 else if (temp == 0x28) 289 else if (temp == 0x28)
98 (*result)->code = psiconv_numberformat_date_monthnameddyyyy; 290 result->code = psiconv_numberformat_date_monthnamedyyyy;
99 else if (temp == 0x2A) 291 else if (temp == 0x2A)
100 (*result)->code = psiconv_numberformat_datetime_ddmmyyyyhhii; 292 result->code = psiconv_numberformat_datetime_ddmmyyyyhhii;
101 else if (temp == 0x2C) 293 else if (temp == 0x2C)
102 (*result)->code = psiconv_numberformat_datetime_ddmmyyyyHHii; 294 result->code = psiconv_numberformat_datetime_ddmmyyyyHHii;
103 else if (temp == 0x2E) 295 else if (temp == 0x2E)
104 (*result)->code = psiconv_numberformat_datetime_mmddyyyyhhii; 296 result->code = psiconv_numberformat_datetime_mmddyyyyhhii;
105 else if (temp == 0x30) 297 else if (temp == 0x30)
106 (*result)->code = psiconv_numberformat_datetime_mmddyyyyHHii; 298 result->code = psiconv_numberformat_datetime_mmddyyyyHHii;
107 else if (temp == 0x32) 299 else if (temp == 0x32)
108 (*result)->code = psiconv_numberformat_datetime_yyyymmddhhii; 300 result->code = psiconv_numberformat_datetime_yyyymmddhhii;
109 else if (temp == 0x34) 301 else if (temp == 0x34)
110 (*result)->code = psiconv_numberformat_datetime_yyyymmddHHii; 302 result->code = psiconv_numberformat_datetime_yyyymmddHHii;
111 else if (temp == 0x36) 303 else if (temp == 0x36)
112 (*result)->code = psiconv_numberformat_time_hhii; 304 result->code = psiconv_numberformat_time_hhii;
113 else if (temp == 0x38) 305 else if (temp == 0x38)
114 (*result)->code = psiconv_numberformat_time_hhiiss; 306 result->code = psiconv_numberformat_time_hhiiss;
115 else if (temp == 0x3A) 307 else if (temp == 0x3A)
116 (*result)->code = psiconv_numberformat_time_HHii; 308 result->code = psiconv_numberformat_time_HHii;
117 else if (temp == 0x3C) 309 else if (temp == 0x3C)
118 (*result)->code = psiconv_numberformat_time_HHiiss; 310 result->code = psiconv_numberformat_time_HHiiss;
119 else { 311 else {
120 psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)"); 312 psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)");
121 (*result)->code = psiconv_numberformat_general; 313 result->code = psiconv_numberformat_general;
122 } 314 }
123 len ++; 315 len ++;
124 316
125 psiconv_progress(lev+2,off+len, "Going to read the number of decimals"); 317 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); 318 result->decimal = psiconv_read_u8(buf,lev+2,off+len,&res) >> 1;
127 if (res) 319 if (res)
128 goto ERROR2; 320 goto ERROR1;
129 psiconv_debug(lev+2,off+len,"Decimals: %d",(*result)->decimal); 321 psiconv_debug(lev+2,off+len,"Decimals: %d",result->decimal);
130 len ++; 322 len ++;
131 323
132 if (length) 324 if (length)
133 *length = len; 325 *length = len;
134 326
135 psiconv_progress(lev,off+len-1, 327 psiconv_progress(lev,off+len-1,
136 "End of sheet number format (total length: %08x)", len); 328 "End of sheet number format (total length: %08x)", len);
137 return 0; 329 return 0;
138 330
139ERROR2:
140 free (*result);
141ERROR1: 331ERROR1:
142 psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed"); 332 psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed");
143 if (length) 333 if (length)
144 *length = 0; 334 *length = 0;
145 if (!res) 335 if (!res)
264 if (res) 454 if (res)
265 goto ERROR2; 455 goto ERROR2;
266 if (temp != 0x00) { 456 if (temp != 0x00) {
267 psiconv_warn(lev+2,off+len, 457 psiconv_warn(lev+2,off+len,
268 "Sheet status section unknown byte unknown value (ignored)"); 458 "Sheet status section unknown byte unknown value (ignored)");
269 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 459 psiconv_debug(lev+2,off+len,"Unknown byte: %02x",temp);
270 } 460 }
271 len ++; 461 len ++;
272 462
273 psiconv_progress(lev+2,off+len,"Going to read sheet display size"); 463 psiconv_progress(lev+2,off+len,"Going to read sheet display size");
274 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); 464 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res);
307 497
308int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev, 498int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev,
309 psiconv_u32 off, int *length, 499 psiconv_u32 off, int *length,
310 psiconv_sheet_workbook_section *result) 500 psiconv_sheet_workbook_section *result)
311{ 501{
312 int res=0; 502 int res=0,with_name;
313 psiconv_u32 temp,formulas_off,worksheets_off; 503 psiconv_u32 temp,formulas_off,worksheets_off,info_off,var_off,name_off=0;
314 int len=0; 504 int len=0;
315 505
316 psiconv_progress(lev+1,off,"Going to read the sheet workbook section"); 506 psiconv_progress(lev+1,off,"Going to read the sheet workbook section");
317 if (!(*result = malloc(sizeof(**result)))) 507 if (!(*result = malloc(sizeof(**result))))
318 goto ERROR1; 508 goto ERROR1;
319 509
320 psiconv_progress(lev+2,off+len, 510 psiconv_progress(lev+2,off+len,
321 "Going to read the initial byte (%02x expected)",0x04); 511 "Going to read the initial byte (%02x or $02xexpected)",
512 0x02,0x04);
322 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 513 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
323 if (res) 514 if (res)
324 goto ERROR2; 515 goto ERROR2;
325 if (temp != 0x04) { 516 if ((temp != 0x04) && temp !=0x02) {
326 psiconv_warn(lev+2,off+len, 517 psiconv_warn(lev+2,off+len,
327 "Sheet workbook section initial byte unknown value (ignored)"); 518 "Sheet workbook section initial byte unknown value (ignored)");
328 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 519 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
329 } 520 }
521 with_name = temp ==0x04;
330 len ++; 522 len ++;
331 523
332 psiconv_progress(lev+2,off+len, 524 psiconv_progress(lev+2,off+len,
333 "Going to read the offset of the 1st ??? Section"); 525 "Going to read the offset of the sheet info Section");
334 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 526 info_off = psiconv_read_u32(buf,lev+2,off+len,&res);
335 if (res) 527 if (res)
336 goto ERROR2; 528 goto ERROR2;
337 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 529 psiconv_debug(lev+2,off+len,"Offset: %04x",info_off);
338 len += 4; 530 len += 4;
339 531
340 psiconv_progress(lev+2,off+len, 532 psiconv_progress(lev+2,off+len,
341 "Going to read the offset of the Formulas List"); 533 "Going to read the offset of the Formulas List");
342 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res); 534 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res);
352 goto ERROR2; 544 goto ERROR2;
353 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off); 545 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off);
354 len += 4; 546 len += 4;
355 547
356 psiconv_progress(lev+2,off+len, 548 psiconv_progress(lev+2,off+len,
357 "Going to read the offset of the 4th ??? Section"); 549 "Going to read the offset of the Variable List");
358 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 550 var_off = psiconv_read_u32(buf,lev+2,off+len,&res);
359 if (res) 551 if (res)
360 goto ERROR2; 552 goto ERROR2;
361 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 553 psiconv_debug(lev+2,off+len,"Offset: %04x",var_off);
362 len += 4; 554 len += 4;
363 555
556 if (with_name) {
557 psiconv_progress(lev+2,off+len,
558 "Going to read the offset of the Name Section");
559 name_off = psiconv_read_u32(buf,lev+2,off+len,&res);
560 if (res)
561 goto ERROR2;
562 psiconv_debug(lev+2,off+len,"Offset: %04x",name_off);
563 len += 4;
564 }
565
566
567 psiconv_progress(lev+2,off+len,"Going to read the info section");
568 if ((res = psiconv_parse_sheet_info_section(buf,lev+2,info_off,NULL,
569 &(*result)->info)))
570 goto ERROR2;
571
572 psiconv_progress(lev+2,off+len,"Going to read the variables list");
573 if ((res = psiconv_parse_sheet_variable_list(buf,lev+2,var_off,NULL,
574 &(*result)->variables)))
575 goto ERROR3;
576
364 psiconv_progress(lev+2,off+len,"Going to read the formulas list"); 577 psiconv_progress(lev+2,off+len,"Going to read the formulas list");
365 if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL, 578 if ((res = psiconv_parse_sheet_formula_list(buf,lev+2,formulas_off,NULL,
366 &(*result)->formulas))) 579 &(*result)->formulas)))
367 goto ERROR2; 580 goto ERROR4;
368 581
369 psiconv_progress(lev+2,off+len,"Going to read the worksheet list"); 582 psiconv_progress(lev+2,off+len,"Going to read the worksheet list");
370 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off, 583 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off,
371 NULL,&(*result)->worksheets))) 584 NULL,&(*result)->worksheets)))
372 goto ERROR2; 585 goto ERROR5;
373 586
587 if (with_name) {
588 psiconv_progress(lev+2,off+len,"Going to read the name section");
589
590 if ((res = psiconv_parse_sheet_name_section(buf,lev+2,name_off,NULL,
591 &(*result)->name)))
592 goto ERROR6;
593 } else
594 (*result)->name = NULL;
374 595
375 if (length) 596 if (length)
376 *length = len; 597 *length = len;
377 598
378 psiconv_progress(lev,off+len-1, 599 psiconv_progress(lev,off+len-1,
379 "End of sheet workbook section (total length: %08x)", len); 600 "End of sheet workbook section (total length: %08x)", len);
380 return 0; 601 return 0;
381 602
603ERROR6:
604 psiconv_free_sheet_worksheet_list((*result)->worksheets);
605ERROR5:
606 psiconv_free_formula_list((*result)->formulas);
607ERROR4:
608 psiconv_free_sheet_variable_list((*result)->variables);
609ERROR3:
610 psiconv_free_sheet_info_section((*result)->info);
382ERROR2: 611ERROR2:
383 free (*result); 612 free (*result);
384ERROR1: 613ERROR1:
385 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed"); 614 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed");
386 if (length) 615 if (length)
389 return -PSICONV_E_NOMEM; 618 return -PSICONV_E_NOMEM;
390 else 619 else
391 return res; 620 return res;
392} 621}
393 622
623int psiconv_parse_sheet_name_section(const psiconv_buffer buf, int lev,
624 psiconv_u32 off, int *length,
625 psiconv_sheet_name_section *result)
626{
627 int res=0;
628 psiconv_u32 temp;
629 int len=0,leng;
630
631 psiconv_progress(lev+1,off,"Going to read the sheet name section");
632 if (!(*result = malloc(sizeof(**result))))
633 goto ERROR1;
634
635 psiconv_progress(lev+2,off+len,
636 "Going to read the initial byte (%02x expected)",0x02);
637 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
638 if (res)
639 goto ERROR2;
640 if (temp != 0x02) {
641 psiconv_warn(lev+2,off+len,
642 "Sheet name section initial byte unknown value (ignored)");
643 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
644 }
645 len ++;
646
647 psiconv_progress(lev+2,off+len, "Going to read the sheet name");
648 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
649 if (res)
650 goto ERROR2;
651 len += leng;
652
653 if (length)
654 *length = len;
655
656 psiconv_progress(lev,off+len-1,
657 "End of sheet name section (total length: %08x)", len);
658 return 0;
659
660ERROR2:
661 free(*result);
662ERROR1:
663 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
664 if (length)
665 *length = 0;
666 if (!res)
667 return -PSICONV_E_NOMEM;
668 else
669 return res;
670}
671
672int psiconv_parse_sheet_info_section(const psiconv_buffer buf, int lev,
673 psiconv_u32 off, int *length,
674 psiconv_sheet_info_section *result)
675{
676 int res=0;
677 psiconv_u32 temp;
678 int len=0;
679
680 psiconv_progress(lev+1,off,"Going to read the sheet info section");
681 if (!(*result = malloc(sizeof(**result))))
682 goto ERROR1;
683
684 psiconv_progress(lev+2,off+len,
685 "Going to read the initial byte (%02x expected)",0x02);
686 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
687 if (res)
688 goto ERROR2;
689 if (temp != 0x02) {
690 psiconv_warn(lev+2,off+len,
691 "Sheet info section initial byte unknown value (ignored)");
692 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
693 }
694 len ++;
695
696 psiconv_progress(lev+2,off+len, "Going to read the flags byte");
697 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
698 if (res)
699 goto ERROR2;
700 (*result)->auto_recalc = temp & 0x01 ? psiconv_bool_true:psiconv_bool_false;
701 psiconv_debug(lev+2,off+len,"Auto recalculation: %02x",
702 (*result)->auto_recalc);
703 if ((temp & 0xfe) != 0x02) {
704 psiconv_warn(lev+2,off+len,"Sheet Info Section flags byte "
705 "contains unknown flags (ignored)");
706 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp &0xfe);
707 }
708
709 len ++;
710
711
712 if (length)
713 *length = len;
714
715 psiconv_progress(lev,off+len-1,
716 "End of sheet info section (total length: %08x)", len);
717 return 0;
718
719ERROR2:
720 free(*result);
721ERROR1:
722 psiconv_warn(lev+1,off,"Reading of Sheet Name Section failed");
723 if (length)
724 *length = 0;
725 if (!res)
726 return -PSICONV_E_NOMEM;
727 else
728 return res;
729}
730
394int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev, 731int psiconv_parse_sheet_formula_list(const psiconv_buffer buf, int lev,
395 psiconv_u32 off, int *length, 732 psiconv_u32 off, int *length,
396 psiconv_formula_list *result) 733 psiconv_formula_list *result)
397{ 734{
398 int res=0; 735 int res=0;
399 int len=0; 736 int len=0;
400 psiconv_u32 temp; 737 psiconv_u32 temp;
401 psiconv_formula formula; 738 psiconv_formula formula;
402 psiconv_u32 listlen,i; 739 psiconv_u32 listlen,i;
403 int leng; 740 int leng;
404 741
405 psiconv_progress(lev+1,off,"Going to read the sheet formula table"); 742 psiconv_progress(lev+1,off,"Going to read the sheet formula list");
406 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 743 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s))))
407 goto ERROR1; 744 goto ERROR1;
408 745
409 psiconv_progress(lev+2,off+len, 746 psiconv_progress(lev+2,off+len,
410 "Going to read the initial byte (%02x expected)",0x02); 747 "Going to read the initial byte (%02x expected)",0x02);
411 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 748 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
412 if (res) 749 if (res)
413 goto ERROR2; 750 goto ERROR2;
414 if (temp != 0x02) { 751 if (temp != 0x02) {
415 psiconv_warn(lev+2,off+len, 752 psiconv_warn(lev+2,off+len,
416 "Sheet formula table initial byte unknown value (ignored)"); 753 "Sheet formula list initial byte unknown value (ignored)");
417 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 754 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
418 } 755 }
419 len ++; 756 len ++;
420 757
421 psiconv_progress(lev+2,off+len, 758 psiconv_progress(lev+2,off+len,
438 775
439 if (length) 776 if (length)
440 *length = len; 777 *length = len;
441 778
442 psiconv_progress(lev,off+len-1, 779 psiconv_progress(lev,off+len-1,
443 "End of sheet formula table (total length: %08x)", len); 780 "End of sheet formula list (total length: %08x)", len);
444 return 0; 781 return 0;
445 782
446ERROR3: 783ERROR3:
447 psiconv_free_formula(formula); 784 psiconv_free_formula(formula);
448ERROR2: 785ERROR2:
449 psiconv_list_free(*result); 786 psiconv_list_free(*result);
450ERROR1: 787ERROR1:
451 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); 788 psiconv_warn(lev+1,off,"Reading of Sheet Formula list failed");
452 if (length) 789 if (length)
453 *length = 0; 790 *length = 0;
454 if (!res) 791 if (!res)
455 return -PSICONV_E_NOMEM; 792 return -PSICONV_E_NOMEM;
456 else 793 else
457 return res; 794 return res;
458} 795}
459 796
460int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, 797int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev,
461 psiconv_u32 off, int *length, 798 psiconv_u32 off, int *length,
462 psiconv_sheet_cell *result) 799 psiconv_sheet_cell *result,
800 const psiconv_sheet_cell_layout default_layout,
801 const psiconv_sheet_line_list row_default_layouts,
802 const psiconv_sheet_line_list col_default_layouts)
463{ 803{
464 int res=0; 804 int res=0;
465 int len=0; 805 int len=0;
466 psiconv_u32 temp; 806 psiconv_u32 temp;
467 psiconv_bool_t has_layout; 807 psiconv_bool_t has_layout;
574 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); 914 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
575 res = PSICONV_E_PARSE; 915 res = PSICONV_E_PARSE;
576 goto ERROR2; 916 goto ERROR2;
577 } 917 }
578 918
919 if (!((*result)->layout = psiconv_clone_cell_layout(
920 psiconv_get_default_layout(row_default_layouts,
921 col_default_layouts,
922 default_layout,
923 (*result)->row,
924 (*result)->column))))
925 goto ERROR2;
579 if (has_layout) { 926 if (has_layout) {
580 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len, 927 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
581 &leng,&(*result)->layout))) 928 &leng,(*result)->layout)))
582 goto ERROR2; 929 goto ERROR2;
583 len += leng; 930 len += leng;
584 } 931 }
585 932
586 if ((*result)->calculated) { 933 if ((*result)->calculated) {
611 else 958 else
612 return res; 959 return res;
613} 960}
614 961
615int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, 962int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev,
616 psiconv_u32 off, int *length, 963 psiconv_u32 off, int *length,
617 psiconv_sheet_cell_list *result) 964 psiconv_sheet_cell_list *result,
965 const psiconv_sheet_cell_layout default_layout,
966 const psiconv_sheet_line_list row_default_layouts,
967 const psiconv_sheet_line_list col_default_layouts)
618{ 968{
619 int res=0; 969 int res=0;
620 int len=0; 970 int len=0;
621 psiconv_u32 temp; 971 psiconv_u32 temp;
622 psiconv_sheet_cell cell; 972 psiconv_sheet_cell cell;
660 len += leng; 1010 len += leng;
661 1011
662 psiconv_progress(lev+2,off+len,"Going to read all cells"); 1012 psiconv_progress(lev+2,off+len,"Going to read all cells");
663 for (i = 0; i < listlen; i++) { 1013 for (i = 0; i < listlen; i++) {
664 psiconv_progress(lev+3,off+len,"Going to read cell %d",i); 1014 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))) 1015 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell,
1016 default_layout,row_default_layouts,
1017 col_default_layouts)))
666 goto ERROR2; 1018 goto ERROR2;
667 if ((res = psiconv_list_add(*result,cell))) 1019 if ((res = psiconv_list_add(*result,cell)))
668 goto ERROR3; 1020 goto ERROR3;
669 free(cell); 1021 free(cell);
670 len += leng; 1022 len += leng;
778 return res; 1130 return res;
779} 1131}
780 1132
781int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev, 1133int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev,
782 psiconv_u32 off, int *length, 1134 psiconv_u32 off, int *length,
783 psiconv_sheet_cell_layout *result) 1135 psiconv_sheet_cell_layout result)
784 1136
785{ 1137{
786 int res=0; 1138 int res=0;
787 int len=0; 1139 int len=0;
788 int leng; 1140 int leng;
789 psiconv_u8 temp; 1141 psiconv_u8 temp;
790 1142
791 psiconv_progress(lev+1,off,"Going to read a sheet cell layout"); 1143 psiconv_progress(lev+1,off,"Going to read a sheet cell layout");
792 if (!(*result = malloc(sizeof(**result))))
793 goto ERROR1;
794 1144
795 (*result)->character = NULL;
796 (*result)->paragraph = NULL;
797 (*result)->numberformat = NULL;
798
799 psiconv_progress(lev+2,off+len, 1145 psiconv_progress(lev+2,off+len,
800 "Going to read the first byte (%02x expected)",0x02); 1146 "Going to read the first byte (%02x expected)",0x02);
801 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1147 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
802 if (res) 1148 if (res)
803 goto ERROR2; 1149 goto ERROR1;
804 if (temp != 0x02) { 1150 if (temp != 0x02) {
805 psiconv_warn(lev+2,off+len, 1151 psiconv_warn(lev+2,off+len,
806 "Worksheet section initial byte unknown value (ignored)"); 1152 "Worksheet section initial byte unknown value (ignored)");
807 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1153 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
808 } 1154 }
809 len ++; 1155 len ++;
810 1156
811 psiconv_progress(lev+2,off+len,"Going to read the default formats flag"); 1157 psiconv_progress(lev+2,off+len,"Going to read the default formats flag");
812 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1158 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
813 if (res) 1159 if (res)
814 goto ERROR2; 1160 goto ERROR1;
815 len ++; 1161 len ++;
816 1162
817 if (temp & 0x01) { 1163 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"); 1164 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, 1165 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng,
822 (*result)->paragraph))) 1166 result->paragraph)))
823 goto ERROR2; 1167 goto ERROR1;
824 len += leng; 1168 len += leng;
825 } 1169 }
826 1170
827 if (temp & 0x02) { 1171 if (temp & 0x02) {
828 psiconv_progress(lev+3,off+len,"Going to read the default character codes"); 1172 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, 1173 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng,
832 (*result)->character))) 1174 result->character)))
833 goto ERROR2; 1175 goto ERROR1;
834 len += leng; 1176 len += leng;
835 } 1177 }
836 1178
837 if (temp & 0x04) { 1179 if (temp & 0x04) {
838 psiconv_progress(lev+3,off+len, "Going to read the default number format"); 1180 psiconv_progress(lev+3,off+len, "Going to read the default number format");
839 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng, 1181 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng,
840 &(*result)->numberformat); 1182 result->numberformat);
841 len += leng; 1183 len += leng;
842 } 1184 }
843 1185
844 if (length) 1186 if (length)
845 *length = len; 1187 *length = len;
847 psiconv_progress(lev,off+len-1, 1189 psiconv_progress(lev,off+len-1,
848 "End of sheet cell layout (total length: %08x)", len); 1190 "End of sheet cell layout (total length: %08x)", len);
849 1191
850 return 0; 1192 return 0;
851 1193
852ERROR2:
853 psiconv_free_sheet_cell_layout(*result);
854ERROR1: 1194ERROR1:
855 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed"); 1195 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed");
856 if (length) 1196 if (length)
857 *length = 0; 1197 *length = 0;
858 if (!res) 1198 if (!res)
865int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev, 1205int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev,
866 psiconv_u32 off, int *length, 1206 psiconv_u32 off, int *length,
867 psiconv_sheet_worksheet *result) 1207 psiconv_sheet_worksheet *result)
868{ 1208{
869 int res=0; 1209 int res=0;
870 psiconv_u32 temp,cells_off,grid_off; 1210 psiconv_u32 temp,cells_off,grid_off,rows_off,cols_off;
871 int len=0; 1211 int len=0;
872 int leng; 1212 int leng;
873 1213
874 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section"); 1214 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section");
875 if (!(*result = malloc(sizeof(**result)))) 1215 if (!(*result = malloc(sizeof(**result))))
898 "Worksheet section flags byte unknown bits (ignored)"); 1238 "Worksheet section flags byte unknown bits (ignored)");
899 } 1239 }
900 len ++; 1240 len ++;
901 1241
902 psiconv_progress(lev+2,off+len,"Going to read the default cell layout"); 1242 psiconv_progress(lev+2,off+len,"Going to read the default cell layout");
1243 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
1244 goto ERROR2;
903 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng, 1245 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng,
904 &(*result)->default_layout))) 1246 (*result)->default_layout)))
905 goto ERROR2; 1247 goto ERROR3;
906 len += leng; 1248 len += leng;
907 1249
908 psiconv_progress(lev+2,off+len, 1250 psiconv_progress(lev+2,off+len,
1251 "Going to read the offset of the row defaults Section");
1252 rows_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1253 if (res)
1254 goto ERROR3;
1255 psiconv_debug(lev+2,off+len,"Offset: %04x",rows_off);
1256 len += 4;
1257
1258 psiconv_progress(lev+2,off+len,
1259 "Going to read the offset of the column defaults Section");
1260 cols_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1261 if (res)
1262 goto ERROR3;
1263 psiconv_debug(lev+2,off+len,"Offset: %04x",cols_off);
1264 len += 4;
1265
1266 psiconv_progress(lev+2,off+len,
1267 "Going to read the offset of the Cells List");
1268 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1269 if (res)
1270 goto ERROR3;
1271 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
1272 len += 4;
1273
1274 psiconv_progress(lev+2,off+len,
1275 "Going to read the offset of the Grid Section");
1276 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
1277 if (res)
1278 goto ERROR3;
1279 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
1280 len += 4;
1281
1282 psiconv_progress(lev+2,off+len,
909 "Going to read the offset of the 1st ??? Section"); 1283 "Going to read the offset of the 3rd ??? Section");
910 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 1284 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
911 if (res) 1285 if (res)
912 goto ERROR3; 1286 goto ERROR3;
913 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 1287 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
914 len += 4; 1288 len += 4;
915 1289
916 psiconv_progress(lev+2,off+len, 1290 psiconv_progress(lev+2,off+len,"Going to read the row defaults");
917 "Going to read the offset of the 2nd ??? Section"); 1291 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,rows_off,NULL,
918 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 1292 &(*result)->row_default_layouts,
919 if (res) 1293 (*result)->default_layout)))
920 goto ERROR3; 1294 goto ERROR3;
921 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
922 len += 4;
923 1295
924 psiconv_progress(lev+2,off+len, 1296 psiconv_progress(lev+2,off+len,"Going to read the column defaults");
925 "Going to read the offset of the Cells List"); 1297 if ((res = psiconv_parse_sheet_line_list(buf,lev+2,cols_off,NULL,
926 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res); 1298 &(*result)->col_default_layouts,
927 if (res) 1299 (*result)->default_layout)))
928 goto ERROR3; 1300 goto ERROR4;
929 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off);
930 len += 4;
931
932 psiconv_progress(lev+2,off+len,
933 "Going to read the offset of the Grid Section");
934 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res);
935 if (res)
936 goto ERROR3;
937 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off);
938 len += 4;
939
940 psiconv_progress(lev+2,off+len,
941 "Going to read the offset of the 3rd ??? Section");
942 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
943 if (res)
944 goto ERROR3;
945 psiconv_debug(lev+2,off+len,"Offset: %04x",temp);
946 len += 4;
947 1301
948 psiconv_progress(lev+2,off+len,"Going to read the cells list"); 1302 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, 1303 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL,
950 &(*result)->cells))) 1304 &(*result)->cells,
1305 (*result)->default_layout,
1306 (*result)->row_default_layouts,
1307 (*result)->col_default_layouts)))
951 goto ERROR3; 1308 goto ERROR5;
952 1309
953 1310
954/* TODO: parse grid section */ 1311/* TODO: parse grid section */
955 1312
956 if (length) 1313 if (length)
958 1315
959 psiconv_progress(lev,off+len-1, 1316 psiconv_progress(lev,off+len-1,
960 "End of sheet worksheet section (total length: %08x)", len); 1317 "End of sheet worksheet section (total length: %08x)", len);
961 return 0; 1318 return 0;
962 1319
1320ERROR5:
1321 psiconv_free_sheet_line_list((*result)->col_default_layouts);
1322ERROR4:
1323 psiconv_free_sheet_line_list((*result)->row_default_layouts);
963ERROR3: 1324ERROR3:
964 psiconv_free_sheet_cell_layout((*result)->default_layout); 1325 psiconv_free_sheet_cell_layout((*result)->default_layout);
965ERROR2: 1326ERROR2:
966 free (*result); 1327 free (*result);
967ERROR1: 1328ERROR1:
972 return -PSICONV_E_NOMEM; 1333 return -PSICONV_E_NOMEM;
973 else 1334 else
974 return res; 1335 return res;
975} 1336}
976 1337
1338int psiconv_parse_sheet_line(const psiconv_buffer buf, int lev,
1339 psiconv_u32 off, int *length,
1340 psiconv_sheet_line *result,
1341 const psiconv_sheet_cell_layout default_layout)
1342{
1343 int res=0;
1344 int len=0;
1345 int leng;
977 1346
1347
1348 psiconv_progress(lev+1,off,"Going to read a sheet line");
1349 if (!(*result = malloc(sizeof(**result))))
1350 goto ERROR1;
1351
1352 psiconv_progress(lev+2,off+len,"Going to read the line number");
1353 (*result)->position = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1354 if (res)
1355 goto ERROR2;
1356 psiconv_debug(lev+2,off+len,"Line number: %d\n",(*result)->position);
1357 len += leng;
1358
1359 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
1360 goto ERROR2;
1361 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,
1362 &leng,(*result)->layout)))
1363 goto ERROR3;
1364 len += leng;
1365
1366 if (length)
1367 *length = len;
1368
1369 psiconv_progress(lev,off+len-1,
1370 "End of the sheet line (total length: %08x)", len);
1371 return 0;
1372
1373ERROR3:
1374 psiconv_free_sheet_cell_layout((*result)->layout);
1375ERROR2:
1376 free (*result);
1377ERROR1:
1378 psiconv_warn(lev+1,off,"Reading of the sheet line failed");
1379 if (length)
1380 *length = 0;
1381 if (!res)
1382 return -PSICONV_E_NOMEM;
1383 else
1384 return res;
1385}
1386
1387
1388int psiconv_parse_sheet_line_list(const psiconv_buffer buf, int lev,
1389 psiconv_u32 off, int *length,
1390 psiconv_sheet_line_list *result,
1391 const psiconv_sheet_cell_layout default_layout)
1392{
1393 int res=0;
1394 int len=0;
1395 psiconv_u32 temp;
1396 psiconv_sheet_line line;
1397 psiconv_u32 listlen,i;
1398 int leng;
1399
1400 psiconv_progress(lev+1,off,"Going to read the sheet line list");
1401 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_line_s))))
1402 goto ERROR1;
1403
1404 psiconv_progress(lev+2,off+len,
1405 "Going to read the initial byte (%02x expected)",0x02);
1406 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1407 if (res)
1408 goto ERROR2;
1409 if (temp != 0x02) {
1410 psiconv_warn(lev+2,off+len,
1411 "Sheet line list initial byte unknown value (ignored)");
1412 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1413 }
1414 len ++;
1415
1416 psiconv_progress(lev+2,off+len,
1417 "Going to read the number of defined lines");
1418 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1419 if (res)
1420 goto ERROR2;
1421 psiconv_debug(lev+2,off+len,"Number of defined lines: %d",listlen);
1422 len += leng;
1423
1424 psiconv_progress(lev+2,off+len,"Going to read all lines");
1425 for (i = 0; i < listlen; i++) {
1426 psiconv_progress(lev+3,off+len,"Going to read line %d",i);
1427 if ((res = psiconv_parse_sheet_line(buf,lev+3,off+len,&leng,&line,
1428 default_layout)))
1429 goto ERROR2;
1430 if ((res = psiconv_list_add(*result,line)))
1431 goto ERROR3;
1432 free(line);
1433 len += leng;
1434 }
1435
1436 if (length)
1437 *length = len;
1438
1439 psiconv_progress(lev,off+len-1,
1440 "End of sheet line list (total length: %08x)", len);
1441 return 0;
1442
1443ERROR3:
1444 psiconv_free_sheet_line(line);
1445ERROR2:
1446 psiconv_free_sheet_line_list(*result);
1447ERROR1:
1448 psiconv_warn(lev+1,off,"Reading of Sheet Line List failed");
1449 if (length)
1450 *length = 0;
1451 if (!res)
1452 return -PSICONV_E_NOMEM;
1453 else
1454 return res;
1455}
1456
1457int psiconv_parse_sheet_variable(const psiconv_buffer buf, int lev,
1458 psiconv_u32 off, int *length,
1459 psiconv_sheet_variable *result)
1460{
1461 int res=0;
1462 int len=0;
1463 psiconv_u32 marker;
1464 int leng;
1465
1466 psiconv_progress(lev+1,off,"Going to read a sheet variable");
1467 if (!(*result = malloc(sizeof(**result))))
1468 goto ERROR1;
1469
1470 psiconv_progress(lev+2,off+len, "Going to read the variable name");
1471 (*result)->name = psiconv_read_string(buf,lev+2,off+len,&leng,&res);
1472 if (res)
1473 goto ERROR2;
1474 len += leng;
1475
1476 psiconv_progress(lev+2,off+len,"Going to read the type marker");
1477 marker = psiconv_read_u8(buf,lev+2,off+len,&res);
1478 if (res)
1479 goto ERROR3;
1480 psiconv_debug(lev+2,off+len,"Marker: %02x",marker);
1481 len ++;
1482
1483 if (marker == 0x00) {
1484 (*result)->type = psiconv_var_int;
1485 psiconv_progress(lev+2,off+len,"Going to read a signed integer");
1486 (*result)->data.dat_int = psiconv_read_sint(buf,lev+2,off+len,&leng,&res);
1487 if (res)
1488 goto ERROR3;
1489 psiconv_debug(lev+2,off+len,"Value: %d",(*result)->data.dat_int);
1490 len += leng;
1491 } else if (marker == 0x01) {
1492 (*result)->type = psiconv_var_float;
1493 psiconv_progress(lev+2,off+len,"Going to read a floating point number");
1494 (*result)->data.dat_float = psiconv_read_float(buf,lev+2,off+len,&leng,
1495 &res);
1496 if (res)
1497 goto ERROR3;
1498 psiconv_debug(lev+2,off+len,"Value: %f",(*result)->data.dat_float);
1499 len += leng;
1500 } else if (marker == 0x02) {
1501 (*result)->type = psiconv_var_string;
1502 psiconv_progress(lev+2,off+len,"Going to read a string");
1503 (*result)->data.dat_string = psiconv_read_string(buf,lev+2,off+len,&leng,
1504 &res);
1505 if (res)
1506 goto ERROR3;
1507 len += leng;
1508 } else if (marker == 0x03) {
1509 (*result)->type = psiconv_var_cellref;
1510 psiconv_progress(lev+2,off+len,"Going to read a cell reference");
1511 (*result)->data.dat_cellref = psiconv_read_var_cellref(buf,lev+2,off+len,
1512 &leng, &res);
1513 if (res)
1514 goto ERROR3;
1515 len += leng;
1516 } else if (marker == 0x04) {
1517 (*result)->type = psiconv_var_cellblock;
1518 psiconv_progress(lev+2,off+len,"Going to read a cell block reference");
1519 (*result)->data.dat_cellblock = psiconv_read_var_cellblock(buf,lev+2,
1520 off+len,
1521 &leng, &res);
1522 if (res)
1523 goto ERROR3;
1524 len += leng;
1525 } else {
1526 psiconv_warn(lev+2,off+len,"Sheet variable unknown type marker");
1527 res = -PSICONV_E_PARSE;
1528 goto ERROR3;
1529 }
1530
1531 psiconv_progress(lev+2,off+len,"Going to read the variable number");
1532 (*result)->number = psiconv_read_u32(buf,lev+2,off+len,&res);
1533 if (res)
1534 goto ERROR4;
1535 psiconv_debug(lev+2,off+len,"Number: %08x",(*result)->number);
1536 len += 4;
1537
1538 if (length)
1539 *length = len;
1540
1541 psiconv_progress(lev,off+len-1,
1542 "End of sheet variable (total length: %08x)", len);
1543 return 0;
1544
1545ERROR4:
1546 if ((*result)->type == psiconv_var_string)
1547 free((*result)->data.dat_string);
1548ERROR3:
1549 free((*result)->name);
1550ERROR2:
1551 free (*result);
1552ERROR1:
1553 psiconv_warn(lev+1,off,"Reading of Sheet Variable failed");
1554 if (length)
1555 *length = 0;
1556 if (!res)
1557 return -PSICONV_E_NOMEM;
1558 else
1559 return res;
1560}
1561
1562
1563int psiconv_parse_sheet_variable_list(const psiconv_buffer buf, int lev,
1564 psiconv_u32 off, int *length,
1565 psiconv_sheet_variable_list *result)
1566{
1567 int res=0;
1568 int len=0;
1569 psiconv_u32 temp;
1570 psiconv_sheet_variable variable;
1571 psiconv_u32 listlen,i;
1572 int leng;
1573
1574 psiconv_progress(lev+1,off,"Going to read the sheet variable list");
1575 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_variable_s))))
1576 goto ERROR1;
1577
1578 psiconv_progress(lev+2,off+len,
1579 "Going to read the initial byte (%02x expected)",0x02);
1580 temp = psiconv_read_u8(buf,lev+2,off+len,&res);
1581 if (res)
1582 goto ERROR2;
1583 if (temp != 0x02) {
1584 psiconv_warn(lev+2,off+len,
1585 "Sheet variable list initial byte unknown value (ignored)");
1586 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp);
1587 }
1588 len ++;
1589
1590 psiconv_progress(lev+2,off+len,
1591 "Going to read the number of variables");
1592 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res);
1593 if (res)
1594 goto ERROR2;
1595 psiconv_debug(lev+2,off+len,"Number of variables: %d",listlen);
1596 len += leng;
1597
1598 psiconv_progress(lev+2,off+len,"Going to read all variables");
1599 for (i = 0; i < listlen; i++) {
1600 psiconv_progress(lev+3,off+len,"Going to read variable %d",i);
1601 if ((res = psiconv_parse_sheet_variable(buf,lev+3,off+len,&leng,&variable)))
1602 goto ERROR2;
1603 if ((res = psiconv_list_add(*result,variable)))
1604 goto ERROR3;
1605 len += leng;
1606 }
1607
1608 if (length)
1609 *length = len;
1610
1611 psiconv_progress(lev,off+len-1,
1612 "End of sheet variabels list (total length: %08x)", len);
1613 return 0;
1614
1615ERROR3:
1616 psiconv_free_sheet_variable(variable);
1617ERROR2:
1618 psiconv_list_free(*result);
1619ERROR1:
1620 psiconv_warn(lev+1,off,"Reading of Sheet Variable list failed");
1621 if (length)
1622 *length = 0;
1623 if (!res)
1624 return -PSICONV_E_NOMEM;
1625 else
1626 return res;
1627}

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

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