1 |
/* |
2 |
data.c - Part of psiconv, a PSION 5 file formats converter |
3 |
Copyright (c) 1999 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 <stdlib.h> |
22 |
#include <string.h> |
23 |
#include "data.h" |
24 |
#include "list.h" |
25 |
#include "general.h" |
26 |
|
27 |
static psiconv_color clone_color(psiconv_color color); |
28 |
static psiconv_font clone_font(psiconv_font font); |
29 |
static psiconv_border clone_border(psiconv_border border); |
30 |
static psiconv_bullet clone_bullet(psiconv_bullet bullet); |
31 |
static psiconv_all_tabs clone_all_tabs(psiconv_all_tabs all_tabs); |
32 |
static void psiconv_free_style_aux(void *style); |
33 |
static void psiconv_free_in_line_layout_aux(void * layout); |
34 |
static void psiconv_free_paragraph_aux(void * paragraph); |
35 |
|
36 |
psiconv_character_layout psiconv_basic_character_layout(void) |
37 |
{ |
38 |
struct psiconv_color black = |
39 |
{ |
40 |
0x00, /* red */ |
41 |
0x00, /* green */ |
42 |
0x00, /* blue */ |
43 |
}; |
44 |
struct psiconv_color white = |
45 |
{ |
46 |
0xff, /* red */ |
47 |
0xff, /* green */ |
48 |
0xff, /* blue */ |
49 |
}; |
50 |
struct psiconv_character_layout cl = |
51 |
{ |
52 |
&black, /* color */ |
53 |
&white, /* back_color */ |
54 |
10.0, /* font_size */ |
55 |
psiconv_bool_false, /* italic */ |
56 |
psiconv_bool_false, /* bold */ |
57 |
psiconv_normalscript, /* super_sub */ |
58 |
psiconv_bool_false, /* underline */ |
59 |
psiconv_bool_false, /* strike_out */ |
60 |
NULL, /* font */ |
61 |
}; |
62 |
|
63 |
cl.color = malloc(sizeof(*cl.color)); |
64 |
cl.color->red = 0x00; |
65 |
cl.color->green = 0x00; |
66 |
cl.color->blue = 0x00; |
67 |
cl.back_color = malloc(sizeof(*cl.color)); |
68 |
cl.back_color->red = 0xff; |
69 |
cl.back_color->green = 0xff; |
70 |
cl.back_color->blue = 0xff; |
71 |
cl.font = malloc(sizeof(*cl.font)); |
72 |
cl.font->name = strdup("Times New Roman"); |
73 |
cl.font->screenfont = 3; |
74 |
return psiconv_clone_character_layout(&cl); |
75 |
} |
76 |
|
77 |
psiconv_paragraph_layout psiconv_basic_paragraph_layout(void) |
78 |
{ |
79 |
char base_font[] = "Times New Roman"; |
80 |
|
81 |
struct psiconv_font font = |
82 |
{ |
83 |
base_font, /* name */ |
84 |
2 /* screenfont */ |
85 |
}; |
86 |
struct psiconv_color black = |
87 |
{ |
88 |
0x00, /* red */ |
89 |
0x00, /* green */ |
90 |
0x00, /* blue */ |
91 |
}; |
92 |
struct psiconv_color white = |
93 |
{ |
94 |
0xff, /* red */ |
95 |
0xff, /* green */ |
96 |
0xff, /* blue */ |
97 |
}; |
98 |
struct psiconv_border no_border = |
99 |
{ |
100 |
psiconv_border_none, /* kind */ |
101 |
1, /* thickness */ |
102 |
&black /* color */ |
103 |
}; |
104 |
struct psiconv_bullet bullet = |
105 |
{ |
106 |
psiconv_bool_false, /* on */ |
107 |
10.0, /* font_size */ |
108 |
0x95, /* character */ |
109 |
psiconv_bool_true, /* indent */ |
110 |
&black, /* color */ |
111 |
&font, /* font */ |
112 |
}; |
113 |
struct psiconv_all_tabs tabs = |
114 |
{ |
115 |
0.64, /* normal */ |
116 |
NULL /* kind */ |
117 |
}; |
118 |
struct psiconv_paragraph_layout pl = |
119 |
{ |
120 |
&white, /* back_color */ |
121 |
0.0, /* indent_left */ |
122 |
0.0, /* indent_right */ |
123 |
0.0, /* indent_first */ |
124 |
psiconv_justify_left, /* justify_hor */ |
125 |
psiconv_justify_middle,/* justify_ver */ |
126 |
0.0, /* interline */ |
127 |
psiconv_bool_false, /* interline_exact */ |
128 |
0.0, /* top_space */ |
129 |
0.0, /* bottom_space */ |
130 |
psiconv_bool_false, /* on_one_page */ |
131 |
psiconv_bool_false, /* together_with */ |
132 |
psiconv_bool_false, /* on_next_page */ |
133 |
psiconv_bool_false, /* no_widow_protection */ |
134 |
0.0, /* left_margin */ |
135 |
&bullet, /* bullet */ |
136 |
&no_border, /* left_border */ |
137 |
&no_border, /* right_border */ |
138 |
&no_border, /* top_border */ |
139 |
&no_border, /* bottom_border */ |
140 |
&tabs, /* tabs */ |
141 |
}; |
142 |
psiconv_paragraph_layout res; |
143 |
|
144 |
pl.tabs->extras = psiconv_list_new(sizeof(struct psiconv_tab)); |
145 |
res = psiconv_clone_paragraph_layout(&pl); |
146 |
psiconv_list_free(pl.tabs->extras); |
147 |
return res; |
148 |
} |
149 |
|
150 |
psiconv_color clone_color(psiconv_color color) |
151 |
{ |
152 |
psiconv_color result; |
153 |
result = malloc(sizeof(*result)); |
154 |
*result = *color; |
155 |
return result; |
156 |
} |
157 |
|
158 |
psiconv_font clone_font(psiconv_font font) |
159 |
{ |
160 |
psiconv_font result; |
161 |
result = malloc(sizeof(*result)); |
162 |
*result = *font; |
163 |
result->name = strdup(result->name); |
164 |
return result; |
165 |
} |
166 |
|
167 |
psiconv_border clone_border(psiconv_border border) |
168 |
{ |
169 |
psiconv_border result; |
170 |
result = malloc(sizeof(*result)); |
171 |
*result = *border; |
172 |
result->color = clone_color(result->color); |
173 |
return result; |
174 |
} |
175 |
|
176 |
psiconv_bullet clone_bullet(psiconv_bullet bullet) |
177 |
{ |
178 |
psiconv_bullet result; |
179 |
result = malloc(sizeof(*result)); |
180 |
*result = *bullet; |
181 |
result->font = clone_font(result->font); |
182 |
result->color = clone_color(result->color); |
183 |
return result; |
184 |
} |
185 |
|
186 |
psiconv_all_tabs clone_all_tabs(psiconv_all_tabs all_tabs) |
187 |
{ |
188 |
psiconv_all_tabs result; |
189 |
result = malloc(sizeof(*result)); |
190 |
*result = *all_tabs; |
191 |
result->extras = psiconv_list_clone(result->extras); |
192 |
return result; |
193 |
} |
194 |
|
195 |
psiconv_character_layout psiconv_clone_character_layout |
196 |
(psiconv_character_layout ls) |
197 |
{ |
198 |
psiconv_character_layout result; |
199 |
|
200 |
result = malloc(sizeof(*result)); |
201 |
*result = *ls; |
202 |
result->color = clone_color(result->color); |
203 |
result->back_color = clone_color(result->back_color); |
204 |
result->font = clone_font(result->font); |
205 |
return result; |
206 |
} |
207 |
|
208 |
psiconv_paragraph_layout psiconv_clone_paragraph_layout |
209 |
(psiconv_paragraph_layout ls) |
210 |
{ |
211 |
psiconv_paragraph_layout result; |
212 |
|
213 |
result = malloc(sizeof(*result)); |
214 |
*result = *ls; |
215 |
result->back_color = clone_color(result->back_color); |
216 |
result->bullet = clone_bullet(result->bullet); |
217 |
result->left_border = clone_border(result->left_border); |
218 |
result->right_border = clone_border(result->right_border); |
219 |
result->top_border = clone_border(result->top_border); |
220 |
result->bottom_border = clone_border(result->bottom_border); |
221 |
result->tabs = clone_all_tabs(result->tabs); |
222 |
return result; |
223 |
} |
224 |
|
225 |
psiconv_word_style psiconv_get_style (psiconv_word_styles_section ss, int nr) |
226 |
{ |
227 |
if (nr == 0) |
228 |
return ss->normal; |
229 |
else |
230 |
return psiconv_list_get(ss->styles,0xff - nr); |
231 |
} |
232 |
|
233 |
void psiconv_free_color (psiconv_color color) |
234 |
{ |
235 |
if (color) |
236 |
free(color); |
237 |
} |
238 |
|
239 |
void psiconv_free_border(psiconv_border border) |
240 |
{ |
241 |
if (border) { |
242 |
psiconv_free_color(border->color); |
243 |
free(border); |
244 |
} |
245 |
} |
246 |
|
247 |
void psiconv_free_font(psiconv_font font) |
248 |
{ |
249 |
if (font) { |
250 |
if (font->name) |
251 |
free(font->name); |
252 |
free(font); |
253 |
} |
254 |
} |
255 |
|
256 |
void psiconv_free_bullet(psiconv_bullet bullet) |
257 |
{ |
258 |
if (bullet) { |
259 |
psiconv_free_color(bullet->color); |
260 |
psiconv_free_font(bullet->font); |
261 |
free(bullet); |
262 |
} |
263 |
} |
264 |
|
265 |
void psiconv_free_character_layout(psiconv_character_layout layout) |
266 |
{ |
267 |
if (layout) { |
268 |
psiconv_free_color(layout->color); |
269 |
psiconv_free_color(layout->back_color); |
270 |
psiconv_free_font(layout->font); |
271 |
free(layout); |
272 |
} |
273 |
} |
274 |
|
275 |
void psiconv_free_tab(psiconv_tab tab) |
276 |
{ |
277 |
if (tab) |
278 |
free(tab); |
279 |
} |
280 |
|
281 |
void psiconv_free_tabs(psiconv_all_tabs tabs) |
282 |
{ |
283 |
if (tabs) { |
284 |
psiconv_list_free(tabs->extras); |
285 |
free(tabs); |
286 |
} |
287 |
} |
288 |
|
289 |
void psiconv_free_paragraph_layout(psiconv_paragraph_layout layout) |
290 |
{ |
291 |
if (layout) { |
292 |
psiconv_free_color(layout->back_color); |
293 |
psiconv_free_bullet(layout->bullet); |
294 |
psiconv_free_border(layout->left_border); |
295 |
psiconv_free_border(layout->right_border); |
296 |
psiconv_free_border(layout->top_border); |
297 |
psiconv_free_border(layout->bottom_border); |
298 |
psiconv_free_tabs(layout->tabs); |
299 |
free(layout); |
300 |
} |
301 |
} |
302 |
|
303 |
void psiconv_free_style_aux(void *style) |
304 |
{ |
305 |
if(((psiconv_word_style) style)->name) |
306 |
free(((psiconv_word_style) style)->name); |
307 |
psiconv_free_character_layout(((psiconv_word_style) style)->character); |
308 |
psiconv_free_paragraph_layout(((psiconv_word_style) style)->paragraph); |
309 |
} |
310 |
|
311 |
void psiconv_free_word_style(psiconv_word_style style) |
312 |
{ |
313 |
if (style) { |
314 |
psiconv_free_style_aux(style); |
315 |
free(style); |
316 |
} |
317 |
} |
318 |
|
319 |
void psiconv_free_word_styles_section(psiconv_word_styles_section styles) |
320 |
{ |
321 |
if (styles) { |
322 |
psiconv_free_word_style(styles->normal); |
323 |
if (styles->styles) |
324 |
psiconv_list_free_el(styles->styles,psiconv_free_style_aux); |
325 |
} |
326 |
} |
327 |
|
328 |
void psiconv_free_header_section(psiconv_header_section header) |
329 |
{ |
330 |
if (header) |
331 |
free(header); |
332 |
} |
333 |
|
334 |
void psiconv_free_section_table_entry(psiconv_section_table_entry entry) |
335 |
{ |
336 |
if (entry) |
337 |
free(entry); |
338 |
} |
339 |
|
340 |
void psiconv_free_section_table_section(psiconv_section_table_section section) |
341 |
{ |
342 |
if (section) |
343 |
psiconv_list_free(section); |
344 |
} |
345 |
|
346 |
void psiconv_free_application_id_section(psiconv_application_id_section section) |
347 |
{ |
348 |
if (section) { |
349 |
if (section->name) |
350 |
free(section->name); |
351 |
free(section); |
352 |
} |
353 |
} |
354 |
|
355 |
void psiconv_free_in_line_layout_aux(void * layout) |
356 |
{ |
357 |
psiconv_free_character_layout(((psiconv_in_line_layout) layout)->layout); |
358 |
} |
359 |
|
360 |
void psiconv_free_in_line_layout(psiconv_in_line_layout layout) |
361 |
{ |
362 |
if (layout) { |
363 |
psiconv_free_in_line_layout_aux(layout); |
364 |
free(layout); |
365 |
} |
366 |
} |
367 |
|
368 |
void psiconv_free_in_line_layouts(psiconv_in_line_layouts layouts) |
369 |
{ |
370 |
if (layouts) |
371 |
psiconv_list_free_el(layouts,&psiconv_free_in_line_layout_aux); |
372 |
} |
373 |
|
374 |
void psiconv_free_replacement(psiconv_replacement replacement) |
375 |
{ |
376 |
if (replacement) |
377 |
free(replacement); |
378 |
} |
379 |
|
380 |
void psiconv_free_replacements(psiconv_replacements replacements) |
381 |
{ |
382 |
if (replacements) |
383 |
psiconv_list_free(replacements); |
384 |
} |
385 |
|
386 |
void psiconv_free_paragraph_aux(void * paragraph) |
387 |
{ |
388 |
if(((psiconv_paragraph) paragraph)->text) |
389 |
free(((psiconv_paragraph) paragraph)->text); |
390 |
psiconv_free_character_layout(((psiconv_paragraph) paragraph) |
391 |
->base_character); |
392 |
psiconv_free_paragraph_layout(((psiconv_paragraph) paragraph) |
393 |
->base_paragraph); |
394 |
psiconv_free_in_line_layouts(((psiconv_paragraph) paragraph) |
395 |
->in_lines); |
396 |
psiconv_free_replacements(((psiconv_paragraph) paragraph) |
397 |
->replacements); |
398 |
} |
399 |
|
400 |
void psiconv_free_paragraph(psiconv_paragraph paragraph) |
401 |
{ |
402 |
if (paragraph) { |
403 |
psiconv_free_paragraph_aux(paragraph); |
404 |
free(paragraph); |
405 |
} |
406 |
} |
407 |
|
408 |
void psiconv_free_text_and_layout(psiconv_text_and_layout text) |
409 |
{ |
410 |
if (text) |
411 |
psiconv_list_free_el(text,&psiconv_free_paragraph_aux); |
412 |
} |
413 |
|
414 |
void psiconv_free_texted_section(psiconv_texted_section section) |
415 |
{ |
416 |
if (section) { |
417 |
psiconv_free_text_and_layout(section->paragraphs); |
418 |
free(section); |
419 |
} |
420 |
} |
421 |
|
422 |
void psiconv_free_page_header(psiconv_page_header header) |
423 |
{ |
424 |
if (header) { |
425 |
psiconv_free_character_layout(header->base_character_layout); |
426 |
psiconv_free_paragraph_layout(header->base_paragraph_layout); |
427 |
psiconv_free_texted_section(header->text); |
428 |
free(header); |
429 |
} |
430 |
} |
431 |
|
432 |
void psiconv_free_page_layout_section(psiconv_page_layout_section section) |
433 |
{ |
434 |
if (section) { |
435 |
psiconv_free_page_header(section->header); |
436 |
psiconv_free_page_header(section->footer); |
437 |
free(section); |
438 |
} |
439 |
} |
440 |
|
441 |
void psiconv_free_word_status_section(psiconv_word_status_section section) |
442 |
{ |
443 |
if (section) |
444 |
free(section); |
445 |
} |
446 |
|
447 |
void psiconv_free_word_f(psiconv_word_f file) |
448 |
{ |
449 |
if (file) { |
450 |
psiconv_free_page_layout_section(file->page_sec); |
451 |
psiconv_free_text_and_layout(file->paragraphs); |
452 |
psiconv_free_word_status_section(file->status_sec); |
453 |
psiconv_free_word_styles_section(file->styles_sec); |
454 |
free(file); |
455 |
} |
456 |
} |
457 |
|
458 |
void psiconv_free_file(psiconv_file file) |
459 |
{ |
460 |
if (file) { |
461 |
if (file->type == psiconv_word_file) |
462 |
psiconv_free_word_f((psiconv_word_f) file->file); |
463 |
free(file); |
464 |
} |
465 |
} |