1 |
/* |
2 |
data.c - Part of psiconv, a PSION 5 file formats converter |
3 |
Copyright (c) 1999, 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 |
#include "data.h" |
25 |
|
26 |
static psiconv_color clone_color(psiconv_color color); |
27 |
static psiconv_font clone_font(psiconv_font font); |
28 |
static psiconv_border clone_border(psiconv_border border); |
29 |
static psiconv_bullet clone_bullet(psiconv_bullet bullet); |
30 |
static psiconv_all_tabs clone_all_tabs(psiconv_all_tabs all_tabs); |
31 |
static void psiconv_free_style_aux(void *style); |
32 |
static void psiconv_free_in_line_layout_aux(void * layout); |
33 |
static void psiconv_free_paragraph_aux(void * paragraph); |
34 |
static void psiconv_free_paint_data_section_aux(void * section); |
35 |
static void psiconv_free_clipart_section_aux(void * section); |
36 |
|
37 |
psiconv_character_layout psiconv_basic_character_layout(void) |
38 |
{ |
39 |
/* Make the structures static, to oblige IRIX */ |
40 |
static struct psiconv_color_s black = |
41 |
{ |
42 |
0x00, /* red */ |
43 |
0x00, /* green */ |
44 |
0x00, /* blue */ |
45 |
}; |
46 |
static struct psiconv_color_s white = |
47 |
{ |
48 |
0xff, /* red */ |
49 |
0xff, /* green */ |
50 |
0xff, /* blue */ |
51 |
}; |
52 |
static struct psiconv_font_s font = |
53 |
{ |
54 |
"Times New Roman", /* name */ |
55 |
3 /* screenfont */ |
56 |
}; |
57 |
struct psiconv_character_layout_s cl = |
58 |
{ |
59 |
&black, /* color */ |
60 |
&white, /* back_color */ |
61 |
10.0, /* font_size */ |
62 |
psiconv_bool_false, /* italic */ |
63 |
psiconv_bool_false, /* bold */ |
64 |
psiconv_normalscript, /* super_sub */ |
65 |
psiconv_bool_false, /* underline */ |
66 |
psiconv_bool_false, /* strike_out */ |
67 |
&font, /* font */ |
68 |
}; |
69 |
|
70 |
return psiconv_clone_character_layout(&cl); |
71 |
} |
72 |
|
73 |
psiconv_paragraph_layout psiconv_basic_paragraph_layout(void) |
74 |
{ |
75 |
static struct psiconv_font_s font = |
76 |
{ |
77 |
"Times New Roman", /* name */ |
78 |
2 /* screenfont */ |
79 |
}; |
80 |
static struct psiconv_color_s black = |
81 |
{ |
82 |
0x00, /* red */ |
83 |
0x00, /* green */ |
84 |
0x00, /* blue */ |
85 |
}; |
86 |
static struct psiconv_color_s white = |
87 |
{ |
88 |
0xff, /* red */ |
89 |
0xff, /* green */ |
90 |
0xff, /* blue */ |
91 |
}; |
92 |
static struct psiconv_border_s no_border = |
93 |
{ |
94 |
psiconv_border_none, /* kind */ |
95 |
1, /* thickness */ |
96 |
&black /* color */ |
97 |
}; |
98 |
static struct psiconv_bullet_s bullet = |
99 |
{ |
100 |
psiconv_bool_false, /* on */ |
101 |
10.0, /* font_size */ |
102 |
0x95, /* character */ |
103 |
psiconv_bool_true, /* indent */ |
104 |
&black, /* color */ |
105 |
&font, /* font */ |
106 |
}; |
107 |
static struct psiconv_all_tabs_s tabs = |
108 |
{ |
109 |
0.64, /* normal */ |
110 |
NULL /* kind */ |
111 |
}; |
112 |
struct psiconv_paragraph_layout_s pl = |
113 |
{ |
114 |
&white, /* back_color */ |
115 |
0.0, /* indent_left */ |
116 |
0.0, /* indent_right */ |
117 |
0.0, /* indent_first */ |
118 |
psiconv_justify_left, /* justify_hor */ |
119 |
psiconv_justify_middle,/* justify_ver */ |
120 |
0.0, /* interline */ |
121 |
psiconv_bool_false, /* interline_exact */ |
122 |
0.0, /* top_space */ |
123 |
0.0, /* bottom_space */ |
124 |
psiconv_bool_false, /* on_one_page */ |
125 |
psiconv_bool_false, /* together_with */ |
126 |
psiconv_bool_false, /* on_next_page */ |
127 |
psiconv_bool_false, /* no_widow_protection */ |
128 |
0.0, /* left_margin */ |
129 |
&bullet, /* bullet */ |
130 |
&no_border, /* left_border */ |
131 |
&no_border, /* right_border */ |
132 |
&no_border, /* top_border */ |
133 |
&no_border, /* bottom_border */ |
134 |
&tabs, /* tabs */ |
135 |
}; |
136 |
psiconv_paragraph_layout res; |
137 |
|
138 |
if (!(pl.tabs->extras = psiconv_list_new(sizeof(struct psiconv_tab_s)))) |
139 |
return NULL; |
140 |
res = psiconv_clone_paragraph_layout(&pl); |
141 |
psiconv_list_free(pl.tabs->extras); |
142 |
return res; |
143 |
} |
144 |
|
145 |
psiconv_color clone_color(psiconv_color color) |
146 |
{ |
147 |
psiconv_color result; |
148 |
if (!(result = malloc(sizeof(*result)))) |
149 |
return NULL; |
150 |
*result = *color; |
151 |
return result; |
152 |
} |
153 |
|
154 |
psiconv_font clone_font(psiconv_font font) |
155 |
{ |
156 |
psiconv_font result; |
157 |
if(!(result = malloc(sizeof(*result)))) |
158 |
goto ERROR1; |
159 |
*result = *font; |
160 |
if (!(result->name = strdup(result->name))) |
161 |
goto ERROR2; |
162 |
return result; |
163 |
ERROR2: |
164 |
free(result); |
165 |
ERROR1: |
166 |
return NULL; |
167 |
} |
168 |
|
169 |
psiconv_border clone_border(psiconv_border border) |
170 |
{ |
171 |
psiconv_border result; |
172 |
if (!(result = malloc(sizeof(*result)))) |
173 |
goto ERROR1; |
174 |
*result = *border; |
175 |
if(!(result->color = clone_color(result->color))) |
176 |
goto ERROR2; |
177 |
return result; |
178 |
ERROR2: |
179 |
free(result); |
180 |
ERROR1: |
181 |
return NULL; |
182 |
} |
183 |
|
184 |
psiconv_bullet clone_bullet(psiconv_bullet bullet) |
185 |
{ |
186 |
psiconv_bullet result; |
187 |
if (!(result = malloc(sizeof(*result)))) |
188 |
goto ERROR1; |
189 |
*result = *bullet; |
190 |
if (!(result->font = clone_font(result->font))) |
191 |
goto ERROR2; |
192 |
if (!(result->color = clone_color(result->color))) |
193 |
goto ERROR3; |
194 |
return result; |
195 |
ERROR3: |
196 |
psiconv_free_font(result->font); |
197 |
ERROR2: |
198 |
free(result); |
199 |
ERROR1: |
200 |
return NULL; |
201 |
} |
202 |
|
203 |
psiconv_all_tabs clone_all_tabs(psiconv_all_tabs all_tabs) |
204 |
{ |
205 |
psiconv_all_tabs result; |
206 |
if (!(result = malloc(sizeof(*result)))) |
207 |
goto ERROR1; |
208 |
*result = *all_tabs; |
209 |
if (!(result->extras = psiconv_list_clone(result->extras))) |
210 |
goto ERROR2; |
211 |
return result; |
212 |
ERROR2: |
213 |
free(result); |
214 |
ERROR1: |
215 |
return NULL; |
216 |
} |
217 |
|
218 |
psiconv_character_layout psiconv_clone_character_layout |
219 |
(psiconv_character_layout ls) |
220 |
{ |
221 |
psiconv_character_layout result; |
222 |
|
223 |
if (!(result = malloc(sizeof(*result)))) |
224 |
goto ERROR1; |
225 |
*result = *ls; |
226 |
if (!(result->color = clone_color(result->color))) |
227 |
goto ERROR2; |
228 |
if (!(result->back_color = clone_color(result->back_color))) |
229 |
goto ERROR3; |
230 |
if (!(result->font = clone_font(result->font))) |
231 |
goto ERROR4; |
232 |
return result; |
233 |
ERROR4: |
234 |
psiconv_free_color(result->back_color); |
235 |
ERROR3: |
236 |
psiconv_free_color(result->color); |
237 |
ERROR2: |
238 |
free(result); |
239 |
ERROR1: |
240 |
return NULL; |
241 |
} |
242 |
|
243 |
psiconv_paragraph_layout psiconv_clone_paragraph_layout |
244 |
(psiconv_paragraph_layout ls) |
245 |
{ |
246 |
psiconv_paragraph_layout result; |
247 |
|
248 |
if (!(result = malloc(sizeof(*result)))) |
249 |
goto ERROR1; |
250 |
*result = *ls; |
251 |
if (!(result->back_color = clone_color(result->back_color))) |
252 |
goto ERROR2; |
253 |
if (!(result->bullet = clone_bullet(result->bullet))) |
254 |
goto ERROR3; |
255 |
if (!(result->left_border = clone_border(result->left_border))) |
256 |
goto ERROR4; |
257 |
if (!(result->right_border = clone_border(result->right_border))) |
258 |
goto ERROR5; |
259 |
if (!(result->top_border = clone_border(result->top_border))) |
260 |
goto ERROR6; |
261 |
if (!(result->bottom_border = clone_border(result->bottom_border))) |
262 |
goto ERROR7; |
263 |
if (!(result->tabs = clone_all_tabs(result->tabs))) |
264 |
goto ERROR8; |
265 |
return result; |
266 |
ERROR8: |
267 |
psiconv_free_border(result->bottom_border); |
268 |
ERROR7: |
269 |
psiconv_free_border(result->top_border); |
270 |
ERROR6: |
271 |
psiconv_free_border(result->right_border); |
272 |
ERROR5: |
273 |
psiconv_free_border(result->left_border); |
274 |
ERROR4: |
275 |
psiconv_free_bullet(result->bullet); |
276 |
ERROR3: |
277 |
psiconv_free_color(result->back_color); |
278 |
ERROR2: |
279 |
free(result); |
280 |
ERROR1: |
281 |
return NULL; |
282 |
} |
283 |
|
284 |
psiconv_word_style psiconv_get_style (psiconv_word_styles_section ss, int nr) |
285 |
{ |
286 |
if (nr == 0) |
287 |
return ss->normal; |
288 |
else |
289 |
return psiconv_list_get(ss->styles,0xff - nr); |
290 |
} |
291 |
|
292 |
void psiconv_free_color (psiconv_color color) |
293 |
{ |
294 |
if (color) |
295 |
free(color); |
296 |
} |
297 |
|
298 |
void psiconv_free_border(psiconv_border border) |
299 |
{ |
300 |
if (border) { |
301 |
psiconv_free_color(border->color); |
302 |
free(border); |
303 |
} |
304 |
} |
305 |
|
306 |
void psiconv_free_font(psiconv_font font) |
307 |
{ |
308 |
if (font) { |
309 |
if (font->name) |
310 |
free(font->name); |
311 |
free(font); |
312 |
} |
313 |
} |
314 |
|
315 |
void psiconv_free_bullet(psiconv_bullet bullet) |
316 |
{ |
317 |
if (bullet) { |
318 |
psiconv_free_color(bullet->color); |
319 |
psiconv_free_font(bullet->font); |
320 |
free(bullet); |
321 |
} |
322 |
} |
323 |
|
324 |
void psiconv_free_character_layout(psiconv_character_layout layout) |
325 |
{ |
326 |
if (layout) { |
327 |
psiconv_free_color(layout->color); |
328 |
psiconv_free_color(layout->back_color); |
329 |
psiconv_free_font(layout->font); |
330 |
free(layout); |
331 |
} |
332 |
} |
333 |
|
334 |
void psiconv_free_tab(psiconv_tab tab) |
335 |
{ |
336 |
if (tab) |
337 |
free(tab); |
338 |
} |
339 |
|
340 |
void psiconv_free_tabs(psiconv_all_tabs tabs) |
341 |
{ |
342 |
if (tabs) { |
343 |
psiconv_list_free(tabs->extras); |
344 |
free(tabs); |
345 |
} |
346 |
} |
347 |
|
348 |
void psiconv_free_paragraph_layout(psiconv_paragraph_layout layout) |
349 |
{ |
350 |
if (layout) { |
351 |
psiconv_free_color(layout->back_color); |
352 |
psiconv_free_bullet(layout->bullet); |
353 |
psiconv_free_border(layout->left_border); |
354 |
psiconv_free_border(layout->right_border); |
355 |
psiconv_free_border(layout->top_border); |
356 |
psiconv_free_border(layout->bottom_border); |
357 |
psiconv_free_tabs(layout->tabs); |
358 |
free(layout); |
359 |
} |
360 |
} |
361 |
|
362 |
void psiconv_free_style_aux(void *style) |
363 |
{ |
364 |
if(((psiconv_word_style) style)->name) |
365 |
free(((psiconv_word_style) style)->name); |
366 |
psiconv_free_character_layout(((psiconv_word_style) style)->character); |
367 |
psiconv_free_paragraph_layout(((psiconv_word_style) style)->paragraph); |
368 |
} |
369 |
|
370 |
void psiconv_free_word_style(psiconv_word_style style) |
371 |
{ |
372 |
if (style) { |
373 |
psiconv_free_style_aux(style); |
374 |
free(style); |
375 |
} |
376 |
} |
377 |
|
378 |
void psiconv_free_word_styles_section(psiconv_word_styles_section styles) |
379 |
{ |
380 |
if (styles) { |
381 |
psiconv_free_word_style(styles->normal); |
382 |
if (styles->styles) |
383 |
psiconv_list_free_el(styles->styles,psiconv_free_style_aux); |
384 |
} |
385 |
} |
386 |
|
387 |
void psiconv_free_header_section(psiconv_header_section header) |
388 |
{ |
389 |
if (header) |
390 |
free(header); |
391 |
} |
392 |
|
393 |
void psiconv_free_section_table_entry(psiconv_section_table_entry entry) |
394 |
{ |
395 |
if (entry) |
396 |
free(entry); |
397 |
} |
398 |
|
399 |
void psiconv_free_section_table_section(psiconv_section_table_section section) |
400 |
{ |
401 |
if (section) |
402 |
psiconv_list_free(section); |
403 |
} |
404 |
|
405 |
void psiconv_free_application_id_section(psiconv_application_id_section section) |
406 |
{ |
407 |
if (section) { |
408 |
if (section->name) |
409 |
free(section->name); |
410 |
free(section); |
411 |
} |
412 |
} |
413 |
|
414 |
void psiconv_free_in_line_layout_aux(void * layout) |
415 |
{ |
416 |
psiconv_free_character_layout(((psiconv_in_line_layout) layout)->layout); |
417 |
} |
418 |
|
419 |
void psiconv_free_in_line_layout(psiconv_in_line_layout layout) |
420 |
{ |
421 |
if (layout) { |
422 |
psiconv_free_in_line_layout_aux(layout); |
423 |
free(layout); |
424 |
} |
425 |
} |
426 |
|
427 |
void psiconv_free_in_line_layouts(psiconv_in_line_layouts layouts) |
428 |
{ |
429 |
if (layouts) |
430 |
psiconv_list_free_el(layouts,&psiconv_free_in_line_layout_aux); |
431 |
} |
432 |
|
433 |
void psiconv_free_replacement(psiconv_replacement replacement) |
434 |
{ |
435 |
if (replacement) |
436 |
free(replacement); |
437 |
} |
438 |
|
439 |
void psiconv_free_replacements(psiconv_replacements replacements) |
440 |
{ |
441 |
if (replacements) |
442 |
psiconv_list_free(replacements); |
443 |
} |
444 |
|
445 |
void psiconv_free_paragraph_aux(void * paragraph) |
446 |
{ |
447 |
if(((psiconv_paragraph) paragraph)->text) |
448 |
free(((psiconv_paragraph) paragraph)->text); |
449 |
psiconv_free_character_layout(((psiconv_paragraph) paragraph) |
450 |
->base_character); |
451 |
psiconv_free_paragraph_layout(((psiconv_paragraph) paragraph) |
452 |
->base_paragraph); |
453 |
psiconv_free_in_line_layouts(((psiconv_paragraph) paragraph) |
454 |
->in_lines); |
455 |
psiconv_free_replacements(((psiconv_paragraph) paragraph) |
456 |
->replacements); |
457 |
} |
458 |
|
459 |
void psiconv_free_paragraph(psiconv_paragraph paragraph) |
460 |
{ |
461 |
if (paragraph) { |
462 |
psiconv_free_paragraph_aux(paragraph); |
463 |
free(paragraph); |
464 |
} |
465 |
} |
466 |
|
467 |
void psiconv_free_text_and_layout(psiconv_text_and_layout text) |
468 |
{ |
469 |
if (text) |
470 |
psiconv_list_free_el(text,&psiconv_free_paragraph_aux); |
471 |
} |
472 |
|
473 |
void psiconv_free_texted_section(psiconv_texted_section section) |
474 |
{ |
475 |
if (section) { |
476 |
psiconv_free_text_and_layout(section->paragraphs); |
477 |
free(section); |
478 |
} |
479 |
} |
480 |
|
481 |
void psiconv_free_page_header(psiconv_page_header header) |
482 |
{ |
483 |
if (header) { |
484 |
psiconv_free_character_layout(header->base_character_layout); |
485 |
psiconv_free_paragraph_layout(header->base_paragraph_layout); |
486 |
psiconv_free_texted_section(header->text); |
487 |
free(header); |
488 |
} |
489 |
} |
490 |
|
491 |
void psiconv_free_page_layout_section(psiconv_page_layout_section section) |
492 |
{ |
493 |
if (section) { |
494 |
psiconv_free_page_header(section->header); |
495 |
psiconv_free_page_header(section->footer); |
496 |
free(section); |
497 |
} |
498 |
} |
499 |
|
500 |
void psiconv_free_word_status_section(psiconv_word_status_section section) |
501 |
{ |
502 |
if (section) |
503 |
free(section); |
504 |
} |
505 |
|
506 |
void psiconv_free_word_f(psiconv_word_f file) |
507 |
{ |
508 |
if (file) { |
509 |
psiconv_free_page_layout_section(file->page_sec); |
510 |
psiconv_free_text_and_layout(file->paragraphs); |
511 |
psiconv_free_word_status_section(file->status_sec); |
512 |
psiconv_free_word_styles_section(file->styles_sec); |
513 |
free(file); |
514 |
} |
515 |
} |
516 |
|
517 |
void psiconv_free_texted_f(psiconv_texted_f file) |
518 |
{ |
519 |
if (file) { |
520 |
psiconv_free_page_layout_section(file->page_sec); |
521 |
psiconv_free_texted_section(file->texted_sec); |
522 |
free(file); |
523 |
} |
524 |
} |
525 |
|
526 |
void psiconv_free_paint_data_section_aux(void * section) |
527 |
{ |
528 |
if (((psiconv_paint_data_section) section)->red) |
529 |
free(((psiconv_paint_data_section)section) -> red); |
530 |
if (((psiconv_paint_data_section) section)->green) |
531 |
free(((psiconv_paint_data_section)section) -> green); |
532 |
if (((psiconv_paint_data_section) section)->blue) |
533 |
free(((psiconv_paint_data_section)section) -> blue); |
534 |
} |
535 |
|
536 |
void psiconv_free_paint_data_section(psiconv_paint_data_section section) |
537 |
{ |
538 |
if (section) { |
539 |
psiconv_free_paint_data_section_aux(section); |
540 |
free(section); |
541 |
} |
542 |
} |
543 |
|
544 |
void psiconv_free_pictures(psiconv_pictures section) |
545 |
{ |
546 |
if (section) |
547 |
psiconv_list_free_el(section,&psiconv_free_paint_data_section_aux); |
548 |
} |
549 |
|
550 |
void psiconv_free_jumptable_section (psiconv_jumptable_section section) |
551 |
{ |
552 |
if (section) |
553 |
psiconv_list_free(section); |
554 |
} |
555 |
|
556 |
void psiconv_free_mbm_f(psiconv_mbm_f file) |
557 |
{ |
558 |
if (file) { |
559 |
psiconv_free_pictures(file->sections); |
560 |
free(file); |
561 |
} |
562 |
} |
563 |
|
564 |
void psiconv_free_sketch_section(psiconv_sketch_section sec) |
565 |
{ |
566 |
if (sec) { |
567 |
psiconv_free_paint_data_section(sec->picture); |
568 |
free(sec); |
569 |
} |
570 |
} |
571 |
|
572 |
void psiconv_free_sketch_f(psiconv_sketch_f file) |
573 |
{ |
574 |
if (file) { |
575 |
psiconv_free_sketch_section(file->sketch_sec); |
576 |
free(file); |
577 |
} |
578 |
} |
579 |
|
580 |
void psiconv_free_clipart_section_aux(void *section) |
581 |
{ |
582 |
if (section) |
583 |
free(((psiconv_clipart_section ) section)->picture); |
584 |
} |
585 |
|
586 |
void psiconv_free_clipart_section(psiconv_clipart_section section) |
587 |
{ |
588 |
if (section) { |
589 |
psiconv_free_clipart_section_aux(section); |
590 |
free(section); |
591 |
} |
592 |
} |
593 |
|
594 |
void psiconv_free_cliparts(psiconv_cliparts section) |
595 |
{ |
596 |
if (section) |
597 |
psiconv_list_free_el(section,&psiconv_free_clipart_section_aux); |
598 |
} |
599 |
|
600 |
void psiconv_free_clipart_f(psiconv_clipart_f file) |
601 |
{ |
602 |
if (file) { |
603 |
psiconv_free_cliparts(file->sections); |
604 |
free(file); |
605 |
} |
606 |
} |
607 |
|
608 |
void psiconv_free_file(psiconv_file file) |
609 |
{ |
610 |
if (file) { |
611 |
if (file->type == psiconv_word_file) |
612 |
psiconv_free_word_f((psiconv_word_f) file->file); |
613 |
else if (file->type == psiconv_texted_file) |
614 |
psiconv_free_texted_f((psiconv_texted_f) file->file); |
615 |
else if (file->type == psiconv_mbm_file) |
616 |
psiconv_free_mbm_f((psiconv_mbm_f) file->file); |
617 |
else if (file->type == psiconv_sketch_file) |
618 |
psiconv_free_sketch_f((psiconv_sketch_f) file->file); |
619 |
else if (file->type == psiconv_clipart_file) |
620 |
psiconv_free_clipart_f((psiconv_clipart_f) file->file); |
621 |
free(file); |
622 |
} |
623 |
} |