/[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 88 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;
145 psiconv_character_layout para_charlayout;
140 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length; 146 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen;
141 147
142 if (!value) { 148 if (!value) {
143 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); 149 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section");
144 return -PSICONV_E_GENERATE; 150 return -PSICONV_E_GENERATE;
145 } 151 }
160 } 166 }
161 167
162 if (!(buf_inlines = psiconv_buffer_new())) { 168 if (!(buf_inlines = psiconv_buffer_new())) {
163 res = -PSICONV_E_NOMEM; 169 res = -PSICONV_E_NOMEM;
164 goto ERROR4; 170 goto ERROR4;
171 }
172
173 if (!(buf_objects = psiconv_buffer_new())) {
174 res = -PSICONV_E_NOMEM;
175 goto ERROR5;
165 } 176 }
166 177
167 for (i = 0; i < psiconv_list_length(value); i++) { 178 for (i = 0; i < psiconv_list_length(value); i++) {
168 if (!(paragraph = psiconv_list_get(value,i))) { 179 if (!(paragraph = psiconv_list_get(value,i))) {
169 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 180 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
170 res = -PSICONV_E_OTHER; 181 res = -PSICONV_E_OTHER;
171 goto ERROR5; 182 goto ERROR6;
172 } 183 }
173 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) 184 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1)))
174 goto ERROR5; 185 goto ERROR6;
175 186
187 /* We need it for the next if-statement */
176 if (psiconv_list_length(paragraph->in_lines)) { 188 if (psiconv_list_length(paragraph->in_lines) == 1)
189 if (!(in_line = psiconv_list_get(paragraph->in_lines,1)))
190 goto ERROR6;
191
192 if ((psiconv_list_length(paragraph->in_lines) > 1) ||
193 ((psiconv_list_length(paragraph->in_lines) == 1) &&
194 (in_line->object != NULL))) {
177 /* Inline layouts, so we generate a paragraph element and inline 195 /* Inline layouts, or an object, so we generate a paragraph element
178 elements */ 196 and inline elements */
179 if ((res = psiconv_write_u8(buf_elements,0x00))) 197 if ((res = psiconv_write_u8(buf_elements,0x00)))
180 goto ERROR5; 198 goto ERROR6;
181 if (!(style = psiconv_get_style(styles,paragraph->base_style))) { 199 if (!(style = psiconv_get_style(styles,paragraph->base_style))) {
182 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); 200 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
183 res = -PSICONV_E_GENERATE; 201 res = -PSICONV_E_GENERATE;
184 goto ERROR5; 202 goto ERROR6;
185 } 203 }
186 if ((res = psiconv_write_paragraph_layout_list(buf_elements, 204 if ((res = psiconv_write_paragraph_layout_list(buf_elements,
187 paragraph->base_paragraph, 205 paragraph->base_paragraph,
188 style->paragraph))) 206 style->paragraph)))
189 goto ERROR5; 207 goto ERROR6;
190 if (with_styles) 208 if (with_styles)
191 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style))) 209 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style)))
192 goto ERROR5; 210 goto ERROR6;
193 if ((res = psiconv_write_u32(buf_elements, 211 if ((res = psiconv_write_u32(buf_elements,
194 psiconv_list_length(paragraph->in_lines)))) 212 psiconv_list_length(paragraph->in_lines))))
195 goto ERROR5; 213 goto ERROR6;
196 214
197 /* Generate the inlines. NB: Against what are all settings relative?!? */ 215 /* Generate the inlines. NB: Against what are all settings relative?!? */
216 paralen = 0;
198 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { 217 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) {
199 nr_of_inlines ++; 218 nr_of_inlines ++;
200 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { 219 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) {
201 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 220 psiconv_warn(0,psiconv_buffer_length(buf),
221 "Massive memory corruption");
202 res = -PSICONV_E_OTHER; 222 res = -PSICONV_E_OTHER;
203 goto ERROR5; 223 goto ERROR6;
204 } 224 }
205 if ((res = psiconv_write_u8(buf_inlines,0x00))) 225 if ((res = psiconv_write_u8(buf_inlines,in_line->object?0x01:0x00)))
206 goto ERROR5; 226 goto ERROR6;
227 thislen = in_line->length;
228 paralen += thislen;
229 /* If this is the last in_line, we need to make sure that the
230 complete length of all inlines equals the text length */
231 if (j == psiconv_list_length(paragraph->in_lines)-1) {
232 if (paralen > strlen(paragraph->text)+1) {
233 res = -PSICONV_E_GENERATE;
234 goto ERROR6;
235 }
236 thislen += strlen(paragraph->text)+1-paralen;
237 }
207 if ((res = psiconv_write_u32(buf_inlines,in_line->length))) 238 if ((res = psiconv_write_u32(buf_inlines,thislen)))
208 goto ERROR5; 239 goto ERROR6;
209 if ((res = psiconv_write_character_layout_list(buf_inlines, 240 if ((res = psiconv_write_character_layout_list(buf_inlines,
210 in_line->layout, 241 in_line->layout,
211 style->character))) 242 style->character)))
212 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 }
213 } 260 }
214 } else { 261 } else {
215 /* No inline layouts, so we generate a paragraph type list */ 262 /* No inline layouts (or only 1), so we generate a paragraph type list */
216 para_type = 0; 263 para_type = 0;
264 /* Set para_charlayout to the correct character-level layout */
265 if (psiconv_list_length(paragraph->in_lines) == 0)
266 para_charlayout = paragraph->base_character;
267 else {
268 if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) {
269 psiconv_warn(0,psiconv_buffer_length(buf),
270 "Massive memory corruption");
271 res = -PSICONV_E_OTHER;
272 goto ERROR6;
273 }
274 para_charlayout = in_line->layout;
275 }
217 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { 276 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) {
218 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { 277 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) {
219 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 278 psiconv_warn(0,psiconv_buffer_length(buf),
279 "Massive memory corruption");
220 res = -PSICONV_E_OTHER; 280 res = -PSICONV_E_OTHER;
221 goto ERROR5; 281 goto ERROR6;
222 } 282 }
223 if ((paragraph->base_style == paragraph_type->style) && 283 if ((paragraph->base_style == paragraph_type->style) &&
224 !psiconv_compare_character_layout(paragraph->base_character, 284 !psiconv_compare_character_layout(para_charlayout,
225 paragraph_type->character) && 285 paragraph_type->character) &&
226 !psiconv_compare_paragraph_layout(paragraph->base_paragraph, 286 !psiconv_compare_paragraph_layout(paragraph->base_paragraph,
227 paragraph_type->paragraph)) { 287 paragraph_type->paragraph)) {
228 para_type = paragraph_type->nr; 288 para_type = paragraph_type->nr;
229 break; 289 break;
232 if (!para_type) { 292 if (!para_type) {
233 /* We need to add a new entry */ 293 /* We need to add a new entry */
234 para_type = new_type.nr = j+1; 294 para_type = new_type.nr = j+1;
235 /* No need to copy them, we won't change them anyway */ 295 /* No need to copy them, we won't change them anyway */
236 new_type.paragraph = paragraph->base_paragraph; 296 new_type.paragraph = paragraph->base_paragraph;
237 new_type.character = paragraph->base_character; 297 new_type.character = para_charlayout;
238 new_type.style = paragraph->base_style; 298 new_type.style = paragraph->base_style;
239 paragraph_type = &new_type; 299 paragraph_type = &new_type;
240 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) 300 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type)))
241 goto ERROR5; 301 goto ERROR6;
242 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) 302 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr)))
243 goto ERROR5; 303 goto ERROR6;
244 if (!(style = psiconv_get_style(styles,paragraph_type->style))) { 304 if (!(style = psiconv_get_style(styles,paragraph_type->style))) {
245 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); 305 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
246 res = -PSICONV_E_GENERATE; 306 res = -PSICONV_E_GENERATE;
247 goto ERROR5; 307 goto ERROR6;
248 } 308 }
249 if ((res = psiconv_write_paragraph_layout_list(buf_types, 309 if ((res = psiconv_write_paragraph_layout_list(buf_types,
250 paragraph_type->paragraph,style->paragraph))) 310 paragraph_type->paragraph,style->paragraph)))
251 goto ERROR5; 311 goto ERROR6;
252 if (with_styles) 312 if (with_styles)
253 if ((res = psiconv_write_u8(buf_types,paragraph_type->style))) 313 if ((res = psiconv_write_u8(buf_types,paragraph_type->style)))
254 goto ERROR5; 314 goto ERROR6;
255 if ((res = psiconv_write_character_layout_list(buf_types, 315 if ((res = psiconv_write_character_layout_list(buf_types,
256 paragraph_type->character,style->character))) 316 paragraph_type->character,style->character)))
257 goto ERROR5; 317 goto ERROR6;
258 } 318 }
259 if ((res = psiconv_write_u8(buf_elements,para_type))) 319 if ((res = psiconv_write_u8(buf_elements,para_type)))
260 goto ERROR5; 320 goto ERROR6;
261 } 321 }
262 } 322 }
263 323
264 /* HACK: special case: no paragraphs at all. We need to improvize. */ 324 /* HACK: special case: no paragraphs at all. We need to improvize. */
265 if (!psiconv_list_length(value)) { 325 if (!psiconv_list_length(value)) {
266 if ((res = psiconv_write_u32(buf_types,1))) 326 if ((res = psiconv_write_u32(buf_types,1)))
267 goto ERROR5; 327 goto ERROR6;
268 if ((res = psiconv_write_u32(buf_types,0))) 328 if ((res = psiconv_write_u32(buf_types,0)))
269 goto ERROR5; 329 goto ERROR6;
270 if (with_styles) 330 if (with_styles)
271 if ((res = psiconv_write_u8(buf_types,0))) 331 if ((res = psiconv_write_u8(buf_types,0)))
272 goto ERROR5; 332 goto ERROR6;
273 if ((res = psiconv_write_u32(buf_types,0))) 333 if ((res = psiconv_write_u32(buf_types,0)))
274 goto ERROR5; 334 goto ERROR6;
275 335
276 if ((res = psiconv_write_u32(buf_elements,1))) 336 if ((res = psiconv_write_u32(buf_elements,1)))
277 goto ERROR5; 337 goto ERROR6;
278 if ((res = psiconv_write_u8(buf_elements,1))) 338 if ((res = psiconv_write_u8(buf_elements,1)))
279 goto ERROR5; 339 goto ERROR6;
280 pel_length = 1; 340 pel_length = 1;
281 ptl_length = 1; 341 ptl_length = 1;
282 } else { 342 } else {
283 pel_length = psiconv_list_length(value); 343 pel_length = psiconv_list_length(value);
284 ptl_length = psiconv_list_length(paragraph_type_list); 344 ptl_length = psiconv_list_length(paragraph_type_list);
285 } 345 }
286 346
287 /* Now append everything */ 347 /* Now append everything */
288 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) 348 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000)))
289 goto ERROR5; 349 goto ERROR6;
290 if ((res = psiconv_write_u8(buf, ptl_length))) 350 if ((res = psiconv_write_u8(buf, ptl_length)))
291 goto ERROR5; 351 goto ERROR6;
292 if ((res = psiconv_buffer_concat(buf,buf_types))) 352 if ((res = psiconv_buffer_concat(buf,buf_types)))
293 goto ERROR5; 353 goto ERROR6;
294 if ((res = psiconv_write_u32(buf,pel_length))) 354 if ((res = psiconv_write_u32(buf,pel_length)))
295 goto ERROR5; 355 goto ERROR6;
296 if ((res = psiconv_buffer_concat(buf,buf_elements))) 356 if ((res = psiconv_buffer_concat(buf,buf_elements)))
297 goto ERROR5; 357 goto ERROR6;
298 if ((res = psiconv_write_u32(buf,nr_of_inlines))) 358 if ((res = psiconv_write_u32(buf,nr_of_inlines)))
299 goto ERROR5; 359 goto ERROR6;
300 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;
301 364
365ERROR6:
366 psiconv_buffer_free(buf_objects);
302ERROR5: 367ERROR5:
303 psiconv_buffer_free(buf_inlines); 368 psiconv_buffer_free(buf_inlines);
304ERROR4: 369ERROR4:
305 psiconv_buffer_free(buf_elements); 370 psiconv_buffer_free(buf_elements);
306ERROR3: 371ERROR3:
361 if (!res) 426 if (!res)
362 return -PSICONV_E_NOMEM; 427 return -PSICONV_E_NOMEM;
363 else 428 else
364 return res; 429 return res;
365} 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.88  
changed lines
  Added in v.167

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