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

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

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

Revision 89 Revision 167
22#include <stdlib.h> 22#include <stdlib.h>
23#include <string.h> 23#include <string.h>
24 24
25#include "generate_routines.h" 25#include "generate_routines.h"
26#include "error.h" 26#include "error.h"
27
28#ifdef DMALLOC
29#include <dmalloc.h>
30#endif
27 31
28static int psiconv_write_layout_section(psiconv_buffer buf, 32static int psiconv_write_layout_section(psiconv_buffer buf,
29 const psiconv_text_and_layout value, 33 const psiconv_text_and_layout value,
30 const psiconv_word_styles_section styles, 34 const psiconv_word_styles_section styles,
31 int with_styles); 35 int with_styles);
128 psiconv_character_layout character; 132 psiconv_character_layout character;
129 psiconv_paragraph_layout paragraph; 133 psiconv_paragraph_layout paragraph;
130 psiconv_u8 style; 134 psiconv_u8 style;
131 psiconv_u8 nr; 135 psiconv_u8 nr;
132 } *psiconv_paragraph_type_list; 136 } *psiconv_paragraph_type_list;
137 psiconv_u32 obj_id;
133 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */ 138 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */
134 psiconv_paragraph_type_list paragraph_type; 139 psiconv_paragraph_type_list paragraph_type;
135 struct psiconv_paragraph_type_list_s new_type; 140 struct psiconv_paragraph_type_list_s new_type;
136 psiconv_buffer buf_types,buf_elements,buf_inlines; 141 psiconv_buffer buf_types,buf_elements,buf_inlines,buf_objects;
137 psiconv_paragraph paragraph; 142 psiconv_paragraph paragraph;
138 psiconv_in_line_layout in_line; 143 psiconv_in_line_layout in_line = NULL;
139 psiconv_word_style style; 144 psiconv_word_style style;
140 psiconv_character_layout para_charlayout; 145 psiconv_character_layout para_charlayout;
141 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen; 146 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen;
142 147
143 if (!value) { 148 if (!value) {
161 } 166 }
162 167
163 if (!(buf_inlines = psiconv_buffer_new())) { 168 if (!(buf_inlines = psiconv_buffer_new())) {
164 res = -PSICONV_E_NOMEM; 169 res = -PSICONV_E_NOMEM;
165 goto ERROR4; 170 goto ERROR4;
171 }
172
173 if (!(buf_objects = psiconv_buffer_new())) {
174 res = -PSICONV_E_NOMEM;
175 goto ERROR5;
166 } 176 }
167 177
168 for (i = 0; i < psiconv_list_length(value); i++) { 178 for (i = 0; i < psiconv_list_length(value); i++) {
169 if (!(paragraph = psiconv_list_get(value,i))) { 179 if (!(paragraph = psiconv_list_get(value,i))) {
170 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 180 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
171 res = -PSICONV_E_OTHER; 181 res = -PSICONV_E_OTHER;
172 goto ERROR5; 182 goto ERROR6;
173 } 183 }
174 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) 184 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1)))
175 goto ERROR5; 185 goto ERROR6;
176 186
187 /* We need it for the next if-statement */
188 if (psiconv_list_length(paragraph->in_lines) == 1)
189 if (!(in_line = psiconv_list_get(paragraph->in_lines,1)))
190 goto ERROR6;
191
177 if (psiconv_list_length(paragraph->in_lines) > 1) { 192 if ((psiconv_list_length(paragraph->in_lines) > 1) ||
193 ((psiconv_list_length(paragraph->in_lines) == 1) &&
194 (in_line->object != NULL))) {
178 /* Inline layouts, so we generate a paragraph element and inline 195 /* Inline layouts, or an object, so we generate a paragraph element
179 elements */ 196 and inline elements */
180 if ((res = psiconv_write_u8(buf_elements,0x00))) 197 if ((res = psiconv_write_u8(buf_elements,0x00)))
181 goto ERROR5; 198 goto ERROR6;
182 if (!(style = psiconv_get_style(styles,paragraph->base_style))) { 199 if (!(style = psiconv_get_style(styles,paragraph->base_style))) {
183 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); 200 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
184 res = -PSICONV_E_GENERATE; 201 res = -PSICONV_E_GENERATE;
185 goto ERROR5; 202 goto ERROR6;
186 } 203 }
187 if ((res = psiconv_write_paragraph_layout_list(buf_elements, 204 if ((res = psiconv_write_paragraph_layout_list(buf_elements,
188 paragraph->base_paragraph, 205 paragraph->base_paragraph,
189 style->paragraph))) 206 style->paragraph)))
190 goto ERROR5; 207 goto ERROR6;
191 if (with_styles) 208 if (with_styles)
192 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style))) 209 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style)))
193 goto ERROR5; 210 goto ERROR6;
194 if ((res = psiconv_write_u32(buf_elements, 211 if ((res = psiconv_write_u32(buf_elements,
195 psiconv_list_length(paragraph->in_lines)))) 212 psiconv_list_length(paragraph->in_lines))))
196 goto ERROR5; 213 goto ERROR6;
197 214
198 /* Generate the inlines. NB: Against what are all settings relative?!? */ 215 /* Generate the inlines. NB: Against what are all settings relative?!? */
199 paralen = 0; 216 paralen = 0;
200 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { 217 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) {
201 nr_of_inlines ++; 218 nr_of_inlines ++;
202 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { 219 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) {
203 psiconv_warn(0,psiconv_buffer_length(buf), 220 psiconv_warn(0,psiconv_buffer_length(buf),
204 "Massive memory corruption"); 221 "Massive memory corruption");
205 res = -PSICONV_E_OTHER; 222 res = -PSICONV_E_OTHER;
206 goto ERROR5; 223 goto ERROR6;
207 } 224 }
208 if ((res = psiconv_write_u8(buf_inlines,0x00))) 225 if ((res = psiconv_write_u8(buf_inlines,in_line->object?0x01:0x00)))
209 goto ERROR5; 226 goto ERROR6;
210 thislen = in_line->length; 227 thislen = in_line->length;
211 paralen += thislen; 228 paralen += thislen;
212 /* If this is the last in_line, we need to make sure that the 229 /* If this is the last in_line, we need to make sure that the
213 complete length of all inlines equals the text length */ 230 complete length of all inlines equals the text length */
214 if (j == psiconv_list_length(paragraph->in_lines)-1) { 231 if (j == psiconv_list_length(paragraph->in_lines)-1) {
215 if (paralen > strlen(paragraph->text)+1) { 232 if (paralen > strlen(paragraph->text)+1) {
216 res = -PSICONV_E_GENERATE; 233 res = -PSICONV_E_GENERATE;
217 goto ERROR5; 234 goto ERROR6;
218 } 235 }
219 thislen += strlen(paragraph->text)+1-paralen; 236 thislen += strlen(paragraph->text)+1-paralen;
220 } 237 }
221 if ((res = psiconv_write_u32(buf_inlines,thislen))) 238 if ((res = psiconv_write_u32(buf_inlines,thislen)))
222 goto ERROR5; 239 goto ERROR6;
223 if ((res = psiconv_write_character_layout_list(buf_inlines, 240 if ((res = psiconv_write_character_layout_list(buf_inlines,
224 in_line->layout, 241 in_line->layout,
225 style->character))) 242 style->character)))
226 goto ERROR5; 243 goto ERROR6;
244 if (in_line->object) {
245 if ((res = psiconv_write_u32(buf_inlines,PSICONV_ID_OBJECT)))
246 goto ERROR6;
247 obj_id = psiconv_buffer_unique_id();
248 if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id)))
249 goto ERROR6;
250 if ((res = psiconv_buffer_add_target(buf_objects,obj_id)))
251 goto ERROR6;
252 if ((res = psiconv_write_embedded_object_section(buf_objects,
253 in_line->object)))
254 goto ERROR6;
255 if ((res = psiconv_write_length(buf_inlines,in_line->object_width)))
256 goto ERROR6;
257 if ((res = psiconv_write_length(buf_inlines,in_line->object_height)))
258 goto ERROR6;
259 }
227 } 260 }
228 } else { 261 } else {
229 /* No inline layouts (or only 1), so we generate a paragraph type list */ 262 /* No inline layouts (or only 1), so we generate a paragraph type list */
230 para_type = 0; 263 para_type = 0;
231 /* Set para_charlayout to the correct character-level layout */ 264 /* Set para_charlayout to the correct character-level layout */
234 else { 267 else {
235 if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { 268 if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) {
236 psiconv_warn(0,psiconv_buffer_length(buf), 269 psiconv_warn(0,psiconv_buffer_length(buf),
237 "Massive memory corruption"); 270 "Massive memory corruption");
238 res = -PSICONV_E_OTHER; 271 res = -PSICONV_E_OTHER;
239 goto ERROR5; 272 goto ERROR6;
240 } 273 }
241 para_charlayout = in_line->layout; 274 para_charlayout = in_line->layout;
242 } 275 }
243 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { 276 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) {
244 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { 277 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) {
245 psiconv_warn(0,psiconv_buffer_length(buf), 278 psiconv_warn(0,psiconv_buffer_length(buf),
246 "Massive memory corruption"); 279 "Massive memory corruption");
247 res = -PSICONV_E_OTHER; 280 res = -PSICONV_E_OTHER;
248 goto ERROR5; 281 goto ERROR6;
249 } 282 }
250 if ((paragraph->base_style == paragraph_type->style) && 283 if ((paragraph->base_style == paragraph_type->style) &&
251 !psiconv_compare_character_layout(para_charlayout, 284 !psiconv_compare_character_layout(para_charlayout,
252 paragraph_type->character) && 285 paragraph_type->character) &&
253 !psiconv_compare_paragraph_layout(paragraph->base_paragraph, 286 !psiconv_compare_paragraph_layout(paragraph->base_paragraph,
263 new_type.paragraph = paragraph->base_paragraph; 296 new_type.paragraph = paragraph->base_paragraph;
264 new_type.character = para_charlayout; 297 new_type.character = para_charlayout;
265 new_type.style = paragraph->base_style; 298 new_type.style = paragraph->base_style;
266 paragraph_type = &new_type; 299 paragraph_type = &new_type;
267 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) 300 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type)))
268 goto ERROR5; 301 goto ERROR6;
269 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) 302 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr)))
270 goto ERROR5; 303 goto ERROR6;
271 if (!(style = psiconv_get_style(styles,paragraph_type->style))) { 304 if (!(style = psiconv_get_style(styles,paragraph_type->style))) {
272 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); 305 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
273 res = -PSICONV_E_GENERATE; 306 res = -PSICONV_E_GENERATE;
274 goto ERROR5; 307 goto ERROR6;
275 } 308 }
276 if ((res = psiconv_write_paragraph_layout_list(buf_types, 309 if ((res = psiconv_write_paragraph_layout_list(buf_types,
277 paragraph_type->paragraph,style->paragraph))) 310 paragraph_type->paragraph,style->paragraph)))
278 goto ERROR5; 311 goto ERROR6;
279 if (with_styles) 312 if (with_styles)
280 if ((res = psiconv_write_u8(buf_types,paragraph_type->style))) 313 if ((res = psiconv_write_u8(buf_types,paragraph_type->style)))
281 goto ERROR5; 314 goto ERROR6;
282 if ((res = psiconv_write_character_layout_list(buf_types, 315 if ((res = psiconv_write_character_layout_list(buf_types,
283 paragraph_type->character,style->character))) 316 paragraph_type->character,style->character)))
284 goto ERROR5; 317 goto ERROR6;
285 } 318 }
286 if ((res = psiconv_write_u8(buf_elements,para_type))) 319 if ((res = psiconv_write_u8(buf_elements,para_type)))
287 goto ERROR5; 320 goto ERROR6;
288 } 321 }
289 } 322 }
290 323
291 /* HACK: special case: no paragraphs at all. We need to improvize. */ 324 /* HACK: special case: no paragraphs at all. We need to improvize. */
292 if (!psiconv_list_length(value)) { 325 if (!psiconv_list_length(value)) {
293 if ((res = psiconv_write_u32(buf_types,1))) 326 if ((res = psiconv_write_u32(buf_types,1)))
294 goto ERROR5; 327 goto ERROR6;
295 if ((res = psiconv_write_u32(buf_types,0))) 328 if ((res = psiconv_write_u32(buf_types,0)))
296 goto ERROR5; 329 goto ERROR6;
297 if (with_styles) 330 if (with_styles)
298 if ((res = psiconv_write_u8(buf_types,0))) 331 if ((res = psiconv_write_u8(buf_types,0)))
299 goto ERROR5; 332 goto ERROR6;
300 if ((res = psiconv_write_u32(buf_types,0))) 333 if ((res = psiconv_write_u32(buf_types,0)))
301 goto ERROR5; 334 goto ERROR6;
302 335
303 if ((res = psiconv_write_u32(buf_elements,1))) 336 if ((res = psiconv_write_u32(buf_elements,1)))
304 goto ERROR5; 337 goto ERROR6;
305 if ((res = psiconv_write_u8(buf_elements,1))) 338 if ((res = psiconv_write_u8(buf_elements,1)))
306 goto ERROR5; 339 goto ERROR6;
307 pel_length = 1; 340 pel_length = 1;
308 ptl_length = 1; 341 ptl_length = 1;
309 } else { 342 } else {
310 pel_length = psiconv_list_length(value); 343 pel_length = psiconv_list_length(value);
311 ptl_length = psiconv_list_length(paragraph_type_list); 344 ptl_length = psiconv_list_length(paragraph_type_list);
312 } 345 }
313 346
314 /* Now append everything */ 347 /* Now append everything */
315 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) 348 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000)))
316 goto ERROR5; 349 goto ERROR6;
317 if ((res = psiconv_write_u8(buf, ptl_length))) 350 if ((res = psiconv_write_u8(buf, ptl_length)))
318 goto ERROR5; 351 goto ERROR6;
319 if ((res = psiconv_buffer_concat(buf,buf_types))) 352 if ((res = psiconv_buffer_concat(buf,buf_types)))
320 goto ERROR5; 353 goto ERROR6;
321 if ((res = psiconv_write_u32(buf,pel_length))) 354 if ((res = psiconv_write_u32(buf,pel_length)))
322 goto ERROR5; 355 goto ERROR6;
323 if ((res = psiconv_buffer_concat(buf,buf_elements))) 356 if ((res = psiconv_buffer_concat(buf,buf_elements)))
324 goto ERROR5; 357 goto ERROR6;
325 if ((res = psiconv_write_u32(buf,nr_of_inlines))) 358 if ((res = psiconv_write_u32(buf,nr_of_inlines)))
326 goto ERROR5; 359 goto ERROR6;
327 res = psiconv_buffer_concat(buf,buf_inlines); 360 if ((res = psiconv_buffer_concat(buf,buf_inlines)))
361 goto ERROR6;
362 if ((res = psiconv_buffer_concat(buf,buf_objects)))
363 goto ERROR6;
328 364
365ERROR6:
366 psiconv_buffer_free(buf_objects);
329ERROR5: 367ERROR5:
330 psiconv_buffer_free(buf_inlines); 368 psiconv_buffer_free(buf_inlines);
331ERROR4: 369ERROR4:
332 psiconv_buffer_free(buf_elements); 370 psiconv_buffer_free(buf_elements);
333ERROR3: 371ERROR3:
388 if (!res) 426 if (!res)
389 return -PSICONV_E_NOMEM; 427 return -PSICONV_E_NOMEM;
390 else 428 else
391 return res; 429 return res;
392} 430}
431
432
433int psiconv_write_embedded_object_section(psiconv_buffer buf,
434 const psiconv_embedded_object_section value)
435{
436 int res;
437 psiconv_u32 display_id,icon_id,table_id;
438 psiconv_buffer extra_buf;
439
440 if (!value) {
441 psiconv_warn(0,psiconv_buffer_length(buf),"Null Object");
442 res = -PSICONV_E_GENERATE;
443 goto ERROR1;
444 }
445
446 if (!(extra_buf = psiconv_buffer_new())) {
447 res = -PSICONV_E_NOMEM;
448 goto ERROR1;
449 }
450
451 display_id = psiconv_buffer_unique_id();
452 icon_id = psiconv_buffer_unique_id();
453 table_id = psiconv_buffer_unique_id();
454 if ((res = psiconv_write_u8(buf,0x06)))
455 goto ERROR2;
456 if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_DISPLAY_SECTION)))
457 goto ERROR2;
458 if ((res = psiconv_buffer_add_reference(buf,display_id)))
459 goto ERROR2;
460 if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_ICON_SECTION)))
461 goto ERROR2;
462 if ((res = psiconv_buffer_add_reference(buf,icon_id)))
463 goto ERROR2;
464 if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION)))
465 goto ERROR2;
466 if ((res = psiconv_buffer_add_reference(buf,table_id)))
467 goto ERROR2;
468
469 if ((res = psiconv_buffer_add_target(buf,display_id)))
470 goto ERROR2;
471 if ((res = psiconv_write_object_display_section(buf,value->display)))
472 goto ERROR2;
473 if ((res = psiconv_buffer_add_target(buf,icon_id)))
474 goto ERROR2;
475 if ((res = psiconv_write_object_icon_section(buf,value->icon)))
476 goto ERROR2;
477 if ((res = psiconv_buffer_add_target(buf,table_id)))
478 goto ERROR2;
479 switch(value->object->type) {
480 case psiconv_word_file:
481 if ((res = psiconv_write_word_file(extra_buf,
482 (psiconv_word_f) value->object->file)))
483 goto ERROR2;
484 break;
485/*
486 case psiconv_sketch_file:
487 if ((res = psiconv_write_sketch_file(extra_buf,
488 (psiconv_sketch_f) value->object->file)))
489 goto ERROR2;
490 break;
491 case psiconv_sheet_file:
492 if ((res = psiconv_write_sheet_file(extra_buf,
493 (psiconv_sheet_f) value->object->file)))
494 goto ERROR2;
495 break;
496*/
497 default:
498 psiconv_warn(0,psiconv_buffer_length(buf),
499 "Unknown or unsupported object type");
500 res = -PSICONV_E_GENERATE;
501 goto ERROR2;
502 }
503
504 if ((res = psiconv_buffer_resolve(extra_buf)))
505 goto ERROR2;
506 if ((res = psiconv_buffer_concat(buf,extra_buf)))
507 goto ERROR2;
508
509 return 0;
510
511ERROR2:
512 psiconv_buffer_free(extra_buf);
513ERROR1:
514 return res;
515}
516
517
518int psiconv_write_object_display_section(psiconv_buffer buf,
519 const psiconv_object_display_section value)
520{
521 int res;
522
523 if (!value) {
524 psiconv_warn(0,psiconv_buffer_length(buf),"Null Object Display Section");
525 res = -PSICONV_E_GENERATE;
526 goto ERROR1;
527 }
528
529 if ((res = psiconv_write_u8(buf,value->show_icon?0x00:0x01)))
530 goto ERROR1;
531 if ((res = psiconv_write_length(buf,value->width)))
532 goto ERROR1;
533 if ((res = psiconv_write_length(buf,value->height)))
534 goto ERROR1;
535 if ((res = psiconv_write_u32(buf,0x00000000)))
536 goto ERROR1;
537
538 return 0;
539
540ERROR1:
541 return res;
542}
543
544int psiconv_write_object_icon_section(psiconv_buffer buf,
545 const psiconv_object_icon_section value)
546{
547 int res;
548
549 if (!value) {
550 psiconv_warn(0,psiconv_buffer_length(buf),"Null Object Icon Section");
551 res = -PSICONV_E_GENERATE;
552 goto ERROR1;
553 }
554
555 if ((res = psiconv_write_string(buf,value->icon_name)))
556 goto ERROR1;
557 if ((res = psiconv_write_length(buf,value->icon_width)))
558 goto ERROR1;
559 if ((res = psiconv_write_length(buf,value->icon_height)))
560 goto ERROR1;
561
562 return 0;
563
564ERROR1:
565 return res;
566}

Legend:
Removed from v.89  
changed lines
  Added in v.167

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