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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 171 - (hide annotations)
Wed Nov 26 20:56:17 2003 UTC (20 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 12031 byte(s)
(Frodo) Support MBM output format

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

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