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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 167 - (hide annotations)
Sun Nov 23 21:47:24 2003 UTC (20 years, 5 months ago) by frodo
File MIME type: text/plain
File size: 18712 byte(s)
(Frodo) Embedded object generation works!

Not too useful though, we also need sketch or sheet generation; the only thing
actually working at the moment is embedding word docs within word docs.
Oh well. It's a good start.

1 frodo 73 /*
2     generate_common.c - Part of psiconv, a PSION 5 file formats converter
3     Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>
4    
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
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9    
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     GNU General Public License for more details.
14    
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18     */
19    
20     #include "config.h"
21     #include "compat.h"
22     #include <stdlib.h>
23     #include <string.h>
24    
25     #include "generate_routines.h"
26     #include "error.h"
27    
28 frodo 142 #ifdef DMALLOC
29     #include <dmalloc.h>
30     #endif
31    
32 frodo 79 static int psiconv_write_layout_section(psiconv_buffer buf,
33     const psiconv_text_and_layout value,
34     const psiconv_word_styles_section styles,
35     int with_styles);
36 frodo 73
37     /* Maybe use a psiconv_header_section variable instead? */
38     int psiconv_write_header_section(psiconv_buffer buf,psiconv_u32 uid1,
39     psiconv_u32 uid2, psiconv_u32 uid3)
40     {
41     int res;
42     if ((res = psiconv_write_u32(buf,uid1)))
43     return res;
44     if ((res = psiconv_write_u32(buf,uid2)))
45     return res;
46     if ((res = psiconv_write_u32(buf,uid3)))
47     return res;
48     return psiconv_write_u32(buf,psiconv_checkuid(uid1,uid2,uid3));
49     }
50    
51     int psiconv_write_section_table_section(psiconv_buffer buf,
52     const psiconv_section_table_section value)
53     {
54     int res,i;
55     psiconv_section_table_entry entry;
56    
57     if (!value) {
58 frodo 79 psiconv_warn(0,psiconv_buffer_length(buf),"Null section table section");
59 frodo 73 return -PSICONV_E_GENERATE;
60     }
61    
62 frodo 76 if ((res = psiconv_write_u8(buf,2 * psiconv_list_length(value))))
63     return res;
64 frodo 73 for (i = 0; i < psiconv_list_length(value); i++) {
65     if (!(entry = psiconv_list_get(value,i))) {
66 frodo 79 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
67 frodo 76 return -PSICONV_E_NOMEM;
68 frodo 73 }
69     if ((res = psiconv_write_u32(buf,entry->id)))
70 frodo 76 return res;
71 frodo 80 if ((res = psiconv_write_offset(buf,entry->offset)))
72 frodo 76 return res;
73 frodo 73 }
74 frodo 76 return -PSICONV_E_OK;
75 frodo 73 }
76    
77     int psiconv_write_application_id_section(psiconv_buffer buf,psiconv_u32 id,
78     const psiconv_string_t text)
79     {
80     int res;
81     if ((res = psiconv_write_u32(buf,id)))
82     return res;
83     return psiconv_write_string(buf,text);
84     }
85    
86     int psiconv_write_text_section(psiconv_buffer buf,
87     const psiconv_text_and_layout value)
88     {
89     int res;
90     psiconv_buffer extra_buf;
91     int i,j;
92     psiconv_paragraph paragraph;
93    
94     if (!value) {
95 frodo 79 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section");
96 frodo 73 return -PSICONV_E_GENERATE;
97     }
98    
99 frodo 88 if (psiconv_list_length(value)) {
100     if (!(extra_buf = psiconv_buffer_new()))
101     return -PSICONV_E_NOMEM;
102     for (i = 0; i < psiconv_list_length(value); i++) {
103     if (!(paragraph = psiconv_list_get(value,i))) {
104     psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
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))))
114 frodo 73 goto ERROR;
115 frodo 88 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);
119 frodo 73
120     ERROR:
121 frodo 79 psiconv_buffer_free(extra_buf);
122 frodo 75 return res;
123 frodo 73 }
124 frodo 79
125     int psiconv_write_layout_section(psiconv_buffer buf,
126     const psiconv_text_and_layout value,
127     const psiconv_word_styles_section styles,
128     int with_styles)
129     {
130     typedef struct psiconv_paragraph_type_list_s
131     {
132     psiconv_character_layout character;
133     psiconv_paragraph_layout paragraph;
134     psiconv_u8 style;
135     psiconv_u8 nr;
136     } *psiconv_paragraph_type_list;
137 frodo 167 psiconv_u32 obj_id;
138 frodo 79 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */
139     psiconv_paragraph_type_list paragraph_type;
140     struct psiconv_paragraph_type_list_s new_type;
141 frodo 167 psiconv_buffer buf_types,buf_elements,buf_inlines,buf_objects;
142 frodo 79 psiconv_paragraph paragraph;
143 frodo 167 psiconv_in_line_layout in_line = NULL;
144 frodo 79 psiconv_word_style style;
145 frodo 89 psiconv_character_layout para_charlayout;
146     int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen;
147 frodo 79
148     if (!value) {
149     psiconv_warn(0,psiconv_buffer_length(buf),"Null text section");
150     return -PSICONV_E_GENERATE;
151     }
152    
153     if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) {
154     res = -PSICONV_E_NOMEM;
155     goto ERROR1;
156     }
157    
158 frodo 80 if (!(buf_types = psiconv_buffer_new())) {
159 frodo 79 res = -PSICONV_E_NOMEM;
160     goto ERROR2;
161     }
162    
163 frodo 80 if (!(buf_elements = psiconv_buffer_new())) {
164 frodo 79 res = -PSICONV_E_NOMEM;
165     goto ERROR3;
166     }
167    
168 frodo 80 if (!(buf_inlines = psiconv_buffer_new())) {
169 frodo 79 res = -PSICONV_E_NOMEM;
170     goto ERROR4;
171     }
172    
173 frodo 167 if (!(buf_objects = psiconv_buffer_new())) {
174     res = -PSICONV_E_NOMEM;
175     goto ERROR5;
176     }
177    
178 frodo 79 for (i = 0; i < psiconv_list_length(value); i++) {
179     if (!(paragraph = psiconv_list_get(value,i))) {
180     psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
181     res = -PSICONV_E_OTHER;
182 frodo 167 goto ERROR6;
183 frodo 79 }
184     if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1)))
185 frodo 167 goto ERROR6;
186 frodo 79
187 frodo 167 /* 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    
192     if ((psiconv_list_length(paragraph->in_lines) > 1) ||
193     ((psiconv_list_length(paragraph->in_lines) == 1) &&
194     (in_line->object != NULL))) {
195     /* Inline layouts, or an object, so we generate a paragraph element
196     and inline elements */
197 frodo 80 if ((res = psiconv_write_u8(buf_elements,0x00)))
198 frodo 167 goto ERROR6;
199 frodo 79 if (!(style = psiconv_get_style(styles,paragraph->base_style))) {
200     psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
201     res = -PSICONV_E_GENERATE;
202 frodo 167 goto ERROR6;
203 frodo 79 }
204     if ((res = psiconv_write_paragraph_layout_list(buf_elements,
205     paragraph->base_paragraph,
206     style->paragraph)))
207 frodo 167 goto ERROR6;
208 frodo 79 if (with_styles)
209     if ((res = psiconv_write_u8(buf_elements,paragraph->base_style)))
210 frodo 167 goto ERROR6;
211 frodo 79 if ((res = psiconv_write_u32(buf_elements,
212     psiconv_list_length(paragraph->in_lines))))
213 frodo 167 goto ERROR6;
214 frodo 79
215     /* Generate the inlines. NB: Against what are all settings relative?!? */
216 frodo 89 paralen = 0;
217 frodo 79 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) {
218     nr_of_inlines ++;
219     if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) {
220 frodo 89 psiconv_warn(0,psiconv_buffer_length(buf),
221     "Massive memory corruption");
222 frodo 79 res = -PSICONV_E_OTHER;
223 frodo 167 goto ERROR6;
224 frodo 79 }
225 frodo 167 if ((res = psiconv_write_u8(buf_inlines,in_line->object?0x01:0x00)))
226     goto ERROR6;
227 frodo 89 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 frodo 167 goto ERROR6;
235 frodo 89 }
236     thislen += strlen(paragraph->text)+1-paralen;
237     }
238     if ((res = psiconv_write_u32(buf_inlines,thislen)))
239 frodo 167 goto ERROR6;
240 frodo 80 if ((res = psiconv_write_character_layout_list(buf_inlines,
241 frodo 79 in_line->layout,
242     style->character)))
243 frodo 167 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     }
260 frodo 79 }
261     } else {
262 frodo 89 /* No inline layouts (or only 1), so we generate a paragraph type list */
263 frodo 79 para_type = 0;
264 frodo 89 /* 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 frodo 167 goto ERROR6;
273 frodo 89 }
274     para_charlayout = in_line->layout;
275     }
276 frodo 79 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) {
277     if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) {
278 frodo 89 psiconv_warn(0,psiconv_buffer_length(buf),
279     "Massive memory corruption");
280 frodo 79 res = -PSICONV_E_OTHER;
281 frodo 167 goto ERROR6;
282 frodo 79 }
283     if ((paragraph->base_style == paragraph_type->style) &&
284 frodo 89 !psiconv_compare_character_layout(para_charlayout,
285 frodo 79 paragraph_type->character) &&
286     !psiconv_compare_paragraph_layout(paragraph->base_paragraph,
287     paragraph_type->paragraph)) {
288     para_type = paragraph_type->nr;
289     break;
290     }
291     }
292     if (!para_type) {
293     /* We need to add a new entry */
294     para_type = new_type.nr = j+1;
295     /* No need to copy them, we won't change them anyway */
296     new_type.paragraph = paragraph->base_paragraph;
297 frodo 89 new_type.character = para_charlayout;
298 frodo 79 new_type.style = paragraph->base_style;
299     paragraph_type = &new_type;
300     if ((res = psiconv_list_add(paragraph_type_list,paragraph_type)))
301 frodo 167 goto ERROR6;
302 frodo 79 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr)))
303 frodo 167 goto ERROR6;
304 frodo 79 if (!(style = psiconv_get_style(styles,paragraph_type->style))) {
305     psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
306     res = -PSICONV_E_GENERATE;
307 frodo 167 goto ERROR6;
308 frodo 79 }
309     if ((res = psiconv_write_paragraph_layout_list(buf_types,
310     paragraph_type->paragraph,style->paragraph)))
311 frodo 167 goto ERROR6;
312 frodo 79 if (with_styles)
313     if ((res = psiconv_write_u8(buf_types,paragraph_type->style)))
314 frodo 167 goto ERROR6;
315 frodo 79 if ((res = psiconv_write_character_layout_list(buf_types,
316     paragraph_type->character,style->character)))
317 frodo 167 goto ERROR6;
318 frodo 79 }
319     if ((res = psiconv_write_u8(buf_elements,para_type)))
320 frodo 167 goto ERROR6;
321 frodo 79 }
322     }
323    
324 frodo 88 /* 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 frodo 167 goto ERROR6;
328 frodo 88 if ((res = psiconv_write_u32(buf_types,0)))
329 frodo 167 goto ERROR6;
330 frodo 88 if (with_styles)
331     if ((res = psiconv_write_u8(buf_types,0)))
332 frodo 167 goto ERROR6;
333 frodo 88 if ((res = psiconv_write_u32(buf_types,0)))
334 frodo 167 goto ERROR6;
335 frodo 88
336     if ((res = psiconv_write_u32(buf_elements,1)))
337 frodo 167 goto ERROR6;
338 frodo 88 if ((res = psiconv_write_u8(buf_elements,1)))
339 frodo 167 goto ERROR6;
340 frodo 88 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);
345     }
346    
347 frodo 79 /* Now append everything */
348 frodo 80 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000)))
349 frodo 167 goto ERROR6;
350 frodo 88 if ((res = psiconv_write_u8(buf, ptl_length)))
351 frodo 167 goto ERROR6;
352 frodo 79 if ((res = psiconv_buffer_concat(buf,buf_types)))
353 frodo 167 goto ERROR6;
354 frodo 88 if ((res = psiconv_write_u32(buf,pel_length)))
355 frodo 167 goto ERROR6;
356 frodo 79 if ((res = psiconv_buffer_concat(buf,buf_elements)))
357 frodo 167 goto ERROR6;
358 frodo 79 if ((res = psiconv_write_u32(buf,nr_of_inlines)))
359 frodo 167 goto ERROR6;
360     if ((res = psiconv_buffer_concat(buf,buf_inlines)))
361     goto ERROR6;
362     if ((res = psiconv_buffer_concat(buf,buf_objects)))
363     goto ERROR6;
364 frodo 79
365 frodo 167 ERROR6:
366     psiconv_buffer_free(buf_objects);
367 frodo 79 ERROR5:
368     psiconv_buffer_free(buf_inlines);
369     ERROR4:
370     psiconv_buffer_free(buf_elements);
371     ERROR3:
372     psiconv_buffer_free(buf_types);
373     ERROR2:
374     psiconv_list_free(paragraph_type_list);
375     ERROR1:
376     return res;
377     }
378    
379     int psiconv_write_styled_layout_section(psiconv_buffer buf,
380     psiconv_text_and_layout result,
381     psiconv_word_styles_section styles)
382     {
383     return psiconv_write_layout_section(buf,result,styles,1);
384     }
385    
386     int psiconv_write_styleless_layout_section(psiconv_buffer buf,
387     const psiconv_text_and_layout value,
388     const psiconv_character_layout base_char,
389     const psiconv_paragraph_layout base_para)
390     {
391     int res = 0;
392     psiconv_word_styles_section styles_section;
393    
394     if (!(styles_section = malloc(sizeof(*styles_section))))
395     goto ERROR1;
396     if (!(styles_section->normal = malloc(sizeof(*styles_section->normal))))
397     goto ERROR2;
398     if (!(styles_section->normal->character =
399     psiconv_clone_character_layout(base_char)))
400     goto ERROR3;
401     if (!(styles_section->normal->paragraph =
402     psiconv_clone_paragraph_layout(base_para)))
403     goto ERROR4;
404     styles_section->normal->hotkey = 0;
405     if (!(styles_section->normal->name = strdup("")))
406     goto ERROR5;
407     if (!(styles_section->styles = psiconv_list_new(sizeof(
408     struct psiconv_word_style_s))))
409     goto ERROR6;
410    
411     res = psiconv_write_layout_section(buf,value,styles_section,0);
412     psiconv_free_word_styles_section(styles_section);
413     return res;
414    
415     ERROR6:
416     free(styles_section->normal->name);
417     ERROR5:
418     psiconv_free_paragraph_layout(styles_section->normal->paragraph);
419     ERROR4:
420     psiconv_free_character_layout(styles_section->normal->character);
421     ERROR3:
422     free(styles_section->normal);
423     ERROR2:
424     free(styles_section);
425     ERROR1:
426     if (!res)
427     return -PSICONV_E_NOMEM;
428     else
429     return res;
430     }
431 frodo 167
432    
433     int 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    
511     ERROR2:
512     psiconv_buffer_free(extra_buf);
513     ERROR1:
514     return res;
515     }
516    
517    
518     int 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    
540     ERROR1:
541     return res;
542     }
543    
544     int 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    
564     ERROR1:
565     return res;
566     }

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