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