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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 196 - (show annotations)
Wed Feb 4 12:19:09 2004 UTC (20 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 21211 byte(s)
(Frodo) Copyright dates update

1 /*
2 generate_common.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 2000-2004 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 #ifdef DMALLOC
29 #include <dmalloc.h>
30 #endif
31
32 static int psiconv_write_layout_section(const psiconv_config config,
33 psiconv_buffer buf, int lev,
34 const psiconv_text_and_layout value,
35 const psiconv_word_styles_section styles,
36 int with_styles);
37
38 /* Maybe use a psiconv_header_section variable instead? */
39 int psiconv_write_header_section(const psiconv_config config,
40 psiconv_buffer buf,int lev,psiconv_u32 uid1,
41 psiconv_u32 uid2, psiconv_u32 uid3)
42 {
43 int res;
44 psiconv_progress(config,lev,0,"Writing header section");
45 if ((res = psiconv_write_u32(config,buf,lev+1,uid1)))
46 return res;
47 if ((res = psiconv_write_u32(config,buf,lev+1,uid2)))
48 return res;
49 if ((res = psiconv_write_u32(config,buf,lev+1,uid3)))
50 return res;
51 return psiconv_write_u32(config,buf,lev+1,psiconv_checkuid(uid1,uid2,uid3));
52 }
53
54 int psiconv_write_section_table_section(const psiconv_config config,
55 psiconv_buffer buf,int lev,
56 const psiconv_section_table_section value)
57 {
58 int res,i;
59 psiconv_section_table_entry entry;
60
61 psiconv_progress(config,lev,0,"Writing section table section");
62 if (!value) {
63 psiconv_error(config,0,psiconv_buffer_length(buf),"Null section table section");
64 return -PSICONV_E_GENERATE;
65 }
66
67 if ((res = psiconv_write_u8(config,buf,lev+1,2 * psiconv_list_length(value))))
68 return res;
69 for (i = 0; i < psiconv_list_length(value); i++) {
70 if (!(entry = psiconv_list_get(value,i))) {
71 psiconv_error(config,0,psiconv_buffer_length(buf),"Massive memory corruption");
72 return -PSICONV_E_NOMEM;
73 }
74 if ((res = psiconv_write_u32(config,buf,lev+1,entry->id)))
75 return res;
76 if ((res = psiconv_write_offset(config,buf,lev+1,entry->offset)))
77 return res;
78 }
79 return -PSICONV_E_OK;
80 }
81
82 int psiconv_write_application_id_section(const psiconv_config config,
83 psiconv_buffer buf,int lev,psiconv_u32 id,
84 const psiconv_string_t text)
85 {
86 int res;
87 psiconv_progress(config,lev,0,"Writing application id section");
88 if ((res = psiconv_write_u32(config,buf,lev+1,id)))
89 return res;
90 return psiconv_write_string(config,buf,lev+1,text);
91 }
92
93 int psiconv_write_text_section(const psiconv_config config,
94 psiconv_buffer buf,int lev,
95 const psiconv_text_and_layout value)
96 {
97 int res;
98 psiconv_buffer extra_buf;
99 int i,j;
100 psiconv_paragraph paragraph;
101
102 psiconv_progress(config,lev,0,"Writing text section");
103 if (!value) {
104 psiconv_error(config,0,psiconv_buffer_length(buf),"Null text section");
105 return -PSICONV_E_GENERATE;
106 }
107
108 if (psiconv_list_length(value)) {
109 if (!(extra_buf = psiconv_buffer_new()))
110 return -PSICONV_E_NOMEM;
111 for (i = 0; i < psiconv_list_length(value); i++) {
112 if (!(paragraph = psiconv_list_get(value,i))) {
113 psiconv_error(config,0,psiconv_buffer_length(buf),"Massive memory corruption");
114 res = -PSICONV_E_NOMEM;
115 goto ERROR;
116 }
117 for (j = 0; j < psiconv_unicode_strlen(paragraph->text); j++)
118 if ((res = psiconv_write_u8(config,extra_buf,lev+1,
119 psiconv_unicode_to_char(config,paragraph->text[j]))))
120 goto ERROR;
121 psiconv_write_u8(config,extra_buf,lev+1,0x06);
122 }
123 if ((res = psiconv_write_X(config,buf,lev+1,psiconv_buffer_length(extra_buf))))
124 goto ERROR;
125 res = psiconv_buffer_concat(buf,extra_buf);
126 } else
127 /* Hack: empty text sections are just not allowed */
128 return psiconv_write_u16(config,buf,lev+1,0x0602);
129
130 ERROR:
131 psiconv_buffer_free(extra_buf);
132 return res;
133 }
134
135 int psiconv_write_layout_section(const psiconv_config config,
136 psiconv_buffer buf,int lev,
137 const psiconv_text_and_layout value,
138 const psiconv_word_styles_section styles,
139 int with_styles)
140 {
141 typedef struct psiconv_paragraph_type_list_s
142 {
143 psiconv_character_layout character;
144 psiconv_paragraph_layout paragraph;
145 psiconv_u8 style;
146 psiconv_u8 nr;
147 } *psiconv_paragraph_type_list;
148 psiconv_u32 obj_id;
149 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */
150 psiconv_paragraph_type_list paragraph_type;
151 struct psiconv_paragraph_type_list_s new_type;
152 psiconv_buffer buf_types,buf_elements,buf_inlines,buf_objects;
153 psiconv_paragraph paragraph;
154 psiconv_in_line_layout in_line = NULL;
155 psiconv_word_style style;
156 psiconv_character_layout para_charlayout;
157 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen;
158
159 psiconv_progress(config,lev,0,"Writing layout section");
160 if (!value) {
161 psiconv_error(config,0,psiconv_buffer_length(buf),"Null text section");
162 return -PSICONV_E_GENERATE;
163 }
164
165 if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) {
166 res = -PSICONV_E_NOMEM;
167 goto ERROR1;
168 }
169
170 if (!(buf_types = psiconv_buffer_new())) {
171 res = -PSICONV_E_NOMEM;
172 goto ERROR2;
173 }
174
175 if (!(buf_elements = psiconv_buffer_new())) {
176 res = -PSICONV_E_NOMEM;
177 goto ERROR3;
178 }
179
180 if (!(buf_inlines = psiconv_buffer_new())) {
181 res = -PSICONV_E_NOMEM;
182 goto ERROR4;
183 }
184
185 if (!(buf_objects = psiconv_buffer_new())) {
186 res = -PSICONV_E_NOMEM;
187 goto ERROR5;
188 }
189
190 for (i = 0; i < psiconv_list_length(value); i++) {
191 if (!(paragraph = psiconv_list_get(value,i))) {
192 psiconv_error(config,0,psiconv_buffer_length(buf),"Massive memory corruption");
193 res = -PSICONV_E_NOMEM;
194 goto ERROR6;
195 }
196 if ((res = psiconv_write_u32(config,buf_elements,lev+1,
197 psiconv_unicode_strlen(paragraph->text)+1)))
198 goto ERROR6;
199
200 /* We need it for the next if-statement */
201 if (psiconv_list_length(paragraph->in_lines) == 1)
202 if (!(in_line = psiconv_list_get(paragraph->in_lines,1)))
203 goto ERROR6;
204
205 if ((psiconv_list_length(paragraph->in_lines) > 1) ||
206 ((psiconv_list_length(paragraph->in_lines) == 1) &&
207 (in_line->object != NULL))) {
208 /* Inline layouts, or an object, so we generate a paragraph element
209 and inline elements */
210 if ((res = psiconv_write_u8(config,buf_elements,lev+1,0x00)))
211 goto ERROR6;
212 if (!(style = psiconv_get_style(styles,paragraph->base_style))) {
213 psiconv_error(config,0,psiconv_buffer_length(buf),"Unknown style");
214 res = -PSICONV_E_GENERATE;
215 goto ERROR6;
216 }
217 if ((res = psiconv_write_paragraph_layout_list(config,buf_elements,lev+1,
218 paragraph->base_paragraph,
219 style->paragraph)))
220 goto ERROR6;
221 if (with_styles)
222 if ((res = psiconv_write_u8(config,buf_elements,lev+1,paragraph->base_style)))
223 goto ERROR6;
224 if ((res = psiconv_write_u32(config,buf_elements,lev+1,
225 psiconv_list_length(paragraph->in_lines))))
226 goto ERROR6;
227
228 /* Generate the inlines. NB: Against what are all settings relative?!? */
229 paralen = 0;
230 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) {
231 nr_of_inlines ++;
232 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) {
233 psiconv_error(config,0,psiconv_buffer_length(buf),
234 "Massive memory corruption");
235 res = -PSICONV_E_NOMEM;
236 goto ERROR6;
237 }
238 if ((res = psiconv_write_u8(config,buf_inlines,lev+1,in_line->object?0x01:0x00)))
239 goto ERROR6;
240 thislen = in_line->length;
241 paralen += thislen;
242 /* If this is the last in_line, we need to make sure that the
243 complete length of all inlines equals the text length */
244 if (j == psiconv_list_length(paragraph->in_lines)-1) {
245 if (paralen > psiconv_unicode_strlen(paragraph->text)+1) {
246 res = -PSICONV_E_GENERATE;
247 goto ERROR6;
248 }
249 thislen += psiconv_unicode_strlen(paragraph->text)+1-paralen;
250 }
251 if ((res = psiconv_write_u32(config,buf_inlines,lev+1,thislen)))
252 goto ERROR6;
253 if ((res = psiconv_write_character_layout_list(config,buf_inlines,lev+1,
254 in_line->layout,
255 style->character)))
256 goto ERROR6;
257 if (in_line->object) {
258 if ((res = psiconv_write_u32(config,buf_inlines,lev+1,PSICONV_ID_OBJECT)))
259 goto ERROR6;
260 obj_id = psiconv_buffer_unique_id();
261 if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id)))
262 goto ERROR6;
263 if ((res = psiconv_buffer_add_target(buf_objects,obj_id)))
264 goto ERROR6;
265 if ((res = psiconv_write_embedded_object_section(config,buf_objects,lev+1,
266 in_line->object)))
267 goto ERROR6;
268 if ((res = psiconv_write_length(config,buf_inlines,lev+1,in_line->object_width)))
269 goto ERROR6;
270 if ((res = psiconv_write_length(config,buf_inlines,lev+1,in_line->object_height)))
271 goto ERROR6;
272 }
273 }
274 } else {
275 /* No inline layouts (or only 1), so we generate a paragraph type list */
276 para_type = 0;
277 /* Set para_charlayout to the correct character-level layout */
278 if (psiconv_list_length(paragraph->in_lines) == 0)
279 para_charlayout = paragraph->base_character;
280 else {
281 if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) {
282 psiconv_error(config,0,psiconv_buffer_length(buf),
283 "Massive memory corruption");
284 res = -PSICONV_E_NOMEM;
285 goto ERROR6;
286 }
287 para_charlayout = in_line->layout;
288 }
289 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) {
290 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) {
291 psiconv_error(config,0,psiconv_buffer_length(buf),
292 "Massive memory corruption");
293 res = -PSICONV_E_NOMEM;
294 goto ERROR6;
295 }
296 if ((paragraph->base_style == paragraph_type->style) &&
297 !psiconv_compare_character_layout(para_charlayout,
298 paragraph_type->character) &&
299 !psiconv_compare_paragraph_layout(paragraph->base_paragraph,
300 paragraph_type->paragraph)) {
301 para_type = paragraph_type->nr;
302 break;
303 }
304 }
305 if (!para_type) {
306 /* We need to add a new entry */
307 para_type = new_type.nr = j+1;
308 /* No need to copy them, we won't change them anyway */
309 new_type.paragraph = paragraph->base_paragraph;
310 new_type.character = para_charlayout;
311 new_type.style = paragraph->base_style;
312 paragraph_type = &new_type;
313 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type)))
314 goto ERROR6;
315 if ((res = psiconv_write_u32(config,buf_types,lev+1,paragraph_type->nr)))
316 goto ERROR6;
317 if (!(style = psiconv_get_style(styles,paragraph_type->style))) {
318 psiconv_error(config,0,psiconv_buffer_length(buf),"Unknown style");
319 res = -PSICONV_E_GENERATE;
320 goto ERROR6;
321 }
322 if ((res = psiconv_write_paragraph_layout_list(config,buf_types,lev+1,
323 paragraph_type->paragraph,style->paragraph)))
324 goto ERROR6;
325 if (with_styles)
326 if ((res = psiconv_write_u8(config,buf_types,lev+1,paragraph_type->style)))
327 goto ERROR6;
328 if ((res = psiconv_write_character_layout_list(config,buf_types,lev+1,
329 paragraph_type->character,style->character)))
330 goto ERROR6;
331 }
332 if ((res = psiconv_write_u8(config,buf_elements,lev+1,para_type)))
333 goto ERROR6;
334 }
335 }
336
337 /* HACK: special case: no paragraphs at all. We need to improvize. */
338 if (!psiconv_list_length(value)) {
339 if ((res = psiconv_write_u32(config,buf_types,lev+1,1)))
340 goto ERROR6;
341 if ((res = psiconv_write_u32(config,buf_types,lev+1,0)))
342 goto ERROR6;
343 if (with_styles)
344 if ((res = psiconv_write_u8(config,buf_types,lev+1,0)))
345 goto ERROR6;
346 if ((res = psiconv_write_u32(config,buf_types,lev+1,0)))
347 goto ERROR6;
348
349 if ((res = psiconv_write_u32(config,buf_elements,lev+1,1)))
350 goto ERROR6;
351 if ((res = psiconv_write_u8(config,buf_elements,lev+1,1)))
352 goto ERROR6;
353 pel_length = 1;
354 ptl_length = 1;
355 } else {
356 pel_length = psiconv_list_length(value);
357 ptl_length = psiconv_list_length(paragraph_type_list);
358 }
359
360 /* Now append everything */
361 if ((res = psiconv_write_u16(config,buf,lev+1,with_styles?0x0001:0x0000)))
362 goto ERROR6;
363 if ((res = psiconv_write_u8(config,buf,lev+1, ptl_length)))
364 goto ERROR6;
365 if ((res = psiconv_buffer_concat(buf,buf_types)))
366 goto ERROR6;
367 if ((res = psiconv_write_u32(config,buf,lev+1,pel_length)))
368 goto ERROR6;
369 if ((res = psiconv_buffer_concat(buf,buf_elements)))
370 goto ERROR6;
371 if ((res = psiconv_write_u32(config,buf,lev+1,nr_of_inlines)))
372 goto ERROR6;
373 if ((res = psiconv_buffer_concat(buf,buf_inlines)))
374 goto ERROR6;
375 if ((res = psiconv_buffer_concat(buf,buf_objects)))
376 goto ERROR6;
377
378 ERROR6:
379 psiconv_buffer_free(buf_objects);
380 ERROR5:
381 psiconv_buffer_free(buf_inlines);
382 ERROR4:
383 psiconv_buffer_free(buf_elements);
384 ERROR3:
385 psiconv_buffer_free(buf_types);
386 ERROR2:
387 psiconv_list_free(paragraph_type_list);
388 ERROR1:
389 return res;
390 }
391
392 int psiconv_write_styled_layout_section(const psiconv_config config,
393 psiconv_buffer buf,int lev,
394 psiconv_text_and_layout result,
395 psiconv_word_styles_section styles)
396 {
397 psiconv_progress(config,lev,0,"Writing styled layout section");
398 return psiconv_write_layout_section(config,buf,lev+1,result,styles,1);
399 }
400
401 int psiconv_write_styleless_layout_section(const psiconv_config config,
402 psiconv_buffer buf,int lev,
403 const psiconv_text_and_layout value,
404 const psiconv_character_layout base_char,
405 const psiconv_paragraph_layout base_para)
406 {
407 int res = 0;
408 psiconv_word_styles_section styles_section;
409
410 psiconv_progress(config,lev,0,"Writing styleless layout section");
411 if (!(styles_section = malloc(sizeof(*styles_section))))
412 goto ERROR1;
413 if (!(styles_section->normal = malloc(sizeof(*styles_section->normal))))
414 goto ERROR2;
415 if (!(styles_section->normal->character =
416 psiconv_clone_character_layout(base_char)))
417 goto ERROR3;
418 if (!(styles_section->normal->paragraph =
419 psiconv_clone_paragraph_layout(base_para)))
420 goto ERROR4;
421 styles_section->normal->hotkey = 0;
422 if ((res = psiconv_unicode_from_chars(config,"",
423 &styles_section->normal->name)))
424 goto ERROR5;
425 if (!(styles_section->styles = psiconv_list_new(sizeof(
426 struct psiconv_word_style_s))))
427 goto ERROR6;
428
429 res = psiconv_write_layout_section(config,buf,lev+1,value,styles_section,0);
430 psiconv_free_word_styles_section(styles_section);
431 return res;
432
433 ERROR6:
434 free(styles_section->normal->name);
435 ERROR5:
436 psiconv_free_paragraph_layout(styles_section->normal->paragraph);
437 ERROR4:
438 psiconv_free_character_layout(styles_section->normal->character);
439 ERROR3:
440 free(styles_section->normal);
441 ERROR2:
442 free(styles_section);
443 ERROR1:
444 if (!res)
445 return -PSICONV_E_NOMEM;
446 else
447 return res;
448 }
449
450
451 int psiconv_write_embedded_object_section(const psiconv_config config,
452 psiconv_buffer buf,int lev,
453 const psiconv_embedded_object_section value)
454 {
455 int res;
456 psiconv_u32 display_id,icon_id,table_id;
457 psiconv_buffer extra_buf;
458
459 psiconv_progress(config,lev,0,"Writing embedded object section");
460 if (!value) {
461 psiconv_error(config,0,psiconv_buffer_length(buf),"Null Object");
462 res = -PSICONV_E_GENERATE;
463 goto ERROR1;
464 }
465
466 if (!(extra_buf = psiconv_buffer_new())) {
467 res = -PSICONV_E_NOMEM;
468 goto ERROR1;
469 }
470
471 display_id = psiconv_buffer_unique_id();
472 icon_id = psiconv_buffer_unique_id();
473 table_id = psiconv_buffer_unique_id();
474 if ((res = psiconv_write_u8(config,buf,lev+1,0x06)))
475 goto ERROR2;
476 if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_DISPLAY_SECTION)))
477 goto ERROR2;
478 if ((res = psiconv_buffer_add_reference(buf,display_id)))
479 goto ERROR2;
480 if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_ICON_SECTION)))
481 goto ERROR2;
482 if ((res = psiconv_buffer_add_reference(buf,icon_id)))
483 goto ERROR2;
484 if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION)))
485 goto ERROR2;
486 if ((res = psiconv_buffer_add_reference(buf,table_id)))
487 goto ERROR2;
488
489 if ((res = psiconv_buffer_add_target(buf,display_id)))
490 goto ERROR2;
491 if ((res = psiconv_write_object_display_section(config,buf,lev+1,value->display)))
492 goto ERROR2;
493 if ((res = psiconv_buffer_add_target(buf,icon_id)))
494 goto ERROR2;
495 if ((res = psiconv_write_object_icon_section(config,buf,lev+1,value->icon)))
496 goto ERROR2;
497 if ((res = psiconv_buffer_add_target(buf,table_id)))
498 goto ERROR2;
499 switch(value->object->type) {
500 case psiconv_word_file:
501 if ((res = psiconv_write_word_file(config,extra_buf,lev+1,
502 (psiconv_word_f) value->object->file)))
503 goto ERROR2;
504 break;
505 case psiconv_sketch_file:
506 if ((res = psiconv_write_sketch_file(config,extra_buf,lev+1,
507 (psiconv_sketch_f) value->object->file)))
508 goto ERROR2;
509 break;
510 /*
511 case psiconv_sheet_file:
512 if ((res = psiconv_write_sheet_file(config,extra_buf,lev+1,
513 (psiconv_sheet_f) value->object->file)))
514 goto ERROR2;
515 break;
516 */
517 default:
518 psiconv_error(config,0,psiconv_buffer_length(buf),
519 "Unknown or unsupported object type");
520 res = -PSICONV_E_GENERATE;
521 goto ERROR2;
522 }
523
524 if ((res = psiconv_buffer_resolve(extra_buf)))
525 goto ERROR2;
526 if ((res = psiconv_buffer_concat(buf,extra_buf)))
527 goto ERROR2;
528
529 return 0;
530
531 ERROR2:
532 psiconv_buffer_free(extra_buf);
533 ERROR1:
534 return res;
535 }
536
537
538 int psiconv_write_object_display_section(const psiconv_config config,
539 psiconv_buffer buf,int lev,
540 const psiconv_object_display_section value)
541 {
542 int res;
543
544 psiconv_progress(config,lev,0,"Writing object display section");
545
546 if (!value) {
547 psiconv_error(config,0,psiconv_buffer_length(buf),"Null Object Display Section");
548 res = -PSICONV_E_GENERATE;
549 goto ERROR1;
550 }
551
552 if ((res = psiconv_write_u8(config,buf,lev+1,value->show_icon?0x00:0x01)))
553 goto ERROR1;
554 if ((res = psiconv_write_length(config,buf,lev+1,value->width)))
555 goto ERROR1;
556 if ((res = psiconv_write_length(config,buf,lev+1,value->height)))
557 goto ERROR1;
558 if ((res = psiconv_write_u32(config,buf,lev+1,0x00000000)))
559 goto ERROR1;
560
561 return 0;
562
563 ERROR1:
564 return res;
565 }
566
567 int psiconv_write_object_icon_section(const psiconv_config config,
568 psiconv_buffer buf,int lev,
569 const psiconv_object_icon_section value)
570 {
571 int res;
572
573 psiconv_progress(config,lev,0,"Writing object icon section");
574
575 if (!value) {
576 psiconv_error(config,0,psiconv_buffer_length(buf),"Null Object Icon Section");
577 res = -PSICONV_E_GENERATE;
578 goto ERROR1;
579 }
580
581 if ((res = psiconv_write_string(config,buf,lev+1,value->icon_name)))
582 goto ERROR1;
583 if ((res = psiconv_write_length(config,buf,lev+1,value->icon_width)))
584 goto ERROR1;
585 if ((res = psiconv_write_length(config,buf,lev+1,value->icon_height)))
586 goto ERROR1;
587
588 return 0;
589
590 ERROR1:
591 return res;
592 }

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