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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 76 Revision 174
23#include <stdlib.h> 23#include <stdlib.h>
24 24
25#include "error.h" 25#include "error.h"
26#include "generate_routines.h" 26#include "generate_routines.h"
27 27
28#ifdef DMALLOC
29#include <dmalloc.h>
30#endif
28 31
29int psiconv_write(psiconv_buffer *buf,const psiconv_file value) 32
33int psiconv_write(const psiconv_config config, psiconv_buffer *buf,
34 const psiconv_file value)
30{ 35{
31 int res; 36 int res;
32 37
33 if (!value) { 38 if (!value) {
34 psiconv_warn(0,0,"Can't parse to an empty buffer!"); 39 psiconv_warn(config,0,0,"Can't parse to an empty buffer!");
35 return -PSICONV_E_OTHER; 40 return -PSICONV_E_OTHER;
36 } 41 }
37 if (!(*buf = psiconv_new_buffer())) 42 if (!(*buf = psiconv_buffer_new()))
38 return -PSICONV_E_NOMEM; 43 return -PSICONV_E_NOMEM;
39 44
40#if 0
41 if (value->type == psiconv_word_file) { 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;
42 if ((res =psiconv_write_word_file(*buf,(psiconv_word_f) (value->file)))) 50 if ((res =psiconv_write_word_file(config,*buf,(psiconv_word_f) (value->file))))
43 goto ERROR; 51 goto ERROR;
44 } else
45#endif
46 if (value->type == psiconv_texted_file) { 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;
47 if ((res =psiconv_write_texted_file(*buf, 57 if ((res =psiconv_write_texted_file(config,*buf,
48 (psiconv_texted_f) (value->file)))) 58 (psiconv_texted_f) (value->file))))
49 goto ERROR; 59 goto ERROR;
50#if 0
51 } else if (value->type == psiconv_sketch_file) { 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;
52 if ((res =psiconv_write_texted_file(*buf, 65 if ((res =psiconv_write_sketch_file(config,*buf,
53 (psiconv_sketch_f) (value->file)))) 66 (psiconv_sketch_f) (value->file))))
54 goto ERROR; 67 goto ERROR;
55 } else if (value->type == psiconv_mbm_file) { 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;
56 if ((res =psiconv_write_texted_file(*buf, 73 if ((res =psiconv_write_mbm_file(config,*buf,
57 (psiconv_mbm_f) (value->file)))) 74 (psiconv_mbm_f) (value->file))))
58 goto ERROR; 75 goto ERROR;
59 } else if (value->type == psiconv_clipart_file) { 76 } else if (value->type == psiconv_clipart_file) {
77 /* No complete header section, so we do it all in the below function */
60 if ((res =psiconv_write_texted_file(*buf, 78 if ((res =psiconv_write_clipart_file(config,*buf,
61 (psiconv_clipart_f) (value->file)))) 79 (psiconv_clipart_f) (value->file))))
62 goto ERROR; 80 goto ERROR;
63#endif
64 } else { 81 } else {
65 psiconv_warn(0,0,"Unknown or unsupported file type"); 82 psiconv_warn(config,0,0,"Unknown or unsupported file type");
66 res = -PSICONV_E_GENERATE; 83 res = -PSICONV_E_GENERATE;
67 goto ERROR; 84 goto ERROR;
68 } 85 }
86 if ((res = psiconv_buffer_resolve(*buf)))
87 goto ERROR;
69 return -PSICONV_E_OK; 88 return -PSICONV_E_OK;
70 89
71ERROR: 90ERROR:
72 psiconv_free_buffer(*buf); 91 psiconv_buffer_free(*buf);
73 return res; 92 return res;
74} 93}
75 94
76int psiconv_write_texted_file(psiconv_buffer buf,psiconv_texted_f value) 95int psiconv_write_texted_file(const psiconv_config config,
96 psiconv_buffer buf,psiconv_texted_f value)
77{ 97{
78 psiconv_character_layout base_char; 98 psiconv_character_layout base_char;
79 psiconv_paragraph_layout base_para; 99 psiconv_paragraph_layout base_para;
80 int res; 100 int res;
81 psiconv_buffer extra_buf;
82 psiconv_section_table_section section_table; 101 psiconv_section_table_section section_table;
83 psiconv_section_table_entry entry; 102 psiconv_section_table_entry entry;
103 psiconv_u32 section_table_id;
104 psiconv_buffer buf_texted;
84 105
85 if (!value) { 106 if (!value) {
86 psiconv_warn(0,0,"Null TextEd file"); 107 psiconv_warn(config,0,0,"Null TextEd file");
87 return -PSICONV_E_GENERATE; 108 return -PSICONV_E_GENERATE;
88 } 109 }
89 110
90 if (!(section_table = psiconv_list_new(sizeof(*entry)))) { 111 if (!(section_table = psiconv_list_new(sizeof(*entry)))) {
91 res = -PSICONV_E_NOMEM; 112 res = -PSICONV_E_NOMEM;
95 if (!(entry = malloc(sizeof(*entry)))) { 116 if (!(entry = malloc(sizeof(*entry)))) {
96 res = -PSICONV_E_NOMEM; 117 res = -PSICONV_E_NOMEM;
97 goto ERROR2; 118 goto ERROR2;
98 } 119 }
99 120
100 if (!(extra_buf = psiconv_new_buffer())) {
101 res = -PSICONV_E_NOMEM;
102 goto ERROR3;
103 }
104
105 if (!(base_char = psiconv_basic_character_layout())) { 121 if (!(base_char = psiconv_basic_character_layout())) {
106 res = -PSICONV_E_NOMEM; 122 res = -PSICONV_E_NOMEM;
123 goto ERROR3;
124 }
125 if (!(base_para = psiconv_basic_paragraph_layout())) {
126 res = -PSICONV_E_NOMEM;
107 goto ERROR4; 127 goto ERROR4;
108 } 128 }
109 if (!(base_para = psiconv_basic_paragraph_layout())) {
110 res = -PSICONV_E_NOMEM;
111 goto ERROR5;
112 }
113 129
114 if ((res = psiconv_write_header_section(buf,PSICONV_ID_PSION5, 130 section_table_id = psiconv_buffer_unique_id();
115 PSICONV_ID_DATA_FILE, 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,
116 PSICONV_ID_TEXTED))) 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)))
117 goto ERROR6; 164 goto ERROR6;
118 165
119 166
120 entry->id = PSICONV_ID_APPL_ID_SECTION; 167 if ((res = psiconv_buffer_add_target(buf,section_table_id)))
121 entry->offset = psiconv_list_length(extra_buf) + 0x14;
122 if ((res = psiconv_list_add(section_table,entry)))
123 goto ERROR6; 168 goto ERROR6;
124 if ((res=psiconv_write_application_id_section(extra_buf,
125 PSICONV_ID_TEXTED,"TextEd.app")))
126 goto ERROR6;
127
128 entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION;
129 entry->offset = psiconv_list_length(extra_buf) + 0x14;
130 if ((res = psiconv_list_add(section_table,entry)))
131 goto ERROR6;
132 if ((res = psiconv_write_page_layout_section(extra_buf,value->page_sec)))
133 goto ERROR6;
134 169
135 entry->id = PSICONV_ID_TEXTED;
136 entry->offset = psiconv_list_length(extra_buf) + 0x14;
137 if ((res = psiconv_list_add(section_table,entry)))
138 goto ERROR6;
139 if ((res = psiconv_write_texted_section(extra_buf,value->texted_sec,
140 base_char,base_para)))
141 goto ERROR6;
142
143 if ((res = psiconv_write_u32(buf,psiconv_list_length(extra_buf) + 0x14)))
144 goto ERROR6;
145
146 if ((res = psiconv_list_concat(buf,extra_buf)))
147 goto ERROR6;
148
149 res = psiconv_write_section_table_section(buf,section_table); 170 res = psiconv_write_section_table_section(config,buf,section_table);
150 171
151ERROR6: 172ERROR6:
173 psiconv_buffer_free(buf_texted);
174ERROR5:
152 psiconv_free_paragraph_layout(base_para); 175 psiconv_free_paragraph_layout(base_para);
153ERROR5: 176ERROR4:
154 psiconv_free_character_layout(base_char); 177 psiconv_free_character_layout(base_char);
155ERROR4:
156 psiconv_free_buffer(extra_buf);
157ERROR3: 178ERROR3:
158 free(entry); 179 free(entry);
159ERROR2: 180ERROR2:
160 psiconv_list_free(section_table); 181 psiconv_list_free(section_table);
161ERROR1: 182ERROR1:
162 return res; 183 return res;
163} 184}
185
186int 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
274ERROR3:
275 free(entry);
276ERROR2:
277 psiconv_list_free(section_table);
278ERROR1:
279 return res;
280}
281
282int 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
332ERROR3:
333 free(entry);
334ERROR2:
335 psiconv_list_free(section_table);
336ERROR1:
337 return res;
338}
339
340int 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
382ERROR2:
383 psiconv_list_free(jumptable);
384ERROR1:
385 return res;
386}
387
388/* Note: this file is special, because it does not have a complete header! */
389int 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
437ERROR3:
438 psiconv_buffer_free(sec_buf);
439ERROR2:
440 psiconv_list_free(jumptable);
441ERROR1:
442 return res;
443}

Legend:
Removed from v.76  
changed lines
  Added in v.174

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