/[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 142 Revision 174
28#ifdef DMALLOC 28#ifdef DMALLOC
29#include <dmalloc.h> 29#include <dmalloc.h>
30#endif 30#endif
31 31
32 32
33int psiconv_write(psiconv_buffer *buf,const psiconv_file value) 33int psiconv_write(const psiconv_config config, psiconv_buffer *buf,
34 const psiconv_file value)
34{ 35{
35 int res; 36 int res;
36 37
37 if (!value) { 38 if (!value) {
38 psiconv_warn(0,0,"Can't parse to an empty buffer!"); 39 psiconv_warn(config,0,0,"Can't parse to an empty buffer!");
39 return -PSICONV_E_OTHER; 40 return -PSICONV_E_OTHER;
40 } 41 }
41 if (!(*buf = psiconv_buffer_new())) 42 if (!(*buf = psiconv_buffer_new()))
42 return -PSICONV_E_NOMEM; 43 return -PSICONV_E_NOMEM;
43 44
44 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;
45 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))))
46 goto ERROR; 51 goto ERROR;
47 } else 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;
48 if ((res =psiconv_write_texted_file(*buf, 57 if ((res =psiconv_write_texted_file(config,*buf,
49 (psiconv_texted_f) (value->file)))) 58 (psiconv_texted_f) (value->file))))
50 goto ERROR; 59 goto ERROR;
51#if 0
52 } 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;
53 if ((res =psiconv_write_sketch_file(*buf, 65 if ((res =psiconv_write_sketch_file(config,*buf,
54 (psiconv_sketch_f) (value->file)))) 66 (psiconv_sketch_f) (value->file))))
55 goto ERROR; 67 goto ERROR;
56 } 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;
57 if ((res =psiconv_write_mbm_file(*buf, 73 if ((res =psiconv_write_mbm_file(config,*buf,
58 (psiconv_mbm_f) (value->file)))) 74 (psiconv_mbm_f) (value->file))))
59 goto ERROR; 75 goto ERROR;
60 } 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 */
61 if ((res =psiconv_write_clipart_file(*buf, 78 if ((res =psiconv_write_clipart_file(config,*buf,
62 (psiconv_clipart_f) (value->file)))) 79 (psiconv_clipart_f) (value->file))))
63 goto ERROR; 80 goto ERROR;
64#endif
65 } else { 81 } else {
66 psiconv_warn(0,0,"Unknown or unsupported file type"); 82 psiconv_warn(config,0,0,"Unknown or unsupported file type");
67 res = -PSICONV_E_GENERATE; 83 res = -PSICONV_E_GENERATE;
68 goto ERROR; 84 goto ERROR;
69 } 85 }
70 if ((res = psiconv_buffer_resolve(*buf))) 86 if ((res = psiconv_buffer_resolve(*buf)))
71 goto ERROR; 87 goto ERROR;
74ERROR: 90ERROR:
75 psiconv_buffer_free(*buf); 91 psiconv_buffer_free(*buf);
76 return res; 92 return res;
77} 93}
78 94
79int 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)
80{ 97{
81 psiconv_character_layout base_char; 98 psiconv_character_layout base_char;
82 psiconv_paragraph_layout base_para; 99 psiconv_paragraph_layout base_para;
83 int res; 100 int res;
84 psiconv_section_table_section section_table; 101 psiconv_section_table_section section_table;
85 psiconv_section_table_entry entry; 102 psiconv_section_table_entry entry;
86 psiconv_u32 section_table_id; 103 psiconv_u32 section_table_id;
87 psiconv_buffer buf_texted; 104 psiconv_buffer buf_texted;
88 105
89 if (!value) { 106 if (!value) {
90 psiconv_warn(0,0,"Null TextEd file"); 107 psiconv_warn(config,0,0,"Null TextEd file");
91 return -PSICONV_E_GENERATE; 108 return -PSICONV_E_GENERATE;
92 } 109 }
93 110
94 if (!(section_table = psiconv_list_new(sizeof(*entry)))) { 111 if (!(section_table = psiconv_list_new(sizeof(*entry)))) {
95 res = -PSICONV_E_NOMEM; 112 res = -PSICONV_E_NOMEM;
108 if (!(base_para = psiconv_basic_paragraph_layout())) { 125 if (!(base_para = psiconv_basic_paragraph_layout())) {
109 res = -PSICONV_E_NOMEM; 126 res = -PSICONV_E_NOMEM;
110 goto ERROR4; 127 goto ERROR4;
111 } 128 }
112 129
113 if ((res = psiconv_write_header_section(buf,PSICONV_ID_PSION5,
114 PSICONV_ID_DATA_FILE,
115 PSICONV_ID_TEXTED)))
116 goto ERROR5;
117
118 section_table_id = psiconv_buffer_unique_id(); 130 section_table_id = psiconv_buffer_unique_id();
119 if ((res = psiconv_write_offset(buf,section_table_id))) 131 if ((res = psiconv_write_offset(config,buf,section_table_id)))
120 goto ERROR5; 132 goto ERROR5;
121 133
122 entry->id = PSICONV_ID_APPL_ID_SECTION; 134 entry->id = PSICONV_ID_APPL_ID_SECTION;
123 entry->offset = psiconv_buffer_unique_id(); 135 entry->offset = psiconv_buffer_unique_id();
124 if ((res = psiconv_list_add(section_table,entry))) 136 if ((res = psiconv_list_add(section_table,entry)))
125 goto ERROR5; 137 goto ERROR5;
126 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 138 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
127 goto ERROR5; 139 goto ERROR5;
128 if ((res=psiconv_write_application_id_section(buf, 140 if ((res=psiconv_write_application_id_section(config,buf,
129 PSICONV_ID_TEXTED,"TextEd.app"))) 141 PSICONV_ID_TEXTED,"TextEd.app")))
130 goto ERROR5; 142 goto ERROR5;
131 143
132 entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; 144 entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION;
133 entry->offset = psiconv_buffer_unique_id(); 145 entry->offset = psiconv_buffer_unique_id();
134 if ((res = psiconv_list_add(section_table,entry))) 146 if ((res = psiconv_list_add(section_table,entry)))
135 goto ERROR5; 147 goto ERROR5;
136 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 148 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
137 goto ERROR5; 149 goto ERROR5;
138 if ((res = psiconv_write_page_layout_section(buf,value->page_sec))) 150 if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec)))
139 goto ERROR5; 151 goto ERROR5;
140 152
141 entry->id = PSICONV_ID_TEXTED; 153 entry->id = PSICONV_ID_TEXTED;
142 entry->offset = psiconv_buffer_unique_id(); 154 entry->offset = psiconv_buffer_unique_id();
143 if ((res = psiconv_list_add(section_table,entry))) 155 if ((res = psiconv_list_add(section_table,entry)))
144 goto ERROR5; 156 goto ERROR5;
145 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 157 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
146 goto ERROR5; 158 goto ERROR5;
147 if ((res = psiconv_write_texted_section(buf,value->texted_sec, 159 if ((res = psiconv_write_texted_section(config,buf,value->texted_sec,
148 base_char,base_para,&buf_texted))) 160 base_char,base_para,&buf_texted)))
149 goto ERROR5; 161 goto ERROR5;
150 162
151 if ((res = psiconv_buffer_concat(buf,buf_texted))) 163 if ((res = psiconv_buffer_concat(buf,buf_texted)))
152 goto ERROR6; 164 goto ERROR6;
153 165
154 166
155 if ((res = psiconv_buffer_add_target(buf,section_table_id))) 167 if ((res = psiconv_buffer_add_target(buf,section_table_id)))
156 goto ERROR6; 168 goto ERROR6;
157 169
158 res = psiconv_write_section_table_section(buf,section_table); 170 res = psiconv_write_section_table_section(config,buf,section_table);
159 171
160ERROR6: 172ERROR6:
161 psiconv_buffer_free(buf_texted); 173 psiconv_buffer_free(buf_texted);
162ERROR5: 174ERROR5:
163 psiconv_free_paragraph_layout(base_para); 175 psiconv_free_paragraph_layout(base_para);
169 psiconv_list_free(section_table); 181 psiconv_list_free(section_table);
170ERROR1: 182ERROR1:
171 return res; 183 return res;
172} 184}
173 185
174int psiconv_write_word_file(psiconv_buffer buf,psiconv_word_f value) 186int psiconv_write_word_file(const psiconv_config config,
187 psiconv_buffer buf,psiconv_word_f value)
175{ 188{
176 int res; 189 int res;
177 psiconv_section_table_section section_table; 190 psiconv_section_table_section section_table;
178 psiconv_section_table_entry entry; 191 psiconv_section_table_entry entry;
179 psiconv_u32 section_table_id; 192 psiconv_u32 section_table_id;
180 193
181 if (!value) { 194 if (!value) {
182 psiconv_warn(0,0,"Null Word file"); 195 psiconv_warn(config,0,0,"Null Word file");
183 return -PSICONV_E_GENERATE; 196 return -PSICONV_E_GENERATE;
184 } 197 }
185 198
186 if (!(section_table = psiconv_list_new(sizeof(*entry)))) { 199 if (!(section_table = psiconv_list_new(sizeof(*entry)))) {
187 res = -PSICONV_E_NOMEM; 200 res = -PSICONV_E_NOMEM;
191 if (!(entry = malloc(sizeof(*entry)))) { 204 if (!(entry = malloc(sizeof(*entry)))) {
192 res = -PSICONV_E_NOMEM; 205 res = -PSICONV_E_NOMEM;
193 goto ERROR2; 206 goto ERROR2;
194 } 207 }
195 208
196 if ((res = psiconv_write_header_section(buf,PSICONV_ID_PSION5,
197 PSICONV_ID_DATA_FILE,
198 PSICONV_ID_WORD)))
199 goto ERROR3;
200
201 section_table_id = psiconv_buffer_unique_id(); 209 section_table_id = psiconv_buffer_unique_id();
202 if ((res = psiconv_write_offset(buf,section_table_id))) 210 if ((res = psiconv_write_offset(config,buf,section_table_id)))
203 goto ERROR3; 211 goto ERROR3;
204 212
205 entry->id = PSICONV_ID_APPL_ID_SECTION; 213 entry->id = PSICONV_ID_APPL_ID_SECTION;
206 entry->offset = psiconv_buffer_unique_id(); 214 entry->offset = psiconv_buffer_unique_id();
207 if ((res = psiconv_list_add(section_table,entry))) 215 if ((res = psiconv_list_add(section_table,entry)))
208 goto ERROR3; 216 goto ERROR3;
209 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 217 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
210 goto ERROR3; 218 goto ERROR3;
211 if ((res=psiconv_write_application_id_section(buf, 219 if ((res=psiconv_write_application_id_section(config,buf,
212 PSICONV_ID_WORD,"Word.app"))) 220 PSICONV_ID_WORD,"Word.app")))
213 goto ERROR3; 221 goto ERROR3;
214 222
215 entry->id = PSICONV_ID_WORD_STATUS_SECTION; 223 entry->id = PSICONV_ID_WORD_STATUS_SECTION;
216 entry->offset = psiconv_buffer_unique_id(); 224 entry->offset = psiconv_buffer_unique_id();
217 if ((res = psiconv_list_add(section_table,entry))) 225 if ((res = psiconv_list_add(section_table,entry)))
218 goto ERROR3; 226 goto ERROR3;
219 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 227 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
220 goto ERROR3; 228 goto ERROR3;
221 if ((res = psiconv_write_word_status_section(buf,value->status_sec))) 229 if ((res = psiconv_write_word_status_section(config,buf,value->status_sec)))
222 goto ERROR3; 230 goto ERROR3;
223 231
224 entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; 232 entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION;
225 entry->offset = psiconv_buffer_unique_id(); 233 entry->offset = psiconv_buffer_unique_id();
226 if ((res = psiconv_list_add(section_table,entry))) 234 if ((res = psiconv_list_add(section_table,entry)))
227 goto ERROR3; 235 goto ERROR3;
228 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 236 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
229 goto ERROR3; 237 goto ERROR3;
230 if ((res = psiconv_write_page_layout_section(buf,value->page_sec))) 238 if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec)))
231 goto ERROR3; 239 goto ERROR3;
232 240
233 entry->id = PSICONV_ID_WORD_STYLES_SECTION; 241 entry->id = PSICONV_ID_WORD_STYLES_SECTION;
234 entry->offset = psiconv_buffer_unique_id(); 242 entry->offset = psiconv_buffer_unique_id();
235 if ((res = psiconv_list_add(section_table,entry))) 243 if ((res = psiconv_list_add(section_table,entry)))
236 goto ERROR3; 244 goto ERROR3;
237 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 245 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
238 goto ERROR3; 246 goto ERROR3;
239 if ((res = psiconv_write_word_styles_section(buf,value->styles_sec))) 247 if ((res = psiconv_write_word_styles_section(config,buf,value->styles_sec)))
240 goto ERROR3; 248 goto ERROR3;
241 249
242 entry->id = PSICONV_ID_TEXT_SECTION; 250 entry->id = PSICONV_ID_TEXT_SECTION;
243 entry->offset = psiconv_buffer_unique_id(); 251 entry->offset = psiconv_buffer_unique_id();
244 if ((res = psiconv_list_add(section_table,entry))) 252 if ((res = psiconv_list_add(section_table,entry)))
245 goto ERROR3; 253 goto ERROR3;
246 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 254 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
247 goto ERROR3; 255 goto ERROR3;
248 if ((res = psiconv_write_text_section(buf,value->paragraphs))) 256 if ((res = psiconv_write_text_section(config,buf,value->paragraphs)))
249 goto ERROR3; 257 goto ERROR3;
250 258
251 entry->id = PSICONV_ID_LAYOUT_SECTION; 259 entry->id = PSICONV_ID_LAYOUT_SECTION;
252 entry->offset = psiconv_buffer_unique_id(); 260 entry->offset = psiconv_buffer_unique_id();
253 if ((res = psiconv_list_add(section_table,entry))) 261 if ((res = psiconv_list_add(section_table,entry)))
254 goto ERROR3; 262 goto ERROR3;
255 if ((res = psiconv_buffer_add_target(buf,entry->offset))) 263 if ((res = psiconv_buffer_add_target(buf,entry->offset)))
256 goto ERROR3; 264 goto ERROR3;
257 if ((res = psiconv_write_styled_layout_section(buf,value->paragraphs, 265 if ((res = psiconv_write_styled_layout_section(config,buf,value->paragraphs,
258 value->styles_sec))) 266 value->styles_sec)))
259 goto ERROR3; 267 goto ERROR3;
260 268
261 if ((res = psiconv_buffer_add_target(buf,section_table_id))) 269 if ((res = psiconv_buffer_add_target(buf,section_table_id)))
262 goto ERROR3; 270 goto ERROR3;
263 271
264 res = psiconv_write_section_table_section(buf,section_table); 272 res = psiconv_write_section_table_section(config,buf,section_table);
265 273
266ERROR3: 274ERROR3:
267 free(entry); 275 free(entry);
268ERROR2: 276ERROR2:
269 psiconv_list_free(section_table); 277 psiconv_list_free(section_table);
270ERROR1: 278ERROR1:
271 return res; 279 return res;
272} 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.142  
changed lines
  Added in v.174

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