/[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 80 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);
90 if (!value) { 94 if (!value) {
91 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); 95 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section");
92 return -PSICONV_E_GENERATE; 96 return -PSICONV_E_GENERATE;
93 } 97 }
94 98
99 if (psiconv_list_length(value)) {
95 if (!(extra_buf = psiconv_buffer_new())) 100 if (!(extra_buf = psiconv_buffer_new()))
96 return -PSICONV_E_NOMEM; 101 return -PSICONV_E_NOMEM;
97 for (i = 0; i < psiconv_list_length(value); i++) { 102 for (i = 0; i < psiconv_list_length(value); i++) {
98 if (!(paragraph = psiconv_list_get(value,i))) { 103 if (!(paragraph = psiconv_list_get(value,i))) {
99 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 104 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
100 res = -PSICONV_E_OTHER; 105 res = -PSICONV_E_OTHER;
106 goto ERROR;
107 }
108 for (j = 0; j < strlen(paragraph->text); j++)
109 if ((res = psiconv_write_u8(extra_buf,paragraph->text[j])))
110 goto ERROR;
111 psiconv_write_u8(extra_buf,0x06);
112 }
113 if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf))))
101 goto ERROR; 114 goto ERROR;
102 }
103 for (j = 0; j < strlen(paragraph->text); j++)
104 if ((res = psiconv_write_u8(extra_buf,paragraph->text[j])))
105 goto ERROR;
106 psiconv_write_u8(extra_buf,0x06);
107 }
108 if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf))))
109 goto ERROR;
110 res = psiconv_buffer_concat(buf,extra_buf); 115 res = psiconv_buffer_concat(buf,extra_buf);
116 } else
117 /* Hack: empty text sections are just not allowed */
118 return psiconv_write_u16(buf,0x0602);
111 119
112ERROR: 120ERROR:
113 psiconv_buffer_free(extra_buf); 121 psiconv_buffer_free(extra_buf);
114 return res; 122 return res;
115} 123}
124 psiconv_character_layout character; 132 psiconv_character_layout character;
125 psiconv_paragraph_layout paragraph; 133 psiconv_paragraph_layout paragraph;
126 psiconv_u8 style; 134 psiconv_u8 style;
127 psiconv_u8 nr; 135 psiconv_u8 nr;
128 } *psiconv_paragraph_type_list; 136 } *psiconv_paragraph_type_list;
137 psiconv_u32 obj_id;
129 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */ 138 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */
130 psiconv_paragraph_type_list paragraph_type; 139 psiconv_paragraph_type_list paragraph_type;
131 struct psiconv_paragraph_type_list_s new_type; 140 struct psiconv_paragraph_type_list_s new_type;
132 psiconv_buffer buf_types,buf_elements,buf_inlines; 141 psiconv_buffer buf_types,buf_elements,buf_inlines,buf_objects;
133 psiconv_paragraph paragraph; 142 psiconv_paragraph paragraph;
134 psiconv_in_line_layout in_line; 143 psiconv_in_line_layout in_line = NULL;
135 psiconv_word_style style; 144 psiconv_word_style style;
136 int i,j,para_type,nr_of_inlines=0,res; 145 psiconv_character_layout para_charlayout;
146 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen;
137 147
138 if (!value) { 148 if (!value) {
139 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); 149 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section");
140 return -PSICONV_E_GENERATE; 150 return -PSICONV_E_GENERATE;
141 } 151 }
156 } 166 }
157 167
158 if (!(buf_inlines = psiconv_buffer_new())) { 168 if (!(buf_inlines = psiconv_buffer_new())) {
159 res = -PSICONV_E_NOMEM; 169 res = -PSICONV_E_NOMEM;
160 goto ERROR4; 170 goto ERROR4;
171 }
172
173 if (!(buf_objects = psiconv_buffer_new())) {
174 res = -PSICONV_E_NOMEM;
175 goto ERROR5;
161 } 176 }
162 177
163 for (i = 0; i < psiconv_list_length(value); i++) { 178 for (i = 0; i < psiconv_list_length(value); i++) {
164 if (!(paragraph = psiconv_list_get(value,i))) { 179 if (!(paragraph = psiconv_list_get(value,i))) {
165 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 180 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
166 res = -PSICONV_E_OTHER; 181 res = -PSICONV_E_OTHER;
167 goto ERROR5; 182 goto ERROR6;
168 } 183 }
169 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) 184 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1)))
170 goto ERROR5; 185 goto ERROR6;
171 186
187 /* We need it for the next if-statement */
172 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))) {
173 /* Inline layouts, so we generate a paragraph element and inline 195 /* Inline layouts, or an object, so we generate a paragraph element
174 elements */ 196 and inline elements */
175 if ((res = psiconv_write_u8(buf_elements,0x00))) 197 if ((res = psiconv_write_u8(buf_elements,0x00)))
176 goto ERROR5; 198 goto ERROR6;
177 if (!(style = psiconv_get_style(styles,paragraph->base_style))) { 199 if (!(style = psiconv_get_style(styles,paragraph->base_style))) {
178 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); 200 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
179 res = -PSICONV_E_GENERATE; 201 res = -PSICONV_E_GENERATE;
180 goto ERROR5; 202 goto ERROR6;
181 } 203 }
182 if ((res = psiconv_write_paragraph_layout_list(buf_elements, 204 if ((res = psiconv_write_paragraph_layout_list(buf_elements,
183 paragraph->base_paragraph, 205 paragraph->base_paragraph,
184 style->paragraph))) 206 style->paragraph)))
185 goto ERROR5; 207 goto ERROR6;
186 if (with_styles) 208 if (with_styles)
187 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style))) 209 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style)))
188 goto ERROR5; 210 goto ERROR6;
189 if ((res = psiconv_write_u32(buf_elements, 211 if ((res = psiconv_write_u32(buf_elements,
190 psiconv_list_length(paragraph->in_lines)))) 212 psiconv_list_length(paragraph->in_lines))))
191 goto ERROR5; 213 goto ERROR6;
192 214
193 /* Generate the inlines. NB: Against what are all settings relative?!? */ 215 /* Generate the inlines. NB: Against what are all settings relative?!? */
216 paralen = 0;
194 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { 217 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) {
195 nr_of_inlines ++; 218 nr_of_inlines ++;
196 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { 219 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) {
197 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 220 psiconv_warn(0,psiconv_buffer_length(buf),
221 "Massive memory corruption");
198 res = -PSICONV_E_OTHER; 222 res = -PSICONV_E_OTHER;
199 goto ERROR5; 223 goto ERROR6;
200 } 224 }
201 if ((res = psiconv_write_u8(buf_inlines,0x00))) 225 if ((res = psiconv_write_u8(buf_inlines,in_line->object?0x01:0x00)))
202 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 }
203 if ((res = psiconv_write_u32(buf_inlines,in_line->length))) 238 if ((res = psiconv_write_u32(buf_inlines,thislen)))
204 goto ERROR5; 239 goto ERROR6;
205 if ((res = psiconv_write_character_layout_list(buf_inlines, 240 if ((res = psiconv_write_character_layout_list(buf_inlines,
206 in_line->layout, 241 in_line->layout,
207 style->character))) 242 style->character)))
208 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 }
209 } 260 }
210 } else { 261 } else {
211 /* No inline layouts, so we generate a paragraph type list */ 262 /* No inline layouts (or only 1), so we generate a paragraph type list */
212 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 }
213 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { 276 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) {
214 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { 277 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) {
215 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); 278 psiconv_warn(0,psiconv_buffer_length(buf),
279 "Massive memory corruption");
216 res = -PSICONV_E_OTHER; 280 res = -PSICONV_E_OTHER;
217 goto ERROR5; 281 goto ERROR6;
218 } 282 }
219 if ((paragraph->base_style == paragraph_type->style) && 283 if ((paragraph->base_style == paragraph_type->style) &&
220 !psiconv_compare_character_layout(paragraph->base_character, 284 !psiconv_compare_character_layout(para_charlayout,
221 paragraph_type->character) && 285 paragraph_type->character) &&
222 !psiconv_compare_paragraph_layout(paragraph->base_paragraph, 286 !psiconv_compare_paragraph_layout(paragraph->base_paragraph,
223 paragraph_type->paragraph)) { 287 paragraph_type->paragraph)) {
224 para_type = paragraph_type->nr; 288 para_type = paragraph_type->nr;
225 break; 289 break;
228 if (!para_type) { 292 if (!para_type) {
229 /* We need to add a new entry */ 293 /* We need to add a new entry */
230 para_type = new_type.nr = j+1; 294 para_type = new_type.nr = j+1;
231 /* No need to copy them, we won't change them anyway */ 295 /* No need to copy them, we won't change them anyway */
232 new_type.paragraph = paragraph->base_paragraph; 296 new_type.paragraph = paragraph->base_paragraph;
233 new_type.character = paragraph->base_character; 297 new_type.character = para_charlayout;
234 new_type.style = paragraph->base_style; 298 new_type.style = paragraph->base_style;
235 paragraph_type = &new_type; 299 paragraph_type = &new_type;
236 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) 300 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type)))
237 goto ERROR5; 301 goto ERROR6;
238 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) 302 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr)))
239 goto ERROR5; 303 goto ERROR6;
240 if (!(style = psiconv_get_style(styles,paragraph_type->style))) { 304 if (!(style = psiconv_get_style(styles,paragraph_type->style))) {
241 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); 305 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
242 res = -PSICONV_E_GENERATE; 306 res = -PSICONV_E_GENERATE;
243 goto ERROR5; 307 goto ERROR6;
244 } 308 }
245 if ((res = psiconv_write_paragraph_layout_list(buf_types, 309 if ((res = psiconv_write_paragraph_layout_list(buf_types,
246 paragraph_type->paragraph,style->paragraph))) 310 paragraph_type->paragraph,style->paragraph)))
247 goto ERROR5; 311 goto ERROR6;
248 if (with_styles) 312 if (with_styles)
249 if ((res = psiconv_write_u8(buf_types,paragraph_type->style))) 313 if ((res = psiconv_write_u8(buf_types,paragraph_type->style)))
250 goto ERROR5; 314 goto ERROR6;
251 if ((res = psiconv_write_character_layout_list(buf_types, 315 if ((res = psiconv_write_character_layout_list(buf_types,
252 paragraph_type->character,style->character))) 316 paragraph_type->character,style->character)))
253 goto ERROR5; 317 goto ERROR6;
254 } 318 }
255 if ((res = psiconv_write_u8(buf_elements,para_type))) 319 if ((res = psiconv_write_u8(buf_elements,para_type)))
256 goto ERROR5; 320 goto ERROR6;
257 } 321 }
322 }
323
324 /* HACK: special case: no paragraphs at all. We need to improvize. */
325 if (!psiconv_list_length(value)) {
326 if ((res = psiconv_write_u32(buf_types,1)))
327 goto ERROR6;
328 if ((res = psiconv_write_u32(buf_types,0)))
329 goto ERROR6;
330 if (with_styles)
331 if ((res = psiconv_write_u8(buf_types,0)))
332 goto ERROR6;
333 if ((res = psiconv_write_u32(buf_types,0)))
334 goto ERROR6;
335
336 if ((res = psiconv_write_u32(buf_elements,1)))
337 goto ERROR6;
338 if ((res = psiconv_write_u8(buf_elements,1)))
339 goto ERROR6;
340 pel_length = 1;
341 ptl_length = 1;
342 } else {
343 pel_length = psiconv_list_length(value);
344 ptl_length = psiconv_list_length(paragraph_type_list);
258 } 345 }
259 346
260 /* Now append everything */ 347 /* Now append everything */
261 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) 348 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000)))
262 goto ERROR5; 349 goto ERROR6;
263 if ((res = psiconv_write_u8(buf,psiconv_list_length(paragraph_type_list)))) 350 if ((res = psiconv_write_u8(buf, ptl_length)))
264 goto ERROR5; 351 goto ERROR6;
265 if ((res = psiconv_buffer_concat(buf,buf_types))) 352 if ((res = psiconv_buffer_concat(buf,buf_types)))
266 goto ERROR5; 353 goto ERROR6;
267 if ((res = psiconv_write_u32(buf,psiconv_list_length(value)))) 354 if ((res = psiconv_write_u32(buf,pel_length)))
268 goto ERROR5; 355 goto ERROR6;
269 if ((res = psiconv_buffer_concat(buf,buf_elements))) 356 if ((res = psiconv_buffer_concat(buf,buf_elements)))
270 goto ERROR5; 357 goto ERROR6;
271 if ((res = psiconv_write_u32(buf,nr_of_inlines))) 358 if ((res = psiconv_write_u32(buf,nr_of_inlines)))
272 goto ERROR5; 359 goto ERROR6;
273 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;
274 364
365ERROR6:
366 psiconv_buffer_free(buf_objects);
275ERROR5: 367ERROR5:
276 psiconv_buffer_free(buf_inlines); 368 psiconv_buffer_free(buf_inlines);
277ERROR4: 369ERROR4:
278 psiconv_buffer_free(buf_elements); 370 psiconv_buffer_free(buf_elements);
279ERROR3: 371ERROR3:
334 if (!res) 426 if (!res)
335 return -PSICONV_E_NOMEM; 427 return -PSICONV_E_NOMEM;
336 else 428 else
337 return res; 429 return res;
338} 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.80  
changed lines
  Added in v.167

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