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