/[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 231 - (hide annotations)
Wed Feb 25 20:57:10 2004 UTC (20 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 15233 byte(s)
(Frodo) Progress information while generating update

Much better progress information now, and better error reporting.

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

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