/[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 111 Revision 270
1/* 1/*
2 parse_sheet.c - Part of psiconv, a PSION 5 file formats converter 2 parse_sheet.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 2001 Frodo Looijaard <frodol@dds.nl> 3 Copyright (c) 2000-2005 Frodo Looijaard <frodo@frodo.looijaard.name>
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
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
28#ifdef DMALLOC
29#include <dmalloc.h>
30#endif
31
32static psiconv_sheet_cell_layout psiconv_basic_cell_layout(void)
33{
34 psiconv_sheet_cell_layout result;
35 if (!(result = malloc(sizeof(*result))))
36 goto ERROR1;
37 if (!(result->character = psiconv_basic_character_layout()))
38 goto ERROR2;
39 if (!(result->paragraph = psiconv_basic_paragraph_layout()))
40 goto ERROR3;
41 if (!(result->numberformat = malloc(sizeof(*result->numberformat))))
42 goto ERROR4;
43 result->numberformat->code = psiconv_numberformat_general;
44 result->numberformat->decimal = 2;
45 return result;
46ERROR4:
47 psiconv_free_paragraph_layout(result->paragraph);
48ERROR3:
49 psiconv_free_character_layout(result->character);
50ERROR2:
51 free(result);
52ERROR1:
53 return NULL;
54}
55
56static psiconv_sheet_cell_layout psiconv_clone_cell_layout
57 (psiconv_sheet_cell_layout original)
58{
59 psiconv_sheet_cell_layout result;
60 if (!(result = malloc(sizeof(*result))))
61 goto ERROR1;
62 if (!(result->character =
63 psiconv_clone_character_layout(original->character)))
64 goto ERROR2;
65 if (!(result->paragraph =
66 psiconv_clone_paragraph_layout(original->paragraph)))
67 goto ERROR3;
68 if (!(result->numberformat = malloc(sizeof(*result->numberformat))))
69 goto ERROR4;
70 result->numberformat->code = original->numberformat->code;
71 result->numberformat->decimal = original->numberformat->decimal;
72 return result;
73ERROR4:
74 psiconv_free_paragraph_layout(result->paragraph);
75ERROR3:
76 psiconv_free_character_layout(result->character);
77ERROR2:
78 free(result);
79ERROR1:
80 return NULL;
81}
82
83static psiconv_sheet_cell_reference_t
84 psiconv_read_var_cellref (const psiconv_config config,
85 const psiconv_buffer buf, int lev,
86 psiconv_u32 off, int *length,
87 int *status)
88{
89 int len=0;
90 int res;
91 psiconv_sheet_cell_reference_t result;
92 psiconv_u32 temp;
93
94 psiconv_progress(config,lev+1,off+len,"Going to read a sheet cell reference");
95 psiconv_progress(config,lev+2,off+len,
96 "Going to read the initial byte (%02x expected)",0x00);
97 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
98 if (res)
99 goto ERROR1;
100 if (temp != 0x00) {
101 psiconv_warn(config,lev+2,off+len,
102 "Sheet cell reference initial byte unknown value (ignored)");
103 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
104 }
105 len ++;
106
107 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
108 if (res)
109 goto ERROR1;
110 if (temp & 0xffff0000) {
111 psiconv_warn(config,lev+2,off+len,
112 "Sheet cell row reference to unknown row (reset)");
113 }
114 result.row.offset = temp;
115 result.row.absolute = psiconv_bool_true;
116 len += 4;
117
118 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
119 if (res)
120 goto ERROR1;
121 if (temp & 0xffff0000) {
122 psiconv_warn(config,lev+2,off+len,
123 "Sheet cell column reference to unknown row (reset)");
124 }
125 result.column.offset = temp;
126 result.column.absolute = psiconv_bool_true;
127 len += 4;
128
129 if (length)
130 *length = len;
131
132 psiconv_progress(config,lev,off+len-1,
133 "End of sheet column reference (total length: %08x)", len);
134 return result;
135ERROR1:
136 psiconv_error(config,lev+1,off,"Reading of Sheet Column Reference failed");
137 if (length)
138 *length = 0;
139 if (status)
140 *status = res?res:-PSICONV_E_NOMEM;
141 return result;
142}
143
144static psiconv_sheet_cell_block_t
145 psiconv_read_var_cellblock (const psiconv_config config,
146 const psiconv_buffer buf, int lev,
147 psiconv_u32 off, int *length,
148 int *status)
149{
150 int len=0;
151 int res;
152 psiconv_sheet_cell_block_t result;
153 psiconv_u32 temp;
154
155 psiconv_progress(config,lev+1,off+len,"Going to read a sheet cell block reference");
156 psiconv_progress(config,lev+2,off+len,
157 "Going to read the initial byte (%02x expected)",0x00);
158 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
159 if (res)
160 goto ERROR1;
161 if (temp != 0x00) {
162 psiconv_warn(config,lev+2,off+len,
163 "Sheet cell reference initial byte unknown value (ignored)");
164 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
165 }
166 len ++;
167
168 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
169 if (res)
170 goto ERROR1;
171 if (temp & 0xffff0000) {
172 psiconv_warn(config,lev+2,off+len,
173 "Sheet block initial row reference to unknown row (reset)");
174 }
175 result.first.row.offset = temp;
176 result.first.row.absolute = psiconv_bool_true;
177 len += 4;
178
179 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
180 if (res)
181 goto ERROR1;
182 if (temp & 0xffff0000) {
183 psiconv_warn(config,lev+2,off+len,
184 "Sheet block initial column reference to unknown row (reset)");
185 }
186 result.first.column.offset = temp;
187 result.first.column.absolute = psiconv_bool_true;
188 len += 4;
189
190 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
191 if (res)
192 goto ERROR1;
193 if (temp & 0xffff0000) {
194 psiconv_warn(config,lev+2,off+len,
195 "Sheet block final row reference to unknown row (reset)");
196 }
197 result.last.row.offset = temp;
198 result.last.row.absolute = psiconv_bool_true;
199 len += 4;
200
201 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
202 if (res)
203 goto ERROR1;
204 if (temp & 0xffff0000) {
205 psiconv_warn(config,lev+2,off+len,
206 "Sheet block final column reference to unknown row (reset)");
207 }
208 result.last.column.offset = temp;
209 result.last.column.absolute = psiconv_bool_true;
210 len += 4;
211
212 if (length)
213 *length = len;
214
215 psiconv_progress(config,lev,off+len-1,
216 "End of sheet cell block reference (total length: %08x)",
217 len);
218 return result;
219ERROR1:
220 psiconv_error(config,lev+1,off,"Reading of Sheet Cell Block Reference failed");
221 if (length)
222 *length = 0;
223 if (status)
224 *status = res?res:-PSICONV_E_NOMEM;
225 return result;
226}
227
28int psiconv_parse_sheet_numberformat(const psiconv_buffer buf, int lev, 228int psiconv_parse_sheet_numberformat(const psiconv_config config,
229 const psiconv_buffer buf, int lev,
29 psiconv_u32 off, int *length, 230 psiconv_u32 off, int *length,
30 psiconv_sheet_numberformat *result) 231 psiconv_sheet_numberformat result)
31{ 232{
32 int res=0; 233 int res=0;
33 int len=0; 234 int len=0;
34 psiconv_u8 temp; 235 psiconv_u8 temp;
35 236
36 psiconv_progress(lev+1,off,"Going to read a sheet numberformat"); 237 psiconv_progress(config,lev+1,off,"Going to read a sheet numberformat");
37 if (!(*result = malloc(sizeof(**result))))
38 goto ERROR1;
39 238
40 psiconv_progress(lev+2,off+len, 239 psiconv_progress(config,lev+2,off+len,
41 "Going to read the initial byte (%02x expected)",0x02); 240 "Going to read the initial byte (%02x expected)",0x02);
42 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 241 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
43 if (res) 242 if (res)
44 goto ERROR2; 243 goto ERROR1;
45 if (temp != 0x02) { 244 if (temp != 0x02) {
46 psiconv_warn(lev+2,off+len, 245 psiconv_warn(config,lev+2,off+len,
47 "Sheet numberformat initial byte unknown value (ignored)"); 246 "Sheet numberformat initial byte unknown value (ignored)");
48 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 247 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
49 } 248 }
50 len ++; 249 len ++;
51 250
52 psiconv_progress(lev+2,off+len, "Going to read the code byte"); 251 psiconv_progress(config,lev+2,off+len, "Going to read the code byte");
53 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 252 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
54 if (res) 253 if (res)
55 goto ERROR2; 254 goto ERROR1;
56 psiconv_debug(lev+2,off+len,"Code: %02x",temp); 255 psiconv_debug(config,lev+2,off+len,"Code: %02x",temp);
57 if (temp == 0x00) 256 if (temp == 0x00)
58 (*result)->code = psiconv_numberformat_general; 257 result->code = psiconv_numberformat_general;
59 else if (temp == 0x02) 258 else if (temp == 0x02)
60 (*result)->code = psiconv_numberformat_fixeddecimal; 259 result->code = psiconv_numberformat_fixeddecimal;
61 else if (temp == 0x04) 260 else if (temp == 0x04)
62 (*result)->code = psiconv_numberformat_scientific; 261 result->code = psiconv_numberformat_scientific;
63 else if (temp == 0x06) 262 else if (temp == 0x06)
64 (*result)->code = psiconv_numberformat_currency; 263 result->code = psiconv_numberformat_currency;
65 else if (temp == 0x08) 264 else if (temp == 0x08)
66 (*result)->code = psiconv_numberformat_percent; 265 result->code = psiconv_numberformat_percent;
67 else if (temp == 0x0A) 266 else if (temp == 0x0A)
68 (*result)->code = psiconv_numberformat_triads; 267 result->code = psiconv_numberformat_triads;
69 else if (temp == 0x0C) 268 else if (temp == 0x0C)
70 (*result)->code = psiconv_numberformat_boolean; 269 result->code = psiconv_numberformat_boolean;
71 else if (temp == 0x0E) 270 else if (temp == 0x0E)
72 (*result)->code = psiconv_numberformat_text; 271 result->code = psiconv_numberformat_text;
73 else if (temp == 0x10) 272 else if (temp == 0x10)
74 (*result)->code = psiconv_numberformat_date_ddmm; 273 result->code = psiconv_numberformat_date_dmm;
75 else if (temp == 0x12) 274 else if (temp == 0x12)
76 (*result)->code = psiconv_numberformat_date_mmdd; 275 result->code = psiconv_numberformat_date_mmd;
77 else if (temp == 0x14) 276 else if (temp == 0x14)
78 (*result)->code = psiconv_numberformat_date_ddmmyy; 277 result->code = psiconv_numberformat_date_ddmmyy;
79 else if (temp == 0x16) 278 else if (temp == 0x16)
80 (*result)->code = psiconv_numberformat_date_mmddyy; 279 result->code = psiconv_numberformat_date_mmddyy;
81 else if (temp == 0x18) 280 else if (temp == 0x18)
82 (*result)->code = psiconv_numberformat_date_yymmdd; 281 result->code = psiconv_numberformat_date_yymmdd;
83 else if (temp == 0x1A) 282 else if (temp == 0x1A)
84 (*result)->code = psiconv_numberformat_date_ddmmm; 283 result->code = psiconv_numberformat_date_dmmm;
85 else if (temp == 0x1C) 284 else if (temp == 0x1C)
86 (*result)->code = psiconv_numberformat_date_ddmmmyy; 285 result->code = psiconv_numberformat_date_dmmmyy;
87 else if (temp == 0x1E) 286 else if (temp == 0x1E)
88 (*result)->code = psiconv_numberformat_date_ddmmmyyyy; 287 result->code = psiconv_numberformat_date_ddmmmyy;
89 else if (temp == 0x20) 288 else if (temp == 0x20)
90 (*result)->code = psiconv_numberformat_date_mmm; 289 result->code = psiconv_numberformat_date_mmm;
91 else if (temp == 0x22) 290 else if (temp == 0x22)
92 (*result)->code = psiconv_numberformat_date_monthname; 291 result->code = psiconv_numberformat_date_monthname;
93 else if (temp == 0x24) 292 else if (temp == 0x24)
94 (*result)->code = psiconv_numberformat_date_mmmyy; 293 result->code = psiconv_numberformat_date_mmmyy;
95 else if (temp == 0x26) 294 else if (temp == 0x26)
96 (*result)->code = psiconv_numberformat_date_monthnameyy; 295 result->code = psiconv_numberformat_date_monthnameyy;
97 else if (temp == 0x28) 296 else if (temp == 0x28)
98 (*result)->code = psiconv_numberformat_date_monthnameddyyyy; 297 result->code = psiconv_numberformat_date_monthnamedyyyy;
99 else if (temp == 0x2A) 298 else if (temp == 0x2A)
100 (*result)->code = psiconv_numberformat_datetime_ddmmyyyyhhii; 299 result->code = psiconv_numberformat_datetime_ddmmyyyyhhii;
101 else if (temp == 0x2C) 300 else if (temp == 0x2C)
102 (*result)->code = psiconv_numberformat_datetime_ddmmyyyyHHii; 301 result->code = psiconv_numberformat_datetime_ddmmyyyyHHii;
103 else if (temp == 0x2E) 302 else if (temp == 0x2E)
104 (*result)->code = psiconv_numberformat_datetime_mmddyyyyhhii; 303 result->code = psiconv_numberformat_datetime_mmddyyyyhhii;
105 else if (temp == 0x30) 304 else if (temp == 0x30)
106 (*result)->code = psiconv_numberformat_datetime_mmddyyyyHHii; 305 result->code = psiconv_numberformat_datetime_mmddyyyyHHii;
107 else if (temp == 0x32) 306 else if (temp == 0x32)
108 (*result)->code = psiconv_numberformat_datetime_yyyymmddhhii; 307 result->code = psiconv_numberformat_datetime_yyyymmddhhii;
109 else if (temp == 0x34) 308 else if (temp == 0x34)
110 (*result)->code = psiconv_numberformat_datetime_yyyymmddHHii; 309 result->code = psiconv_numberformat_datetime_yyyymmddHHii;
111 else if (temp == 0x36) 310 else if (temp == 0x36)
112 (*result)->code = psiconv_numberformat_time_hhii; 311 result->code = psiconv_numberformat_time_hhii;
113 else if (temp == 0x38) 312 else if (temp == 0x38)
114 (*result)->code = psiconv_numberformat_time_hhiiss; 313 result->code = psiconv_numberformat_time_hhiiss;
115 else if (temp == 0x3A) 314 else if (temp == 0x3A)
116 (*result)->code = psiconv_numberformat_time_HHii; 315 result->code = psiconv_numberformat_time_HHii;
117 else if (temp == 0x3C) 316 else if (temp == 0x3C)
118 (*result)->code = psiconv_numberformat_time_HHiiss; 317 result->code = psiconv_numberformat_time_HHiiss;
119 else { 318 else {
120 psiconv_warn(lev+2,off+len,"Unknown number format (assumed general)"); 319 psiconv_warn(config,lev+2,off+len,"Unknown number format (assumed general)");
121 (*result)->code = psiconv_numberformat_general; 320 result->code = psiconv_numberformat_general;
122 } 321 }
123 len ++; 322 len ++;
124 323
125 psiconv_progress(lev+2,off+len, "Going to read the number of decimals"); 324 psiconv_progress(config,lev+2,off+len, "Going to read the number of decimals");
126 (*result)->decimal = psiconv_read_u8(buf,lev+2,off+len,&res); 325 result->decimal = psiconv_read_u8(config,buf,lev+2,off+len,&res) >> 1;
127 if (res) 326 if (res)
128 goto ERROR2; 327 goto ERROR1;
129 psiconv_debug(lev+2,off+len,"Decimals: %d",(*result)->decimal); 328 psiconv_debug(config,lev+2,off+len,"Decimals: %d",result->decimal);
130 len ++; 329 len ++;
131 330
132 if (length) 331 if (length)
133 *length = len; 332 *length = len;
134 333
135 psiconv_progress(lev,off+len-1, 334 psiconv_progress(config,lev,off+len-1,
136 "End of sheet number format (total length: %08x)", len); 335 "End of sheet number format (total length: %08x)", len);
137 return 0; 336 return 0;
138 337
139ERROR2: 338ERROR1:
140 free (*result);
141ERROR1:
142 psiconv_warn(lev+1,off,"Reading of Sheet Number Format failed"); 339 psiconv_error(config,lev+1,off,"Reading of Sheet Number Format failed");
143 if (length) 340 if (length)
144 *length = 0; 341 *length = 0;
145 if (!res) 342 if (!res)
146 return -PSICONV_E_NOMEM; 343 return -PSICONV_E_NOMEM;
147 else 344 else
148 return res; 345 return res;
149} 346}
150 347
151int psiconv_parse_sheet_status_section(const psiconv_buffer buf, int lev, 348int psiconv_parse_sheet_status_section(const psiconv_config config,
349 const psiconv_buffer buf, int lev,
152 psiconv_u32 off, int *length, 350 psiconv_u32 off, int *length,
153 psiconv_sheet_status_section *result) 351 psiconv_sheet_status_section *result)
154{ 352{
155 int res=0; 353 int res=0;
156 int len=0; 354 int len=0;
157 psiconv_u32 temp; 355 psiconv_u32 temp;
158 int leng; 356 int leng;
159 357
160 psiconv_progress(lev+1,off,"Going to read the sheet status section"); 358 psiconv_progress(config,lev+1,off,"Going to read the sheet status section");
161 if (!(*result = malloc(sizeof(**result)))) 359 if (!(*result = malloc(sizeof(**result))))
162 goto ERROR1; 360 goto ERROR1;
163 361
164 psiconv_progress(lev+2,off+len, 362 psiconv_progress(config,lev+2,off+len,
165 "Going to read the initial byte (%02x expected)",0x02); 363 "Going to read the initial byte (%02x expected)",0x02);
166 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 364 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
167 if (res) 365 if (res)
168 goto ERROR2; 366 goto ERROR2;
169 if (temp != 0x02) { 367 if (temp != 0x02) {
170 psiconv_warn(lev+2,off+len, 368 psiconv_warn(config,lev+2,off+len,
171 "Sheet status section initial byte unknown value (ignored)"); 369 "Sheet status section initial byte unknown value (ignored)");
172 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 370 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
173 } 371 }
174 len ++; 372 len ++;
175 373
176 psiconv_progress(lev+2,off+len, 374 psiconv_progress(config,lev+2,off+len,
177 "Going to read the cursor row"); 375 "Going to read the cursor row");
178 (*result)->cursor_row = psiconv_read_u32(buf,lev+2,off + len,&res); 376 (*result)->cursor_row = psiconv_read_u32(config,buf,lev+2,off + len,&res);
179 if (res) 377 if (res)
180 goto ERROR2; 378 goto ERROR2;
181 psiconv_debug(lev+2,off+len,"Cursor row: %08x", 379 psiconv_debug(config,lev+2,off+len,"Cursor row: %08x",
182 (*result)->cursor_row); 380 (*result)->cursor_row);
183 len += 0x04; 381 len += 0x04;
184 382
185 psiconv_progress(lev+2,off+len, 383 psiconv_progress(config,lev+2,off+len,
186 "Going to read the cursor column"); 384 "Going to read the cursor column");
187 (*result)->cursor_column = psiconv_read_u32(buf,lev+2,off + len,&res); 385 (*result)->cursor_column = psiconv_read_u32(config,buf,lev+2,off + len,&res);
188 if (res) 386 if (res)
189 goto ERROR2; 387 goto ERROR2;
190 psiconv_debug(lev+2,off+len,"Cursor column: %08x", 388 psiconv_debug(config,lev+2,off+len,"Cursor column: %08x",
191 (*result)->cursor_column); 389 (*result)->cursor_column);
192 len += 0x04; 390 len += 0x04;
193 391
194 psiconv_progress(lev+2,off+len,"Going to read initially display graph"); 392 psiconv_progress(config,lev+2,off+len,"Going to read initially display graph");
195 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, 393 if ((res = psiconv_parse_bool(config,buf,lev+2,off+len,&leng,
196 &(*result)->show_graph))) 394 &(*result)->show_graph)))
197 goto ERROR2; 395 goto ERROR2;
198 len += leng; 396 len += leng;
199 397
200 psiconv_progress(lev+2,off+len, 398 psiconv_progress(config,lev+2,off+len,
201 "Going to read the toolbar status byte"); 399 "Going to read the toolbar status byte");
202 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 400 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
203 if (res) 401 if (res)
204 goto ERROR2; 402 goto ERROR2;
205 403
206 (*result)->show_side_sheet_toolbar = temp&0x01 ? psiconv_bool_true : 404 (*result)->show_side_sheet_toolbar = temp&0x01 ? psiconv_bool_true :
207 psiconv_bool_false; 405 psiconv_bool_false;
208 psiconv_debug(lev+2,off+len,"Show side sheet toolbar: %02x", 406 psiconv_debug(config,lev+2,off+len,"Show side sheet toolbar: %02x",
209 (*result)->show_side_sheet_toolbar); 407 (*result)->show_side_sheet_toolbar);
210 (*result)->show_top_sheet_toolbar = temp&0x02 ? psiconv_bool_true : 408 (*result)->show_top_sheet_toolbar = temp&0x02 ? psiconv_bool_true :
211 psiconv_bool_false; 409 psiconv_bool_false;
212 psiconv_debug(lev+2,off+len,"Show top sheet toolbar: %02x", 410 psiconv_debug(config,lev+2,off+len,"Show top sheet toolbar: %02x",
213 (*result)->show_top_sheet_toolbar); 411 (*result)->show_top_sheet_toolbar);
214 (*result)->show_side_graph_toolbar = temp&0x04 ? psiconv_bool_true : 412 (*result)->show_side_graph_toolbar = temp&0x04 ? psiconv_bool_true :
215 psiconv_bool_false; 413 psiconv_bool_false;
216 psiconv_debug(lev+2,off+len,"Show side graph toolbar: %02x", 414 psiconv_debug(config,lev+2,off+len,"Show side graph toolbar: %02x",
217 (*result)->show_side_graph_toolbar); 415 (*result)->show_side_graph_toolbar);
218 (*result)->show_top_graph_toolbar = temp&0x08 ? psiconv_bool_true : 416 (*result)->show_top_graph_toolbar = temp&0x08 ? psiconv_bool_true :
219 psiconv_bool_false; 417 psiconv_bool_false;
220 psiconv_debug(lev+2,off+len,"Show top graph toolbar: %02x", 418 psiconv_debug(config,lev+2,off+len,"Show top graph toolbar: %02x",
221 (*result)->show_top_graph_toolbar); 419 (*result)->show_top_graph_toolbar);
222 if (temp & 0xf0) { 420 if (temp & 0xf0) {
223 psiconv_warn(lev+2,off+len,"Sheet status section toolbar byte " 421 psiconv_warn(config,lev+2,off+len,"Sheet status section toolbar byte "
224 "flags contains unknown flags (ignored)"); 422 "flags contains unknown flags (ignored)");
225 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0); 423 psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0xf0);
226 } 424 }
227 len ++; 425 len ++;
228 426
229 psiconv_progress(lev+2,off+len, 427 psiconv_progress(config,lev+2,off+len,
230 "Going to read the scrollbar status byte"); 428 "Going to read the scrollbar status byte");
231 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 429 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
232 if (res) 430 if (res)
233 goto ERROR2; 431 goto ERROR2;
234 if ((temp & 0x03) == 0x03) { 432 if ((temp & 0x03) == 0x03) {
235 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " 433 psiconv_warn(config,lev+2,off+len,"Sheet status section scrollbar byte "
236 "flags contains unknown flags (ignored)"); 434 "flags contains unknown flags (ignored)");
237 psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x03); 435 psiconv_debug(config,lev+2,off+len,"Unknown flag: %02x",temp & 0x03);
238 } 436 }
239 (*result)->show_horizontal_scrollbar = (temp&0x03) == 1? psiconv_triple_off : 437 (*result)->show_horizontal_scrollbar = (temp&0x03) == 1? psiconv_triple_off :
240 (temp&0x03) == 2? psiconv_triple_auto: 438 (temp&0x03) == 2? psiconv_triple_auto:
241 psiconv_triple_on; 439 psiconv_triple_on;
242 psiconv_debug(lev+2,off+len,"Show horizontal scrollbar: %02x", 440 psiconv_debug(config,lev+2,off+len,"Show horizontal scrollbar: %02x",
243 (*result)->show_horizontal_scrollbar); 441 (*result)->show_horizontal_scrollbar);
244 if ((temp & 0x0c) == 0x0c) { 442 if ((temp & 0x0c) == 0x0c) {
245 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " 443 psiconv_warn(config,lev+2,off+len,"Sheet status section scrollbar byte "
246 "flags contains unknown flags (ignored)"); 444 "flags contains unknown flags (ignored)");
247 psiconv_debug(lev+2,off+len,"Unknown flag: %02x",temp & 0x0c); 445 psiconv_debug(config,lev+2,off+len,"Unknown flag: %02x",temp & 0x0c);
248 } 446 }
249 (*result)->show_vertical_scrollbar = (temp&0x0c) ==0x04? psiconv_triple_off: 447 (*result)->show_vertical_scrollbar = (temp&0x0c) ==0x04? psiconv_triple_off:
250 (temp&0x0c) ==0x08? psiconv_triple_auto: 448 (temp&0x0c) ==0x08? psiconv_triple_auto:
251 psiconv_triple_on; 449 psiconv_triple_on;
252 psiconv_debug(lev+2,off+len,"Show vertical scrollbar: %02x", 450 psiconv_debug(config,lev+2,off+len,"Show vertical scrollbar: %02x",
253 (*result)->show_vertical_scrollbar); 451 (*result)->show_vertical_scrollbar);
254 if (temp & 0xf0) { 452 if (temp & 0xf0) {
255 psiconv_warn(lev+2,off+len,"Sheet status section scrollbar byte " 453 psiconv_warn(config,lev+2,off+len,"Sheet status section scrollbar byte "
256 "flags contains unknown flags (ignored)"); 454 "flags contains unknown flags (ignored)");
257 psiconv_debug(lev+2,off+len,"Unknown flags: %02x",temp & 0xf0); 455 psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0xf0);
258 } 456 }
259 len ++; 457 len ++;
260 458
261 psiconv_progress(lev+2,off+len, 459 psiconv_progress(config,lev+2,off+len,
262 "Going to read an unknown byte (%02x expected)",0x00); 460 "Going to read an unknown byte (%02x expected)",0x00);
263 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 461 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
264 if (res) 462 if (res)
265 goto ERROR2; 463 goto ERROR2;
266 if (temp != 0x00) { 464 if (temp != 0x00) {
267 psiconv_warn(lev+2,off+len, 465 psiconv_warn(config,lev+2,off+len,
268 "Sheet status section unknown byte unknown value (ignored)"); 466 "Sheet status section unknown byte unknown value (ignored)");
269 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 467 psiconv_debug(config,lev+2,off+len,"Unknown byte: %02x",temp);
270 } 468 }
271 len ++; 469 len ++;
272 470
273 psiconv_progress(lev+2,off+len,"Going to read sheet display size"); 471 psiconv_progress(config,lev+2,off+len,"Going to read sheet display size");
274 (*result)->sheet_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); 472 (*result)->sheet_display_size = psiconv_read_u32(config,buf,lev+2,off + len,&res);
275 if (res) 473 if (res)
276 goto ERROR2; 474 goto ERROR2;
277 psiconv_debug(lev+2,off+len,"Sheet display size: %08x", 475 psiconv_debug(config,lev+2,off+len,"Sheet display size: %08x",
278 (*result)->sheet_display_size); 476 (*result)->sheet_display_size);
279 len += 0x04; 477 len += 0x04;
280 478
281 psiconv_progress(lev+2,off+len,"Going to read graph display size"); 479 psiconv_progress(config,lev+2,off+len,"Going to read graph display size");
282 (*result)->graph_display_size = psiconv_read_u32(buf,lev+2,off + len,&res); 480 (*result)->graph_display_size = psiconv_read_u32(config,buf,lev+2,off + len,&res);
283 if (res) 481 if (res)
284 goto ERROR2; 482 goto ERROR2;
285 psiconv_debug(lev+2,off+len,"Graph display size: %08x", 483 psiconv_debug(config,lev+2,off+len,"Graph display size: %08x",
286 (*result)->graph_display_size); 484 (*result)->graph_display_size);
287 len += 0x04; 485 len += 0x04;
288 486
289 if (length) 487 if (length)
290 *length = len; 488 *length = len;
291 489
292 psiconv_progress(lev,off+len-1, 490 psiconv_progress(config,lev,off+len-1,
293 "End of sheet status section (total length: %08x)", len); 491 "End of sheet status section (total length: %08x)", len);
294 return 0; 492 return 0;
295 493
296ERROR2: 494ERROR2:
297 free (*result); 495 free (*result);
298ERROR1: 496ERROR1:
299 psiconv_warn(lev+1,off,"Reading of Sheet Status Section failed"); 497 psiconv_error(config,lev+1,off,"Reading of Sheet Status Section failed");
300 if (length) 498 if (length)
301 *length = 0; 499 *length = 0;
302 if (!res) 500 if (!res)
303 return -PSICONV_E_NOMEM; 501 return -PSICONV_E_NOMEM;
304 else 502 else
305 return res; 503 return res;
306} 504}
307 505
308int psiconv_parse_sheet_workbook_section(const psiconv_buffer buf, int lev, 506int psiconv_parse_sheet_workbook_section(const psiconv_config config,
507 const psiconv_buffer buf, int lev,
309 psiconv_u32 off, int *length, 508 psiconv_u32 off, int *length,
310 psiconv_sheet_workbook_section *result) 509 psiconv_sheet_workbook_section *result)
311{ 510{
312 int res=0; 511 int res=0,with_name;
313 psiconv_u32 temp,formulas_off,worksheets_off; 512 psiconv_u32 temp,formulas_off,worksheets_off,info_off,var_off,name_off=0;
314 int len=0; 513 int len=0;
315 514
316 psiconv_progress(lev+1,off,"Going to read the sheet workbook section"); 515 psiconv_progress(config,lev+1,off,"Going to read the sheet workbook section");
317 if (!(*result = malloc(sizeof(**result)))) 516 if (!(*result = malloc(sizeof(**result))))
318 goto ERROR1; 517 goto ERROR1;
319 518
320 psiconv_progress(lev+2,off+len, 519 psiconv_progress(config,lev+2,off+len,
321 "Going to read the initial byte (%02x expected)",0x04); 520 "Going to read the initial byte (%02x or %02x expected)",
521 0x02,0x04);
322 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 522 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
323 if (res) 523 if (res)
324 goto ERROR2; 524 goto ERROR2;
325 if (temp != 0x04) { 525 if ((temp != 0x04) && temp !=0x02) {
326 psiconv_warn(lev+2,off+len, 526 psiconv_warn(config,lev+2,off+len,
327 "Sheet workbook section initial byte unknown value (ignored)"); 527 "Sheet workbook section initial byte unknown value (ignored)");
328 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 528 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
329 } 529 }
530 with_name = temp ==0x04;
330 len ++; 531 len ++;
331 532
332 psiconv_progress(lev+2,off+len, 533 psiconv_progress(config,lev+2,off+len,
333 "Going to read the offset of the 1st ??? Section"); 534 "Going to read the offset of the sheet info Section");
334 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 535 info_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
335 if (res) 536 if (res)
336 goto ERROR2; 537 goto ERROR2;
337 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 538 psiconv_debug(config,lev+2,off+len,"Offset: %04x",info_off);
338 len += 4; 539 len += 4;
339 540
340 psiconv_progress(lev+2,off+len, 541 psiconv_progress(config,lev+2,off+len,
341 "Going to read the offset of the Formulas List"); 542 "Going to read the offset of the Formulas List");
342 formulas_off = psiconv_read_u32(buf,lev+2,off+len,&res); 543 formulas_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
343 if (res) 544 if (res)
344 goto ERROR2; 545 goto ERROR2;
345 psiconv_debug(lev+2,off+len,"Offset: %04x",formulas_off); 546 psiconv_debug(config,lev+2,off+len,"Offset: %04x",formulas_off);
346 len += 4; 547 len += 4;
347 548
348 psiconv_progress(lev+2,off+len, 549 psiconv_progress(config,lev+2,off+len,
349 "Going to read the offset of the Worksheet List"); 550 "Going to read the offset of the Worksheet List");
350 worksheets_off = psiconv_read_u32(buf,lev+2,off+len,&res); 551 worksheets_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
351 if (res) 552 if (res)
352 goto ERROR2; 553 goto ERROR2;
353 psiconv_debug(lev+2,off+len,"Offset: %04x",worksheets_off); 554 psiconv_debug(config,lev+2,off+len,"Offset: %04x",worksheets_off);
354 len += 4; 555 len += 4;
355 556
356 psiconv_progress(lev+2,off+len, 557 psiconv_progress(config,lev+2,off+len,
357 "Going to read the offset of the 4th ??? Section"); 558 "Going to read the offset of the Variable List");
358 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 559 var_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
359 if (res) 560 if (res)
360 goto ERROR2; 561 goto ERROR2;
361 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 562 psiconv_debug(config,lev+2,off+len,"Offset: %04x",var_off);
362 len += 4; 563 len += 4;
363 564
565 if (with_name) {
566 psiconv_progress(config,lev+2,off+len,
567 "Going to read the offset of the Name Section");
568 name_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
569 if (res)
570 goto ERROR2;
571 psiconv_debug(config,lev+2,off+len,"Offset: %04x",name_off);
572 len += 4;
573 }
574
575
576 psiconv_progress(config,lev+2,off+len,"Going to read the info section");
577 if ((res = psiconv_parse_sheet_info_section(config,buf,lev+2,info_off,NULL,
578 &(*result)->info)))
579 goto ERROR2;
580
581 psiconv_progress(config,lev+2,off+len,"Going to read the variables list");
582 if ((res = psiconv_parse_sheet_variable_list(config,buf,lev+2,var_off,NULL,
583 &(*result)->variables)))
584 goto ERROR3;
585
364 psiconv_progress(lev+2,off+len,"Going to read the formulas list"); 586 psiconv_progress(config,lev+2,off+len,"Going to read the formulas list");
365 if ((res = psiconv_parse_sheet_formula_table(buf,lev+2,formulas_off,NULL, 587 if ((res = psiconv_parse_sheet_formula_list(config,buf,lev+2,formulas_off,NULL,
366 &(*result)->formulas))) 588 &(*result)->formulas)))
367 goto ERROR2; 589 goto ERROR4;
368 590
369 psiconv_progress(lev+2,off+len,"Going to read the worksheet list"); 591 psiconv_progress(config,lev+2,off+len,"Going to read the worksheet list");
370 if ((res = psiconv_parse_sheet_worksheet_list(buf,lev+2,worksheets_off, 592 if ((res = psiconv_parse_sheet_worksheet_list(config,buf,lev+2,worksheets_off,
371 NULL,&(*result)->worksheets))) 593 NULL,&(*result)->worksheets)))
372 goto ERROR2; 594 goto ERROR5;
373 595
596 if (with_name) {
597 psiconv_progress(config,lev+2,off+len,"Going to read the name section");
598 if ((res = psiconv_parse_sheet_name_section(config,buf,lev+2,name_off,NULL,
599 &(*result)->name)))
600 goto ERROR6;
601 } else
602 (*result)->name = NULL;
374 603
375 if (length) 604 if (length)
376 *length = len; 605 *length = len;
377 606
378 psiconv_progress(lev,off+len-1, 607 psiconv_progress(config,lev,off+len-1,
379 "End of sheet workbook section (total length: %08x)", len); 608 "End of sheet workbook section (total length: %08x)", len);
380 return 0; 609 return 0;
381 610
611ERROR6:
612 psiconv_free_sheet_worksheet_list((*result)->worksheets);
613ERROR5:
614 psiconv_free_formula_list((*result)->formulas);
615ERROR4:
616 psiconv_free_sheet_variable_list((*result)->variables);
617ERROR3:
618 psiconv_free_sheet_info_section((*result)->info);
382ERROR2: 619ERROR2:
383 free (*result); 620 free (*result);
384ERROR1: 621ERROR1:
385 psiconv_warn(lev+1,off,"Reading of Sheet Workbook Section failed"); 622 psiconv_error(config,lev+1,off,"Reading of Sheet Workbook Section failed");
386 if (length) 623 if (length)
387 *length = 0; 624 *length = 0;
388 if (!res) 625 if (!res)
389 return -PSICONV_E_NOMEM; 626 return -PSICONV_E_NOMEM;
390 else 627 else
391 return res; 628 return res;
392} 629}
393 630
394int psiconv_parse_sheet_formula_table(const psiconv_buffer buf, int lev, 631int psiconv_parse_sheet_name_section(const psiconv_config config,
632 const psiconv_buffer buf, int lev,
395 psiconv_u32 off, int *length, 633 psiconv_u32 off, int *length,
634 psiconv_sheet_name_section *result)
635{
636 int res=0;
637 psiconv_u32 temp;
638 int len=0,leng;
639
640 psiconv_progress(config,lev+1,off,"Going to read the sheet name section");
641 if (!(*result = malloc(sizeof(**result))))
642 goto ERROR1;
643
644 psiconv_progress(config,lev+2,off+len,
645 "Going to read the initial byte (%02x expected)",0x02);
646 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
647 if (res)
648 goto ERROR2;
649 if (temp != 0x02) {
650 psiconv_warn(config,lev+2,off+len,
651 "Sheet name section initial byte unknown value (ignored)");
652 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
653 }
654 len ++;
655
656 psiconv_progress(config,lev+2,off+len, "Going to read the sheet name");
657 (*result)->name = psiconv_read_string(config,buf,lev+2,off+len,&leng,&res);
658 if (res)
659 goto ERROR2;
660 len += leng;
661
662 if (length)
663 *length = len;
664
665 psiconv_progress(config,lev,off+len-1,
666 "End of sheet name section (total length: %08x)", len);
667 return 0;
668
669ERROR2:
670 free(*result);
671ERROR1:
672 psiconv_error(config,lev+1,off,"Reading of Sheet Name Section failed");
673 if (length)
674 *length = 0;
675 if (!res)
676 return -PSICONV_E_NOMEM;
677 else
678 return res;
679}
680
681int psiconv_parse_sheet_info_section(const psiconv_config config,
682 const psiconv_buffer buf, int lev,
683 psiconv_u32 off, int *length,
684 psiconv_sheet_info_section *result)
685{
686 int res=0;
687 psiconv_u32 temp;
688 int len=0,leng;
689
690 psiconv_progress(config,lev+1,off,"Going to read the sheet info section");
691 if (!(*result = malloc(sizeof(**result))))
692 goto ERROR1;
693
694 psiconv_progress(config,lev+2,off+len,
695 "Going to read the initial byte (%02x expected)",0x02);
696 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
697 if (res)
698 goto ERROR2;
699 if (temp != 0x02) {
700 psiconv_warn(config,lev+2,off+len,
701 "Sheet info section initial byte unknown value (ignored)");
702 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
703 }
704 len ++;
705
706 psiconv_progress(config,lev+2,off+len, "Going to read an unknown Xint");
707 temp = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
708 if (res)
709 goto ERROR2;
710 psiconv_debug(config,lev+2,off+len,"Value: %d\n",temp);
711 len += leng;
712
713 psiconv_progress(config,lev+2,off+len, "Going to read the flags byte");
714 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
715 if (res)
716 goto ERROR2;
717 (*result)->auto_recalc = temp & 0x01 ? psiconv_bool_true:psiconv_bool_false;
718 psiconv_debug(config,lev+2,off+len,"Auto recalculation: %02x",
719 (*result)->auto_recalc);
720 if ((temp & 0xfe) != 0x02) {
721 psiconv_warn(config,lev+2,off+len,"Sheet Info Section flags byte "
722 "contains unknown flags (ignored)");
723 psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp &0xfe);
724 }
725
726 len ++;
727
728
729 if (length)
730 *length = len;
731
732 psiconv_progress(config,lev,off+len-1,
733 "End of sheet info section (total length: %08x)", len);
734 return 0;
735
736ERROR2:
737 free(*result);
738ERROR1:
739 psiconv_error(config,lev+1,off,"Reading of Sheet Name Section failed");
740 if (length)
741 *length = 0;
742 if (!res)
743 return -PSICONV_E_NOMEM;
744 else
745 return res;
746}
747
748int psiconv_parse_sheet_formula_list(const psiconv_config config,
749 const psiconv_buffer buf, int lev,
750 psiconv_u32 off, int *length,
396 psiconv_formula_list *result) 751 psiconv_formula_list *result)
397{ 752{
398 int res=0; 753 int res=0;
399 int len=0; 754 int len=0;
400 psiconv_u32 temp; 755 psiconv_u32 temp;
401 psiconv_formula formula; 756 psiconv_formula formula;
402 psiconv_u32 listlen,i; 757 psiconv_u32 listlen,i;
403 int leng; 758 int leng;
404 759
405 psiconv_progress(lev+1,off,"Going to read the sheet formula table"); 760 psiconv_progress(config,lev+1,off,"Going to read the sheet formula list");
406 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s)))) 761 if (!(*result = psiconv_list_new(sizeof(struct psiconv_formula_s))))
407 goto ERROR1; 762 goto ERROR1;
408 763
409 psiconv_progress(lev+2,off+len, 764 psiconv_progress(config,lev+2,off+len,
410 "Going to read the initial byte (%02x expected)",0x02); 765 "Going to read the initial byte (%02x expected)",0x02);
411 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 766 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
412 if (res) 767 if (res)
413 goto ERROR2; 768 goto ERROR2;
414 if (temp != 0x02) { 769 if (temp != 0x02) {
415 psiconv_warn(lev+2,off+len, 770 psiconv_warn(config,lev+2,off+len,
416 "Sheet formula table initial byte unknown value (ignored)"); 771 "Sheet formula list initial byte unknown value (ignored)");
417 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 772 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
418 } 773 }
419 len ++; 774 len ++;
420 775
421 psiconv_progress(lev+2,off+len, 776 psiconv_progress(config,lev+2,off+len,
422 "Going to read the number of formulas"); 777 "Going to read the number of formulas");
423 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res); 778 listlen = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
424 if (res) 779 if (res)
425 goto ERROR2; 780 goto ERROR2;
426 psiconv_debug(lev+2,off+len,"Number of formulas: %d",listlen); 781 psiconv_debug(config,lev+2,off+len,"Number of formulas: %d",listlen);
427 len += leng; 782 len += leng;
428 783
429 psiconv_progress(lev+2,off+len,"Going to read all formulas"); 784 psiconv_progress(config,lev+2,off+len,"Going to read all formulas");
430 for (i = 0; i < listlen; i++) { 785 for (i = 0; i < listlen; i++) {
431 psiconv_progress(lev+3,off+len,"Going to read formula %d",i); 786 psiconv_progress(config,lev+3,off+len,"Going to read formula %d",i);
432 if ((res = psiconv_parse_formula(buf,lev+3,off+len,&leng,&formula))) 787 if ((res = psiconv_parse_formula(config,buf,lev+3,off+len,&leng,&formula)))
433 goto ERROR2; 788 goto ERROR2;
434 if ((res = psiconv_list_add(*result,formula))) 789 if ((res = psiconv_list_add(*result,formula)))
435 goto ERROR3; 790 goto ERROR3;
791 free(formula);
436 len += leng; 792 len += leng;
437 } 793 }
438 794
439 if (length) 795 if (length)
440 *length = len; 796 *length = len;
441 797
442 psiconv_progress(lev,off+len-1, 798 psiconv_progress(config,lev,off+len-1,
443 "End of sheet formula table (total length: %08x)", len); 799 "End of sheet formula list (total length: %08x)", len);
444 return 0; 800 return 0;
445 801
446ERROR3: 802ERROR3:
447 psiconv_free_formula(formula); 803 psiconv_free_formula(formula);
448ERROR2: 804ERROR2:
449 psiconv_list_free(*result); 805 psiconv_list_free(*result);
450ERROR1: 806ERROR1:
451 psiconv_warn(lev+1,off,"Reading of Sheet Formula Table failed"); 807 psiconv_error(config,lev+1,off,"Reading of Sheet Formula list failed");
452 if (length) 808 if (length)
453 *length = 0; 809 *length = 0;
454 if (!res) 810 if (!res)
455 return -PSICONV_E_NOMEM; 811 return -PSICONV_E_NOMEM;
456 else 812 else
457 return res; 813 return res;
458} 814}
459 815
460int psiconv_parse_sheet_cell(const psiconv_buffer buf, int lev, 816int psiconv_parse_sheet_cell(const psiconv_config config,
817 const psiconv_buffer buf, int lev,
461 psiconv_u32 off, int *length, 818 psiconv_u32 off, int *length,
462 psiconv_sheet_cell *result) 819 psiconv_sheet_cell *result,
820 const psiconv_sheet_cell_layout default_layout,
821 const psiconv_sheet_line_list row_default_layouts,
822 const psiconv_sheet_line_list col_default_layouts)
463{ 823{
464 int res=0; 824 int res=0;
465 int len=0; 825 int len=0;
466 psiconv_u32 temp; 826 psiconv_u32 temp;
467 psiconv_bool_t has_layout; 827 psiconv_bool_t has_layout;
468 int leng; 828 int leng;
829 char *auxstr;
469 830
470 psiconv_progress(lev+1,off,"Going to read a sheet cell structure"); 831 psiconv_progress(config,lev+1,off,"Going to read a sheet cell structure");
471 if (!(*result = malloc(sizeof(**result)))) 832 if (!(*result = malloc(sizeof(**result))))
472 goto ERROR1; 833 goto ERROR1;
473 834
474 (*result)->layout = NULL; 835 (*result)->layout = NULL;
475 (*result)->type = psiconv_cell_blank; 836 (*result)->type = psiconv_cell_blank;
476 837
477 psiconv_progress(lev+2,off+len,"Going to read the cell position"); 838 psiconv_progress(config,lev+2,off+len,"Going to read the cell position");
478 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 839 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
479 if (res) 840 if (res)
480 goto ERROR2; 841 goto ERROR2;
481 len ++; 842 len ++;
482 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 8; 843 temp += psiconv_read_u8(config,buf,lev+2,off+len,&res) << 8;
483 if (res) 844 if (res)
484 goto ERROR2; 845 goto ERROR2;
485 len ++; 846 len ++;
486 temp += psiconv_read_u8(buf,lev+2,off+len,&res) << 16; 847 temp += psiconv_read_u8(config,buf,lev+2,off+len,&res) << 16;
487 if (res) 848 if (res)
488 goto ERROR2; 849 goto ERROR2;
489 len ++; 850 len ++;
490 (*result)->column = (temp >> 2) & 0xFF; 851 (*result)->column = (temp >> 2) & 0xFF;
491 (*result)->row = (temp >> 10) & 0x3FFF; 852 (*result)->row = (temp >> 10) & 0x3FFF;
492 psiconv_debug(lev+2,off+len,"Cell position is col:%02x row:%04x", 853 psiconv_debug(config,lev+2,off+len,"Cell position is col:%02x row:%04x",
493 (*result)->column,(*result)->row); 854 (*result)->column,(*result)->row);
494 if (temp & 0x03) { 855 if (temp & 0x03) {
495 psiconv_warn(lev+2,off+len,"Unknown flags in cell position (ignored)"); 856 psiconv_warn(config,lev+2,off+len,"Unknown flags in cell position (ignored)");
496 psiconv_debug(lev+2,off+len,"Flags: %02x",temp & 0x03); 857 psiconv_debug(config,lev+2,off+len,"Flags: %02x",temp & 0x03);
497 } 858 }
498 859
499 psiconv_progress(lev+2,off+len,"Going to read the cell type"); 860 psiconv_progress(config,lev+2,off+len,"Going to read the cell type");
500 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 861 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
501 if (res) 862 if (res)
502 goto ERROR2; 863 goto ERROR2;
503 len ++; 864 len ++;
504 (*result)->type = (temp >> 5) & 0x07; 865 (*result)->type = (temp >> 5) & 0x07;
505 (*result)->calculated = (temp & 0x08)?psiconv_bool_true:psiconv_bool_false; 866 (*result)->calculated = (temp & 0x08)?psiconv_bool_true:psiconv_bool_false;
506 has_layout = (temp & 0x10)?psiconv_bool_true:psiconv_bool_false; 867 has_layout = (temp & 0x10)?psiconv_bool_true:psiconv_bool_false;
507 868
508 psiconv_progress(lev+2,off+len,"Going to read the cell value"); 869 psiconv_progress(config,lev+2,off+len,"Going to read the cell value");
509 if ((*result)->type == psiconv_cell_blank) { 870 if ((*result)->type == psiconv_cell_blank) {
510 psiconv_debug(lev+2,off+len,"Cell type is blank: no value given."); 871 psiconv_debug(config,lev+2,off+len,"Cell type is blank: no value given.");
511 } else if ((*result)->type == psiconv_cell_int) { 872 } else if ((*result)->type == psiconv_cell_int) {
512 psiconv_progress(lev+2,off+len,"Going to read an integer"); 873 psiconv_progress(config,lev+2,off+len,"Going to read an integer");
513 (*result)->data.dat_int = psiconv_read_u32(buf,lev+2,off+len,&res); 874 (*result)->data.dat_int = psiconv_read_u32(config,buf,lev+2,off+len,&res);
514 if (res) 875 if (res)
515 goto ERROR2; 876 goto ERROR2;
516 len += 4; 877 len += 4;
517 psiconv_debug(lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int); 878 psiconv_debug(config,lev+2,off+len,"Cell contents: %ld",(*result)->data.dat_int);
518 879
519 } else if ((*result)->type == psiconv_cell_bool) { 880 } else if ((*result)->type == psiconv_cell_bool) {
520 psiconv_progress(lev+2,off+len,"Going to read a boolean"); 881 psiconv_progress(config,lev+2,off+len,"Going to read a boolean");
521 if ((res = psiconv_parse_bool(buf,lev+2,off+len,&leng, 882 if ((res = psiconv_parse_bool(config,buf,lev+2,off+len,&leng,
522 &(*result)->data.dat_bool))) 883 &(*result)->data.dat_bool)))
523 goto ERROR2; 884 goto ERROR2;
524 psiconv_debug(lev+2,off+len,"Cell contents: %01x",temp); 885 psiconv_debug(config,lev+2,off+len,"Cell contents: %01x",temp);
525 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false; 886 (*result)->data.dat_bool = temp?psiconv_bool_true:psiconv_bool_false;
526 len += leng; 887 len += leng;
527 } else if ((*result)->type == psiconv_cell_error) { 888 } else if ((*result)->type == psiconv_cell_error) {
528 psiconv_progress(lev+2,off+len,"Going to read the error code"); 889 psiconv_progress(config,lev+2,off+len,"Going to read the error code");
529 temp = psiconv_read_u16(buf,lev+2,off+len,&res); 890 temp = psiconv_read_u16(config,buf,lev+2,off+len,&res);
530 if (res) 891 if (res)
531 goto ERROR2; 892 goto ERROR2;
532 if (temp == 0) 893 if (temp == 0)
533 (*result)->data.dat_error = psiconv_sheet_error_none; 894 (*result)->data.dat_error = psiconv_sheet_error_none;
534 else if (temp == 1) 895 else if (temp == 1)
544 else if (temp == 6) 905 else if (temp == 6)
545 (*result)->data.dat_error = psiconv_sheet_error_number; 906 (*result)->data.dat_error = psiconv_sheet_error_number;
546 else if (temp == 7) 907 else if (temp == 7)
547 (*result)->data.dat_error = psiconv_sheet_error_notavail; 908 (*result)->data.dat_error = psiconv_sheet_error_notavail;
548 else { 909 else {
549 psiconv_warn(lev+2,off+len,"Unknown error code (default assumed)"); 910 psiconv_warn(config,lev+2,off+len,"Unknown error code (default assumed)");
550 psiconv_debug(lev+2,off+len,"Error code: %04x",temp); 911 psiconv_debug(config,lev+2,off+len,"Error code: %04x",temp);
551 (*result)->data.dat_error = psiconv_sheet_error_none; 912 (*result)->data.dat_error = psiconv_sheet_error_none;
552 } 913 }
553 psiconv_debug(lev+2,off+len,"Cell contents: %04x", 914 psiconv_debug(config,lev+2,off+len,"Cell contents: %04x",
554 (*result)->data.dat_error); 915 (*result)->data.dat_error);
555 len += 2; 916 len += 2;
556 } else if ((*result)->type == psiconv_cell_float) { 917 } else if ((*result)->type == psiconv_cell_float) {
557 psiconv_progress(lev+2,off+len,"Going to read a float"); 918 psiconv_progress(config,lev+2,off+len,"Going to read a float");
558 (*result)->data.dat_float = 919 (*result)->data.dat_float =
559 psiconv_read_float(buf,lev+2,off+len,&leng,&res); 920 psiconv_read_float(config,buf,lev+2,off+len,&leng,&res);
560 if (res) 921 if (res)
561 goto ERROR2; 922 goto ERROR2;
562 psiconv_debug(lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float); 923 psiconv_debug(config,lev+2,off+len,"Cell contents: %f",(*result)->data.dat_float);
563 len += leng; 924 len += leng;
564 } else if ((*result)->type == psiconv_cell_string) { 925 } else if ((*result)->type == psiconv_cell_string) {
565 psiconv_progress(lev+2,off+len,"Going to read a string"); 926 psiconv_progress(config,lev+2,off+len,"Going to read a string");
566 (*result)->data.dat_string = 927 (*result)->data.dat_string =
567 psiconv_read_string(buf,lev+2,off+len,&leng,&res); 928 psiconv_read_string(config,buf,lev+2,off+len,&leng,&res);
568 if (res) 929 if (res)
569 goto ERROR2; 930 goto ERROR2;
931 if (!(auxstr = psiconv_make_printable(config,(*result)->data.dat_string)))
932 goto ERROR2;
570 psiconv_debug(lev+2,off+len,"Cell contents: `%s'", 933 psiconv_debug(config,lev+2,off+len,"Cell contents: `%s'",auxstr);
571 (*result)->data.dat_string); 934 free(auxstr);
572 len += leng; 935 len += leng;
573 } else { 936 } else {
574 psiconv_warn(lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type); 937 psiconv_error(config,lev+2,off+len,"Unknown Sheet Cell type: %02x",(*result)->type);
575 res = PSICONV_E_PARSE; 938 res = PSICONV_E_PARSE;
576 goto ERROR2; 939 goto ERROR2;
577 } 940 }
578 941
942 if (!((*result)->layout = psiconv_clone_cell_layout(
943 psiconv_get_default_layout(row_default_layouts,
944 col_default_layouts,
945 default_layout,
946 (*result)->row,
947 (*result)->column))))
948 goto ERROR2;
579 if (has_layout) { 949 if (has_layout) {
580 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len, 950 if ((res = psiconv_parse_sheet_cell_layout(config,buf,lev+2,off+len,
581 &leng,&(*result)->layout))) 951 &leng,(*result)->layout)))
582 goto ERROR2; 952 goto ERROR2;
583 len += leng; 953 len += leng;
584 } 954 }
585 955
586 if ((*result)->calculated) { 956 if ((*result)->calculated) {
587 psiconv_progress(lev+2,off+len,"Going to read the cell formula reference"); 957 psiconv_progress(config,lev+2,off+len,"Going to read the cell formula reference");
588 temp = psiconv_read_X(buf,lev+2,off+len,&leng,&res); 958 temp = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
589 if (res) 959 if (res)
590 goto ERROR2; 960 goto ERROR2;
591 psiconv_debug(lev+2,off+len,"Cell formula reference: %d",temp); 961 psiconv_debug(config,lev+2,off+len,"Cell formula reference: %d",temp);
592 len += leng; 962 len += leng;
593 (*result)->ref_formula = temp; 963 (*result)->ref_formula = temp;
594 } 964 }
595 965
596 if (length) 966 if (length)
597 *length = len; 967 *length = len;
598 968
599 psiconv_progress(lev,off+len-1, 969 psiconv_progress(config,lev,off+len-1,
600 "End of sheet cell structure (total length: %08x)", len); 970 "End of sheet cell structure (total length: %08x)", len);
601 return 0; 971 return 0;
602 972
603ERROR2: 973ERROR2:
604 psiconv_free_sheet_cell(*result); 974 psiconv_free_sheet_cell(*result);
605ERROR1: 975ERROR1:
606 psiconv_warn(lev+1,off,"Reading of Sheet Cell Structure failed"); 976 psiconv_warn(config,lev+1,off,"Reading of Sheet Cell Structure failed");
607 if (length) 977 if (length)
608 *length = 0; 978 *length = 0;
609 if (!res) 979 if (!res)
610 return -PSICONV_E_NOMEM; 980 return -PSICONV_E_NOMEM;
611 else 981 else
612 return res; 982 return res;
613} 983}
614 984
615int psiconv_parse_sheet_cell_list(const psiconv_buffer buf, int lev, 985int psiconv_parse_sheet_cell_list(const psiconv_config config,
986 const psiconv_buffer buf, int lev,
616 psiconv_u32 off, int *length, 987 psiconv_u32 off, int *length,
617 psiconv_sheet_cell_list *result) 988 psiconv_sheet_cell_list *result,
989 const psiconv_sheet_cell_layout default_layout,
990 const psiconv_sheet_line_list row_default_layouts,
991 const psiconv_sheet_line_list col_default_layouts)
618{ 992{
619 int res=0; 993 int res=0;
620 int len=0; 994 int len=0;
621 psiconv_u32 temp; 995 psiconv_u32 temp;
622 psiconv_sheet_cell cell; 996 psiconv_sheet_cell cell;
623 psiconv_u32 listlen,i; 997 psiconv_u32 listlen,i;
624 int leng; 998 int leng;
625 999
626 psiconv_progress(lev+1,off,"Going to read the sheet cell list"); 1000 psiconv_progress(config,lev+1,off,"Going to read the sheet cell list");
627 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s)))) 1001 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_cell_s))))
628 goto ERROR1; 1002 goto ERROR1;
629 1003
630 psiconv_progress(lev+2,off+len, 1004 psiconv_progress(config,lev+2,off+len,
631 "Going to read the initial byte (%02x expected)",0x02); 1005 "Going to read the initial byte (%02x expected)",0x02);
632 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1006 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
633 if (res) 1007 if (res)
634 goto ERROR2; 1008 goto ERROR2;
635 if (temp != 0x02) { 1009 if (temp != 0x02) {
636 psiconv_warn(lev+2,off+len, 1010 psiconv_warn(config,lev+2,off+len,
637 "Sheet cell list initial byte unknown value (ignored)"); 1011 "Sheet cell list initial byte unknown value (ignored)");
638 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1012 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
639 } 1013 }
640 len ++; 1014 len ++;
641 1015
642 psiconv_progress(lev+2,off+len, 1016 psiconv_progress(config,lev+2,off+len,
643 "Going to read the initial byte (%02x expected)",0x00); 1017 "Going to read the initial byte (%02x expected)",0x00);
644 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1018 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
645 if (res) 1019 if (res)
646 goto ERROR2; 1020 goto ERROR2;
647 if (temp != 0x00) { 1021 if (temp != 0x00) {
648 psiconv_warn(lev+2,off+len, 1022 psiconv_warn(config,lev+2,off+len,
649 "Sheet cell list initial byte unknown value (ignored)"); 1023 "Sheet cell list initial byte unknown value (ignored)");
650 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1024 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
651 } 1025 }
652 len ++; 1026 len ++;
653 1027
654 psiconv_progress(lev+2,off+len, 1028 psiconv_progress(config,lev+2,off+len,
655 "Going to read the number of defined cells"); 1029 "Going to read the number of defined cells");
656 listlen = psiconv_read_X(buf,lev+2,off+len,&leng,&res); 1030 listlen = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
657 if (res) 1031 if (res)
658 goto ERROR2; 1032 goto ERROR2;
659 psiconv_debug(lev+2,off+len,"Number of defined cells: %d",listlen); 1033 psiconv_debug(config,lev+2,off+len,"Number of defined cells: %d",listlen);
660 len += leng; 1034 len += leng;
661 1035
662 psiconv_progress(lev+2,off+len,"Going to read all cells"); 1036 psiconv_progress(config,lev+2,off+len,"Going to read all cells");
663 for (i = 0; i < listlen; i++) { 1037 for (i = 0; i < listlen; i++) {
664 psiconv_progress(lev+3,off+len,"Going to read cell %d",i); 1038 psiconv_progress(config,lev+3,off+len,"Going to read cell %d",i);
665 if ((res = psiconv_parse_sheet_cell(buf,lev+3,off+len,&leng,&cell))) 1039 if ((res = psiconv_parse_sheet_cell(config,buf,lev+3,off+len,&leng,&cell,
1040 default_layout,row_default_layouts,
1041 col_default_layouts)))
666 goto ERROR2; 1042 goto ERROR2;
667 if ((res = psiconv_list_add(*result,cell))) 1043 if ((res = psiconv_list_add(*result,cell)))
668 goto ERROR3; 1044 goto ERROR3;
1045 free(cell);
669 len += leng; 1046 len += leng;
670 } 1047 }
671 1048
672 if (length) 1049 if (length)
673 *length = len; 1050 *length = len;
674 1051
675 psiconv_progress(lev,off+len-1, 1052 psiconv_progress(config,lev,off+len-1,
676 "End of sheet cell list (total length: %08x)", len); 1053 "End of sheet cell list (total length: %08x)", len);
677 return 0; 1054 return 0;
678 1055
679ERROR3: 1056ERROR3:
680 psiconv_free_sheet_cell(cell); 1057 psiconv_free_sheet_cell(cell);
681ERROR2: 1058ERROR2:
682 psiconv_free_sheet_cell_list(*result); 1059 psiconv_free_sheet_cell_list(*result);
683ERROR1: 1060ERROR1:
684 psiconv_warn(lev+1,off,"Reading of Sheet Cells List failed"); 1061 psiconv_error(config,lev+1,off,"Reading of Sheet Cells List failed");
685 if (length) 1062 if (length)
686 *length = 0; 1063 *length = 0;
687 if (!res) 1064 if (!res)
688 return -PSICONV_E_NOMEM; 1065 return -PSICONV_E_NOMEM;
689 else 1066 else
690 return res; 1067 return res;
691} 1068}
692 1069
693 1070
694int psiconv_parse_sheet_worksheet_list( const psiconv_buffer buf, int lev, 1071int psiconv_parse_sheet_worksheet_list(const psiconv_config config,
1072 const psiconv_buffer buf, int lev,
695 psiconv_u32 off, int *length, 1073 psiconv_u32 off, int *length,
696 psiconv_sheet_worksheet_list *result) 1074 psiconv_sheet_worksheet_list *result)
697{ 1075{
698 psiconv_sheet_worksheet worksheet; 1076 psiconv_sheet_worksheet worksheet;
699 int res=0; 1077 int res=0;
700 int len=0; 1078 int len=0;
701 psiconv_u8 temp; 1079 psiconv_u8 temp;
702 psiconv_u32 offset; 1080 psiconv_u32 offset;
703 int leng,i,nr; 1081 int leng,i,nr;
704 1082
705 psiconv_progress(lev+1,off,"Going to read the worksheet list"); 1083 psiconv_progress(config,lev+1,off,"Going to read the worksheet list");
706 if (!(*result = psiconv_list_new(sizeof(*worksheet)))) 1084 if (!(*result = psiconv_list_new(sizeof(*worksheet))))
707 goto ERROR1; 1085 goto ERROR1;
708 1086
709 psiconv_progress(lev+2,off+len, 1087 psiconv_progress(config,lev+2,off+len,
710 "Going to read the initial bytes (%02x expected)",0x02); 1088 "Going to read the initial bytes (%02x expected)",0x02);
711 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1089 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
712 if (res) 1090 if (res)
713 goto ERROR2; 1091 goto ERROR2;
714 if (temp != 0x02) { 1092 if (temp != 0x02) {
715 psiconv_warn(lev+2,off+len, 1093 psiconv_warn(config,lev+2,off+len,
716 "Sheet worksheet list initial byte unknown value (ignored)"); 1094 "Sheet worksheet list initial byte unknown value (ignored)");
717 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1095 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
718 } 1096 }
719 len ++; 1097 len ++;
720 1098
721 psiconv_progress(lev+2,off+len,"Going to read the list length"); 1099 psiconv_progress(config,lev+2,off+len,"Going to read the list length");
722 nr = psiconv_read_X(buf,lev+2,off+len,&leng,&res); 1100 nr = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
723 if (res) 1101 if (res)
724 goto ERROR2; 1102 goto ERROR2;
725 psiconv_debug(lev+2,off+len,"Length: %02x",nr); 1103 psiconv_debug(config,lev+2,off+len,"Length: %02x",nr);
726 len += leng; 1104 len += leng;
727 1105
728 psiconv_progress(lev+2,off+len,"Going to read the list"); 1106 psiconv_progress(config,lev+2,off+len,"Going to read the list");
729 for (i=0 ; i < nr; i++) { 1107 for (i=0 ; i < nr; i++) {
730 psiconv_progress(lev+3,off+len,"Going to read element %d",i); 1108 psiconv_progress(config,lev+3,off+len,"Going to read element %d",i);
731 psiconv_progress(lev+4,off+len, 1109 psiconv_progress(config,lev+4,off+len,
732 "Going to read the initial byte (%02x expected)",0x00); 1110 "Going to read the initial byte (%02x expected)",0x00);
733 temp = psiconv_read_u8(buf,lev+4,off+len,&res); 1111 temp = psiconv_read_u8(config,buf,lev+4,off+len,&res);
734 if (res) 1112 if (res)
735 goto ERROR2; 1113 goto ERROR2;
736 if (temp != 0x00) { 1114 if (temp != 0x00) {
737 psiconv_warn(lev+4,off+len, 1115 psiconv_warn(config,lev+4,off+len,
738 "Sheet worksheet element initial byte unknown value (ignored)"); 1116 "Sheet worksheet element initial byte unknown value (ignored)");
739 psiconv_debug(lev+4,off+len,"Initial byte: %02x",temp); 1117 psiconv_debug(config,lev+4,off+len,"Initial byte: %02x",temp);
740 } 1118 }
741 len ++; 1119 len ++;
742 1120
743 psiconv_progress(lev+4,off+len,"Going to read the worksheet offset"); 1121 psiconv_progress(config,lev+4,off+len,"Going to read the worksheet offset");
744 offset = psiconv_read_u32(buf,lev+2,off+len,&res); 1122 offset = psiconv_read_u32(config,buf,lev+2,off+len,&res);
745 if (res) 1123 if (res)
746 goto ERROR2; 1124 goto ERROR2;
747 psiconv_debug(lev+4,off+len,"Offset: %08x",offset); 1125 psiconv_debug(config,lev+4,off+len,"Offset: %08x",offset);
748 len += 4; 1126 len += 4;
749 1127
750 if ((res = psiconv_parse_sheet_worksheet(buf,lev+4,offset,NULL, 1128 if ((res = psiconv_parse_sheet_worksheet(config,buf,lev+4,offset,NULL,
751 &worksheet))) 1129 &worksheet)))
752 goto ERROR2; 1130 goto ERROR2;
753 if ((res = psiconv_list_add(*result,worksheet))) 1131 if ((res = psiconv_list_add(*result,worksheet)))
754 goto ERROR3; 1132 goto ERROR3;
755 psiconv_free_sheet_worksheet(worksheet); 1133 free(worksheet);
756 } 1134 }
757 1135
758 if (length) 1136 if (length)
759 *length = len; 1137 *length = len;
760 1138
761 psiconv_progress(lev,off+len-1, 1139 psiconv_progress(config,lev,off+len-1,
762 "End of worksheet list (total length: %08x)", len); 1140 "End of worksheet list (total length: %08x)", len);
763 1141
764 return 0; 1142 return 0;
765 1143
766ERROR3: 1144ERROR3:
767 psiconv_free_sheet_worksheet(worksheet); 1145 psiconv_free_sheet_worksheet(worksheet);
768ERROR2: 1146ERROR2:
769 psiconv_free_sheet_worksheet_list(*result); 1147 psiconv_free_sheet_worksheet_list(*result);
770ERROR1: 1148ERROR1:
771 psiconv_warn(lev+1,off,"Reading of worksheet list failed"); 1149 psiconv_error(config,lev+1,off,"Reading of worksheet list failed");
772 if (length) 1150 if (length)
773 *length = 0; 1151 *length = 0;
774 if (!res) 1152 if (!res)
775 return -PSICONV_E_NOMEM; 1153 return -PSICONV_E_NOMEM;
776 else 1154 else
777 return res; 1155 return res;
778} 1156}
779 1157
780int psiconv_parse_sheet_cell_layout(const psiconv_buffer buf, int lev, 1158int psiconv_parse_sheet_cell_layout(const psiconv_config config,
1159 const psiconv_buffer buf, int lev,
781 psiconv_u32 off, int *length, 1160 psiconv_u32 off, int *length,
782 psiconv_sheet_cell_layout *result) 1161 psiconv_sheet_cell_layout result)
783 1162
784{ 1163{
785 int res=0; 1164 int res=0;
786 int len=0; 1165 int len=0;
787 int leng; 1166 int leng;
788 psiconv_u8 temp; 1167 psiconv_u8 temp;
789 1168
790 psiconv_progress(lev+1,off,"Going to read a sheet cell layout"); 1169 psiconv_progress(config,lev+1,off,"Going to read a sheet cell layout");
791 if (!(*result = malloc(sizeof(**result))))
792 goto ERROR1;
793 1170
794 (*result)->character = NULL;
795 (*result)->paragraph = NULL;
796 (*result)->numberformat = NULL;
797
798 psiconv_progress(lev+2,off+len, 1171 psiconv_progress(config,lev+2,off+len,
799 "Going to read the first byte (%02x expected)",0x02); 1172 "Going to read the first byte (%02x expected)",0x02);
800 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1173 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
801 if (res) 1174 if (res)
802 goto ERROR2; 1175 goto ERROR1;
803 if (temp != 0x02) { 1176 if (temp != 0x02) {
804 psiconv_warn(lev+2,off+len, 1177 psiconv_warn(config,lev+2,off+len,
805 "Worksheet section initial byte unknown value (ignored)"); 1178 "Worksheet section initial byte unknown value (ignored)");
806 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1179 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
807 } 1180 }
808 len ++; 1181 len ++;
809 1182
810 psiconv_progress(lev+2,off+len,"Going to read the default formats flag"); 1183 psiconv_progress(config,lev+2,off+len,"Going to read the default formats flag");
811 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1184 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
812 if (res) 1185 if (res)
813 goto ERROR2; 1186 goto ERROR1;
814 len ++; 1187 len ++;
815 1188
816 if (temp & 0x01) { 1189 if (temp & 0x01) {
817 if (!((*result)->paragraph = psiconv_basic_paragraph_layout()))
818 goto ERROR2;
819 psiconv_progress(lev+3,off+len,"Going to read the default paragraph codes"); 1190 psiconv_progress(config,lev+3,off+len,"Going to read the default paragraph codes");
820 if ((res = psiconv_parse_paragraph_layout_list(buf,lev+3,off+len,&leng, 1191 if ((res = psiconv_parse_paragraph_layout_list(config,buf,lev+3,off+len,&leng,
821 (*result)->paragraph))) 1192 result->paragraph)))
822 goto ERROR2; 1193 goto ERROR1;
823 len += leng; 1194 len += leng;
824 } 1195 }
825 1196
826 if (temp & 0x02) { 1197 if (temp & 0x02) {
827 psiconv_progress(lev+3,off+len,"Going to read the default character codes"); 1198 psiconv_progress(config,lev+3,off+len,"Going to read the default character codes");
828 if (!((*result)->character = psiconv_basic_character_layout()))
829 goto ERROR2;
830 if ((res = psiconv_parse_character_layout_list(buf,lev+3,off+len,&leng, 1199 if ((res = psiconv_parse_character_layout_list(config,buf,lev+3,off+len,&leng,
831 (*result)->character))) 1200 result->character)))
832 goto ERROR2; 1201 goto ERROR1;
833 len += leng; 1202 len += leng;
834 } 1203 }
835 1204
836 if (temp & 0x04) { 1205 if (temp & 0x04) {
837 psiconv_progress(lev+3,off+len, "Going to read the default number format"); 1206 psiconv_progress(config,lev+3,off+len, "Going to read the default number format");
838 psiconv_parse_sheet_numberformat(buf,lev+3,off+len,&leng, 1207 psiconv_parse_sheet_numberformat(config,buf,lev+3,off+len,&leng,
839 &(*result)->numberformat); 1208 result->numberformat);
840 len += leng; 1209 len += leng;
841 } 1210 }
842 1211
843 if (length) 1212 if (length)
844 *length = len; 1213 *length = len;
845 1214
846 psiconv_progress(lev,off+len-1, 1215 psiconv_progress(config,lev,off+len-1,
847 "End of sheet cell layout (total length: %08x)", len); 1216 "End of sheet cell layout (total length: %08x)", len);
848 1217
849 return 0; 1218 return 0;
850 1219
851ERROR2: 1220ERROR1:
852 psiconv_free_sheet_cell_layout(*result);
853ERROR1:
854 psiconv_warn(lev+1,off,"Reading of sheet cell layout failed"); 1221 psiconv_error(config,lev+1,off,"Reading of sheet cell layout failed");
855 if (length) 1222 if (length)
856 *length = 0; 1223 *length = 0;
857 if (!res) 1224 if (!res)
858 return -PSICONV_E_NOMEM; 1225 return -PSICONV_E_NOMEM;
859 else 1226 else
860 return res; 1227 return res;
861} 1228}
862 1229
863 1230
864int psiconv_parse_sheet_worksheet(const psiconv_buffer buf, int lev, 1231int psiconv_parse_sheet_worksheet(const psiconv_config config,
1232 const psiconv_buffer buf, int lev,
865 psiconv_u32 off, int *length, 1233 psiconv_u32 off, int *length,
866 psiconv_sheet_worksheet *result) 1234 psiconv_sheet_worksheet *result)
867{ 1235{
868 int res=0; 1236 int res=0;
869 psiconv_u32 temp,cells_off,grid_off; 1237 psiconv_u32 temp,cells_off,grid_off,rows_off,cols_off,unknown_off;
870 int len=0; 1238 int len=0;
871 int leng; 1239 int leng;
872 1240
873 psiconv_progress(lev+1,off,"Going to read the sheet worksheet section"); 1241 psiconv_progress(config,lev+1,off,"Going to read the sheet worksheet section");
874 if (!(*result = malloc(sizeof(**result)))) 1242 if (!(*result = malloc(sizeof(**result))))
875 goto ERROR1; 1243 goto ERROR1;
876 1244
877 psiconv_progress(lev+2,off+len, 1245 psiconv_progress(config,lev+2,off+len,
878 "Going to read the initial bytes (%02x expected)",0x04); 1246 "Going to read the initial bytes (%02x expected)",0x04);
879 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1247 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
880 if (res) 1248 if (res)
881 goto ERROR2; 1249 goto ERROR2;
882 if (temp != 0x04) { 1250 if (temp != 0x04) {
883 psiconv_warn(lev+2,off+len, 1251 psiconv_warn(config,lev+2,off+len,
884 "Worksheet section initial byte unknown value (ignored)"); 1252 "Worksheet section initial byte unknown value (ignored)");
885 psiconv_debug(lev+2,off+len,"Initial byte: %02x",temp); 1253 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
886 } 1254 }
887 len ++; 1255 len ++;
888 1256
889 psiconv_progress(lev+2,off+len, "Going to read the flags byte"); 1257 psiconv_progress(config,lev+2,off+len, "Going to read the flags byte");
890 temp = psiconv_read_u8(buf,lev+2,off+len,&res); 1258 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
891 if (res) 1259 if (res)
892 goto ERROR2; 1260 goto ERROR2;
893 psiconv_debug(lev+2,off+len,"Flags byte: %02x",temp); 1261 psiconv_debug(config,lev+2,off+len,"Flags byte: %02x",temp);
894 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false; 1262 (*result)->show_zeros = (temp & 0x01)?psiconv_bool_true:psiconv_bool_false;
895 if (temp & 0xfe) { 1263 if (temp & 0xfe) {
896 psiconv_warn(lev+2,off+len, 1264 psiconv_warn(config,lev+2,off+len,
897 "Worksheet section flags byte unknown bits (ignored)"); 1265 "Worksheet section flags byte unknown bits (ignored)");
898 } 1266 }
899 len ++; 1267 len ++;
900 1268
901 psiconv_progress(lev+2,off+len,"Going to read the default cell layout"); 1269 psiconv_progress(config,lev+2,off+len,"Going to read the default cell layout");
1270 if (!((*result)->default_layout = psiconv_basic_cell_layout()))
1271 goto ERROR2;
902 if ((res = psiconv_parse_sheet_cell_layout(buf,lev+2,off+len,&leng, 1272 if ((res = psiconv_parse_sheet_cell_layout(config,buf,lev+2,off+len,&leng,
903 &(*result)->default_layout))) 1273 (*result)->default_layout)))
904 goto ERROR2; 1274 goto ERROR3;
905 len += leng; 1275 len += leng;
906 1276
907 psiconv_progress(lev+2,off+len, 1277 psiconv_progress(config,lev+2,off+len,
908 "Going to read the offset of the 1st ??? Section"); 1278 "Going to read the offset of the row defaults Section");
909 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 1279 rows_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
910 if (res) 1280 if (res)
911 goto ERROR3; 1281 goto ERROR3;
912 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 1282 psiconv_debug(config,lev+2,off+len,"Offset: %04x",rows_off);
913 len += 4; 1283 len += 4;
914 1284
915 psiconv_progress(lev+2,off+len, 1285 psiconv_progress(config,lev+2,off+len,
916 "Going to read the offset of the 2nd ??? Section"); 1286 "Going to read the offset of the column defaults Section");
917 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 1287 cols_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
918 if (res) 1288 if (res)
919 goto ERROR3; 1289 goto ERROR3;
920 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 1290 psiconv_debug(config,lev+2,off+len,"Offset: %04x",cols_off);
921 len += 4; 1291 len += 4;
922 1292
923 psiconv_progress(lev+2,off+len, 1293 psiconv_progress(config,lev+2,off+len,
924 "Going to read the offset of the Cells List"); 1294 "Going to read the offset of the Cells List");
925 cells_off = psiconv_read_u32(buf,lev+2,off+len,&res); 1295 cells_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
926 if (res) 1296 if (res)
927 goto ERROR3; 1297 goto ERROR3;
928 psiconv_debug(lev+2,off+len,"Offset: %04x",cells_off); 1298 psiconv_debug(config,lev+2,off+len,"Offset: %04x",cells_off);
929 len += 4; 1299 len += 4;
930 1300
931 psiconv_progress(lev+2,off+len, 1301 psiconv_progress(config,lev+2,off+len,
932 "Going to read the offset of the Grid Section"); 1302 "Going to read the offset of the Grid Section");
933 grid_off = psiconv_read_u32(buf,lev+2,off+len,&res); 1303 grid_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
934 if (res) 1304 if (res)
935 goto ERROR3; 1305 goto ERROR3;
936 psiconv_debug(lev+2,off+len,"Offset: %04x",grid_off); 1306 psiconv_debug(config,lev+2,off+len,"Offset: %04x",grid_off);
937 len += 4; 1307 len += 4;
938 1308
939 psiconv_progress(lev+2,off+len, 1309 psiconv_progress(config,lev+2,off+len,
940 "Going to read the offset of the 3rd ??? Section"); 1310 "Going to read the offset of the 3rd ??? Section");
941 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 1311 unknown_off = psiconv_read_u32(config,buf,lev+2,off+len,&res);
942 if (res) 1312 if (res)
943 goto ERROR3; 1313 goto ERROR3;
944 psiconv_debug(lev+2,off+len,"Offset: %04x",temp); 1314 psiconv_debug(config,lev+2,off+len,"Offset: %04x",unknown_off);
945 len += 4; 1315 len += 4;
946 1316
947 psiconv_progress(lev+2,off+len,"Going to read the cells list"); 1317 psiconv_progress(config,lev+2,off+len,
948 if ((res = psiconv_parse_sheet_cell_list(buf,lev+2,cells_off,NULL, 1318 "Going to read a long of the 3rd ??? Section "
949 &(*result)->cells))) 1319 "(%08x expected)",0x00);
1320 temp = psiconv_read_u32(config,buf,lev+2,unknown_off,&res);
1321 if (res)
950 goto ERROR3; 1322 goto ERROR3;
1323 if (temp != 0x00) {
1324 psiconv_warn(config,lev+2,unknown_off,
1325 "Unknown worksheet subsection has unknown contents (ignored)");
1326 psiconv_debug(config,lev+2,unknown_off,"Offset: %04x",temp);
951 1327 }
1328 len += 4;
952 1329
953/* TODO: parse grid section */ 1330 psiconv_progress(config,lev+2,off+len,"Going to read the row defaults");
1331 if ((res = psiconv_parse_sheet_line_list(config,buf,lev+2,rows_off,NULL,
1332 &(*result)->row_default_layouts,
1333 (*result)->default_layout)))
1334 goto ERROR3;
1335
1336 psiconv_progress(config,lev+2,off+len,"Going to read the column defaults");
1337 if ((res = psiconv_parse_sheet_line_list(config,buf,lev+2,cols_off,NULL,
1338 &(*result)->col_default_layouts,
1339 (*result)->default_layout)))
1340 goto ERROR4;
1341
1342 psiconv_progress(config,lev+2,off+len,"Going to read the cells list");
1343 if ((res = psiconv_parse_sheet_cell_list(config,buf,lev+2,cells_off,NULL,
1344 &(*result)->cells,
1345 (*result)->default_layout,
1346 (*result)->row_default_layouts,
1347 (*result)->col_default_layouts)))
1348 goto ERROR5;
1349
1350
1351 psiconv_progress(config,lev+2,off+len,"Going to read the grid section");
1352 if ((res = psiconv_parse_sheet_grid_section(config,buf,lev+2,grid_off,NULL,
1353 &(*result)->grid)))
1354 goto ERROR6;
1355
1356
954 1357
955 if (length) 1358 if (length)
956 *length = len; 1359 *length = len;
957 1360
958 psiconv_progress(lev,off+len-1, 1361 psiconv_progress(config,lev,off+len-1,
959 "End of sheet worksheet section (total length: %08x)", len); 1362 "End of sheet worksheet section (total length: %08x)", len);
960 return 0; 1363 return 0;
961 1364
1365ERROR6:
1366 psiconv_free_sheet_cell_list((*result)->cells);
1367ERROR5:
1368 psiconv_free_sheet_line_list((*result)->col_default_layouts);
1369ERROR4:
1370 psiconv_free_sheet_line_list((*result)->row_default_layouts);
962ERROR3: 1371ERROR3:
963 psiconv_free_sheet_cell_layout((*result)->default_layout); 1372 psiconv_free_sheet_cell_layout((*result)->default_layout);
964ERROR2: 1373ERROR2:
965 free (*result); 1374 free (*result);
966ERROR1: 1375ERROR1:
967 psiconv_warn(lev+1,off,"Reading of Sheet Worksheet Section failed"); 1376 psiconv_error(config,lev+1,off,"Reading of Sheet Worksheet Section failed");
968 if (length) 1377 if (length)
969 *length = 0; 1378 *length = 0;
970 if (!res) 1379 if (!res)
971 return -PSICONV_E_NOMEM; 1380 return -PSICONV_E_NOMEM;
972 else 1381 else
973 return res; 1382 return res;
974} 1383}
975 1384
1385int psiconv_parse_sheet_line(const psiconv_config config,
1386 const psiconv_buffer buf, int lev,
1387 psiconv_u32 off, int *length,
1388 psiconv_sheet_line *result,
1389 const psiconv_sheet_cell_layout default_layout)
1390{
1391 int res=0;
1392 int len=0;
1393 int leng;
976 1394
1395
1396 psiconv_progress(config,lev+1,off,"Going to read a sheet line");
1397 if (!(*result = malloc(sizeof(**result))))
1398 goto ERROR1;
1399
1400 psiconv_progress(config,lev+2,off+len,"Going to read the line number");
1401 (*result)->position = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
1402 if (res)
1403 goto ERROR2;
1404 psiconv_debug(config,lev+2,off+len,"Line number: %d\n",(*result)->position);
1405 len += leng;
1406
1407 if (!((*result)->layout = psiconv_clone_cell_layout(default_layout)))
1408 goto ERROR2;
1409 if ((res = psiconv_parse_sheet_cell_layout(config,buf,lev+2,off+len,
1410 &leng,(*result)->layout)))
1411 goto ERROR3;
1412 len += leng;
1413
1414 if (length)
1415 *length = len;
1416
1417 psiconv_progress(config,lev,off+len-1,
1418 "End of the sheet line (total length: %08x)", len);
1419 return 0;
1420
1421ERROR3:
1422 psiconv_free_sheet_cell_layout((*result)->layout);
1423ERROR2:
1424 free (*result);
1425ERROR1:
1426 psiconv_error(config,lev+1,off,"Reading of the sheet line failed");
1427 if (length)
1428 *length = 0;
1429 if (!res)
1430 return -PSICONV_E_NOMEM;
1431 else
1432 return res;
1433}
1434
1435
1436int psiconv_parse_sheet_line_list(const psiconv_config config,
1437 const psiconv_buffer buf, int lev,
1438 psiconv_u32 off, int *length,
1439 psiconv_sheet_line_list *result,
1440 const psiconv_sheet_cell_layout default_layout)
1441{
1442 int res=0;
1443 int len=0;
1444 psiconv_u32 temp;
1445 psiconv_sheet_line line;
1446 psiconv_u32 listlen,i;
1447 int leng;
1448
1449 psiconv_progress(config,lev+1,off,"Going to read the sheet line list");
1450 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_line_s))))
1451 goto ERROR1;
1452
1453 psiconv_progress(config,lev+2,off+len,
1454 "Going to read the initial byte (%02x expected)",0x02);
1455 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1456 if (res)
1457 goto ERROR2;
1458 if (temp != 0x02) {
1459 psiconv_warn(config,lev+2,off+len,
1460 "Sheet line list initial byte unknown value (ignored)");
1461 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
1462 }
1463 len ++;
1464
1465 psiconv_progress(config,lev+2,off+len,
1466 "Going to read the number of defined lines");
1467 listlen = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
1468 if (res)
1469 goto ERROR2;
1470 psiconv_debug(config,lev+2,off+len,"Number of defined lines: %d",listlen);
1471 len += leng;
1472
1473 psiconv_progress(config,lev+2,off+len,"Going to read all lines");
1474 for (i = 0; i < listlen; i++) {
1475 psiconv_progress(config,lev+3,off+len,"Going to read line %d",i);
1476 if ((res = psiconv_parse_sheet_line(config,buf,lev+3,off+len,&leng,&line,
1477 default_layout)))
1478 goto ERROR2;
1479 if ((res = psiconv_list_add(*result,line)))
1480 goto ERROR3;
1481 free(line);
1482 len += leng;
1483 }
1484
1485 if (length)
1486 *length = len;
1487
1488 psiconv_progress(config,lev,off+len-1,
1489 "End of sheet line list (total length: %08x)", len);
1490 return 0;
1491
1492ERROR3:
1493 psiconv_free_sheet_line(line);
1494ERROR2:
1495 psiconv_free_sheet_line_list(*result);
1496ERROR1:
1497 psiconv_error(config,lev+1,off,"Reading of Sheet Line List failed");
1498 if (length)
1499 *length = 0;
1500 if (!res)
1501 return -PSICONV_E_NOMEM;
1502 else
1503 return res;
1504}
1505
1506int psiconv_parse_sheet_variable(const psiconv_config config,
1507 const psiconv_buffer buf, int lev,
1508 psiconv_u32 off, int *length,
1509 psiconv_sheet_variable *result)
1510{
1511 int res=0;
1512 int len=0;
1513 psiconv_u32 marker;
1514 int leng;
1515
1516 psiconv_progress(config,lev+1,off,"Going to read a sheet variable");
1517 if (!(*result = malloc(sizeof(**result))))
1518 goto ERROR1;
1519
1520 psiconv_progress(config,lev+2,off+len, "Going to read the variable name");
1521 (*result)->name = psiconv_read_string(config,buf,lev+2,off+len,&leng,&res);
1522 if (res)
1523 goto ERROR2;
1524 len += leng;
1525
1526 psiconv_progress(config,lev+2,off+len,"Going to read the type marker");
1527 marker = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1528 if (res)
1529 goto ERROR3;
1530 psiconv_debug(config,lev+2,off+len,"Marker: %02x",marker);
1531 len ++;
1532
1533 if (marker == 0x00) {
1534 (*result)->type = psiconv_var_int;
1535 psiconv_progress(config,lev+2,off+len,"Going to read a signed integer");
1536 (*result)->data.dat_int = psiconv_read_sint(config,buf,lev+2,off+len,&leng,&res);
1537 if (res)
1538 goto ERROR3;
1539 psiconv_debug(config,lev+2,off+len,"Value: %d",(*result)->data.dat_int);
1540 len += leng;
1541 } else if (marker == 0x01) {
1542 (*result)->type = psiconv_var_float;
1543 psiconv_progress(config,lev+2,off+len,"Going to read a floating point number");
1544 (*result)->data.dat_float = psiconv_read_float(config,buf,lev+2,off+len,&leng,
1545 &res);
1546 if (res)
1547 goto ERROR3;
1548 psiconv_debug(config,lev+2,off+len,"Value: %f",(*result)->data.dat_float);
1549 len += leng;
1550 } else if (marker == 0x02) {
1551 (*result)->type = psiconv_var_string;
1552 psiconv_progress(config,lev+2,off+len,"Going to read a string");
1553 (*result)->data.dat_string = psiconv_read_string(config,buf,lev+2,off+len,
1554 &leng, &res);
1555 if (res)
1556 goto ERROR3;
1557 len += leng;
1558 } else if (marker == 0x03) {
1559 (*result)->type = psiconv_var_cellref;
1560 psiconv_progress(config,lev+2,off+len,"Going to read a cell reference");
1561 (*result)->data.dat_cellref = psiconv_read_var_cellref(config,buf,lev+2,off+len,
1562 &leng, &res);
1563 if (res)
1564 goto ERROR3;
1565 len += leng;
1566 } else if (marker == 0x04) {
1567 (*result)->type = psiconv_var_cellblock;
1568 psiconv_progress(config,lev+2,off+len,"Going to read a cell block reference");
1569 (*result)->data.dat_cellblock = psiconv_read_var_cellblock(config,buf,lev+2,
1570 off+len,
1571 &leng, &res);
1572 if (res)
1573 goto ERROR3;
1574 len += leng;
1575 } else {
1576 psiconv_error(config,lev+2,off+len,"Sheet variable unknown type marker");
1577 res = -PSICONV_E_PARSE;
1578 goto ERROR3;
1579 }
1580
1581 psiconv_progress(config,lev+2,off+len,"Going to read the variable number");
1582 (*result)->number = psiconv_read_u32(config,buf,lev+2,off+len,&res);
1583 if (res)
1584 goto ERROR4;
1585 psiconv_debug(config,lev+2,off+len,"Number: %08x",(*result)->number);
1586 len += 4;
1587
1588 if (length)
1589 *length = len;
1590
1591 psiconv_progress(config,lev,off+len-1,
1592 "End of sheet variable (total length: %08x)", len);
1593 return 0;
1594
1595ERROR4:
1596 if ((*result)->type == psiconv_var_string)
1597 free((*result)->data.dat_string);
1598ERROR3:
1599 free((*result)->name);
1600ERROR2:
1601 free (*result);
1602ERROR1:
1603 psiconv_error(config,lev+1,off,"Reading of Sheet Variable failed");
1604 if (length)
1605 *length = 0;
1606 if (!res)
1607 return -PSICONV_E_NOMEM;
1608 else
1609 return res;
1610}
1611
1612
1613int psiconv_parse_sheet_variable_list(const psiconv_config config,
1614 const psiconv_buffer buf, int lev,
1615 psiconv_u32 off, int *length,
1616 psiconv_sheet_variable_list *result)
1617{
1618 int res=0;
1619 int len=0;
1620 psiconv_u32 temp;
1621 psiconv_sheet_variable variable;
1622 psiconv_u32 listlen,i;
1623 int leng;
1624
1625 psiconv_progress(config,lev+1,off,"Going to read the sheet variable list");
1626 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_variable_s))))
1627 goto ERROR1;
1628
1629 psiconv_progress(config,lev+2,off+len,
1630 "Going to read the initial byte (%02x expected)",0x02);
1631 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1632 if (res)
1633 goto ERROR2;
1634 if (temp != 0x02) {
1635 psiconv_warn(config,lev+2,off+len,
1636 "Sheet variable list initial byte unknown value (ignored)");
1637 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
1638 }
1639 len ++;
1640
1641 psiconv_progress(config,lev+2,off+len,
1642 "Going to read the number of variables");
1643 listlen = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
1644 if (res)
1645 goto ERROR2;
1646 psiconv_debug(config,lev+2,off+len,"Number of variables: %d",listlen);
1647 len += leng;
1648
1649 psiconv_progress(config,lev+2,off+len,"Going to read all variables");
1650 for (i = 0; i < listlen; i++) {
1651 psiconv_progress(config,lev+3,off+len,"Going to read variable %d",i);
1652 if ((res = psiconv_parse_sheet_variable(config,buf,lev+3,off+len,&leng,&variable)))
1653 goto ERROR2;
1654 if ((res = psiconv_list_add(*result,variable)))
1655 goto ERROR3;
1656 len += leng;
1657 }
1658
1659 if (length)
1660 *length = len;
1661
1662 psiconv_progress(config,lev,off+len-1,
1663 "End of sheet variabels list (total length: %08x)", len);
1664 return 0;
1665
1666ERROR3:
1667 psiconv_free_sheet_variable(variable);
1668ERROR2:
1669 psiconv_list_free(*result);
1670ERROR1:
1671 psiconv_error(config,lev+1,off,"Reading of Sheet Variable list failed");
1672 if (length)
1673 *length = 0;
1674 if (!res)
1675 return -PSICONV_E_NOMEM;
1676 else
1677 return res;
1678}
1679
1680int psiconv_parse_sheet_grid_section(const psiconv_config config,
1681 const psiconv_buffer buf, int lev,
1682 psiconv_u32 off, int *length,
1683 psiconv_sheet_grid_section *result)
1684{
1685 int res=0,i;
1686 int len=0,leng;
1687 psiconv_u32 temp;
1688
1689 psiconv_progress(config,lev+1,off,"Going to read the sheet grid section");
1690 if (!(*result = malloc(sizeof(**result))))
1691 goto ERROR1;
1692
1693 psiconv_progress(config,lev+2,off+len, "Going to read the first flags byte");
1694 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1695 if (res)
1696 goto ERROR2;
1697 (*result)->show_column_titles = temp&0x01?psiconv_bool_true:
1698 psiconv_bool_false;
1699 psiconv_debug(config,lev+2,off+len,
1700 "Show column titles: %s",
1701 (*result)->show_column_titles?"true":"false");
1702 (*result)->show_row_titles = temp&0x02?psiconv_bool_true:psiconv_bool_false;
1703 psiconv_debug(config,lev+2,off+len,
1704 "Show row titles: %s",
1705 (*result)->show_row_titles?"true":"false");
1706 (*result)->show_vertical_grid = temp&0x04?psiconv_bool_true:
1707 psiconv_bool_false;
1708 psiconv_debug(config,lev+2,off+len,
1709 "Show vertical grid: %s",
1710 (*result)->show_vertical_grid?"true":"false");
1711 (*result)->show_horizontal_grid = temp&0x07?psiconv_bool_true:
1712 psiconv_bool_false;
1713 psiconv_debug(config,lev+2,off+len,
1714 "Show horizontal grid: %s",
1715 (*result)->show_horizontal_grid?"true":"false");
1716 (*result)->freeze_rows = temp&0x80?psiconv_bool_true:psiconv_bool_false;
1717 psiconv_debug(config,lev+2,off+len,
1718 "Freeze rows: %s",
1719 (*result)->freeze_rows?"true":"false");
1720 if ((temp & 0x70) != 0x30) {
1721 psiconv_warn(config,lev+2,off+len,
1722 "Grid section first flag byte has unknown bits (ignored)");
1723 psiconv_debug(config,lev+2,off+len,"Bits: %02x (%02x expected)",temp&0x70,0x30);
1724 }
1725 len ++;
1726
1727 psiconv_progress(config,lev+2,off+len, "Going to read the second flags byte");
1728 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1729 if (res)
1730 goto ERROR2;
1731 (*result)->freeze_columns = temp&0x01?psiconv_bool_true:psiconv_bool_false;
1732 psiconv_debug(config,lev+2,off+len,
1733 "Freeze columns: %s", (*result)->freeze_columns?"true":"false");
1734 if ((temp & 0xfe) != 0x80) {
1735 psiconv_warn(config,lev+2,off+len,
1736 "Grid section second flag byte has unknown bits (ignored)");
1737 psiconv_debug(config,lev+2,off+len,"Bits: %02x (%02x expected)",temp&0xfe,0x80);
1738 }
1739 len ++;
1740
1741 psiconv_progress(config,lev+2,off+len,
1742 "Going to an unknown byte (%02x expected)",0x90);
1743 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1744 if (res)
1745 goto ERROR2;
1746 if (temp != 0x90) {
1747 psiconv_warn(config,lev+2,off+len,
1748 "Grid section third byte unknown value (ignored)");
1749 psiconv_debug(config,lev+2,off+len,"Value: %02x",temp);
1750 }
1751 len ++;
1752
1753 psiconv_progress(config,lev+2,off+len, "Going to read the fourth flags byte");
1754 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1755 if (res)
1756 goto ERROR2;
1757 (*result)->show_page_breaks = temp&0x04?psiconv_bool_true:psiconv_bool_false;
1758 psiconv_debug(config,lev+2,off+len,
1759 "Show page breaks: %s",
1760 (*result)->show_page_breaks?"true":"false");
1761 if ((temp & 0xfc) != 0x00) {
1762 psiconv_warn(config,lev+2,off+len,
1763 "Grid section fourth flag byte has unknown bits (ignored)");
1764 psiconv_debug(config,lev+2,off+len,"Bits: %02x (%02x expected)",temp&0xfc,0x00);
1765 }
1766 len ++;
1767
1768 psiconv_progress(config,lev+2,off+len,"Going to read the first visible row");
1769 (*result)->first_row = psiconv_read_u32(config,buf,lev+2,off+len,&res);
1770 if (res)
1771 goto ERROR2;
1772 psiconv_debug(config,lev+2,off+len,"First row: %d",(*result)->first_row);
1773 len += 4;
1774
1775 psiconv_progress(config,lev+2,off+len,"Going to read the first visible column");
1776 (*result)->first_column = psiconv_read_u32(config,buf,lev+2,off+len,&res);
1777 if (res)
1778 goto ERROR2;
1779 psiconv_debug(config,lev+2,off+len,"First column: %d",(*result)->first_column);
1780 len += 4;
1781
1782 psiconv_progress(config,lev+2,off+len,"Going to read the last visible row");
1783 (*result)->last_row = psiconv_read_u32(config,buf,lev+2,off+len,&res);
1784 if (res)
1785 goto ERROR2;
1786 psiconv_debug(config,lev+2,off+len,"Last row: %d",(*result)->last_row);
1787 len += 4;
1788
1789 psiconv_progress(config,lev+2,off+len,"Going to read the last visible column");
1790 (*result)->last_column = psiconv_read_u32(config,buf,lev+2,off+len,&res);
1791 if (res)
1792 goto ERROR2;
1793 psiconv_debug(config,lev+2,off+len,"Last column: %d",(*result)->last_column);
1794 len += 4;
1795
1796 psiconv_progress(config,lev+2,off+len,"Going to read the default row height");
1797 (*result)->default_row_height = psiconv_read_length(config,buf,lev+2,off+len,
1798 &leng,&res);
1799 if (res)
1800 goto ERROR2;
1801 psiconv_debug(config,lev+2,off+len,"Default row height: %f",
1802 (*result)->default_row_height);
1803 len += leng;
1804
1805 psiconv_progress(config,lev+2,off+len,"Going to read the row heights list");
1806 if ((res = psiconv_parse_sheet_grid_size_list(config,buf,lev+2,off+len,&leng,
1807 &(*result)->row_heights)))
1808 goto ERROR2;
1809 len += leng;
1810
1811 psiconv_progress(config,lev+2,off+len,"Going to read the default column height");
1812 (*result)->default_column_width = psiconv_read_length(config,buf,lev+2,off+len,
1813 &leng,&res);
1814 if (res)
1815 goto ERROR3;
1816 psiconv_debug(config,lev+2,off+len,"Default column width: %f",
1817 (*result)->default_column_width);
1818 len += leng;
1819
1820 psiconv_progress(config,lev+2,off+len,"Going to read the column heights list");
1821 if ((res = psiconv_parse_sheet_grid_size_list(config,buf,lev+2,off+len,&leng,
1822 &(*result)->column_heights)))
1823 goto ERROR3;
1824 len += leng;
1825
1826 psiconv_progress(config,lev+2,off+len,
1827 "Going to read an unknown word (%04x expected)",0x00);
1828 temp = psiconv_read_u16(config,buf,lev+2,off+len,&res);
1829 if (res)
1830 goto ERROR4;
1831 if (temp != 0x00) {
1832 psiconv_warn(config,lev+2,off+len,
1833 "Grid section unknown word has unknown value (ignored)");
1834 psiconv_debug(config,lev+2,off+len,"Value: %04x",temp);
1835 }
1836 len += 2;
1837
1838 psiconv_progress(config,lev+2,off+len,"Going to read the row breaks list");
1839 if ((res = psiconv_parse_sheet_grid_break_list(config,buf,lev+2,off+len,&leng,
1840 &(*result)->row_page_breaks)))
1841 goto ERROR4;
1842 len += leng;
1843
1844 psiconv_progress(config,lev+2,off+len,"Going to read the column breaks list");
1845 if ((res = psiconv_parse_sheet_grid_break_list(config,buf,lev+2,off+len,&leng,
1846 &(*result)->column_page_breaks)))
1847 goto ERROR5;
1848 len += leng;
1849
1850
1851 psiconv_progress(config,lev+2,off+len,
1852 "Going to read 22 unknown bytes (%02x expected)",0x00);
1853 for (i = 0; i < 22 ; i++) {
1854 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1855 if (res)
1856 goto ERROR6;
1857 if (temp != 0x00) {
1858 psiconv_warn(config,lev+2,off+len,
1859 "Grid section unknown byte %d has unknown value (ignored)",
1860 i);
1861 psiconv_debug(config,lev+2,off+len,"Value: %02x",temp);
1862 }
1863 len += 1;
1864 }
1865
1866 if ((*result)->freeze_rows || (*result)->freeze_columns) {
1867
1868 psiconv_progress(config,lev+2,off+len,"Going to read number of frozen rows");
1869 (*result)->frozen_rows = psiconv_read_u32(config,buf,lev+2,off+len, &res);
1870 if (res)
1871 goto ERROR6;
1872 psiconv_debug(config,lev+2,off+len,"Number of frozen rows: %d",
1873 (*result)->frozen_rows);
1874 len += leng;
1875
1876 psiconv_progress(config,lev+2,off+len,"Going to read number of frozen columns");
1877 (*result)->frozen_columns = psiconv_read_u32(config,buf,lev+2,off+len, &res);
1878 if (res)
1879 goto ERROR6;
1880 psiconv_debug(config,lev+2,off+len,"Number of frozen columns: %d",
1881 (*result)->frozen_columns);
1882 len += leng;
1883
1884 psiconv_progress(config,lev+2,off+len,"Going to read first unfrozen row");
1885 (*result)->first_unfrozen_row_displayed = psiconv_read_u32(config,buf,lev+2,
1886 off+len, &res);
1887 if (res)
1888 goto ERROR6;
1889 psiconv_debug(config,lev+2,off+len,"First row: %d",
1890 (*result)->first_unfrozen_row_displayed);
1891 len += leng;
1892
1893 psiconv_progress(config,lev+2,off+len,"Going to read first unfrozen column");
1894 (*result)->first_unfrozen_column_displayed = psiconv_read_u32(config,buf,lev+2,
1895 off+len,&res);
1896 if (res)
1897 goto ERROR6;
1898 psiconv_debug(config,lev+2,off+len,"First column: %d",
1899 (*result)->first_unfrozen_column_displayed);
1900 len += leng;
1901 } else
1902 (*result)->frozen_rows = (*result)->frozen_columns =
1903 (*result)->first_unfrozen_row_displayed =
1904 (*result)->first_unfrozen_column_displayed = 0;
1905
1906 psiconv_progress(config,lev+2,off+len,
1907 "Going to read 3 unknown bytes (%02x expected)",0xff);
1908 for (i = 0; i < 3 ; i++) {
1909 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
1910 if (res)
1911 goto ERROR6;
1912 if (temp != 0xff) {
1913 psiconv_warn(config,lev+2,off+len,
1914 "Grid section unknown byte %d has unknown value (ignored)",
1915 i);
1916 psiconv_debug(config,lev+2,off+len,"Value: %02x",temp);
1917 }
1918 len ++;
1919 }
1920
1921 if (length)
1922 *length = len;
1923
1924 psiconv_progress(config,lev,off+len-1,
1925 "End of sheet grid section (total length: %08x)", len);
1926 return 0;
1927
1928ERROR6:
1929 psiconv_free_sheet_grid_break_list((*result)->column_page_breaks);
1930ERROR5:
1931 psiconv_free_sheet_grid_break_list((*result)->row_page_breaks);
1932ERROR4:
1933 psiconv_free_sheet_grid_size_list((*result)->column_heights);
1934ERROR3:
1935 psiconv_free_sheet_grid_size_list((*result)->row_heights);
1936ERROR2:
1937 free(*result);
1938ERROR1:
1939 psiconv_error(config,lev+1,off,"Reading of Sheet Grid Section failed");
1940 if (length)
1941 *length = 0;
1942 if (!res)
1943 return -PSICONV_E_NOMEM;
1944 else
1945 return res;
1946}
1947
1948
1949int psiconv_parse_sheet_grid_size_list(const psiconv_config config,
1950 const psiconv_buffer buf, int lev,
1951 psiconv_u32 off, int *length,
1952 psiconv_sheet_grid_size_list *result)
1953{
1954 int res=0;
1955 int len=0,i;
1956 int leng,listlen;
1957 psiconv_sheet_grid_size size;
1958
1959 psiconv_progress(config,lev+1,off,"Going to read a sheet grid size list");
1960 if (!(*result = psiconv_list_new(sizeof(struct psiconv_sheet_grid_size_s))))
1961 goto ERROR1;
1962
1963 psiconv_progress(config,lev+2,off+len,
1964 "Going to read the number of elements");
1965 listlen = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
1966 if (res)
1967 goto ERROR2;
1968 psiconv_debug(config,lev+2,off+len,"Number of elements: %d",listlen);
1969 len += leng;
1970
1971 psiconv_progress(config,lev+2,off+len,"Going to read all elements");
1972 for (i = 0; i < listlen; i++) {
1973 psiconv_progress(config,lev+3,off+len,"Going to read element %d",i);
1974 if ((res = psiconv_parse_sheet_grid_size(config,buf,lev+3,off+len,&leng,&size)))
1975 goto ERROR2;
1976 if ((res = psiconv_list_add(*result,size)))
1977 goto ERROR3;
1978 free(size);
1979 len += leng;
1980 }
1981
1982 if (length)
1983 *length = len;
1984
1985 psiconv_progress(config,lev,off+len-1,
1986 "End of sheet grid size list (total length: %08x)", len);
1987 return 0;
1988
1989ERROR3:
1990 psiconv_free_sheet_grid_size(size);
1991ERROR2:
1992 psiconv_list_free(*result);
1993ERROR1:
1994 psiconv_error(config,lev+1,off,"Reading of Sheet Grid Size List failed");
1995 if (length)
1996 *length = 0;
1997 if (!res)
1998 return -PSICONV_E_NOMEM;
1999 else
2000 return res;
2001}
2002
2003int psiconv_parse_sheet_grid_size(const psiconv_config config,
2004 const psiconv_buffer buf, int lev,
2005 psiconv_u32 off, int *length,
2006 psiconv_sheet_grid_size *result)
2007{
2008 int res=0;
2009 int len=0;
2010 int leng;
2011
2012 psiconv_progress(config,lev+1,off,"Going to read a sheet grid size");
2013
2014 if (!(*result = malloc(sizeof(**result))))
2015 goto ERROR1;
2016
2017 psiconv_progress(config,lev+2,off+len, "Going to read the row or column number");
2018 (*result)->line_number = psiconv_read_u32(config,buf,lev+2,off+len,&res);
2019 if (res)
2020 goto ERROR2;
2021 psiconv_debug(config,lev+2,off+len,"Line number: %d\n",(*result)->line_number);
2022 len += 4;
2023
2024 psiconv_progress(config,lev+2,off+len, "Going to read the row or column height");
2025 (*result)->size = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
2026 if (res)
2027 goto ERROR2;
2028 psiconv_debug(config,lev+2,off+len,"Size: %f\n",(*result)->size);
2029 len += leng;
2030
2031 if (length)
2032 *length = len;
2033
2034 psiconv_progress(config,lev,off+len-1,
2035 "End of sheet grid size(total length: %08x)", len);
2036 return 0;
2037
2038ERROR2:
2039 free (*result);
2040ERROR1:
2041 psiconv_error(config,lev+1,off,"Reading of Sheet Grid Size failed");
2042 if (length)
2043 *length = 0;
2044 if (!res)
2045 return -PSICONV_E_NOMEM;
2046 else
2047 return res;
2048}
2049
2050
2051int psiconv_parse_sheet_grid_break_list(const psiconv_config config,
2052 const psiconv_buffer buf, int lev,
2053 psiconv_u32 off, int *length,
2054 psiconv_sheet_grid_break_list *result)
2055{
2056 int res=0;
2057 int len=0,i;
2058 int leng,listlen;
2059 psiconv_u32 nr;
2060
2061 psiconv_progress(config,lev+1,off,"Going to read a sheet grid break list");
2062 if (!(*result = psiconv_list_new(sizeof(psiconv_u32))))
2063 goto ERROR1;
2064
2065 psiconv_progress(config,lev+2,off+len,
2066 "Going to read the number of elements");
2067 listlen = psiconv_read_X(config,buf,lev+2,off+len,&leng,&res);
2068 if (res)
2069 goto ERROR2;
2070 psiconv_debug(config,lev+2,off+len,"Number of elements: %d",listlen);
2071 len += leng;
2072
2073 psiconv_progress(config,lev+2,off+len,"Going to read all elements");
2074 for (i = 0; i < listlen; i++) {
2075 psiconv_progress(config,lev+3,off+len,"Going to read element %d",i);
2076 nr = psiconv_read_u32(config,buf,lev+3,off+len,&res);
2077 if (res)
2078 goto ERROR2;
2079 if ((res = psiconv_list_add(*result,&nr)))
2080 goto ERROR2;
2081 len += leng;
2082 }
2083
2084 if (length)
2085 *length = len;
2086
2087 psiconv_progress(config,lev,off+len-1,
2088 "End of sheet grid break list (total length: %08x)", len);
2089 return 0;
2090
2091ERROR2:
2092 psiconv_list_free(*result);
2093ERROR1:
2094 psiconv_error(config,lev+1,off,"Reading of Sheet Grid break List failed");
2095 if (length)
2096 *length = 0;
2097 if (!res)
2098 return -PSICONV_E_NOMEM;
2099 else
2100 return res;
2101}
2102

Legend:
Removed from v.111  
changed lines
  Added in v.270

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