1 |
/* |
2 |
generate_driver.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 |
|
23 |
#include <stdlib.h> |
24 |
|
25 |
#include "error.h" |
26 |
#include "generate_routines.h" |
27 |
|
28 |
#ifdef DMALLOC |
29 |
#include <dmalloc.h> |
30 |
#endif |
31 |
|
32 |
|
33 |
int psiconv_write(const psiconv_config config, psiconv_buffer *buf, |
34 |
const psiconv_file value) |
35 |
{ |
36 |
int res; |
37 |
|
38 |
if (!value) { |
39 |
psiconv_warn(config,0,0,"Can't parse to an empty buffer!"); |
40 |
return -PSICONV_E_OTHER; |
41 |
} |
42 |
if (!(*buf = psiconv_buffer_new())) |
43 |
return -PSICONV_E_NOMEM; |
44 |
|
45 |
if (value->type == psiconv_word_file) { |
46 |
if ((res = psiconv_write_header_section(config,*buf,PSICONV_ID_PSION5, |
47 |
PSICONV_ID_DATA_FILE, |
48 |
PSICONV_ID_WORD))) |
49 |
goto ERROR; |
50 |
if ((res =psiconv_write_word_file(config,*buf,(psiconv_word_f) (value->file)))) |
51 |
goto ERROR; |
52 |
} else if (value->type == psiconv_texted_file) { |
53 |
if ((res = psiconv_write_header_section(config,*buf,PSICONV_ID_PSION5, |
54 |
PSICONV_ID_DATA_FILE, |
55 |
PSICONV_ID_TEXTED))) |
56 |
goto ERROR; |
57 |
if ((res =psiconv_write_texted_file(config,*buf, |
58 |
(psiconv_texted_f) (value->file)))) |
59 |
goto ERROR; |
60 |
} else if (value->type == psiconv_sketch_file) { |
61 |
if ((res = psiconv_write_header_section(config,*buf,PSICONV_ID_PSION5, |
62 |
PSICONV_ID_DATA_FILE, |
63 |
PSICONV_ID_SKETCH))) |
64 |
goto ERROR; |
65 |
if ((res =psiconv_write_sketch_file(config,*buf, |
66 |
(psiconv_sketch_f) (value->file)))) |
67 |
goto ERROR; |
68 |
} else if (value->type == psiconv_mbm_file) { |
69 |
if ((res = psiconv_write_header_section(config,*buf,PSICONV_ID_PSION5, |
70 |
PSICONV_ID_MBM_FILE, |
71 |
0x00000000))) |
72 |
goto ERROR; |
73 |
if ((res =psiconv_write_mbm_file(config,*buf, |
74 |
(psiconv_mbm_f) (value->file)))) |
75 |
goto ERROR; |
76 |
} else if (value->type == psiconv_clipart_file) { |
77 |
/* No complete header section, so we do it all in the below function */ |
78 |
if ((res =psiconv_write_clipart_file(config,*buf, |
79 |
(psiconv_clipart_f) (value->file)))) |
80 |
goto ERROR; |
81 |
} else { |
82 |
psiconv_warn(config,0,0,"Unknown or unsupported file type"); |
83 |
res = -PSICONV_E_GENERATE; |
84 |
goto ERROR; |
85 |
} |
86 |
if ((res = psiconv_buffer_resolve(*buf))) |
87 |
goto ERROR; |
88 |
return -PSICONV_E_OK; |
89 |
|
90 |
ERROR: |
91 |
psiconv_buffer_free(*buf); |
92 |
return res; |
93 |
} |
94 |
|
95 |
int psiconv_write_texted_file(const psiconv_config config, |
96 |
psiconv_buffer buf,psiconv_texted_f value) |
97 |
{ |
98 |
psiconv_character_layout base_char; |
99 |
psiconv_paragraph_layout base_para; |
100 |
int res; |
101 |
psiconv_section_table_section section_table; |
102 |
psiconv_section_table_entry entry; |
103 |
psiconv_u32 section_table_id; |
104 |
psiconv_buffer buf_texted; |
105 |
|
106 |
if (!value) { |
107 |
psiconv_warn(config,0,0,"Null TextEd file"); |
108 |
return -PSICONV_E_GENERATE; |
109 |
} |
110 |
|
111 |
if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
112 |
res = -PSICONV_E_NOMEM; |
113 |
goto ERROR1; |
114 |
} |
115 |
|
116 |
if (!(entry = malloc(sizeof(*entry)))) { |
117 |
res = -PSICONV_E_NOMEM; |
118 |
goto ERROR2; |
119 |
} |
120 |
|
121 |
if (!(base_char = psiconv_basic_character_layout())) { |
122 |
res = -PSICONV_E_NOMEM; |
123 |
goto ERROR3; |
124 |
} |
125 |
if (!(base_para = psiconv_basic_paragraph_layout())) { |
126 |
res = -PSICONV_E_NOMEM; |
127 |
goto ERROR4; |
128 |
} |
129 |
|
130 |
section_table_id = psiconv_buffer_unique_id(); |
131 |
if ((res = psiconv_write_offset(config,buf,section_table_id))) |
132 |
goto ERROR5; |
133 |
|
134 |
entry->id = PSICONV_ID_APPL_ID_SECTION; |
135 |
entry->offset = psiconv_buffer_unique_id(); |
136 |
if ((res = psiconv_list_add(section_table,entry))) |
137 |
goto ERROR5; |
138 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
139 |
goto ERROR5; |
140 |
if ((res=psiconv_write_application_id_section(config,buf, |
141 |
PSICONV_ID_TEXTED,"TextEd.app"))) |
142 |
goto ERROR5; |
143 |
|
144 |
entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
145 |
entry->offset = psiconv_buffer_unique_id(); |
146 |
if ((res = psiconv_list_add(section_table,entry))) |
147 |
goto ERROR5; |
148 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
149 |
goto ERROR5; |
150 |
if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
151 |
goto ERROR5; |
152 |
|
153 |
entry->id = PSICONV_ID_TEXTED; |
154 |
entry->offset = psiconv_buffer_unique_id(); |
155 |
if ((res = psiconv_list_add(section_table,entry))) |
156 |
goto ERROR5; |
157 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
158 |
goto ERROR5; |
159 |
if ((res = psiconv_write_texted_section(config,buf,value->texted_sec, |
160 |
base_char,base_para,&buf_texted))) |
161 |
goto ERROR5; |
162 |
|
163 |
if ((res = psiconv_buffer_concat(buf,buf_texted))) |
164 |
goto ERROR6; |
165 |
|
166 |
|
167 |
if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
168 |
goto ERROR6; |
169 |
|
170 |
res = psiconv_write_section_table_section(config,buf,section_table); |
171 |
|
172 |
ERROR6: |
173 |
psiconv_buffer_free(buf_texted); |
174 |
ERROR5: |
175 |
psiconv_free_paragraph_layout(base_para); |
176 |
ERROR4: |
177 |
psiconv_free_character_layout(base_char); |
178 |
ERROR3: |
179 |
free(entry); |
180 |
ERROR2: |
181 |
psiconv_list_free(section_table); |
182 |
ERROR1: |
183 |
return res; |
184 |
} |
185 |
|
186 |
int psiconv_write_word_file(const psiconv_config config, |
187 |
psiconv_buffer buf,psiconv_word_f value) |
188 |
{ |
189 |
int res; |
190 |
psiconv_section_table_section section_table; |
191 |
psiconv_section_table_entry entry; |
192 |
psiconv_u32 section_table_id; |
193 |
|
194 |
if (!value) { |
195 |
psiconv_warn(config,0,0,"Null Word file"); |
196 |
return -PSICONV_E_GENERATE; |
197 |
} |
198 |
|
199 |
if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
200 |
res = -PSICONV_E_NOMEM; |
201 |
goto ERROR1; |
202 |
} |
203 |
|
204 |
if (!(entry = malloc(sizeof(*entry)))) { |
205 |
res = -PSICONV_E_NOMEM; |
206 |
goto ERROR2; |
207 |
} |
208 |
|
209 |
section_table_id = psiconv_buffer_unique_id(); |
210 |
if ((res = psiconv_write_offset(config,buf,section_table_id))) |
211 |
goto ERROR3; |
212 |
|
213 |
entry->id = PSICONV_ID_APPL_ID_SECTION; |
214 |
entry->offset = psiconv_buffer_unique_id(); |
215 |
if ((res = psiconv_list_add(section_table,entry))) |
216 |
goto ERROR3; |
217 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
218 |
goto ERROR3; |
219 |
if ((res=psiconv_write_application_id_section(config,buf, |
220 |
PSICONV_ID_WORD,"Word.app"))) |
221 |
goto ERROR3; |
222 |
|
223 |
entry->id = PSICONV_ID_WORD_STATUS_SECTION; |
224 |
entry->offset = psiconv_buffer_unique_id(); |
225 |
if ((res = psiconv_list_add(section_table,entry))) |
226 |
goto ERROR3; |
227 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
228 |
goto ERROR3; |
229 |
if ((res = psiconv_write_word_status_section(config,buf,value->status_sec))) |
230 |
goto ERROR3; |
231 |
|
232 |
entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
233 |
entry->offset = psiconv_buffer_unique_id(); |
234 |
if ((res = psiconv_list_add(section_table,entry))) |
235 |
goto ERROR3; |
236 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
237 |
goto ERROR3; |
238 |
if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
239 |
goto ERROR3; |
240 |
|
241 |
entry->id = PSICONV_ID_WORD_STYLES_SECTION; |
242 |
entry->offset = psiconv_buffer_unique_id(); |
243 |
if ((res = psiconv_list_add(section_table,entry))) |
244 |
goto ERROR3; |
245 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
246 |
goto ERROR3; |
247 |
if ((res = psiconv_write_word_styles_section(config,buf,value->styles_sec))) |
248 |
goto ERROR3; |
249 |
|
250 |
entry->id = PSICONV_ID_TEXT_SECTION; |
251 |
entry->offset = psiconv_buffer_unique_id(); |
252 |
if ((res = psiconv_list_add(section_table,entry))) |
253 |
goto ERROR3; |
254 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
255 |
goto ERROR3; |
256 |
if ((res = psiconv_write_text_section(config,buf,value->paragraphs))) |
257 |
goto ERROR3; |
258 |
|
259 |
entry->id = PSICONV_ID_LAYOUT_SECTION; |
260 |
entry->offset = psiconv_buffer_unique_id(); |
261 |
if ((res = psiconv_list_add(section_table,entry))) |
262 |
goto ERROR3; |
263 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
264 |
goto ERROR3; |
265 |
if ((res = psiconv_write_styled_layout_section(config,buf,value->paragraphs, |
266 |
value->styles_sec))) |
267 |
goto ERROR3; |
268 |
|
269 |
if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
270 |
goto ERROR3; |
271 |
|
272 |
res = psiconv_write_section_table_section(config,buf,section_table); |
273 |
|
274 |
ERROR3: |
275 |
free(entry); |
276 |
ERROR2: |
277 |
psiconv_list_free(section_table); |
278 |
ERROR1: |
279 |
return res; |
280 |
} |
281 |
|
282 |
int psiconv_write_sketch_file(const psiconv_config config, |
283 |
psiconv_buffer buf,psiconv_sketch_f value) |
284 |
{ |
285 |
int res; |
286 |
psiconv_section_table_section section_table; |
287 |
psiconv_section_table_entry entry; |
288 |
psiconv_u32 section_table_id; |
289 |
|
290 |
if (!value) { |
291 |
psiconv_warn(config,0,0,"Null Sketch file"); |
292 |
return -PSICONV_E_GENERATE; |
293 |
} |
294 |
|
295 |
if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
296 |
res = -PSICONV_E_NOMEM; |
297 |
goto ERROR1; |
298 |
} |
299 |
|
300 |
if (!(entry = malloc(sizeof(*entry)))) { |
301 |
res = -PSICONV_E_NOMEM; |
302 |
goto ERROR2; |
303 |
} |
304 |
|
305 |
section_table_id = psiconv_buffer_unique_id(); |
306 |
if ((res = psiconv_write_offset(config,buf,section_table_id))) |
307 |
goto ERROR3; |
308 |
|
309 |
entry->id = PSICONV_ID_APPL_ID_SECTION; |
310 |
entry->offset = psiconv_buffer_unique_id(); |
311 |
if ((res = psiconv_list_add(section_table,entry))) |
312 |
goto ERROR3; |
313 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
314 |
goto ERROR3; |
315 |
if ((res=psiconv_write_application_id_section(config,buf, |
316 |
PSICONV_ID_SKETCH,"Paint.app"))) |
317 |
goto ERROR3; |
318 |
|
319 |
entry->id = PSICONV_ID_SKETCH_SECTION; |
320 |
entry->offset = psiconv_buffer_unique_id(); |
321 |
if ((res = psiconv_list_add(section_table,entry))) |
322 |
goto ERROR3; |
323 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
324 |
goto ERROR3; |
325 |
if ((res = psiconv_write_sketch_section(config,buf,value->sketch_sec))) |
326 |
goto ERROR3; |
327 |
|
328 |
if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
329 |
goto ERROR3; |
330 |
res = psiconv_write_section_table_section(config,buf,section_table); |
331 |
|
332 |
ERROR3: |
333 |
free(entry); |
334 |
ERROR2: |
335 |
psiconv_list_free(section_table); |
336 |
ERROR1: |
337 |
return res; |
338 |
} |
339 |
|
340 |
int psiconv_write_mbm_file(const psiconv_config config, |
341 |
psiconv_buffer buf,psiconv_mbm_f value) |
342 |
{ |
343 |
int res,i; |
344 |
psiconv_jumptable_section jumptable; |
345 |
psiconv_u32 *entry,id,table_id; |
346 |
psiconv_paint_data_section section; |
347 |
|
348 |
if (!value) { |
349 |
psiconv_warn(config,0,0,"Null MBM file"); |
350 |
return -PSICONV_E_GENERATE; |
351 |
} |
352 |
|
353 |
if (!(jumptable = psiconv_list_new(sizeof(*entry)))) { |
354 |
res = -PSICONV_E_NOMEM; |
355 |
goto ERROR1; |
356 |
} |
357 |
|
358 |
table_id = psiconv_buffer_unique_id(); |
359 |
if ((res = psiconv_buffer_add_reference(buf,table_id))) |
360 |
goto ERROR2; |
361 |
|
362 |
for (i = 0; i < psiconv_list_length(value->sections); i++) { |
363 |
if (!(section = psiconv_list_get(value->sections,i))) { |
364 |
psiconv_warn(config,0,0,"Massive memory corruption"); |
365 |
res = -PSICONV_E_NOMEM; |
366 |
goto ERROR2; |
367 |
} |
368 |
id = psiconv_buffer_unique_id(); |
369 |
psiconv_list_add(jumptable,&id); |
370 |
if ((res = psiconv_buffer_add_target(buf,id))) |
371 |
goto ERROR2; |
372 |
if ((res = psiconv_write_paint_data_section(config,buf,section,0))) |
373 |
goto ERROR2; |
374 |
} |
375 |
|
376 |
if ((res = psiconv_buffer_add_target(buf,table_id))) |
377 |
goto ERROR2; |
378 |
if ((res = psiconv_write_jumptable_section(config,buf,jumptable))) |
379 |
goto ERROR2; |
380 |
|
381 |
|
382 |
ERROR2: |
383 |
psiconv_list_free(jumptable); |
384 |
ERROR1: |
385 |
return res; |
386 |
} |
387 |
|
388 |
/* Note: this file is special, because it does not have a complete header! */ |
389 |
int psiconv_write_clipart_file(const psiconv_config config, |
390 |
psiconv_buffer buf,psiconv_clipart_f value) |
391 |
{ |
392 |
int res,i; |
393 |
psiconv_jumptable_section jumptable; |
394 |
psiconv_u32 *entry,id; |
395 |
psiconv_clipart_section section; |
396 |
psiconv_buffer sec_buf; |
397 |
|
398 |
if (!value) { |
399 |
psiconv_warn(config,0,0,"Null Clipart file"); |
400 |
return -PSICONV_E_GENERATE; |
401 |
} |
402 |
|
403 |
if (!(jumptable = psiconv_list_new(sizeof(*entry)))) { |
404 |
res = -PSICONV_E_NOMEM; |
405 |
goto ERROR1; |
406 |
} |
407 |
|
408 |
if (!(sec_buf = psiconv_buffer_new())) { |
409 |
res = -PSICONV_E_NOMEM; |
410 |
goto ERROR2; |
411 |
} |
412 |
|
413 |
if ((res = psiconv_write_u32(config,buf,PSICONV_ID_CLIPART))) |
414 |
goto ERROR3; |
415 |
|
416 |
for (i = 0; i < psiconv_list_length(value->sections); i++) { |
417 |
if (!(section = psiconv_list_get(value->sections,i))) { |
418 |
psiconv_warn(config,0,0,"Massive memory corruption"); |
419 |
res = -PSICONV_E_NOMEM; |
420 |
goto ERROR3; |
421 |
} |
422 |
id = psiconv_buffer_unique_id(); |
423 |
psiconv_list_add(jumptable,&id); |
424 |
if ((res = psiconv_buffer_add_target(sec_buf,id))) |
425 |
goto ERROR3; |
426 |
if ((res = psiconv_write_clipart_section(config,sec_buf, section))) |
427 |
goto ERROR3; |
428 |
} |
429 |
|
430 |
if ((res = psiconv_write_jumptable_section(config,buf,jumptable))) |
431 |
goto ERROR3; |
432 |
|
433 |
if ((res = psiconv_buffer_concat(buf,sec_buf))) |
434 |
goto ERROR3; |
435 |
|
436 |
|
437 |
ERROR3: |
438 |
psiconv_buffer_free(sec_buf); |
439 |
ERROR2: |
440 |
psiconv_list_free(jumptable); |
441 |
ERROR1: |
442 |
return res; |
443 |
} |