… | |
… | |
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 | |
|
|
33 | int 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_buffer_new(0))) |
42 | if (!(*buf = psiconv_buffer_new())) |
38 | return -PSICONV_E_NOMEM; |
43 | return -PSICONV_E_NOMEM; |
39 | |
44 | |
|
|
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; |
|
|
50 | if ((res =psiconv_write_word_file(config,*buf,(psiconv_word_f) (value->file)))) |
|
|
51 | goto ERROR; |
|
|
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; |
|
|
57 | if ((res =psiconv_write_texted_file(config,*buf, |
|
|
58 | (psiconv_texted_f) (value->file)))) |
|
|
59 | goto ERROR; |
|
|
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; |
|
|
65 | if ((res =psiconv_write_sketch_file(config,*buf, |
|
|
66 | (psiconv_sketch_f) (value->file)))) |
|
|
67 | goto ERROR; |
40 | #if 0 |
68 | #if 0 |
41 | if (value->type == psiconv_word_file) { |
|
|
42 | if ((res =psiconv_write_word_file(*buf,(psiconv_word_f) (value->file)))) |
|
|
43 | goto ERROR; |
|
|
44 | } else |
|
|
45 | #endif |
|
|
46 | if (value->type == psiconv_texted_file) { |
|
|
47 | if ((res =psiconv_write_texted_file(*buf, |
|
|
48 | (psiconv_texted_f) (value->file)))) |
|
|
49 | goto ERROR; |
|
|
50 | #if 0 |
|
|
51 | } else if (value->type == psiconv_sketch_file) { |
|
|
52 | if ((res =psiconv_write_texted_file(*buf, |
|
|
53 | (psiconv_sketch_f) (value->file)))) |
|
|
54 | goto ERROR; |
|
|
55 | } else if (value->type == psiconv_mbm_file) { |
69 | } else if (value->type == psiconv_mbm_file) { |
56 | if ((res =psiconv_write_texted_file(*buf, |
70 | if ((res =psiconv_write_mbm_file(config,*buf, |
57 | (psiconv_mbm_f) (value->file)))) |
71 | (psiconv_mbm_f) (value->file)))) |
58 | goto ERROR; |
72 | goto ERROR; |
59 | } else if (value->type == psiconv_clipart_file) { |
73 | } else if (value->type == psiconv_clipart_file) { |
60 | if ((res =psiconv_write_texted_file(*buf, |
74 | if ((res =psiconv_write_clipart_file(config,*buf, |
61 | (psiconv_clipart_f) (value->file)))) |
75 | (psiconv_clipart_f) (value->file)))) |
62 | goto ERROR; |
76 | goto ERROR; |
63 | #endif |
77 | #endif |
64 | } else { |
78 | } else { |
65 | psiconv_warn(0,0,"Unknown or unsupported file type"); |
79 | psiconv_warn(config,0,0,"Unknown or unsupported file type"); |
66 | res = -PSICONV_E_GENERATE; |
80 | res = -PSICONV_E_GENERATE; |
67 | goto ERROR; |
81 | goto ERROR; |
68 | } |
82 | } |
|
|
83 | if ((res = psiconv_buffer_resolve(*buf))) |
|
|
84 | goto ERROR; |
69 | return -PSICONV_E_OK; |
85 | return -PSICONV_E_OK; |
70 | |
86 | |
71 | ERROR: |
87 | ERROR: |
72 | psiconv_buffer_free(*buf); |
88 | psiconv_buffer_free(*buf); |
73 | return res; |
89 | return res; |
74 | } |
90 | } |
75 | |
91 | |
76 | int psiconv_write_texted_file(psiconv_buffer buf,psiconv_texted_f value) |
92 | int psiconv_write_texted_file(const psiconv_config config, |
|
|
93 | psiconv_buffer buf,psiconv_texted_f value) |
77 | { |
94 | { |
78 | psiconv_character_layout base_char; |
95 | psiconv_character_layout base_char; |
79 | psiconv_paragraph_layout base_para; |
96 | psiconv_paragraph_layout base_para; |
80 | int res; |
97 | int res; |
81 | psiconv_buffer extra_buf; |
|
|
82 | psiconv_section_table_section section_table; |
98 | psiconv_section_table_section section_table; |
83 | psiconv_section_table_entry entry; |
99 | psiconv_section_table_entry entry; |
|
|
100 | psiconv_u32 section_table_id; |
|
|
101 | psiconv_buffer buf_texted; |
84 | |
102 | |
85 | if (!value) { |
103 | if (!value) { |
86 | psiconv_warn(0,0,"Null TextEd file"); |
104 | psiconv_warn(config,0,0,"Null TextEd file"); |
87 | return -PSICONV_E_GENERATE; |
105 | return -PSICONV_E_GENERATE; |
88 | } |
106 | } |
89 | |
107 | |
90 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
108 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
91 | res = -PSICONV_E_NOMEM; |
109 | res = -PSICONV_E_NOMEM; |
… | |
… | |
95 | if (!(entry = malloc(sizeof(*entry)))) { |
113 | if (!(entry = malloc(sizeof(*entry)))) { |
96 | res = -PSICONV_E_NOMEM; |
114 | res = -PSICONV_E_NOMEM; |
97 | goto ERROR2; |
115 | goto ERROR2; |
98 | } |
116 | } |
99 | |
117 | |
100 | /* Base offset is 0x18: 0x10 header section plus the section table offset */ |
|
|
101 | if (!(extra_buf = psiconv_buffer_new(0x14))) { |
|
|
102 | res = -PSICONV_E_NOMEM; |
|
|
103 | goto ERROR3; |
|
|
104 | } |
|
|
105 | |
|
|
106 | if (!(base_char = psiconv_basic_character_layout())) { |
118 | if (!(base_char = psiconv_basic_character_layout())) { |
107 | res = -PSICONV_E_NOMEM; |
119 | res = -PSICONV_E_NOMEM; |
|
|
120 | goto ERROR3; |
|
|
121 | } |
|
|
122 | if (!(base_para = psiconv_basic_paragraph_layout())) { |
|
|
123 | res = -PSICONV_E_NOMEM; |
108 | goto ERROR4; |
124 | goto ERROR4; |
109 | } |
125 | } |
110 | if (!(base_para = psiconv_basic_paragraph_layout())) { |
|
|
111 | res = -PSICONV_E_NOMEM; |
|
|
112 | goto ERROR5; |
|
|
113 | } |
|
|
114 | |
126 | |
115 | if ((res = psiconv_write_header_section(buf,PSICONV_ID_PSION5, |
127 | section_table_id = psiconv_buffer_unique_id(); |
116 | PSICONV_ID_DATA_FILE, |
128 | if ((res = psiconv_write_offset(config,buf,section_table_id))) |
|
|
129 | goto ERROR5; |
|
|
130 | |
|
|
131 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
|
|
132 | entry->offset = psiconv_buffer_unique_id(); |
|
|
133 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
134 | goto ERROR5; |
|
|
135 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
136 | goto ERROR5; |
|
|
137 | if ((res=psiconv_write_application_id_section(config,buf, |
117 | PSICONV_ID_TEXTED))) |
138 | PSICONV_ID_TEXTED,"TextEd.app"))) |
|
|
139 | goto ERROR5; |
|
|
140 | |
|
|
141 | entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
|
|
142 | entry->offset = psiconv_buffer_unique_id(); |
|
|
143 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
144 | goto ERROR5; |
|
|
145 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
146 | goto ERROR5; |
|
|
147 | if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
|
|
148 | goto ERROR5; |
|
|
149 | |
|
|
150 | entry->id = PSICONV_ID_TEXTED; |
|
|
151 | entry->offset = psiconv_buffer_unique_id(); |
|
|
152 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
153 | goto ERROR5; |
|
|
154 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
155 | goto ERROR5; |
|
|
156 | if ((res = psiconv_write_texted_section(config,buf,value->texted_sec, |
|
|
157 | base_char,base_para,&buf_texted))) |
|
|
158 | goto ERROR5; |
|
|
159 | |
|
|
160 | if ((res = psiconv_buffer_concat(buf,buf_texted))) |
118 | goto ERROR6; |
161 | goto ERROR6; |
119 | |
162 | |
120 | |
163 | |
121 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
164 | if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
122 | entry->offset = psiconv_buffer_length(extra_buf) + |
|
|
123 | psiconv_buffer_base_offset(extra_buf); |
|
|
124 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
125 | goto ERROR6; |
165 | goto ERROR6; |
126 | if ((res=psiconv_write_application_id_section(extra_buf, |
|
|
127 | PSICONV_ID_TEXTED,"TextEd.app"))) |
|
|
128 | goto ERROR6; |
|
|
129 | |
|
|
130 | entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
|
|
131 | entry->offset = psiconv_buffer_length(extra_buf) + |
|
|
132 | psiconv_buffer_base_offset(extra_buf); |
|
|
133 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
134 | goto ERROR6; |
|
|
135 | if ((res = psiconv_write_page_layout_section(extra_buf,value->page_sec))) |
|
|
136 | goto ERROR6; |
|
|
137 | |
166 | |
138 | entry->id = PSICONV_ID_TEXTED; |
|
|
139 | entry->offset = psiconv_buffer_length(extra_buf) + |
|
|
140 | psiconv_buffer_base_offset(extra_buf); |
|
|
141 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
142 | goto ERROR6; |
|
|
143 | if ((res = psiconv_write_texted_section(extra_buf,value->texted_sec, |
|
|
144 | base_char,base_para))) |
|
|
145 | goto ERROR6; |
|
|
146 | |
|
|
147 | if ((res = psiconv_write_u32(buf,psiconv_buffer_length(extra_buf) + |
|
|
148 | psiconv_buffer_base_offset(extra_buf)))) |
|
|
149 | goto ERROR6; |
|
|
150 | |
|
|
151 | if ((res = psiconv_buffer_concat(buf,extra_buf))) |
|
|
152 | goto ERROR6; |
|
|
153 | |
|
|
154 | res = psiconv_write_section_table_section(buf,section_table); |
167 | res = psiconv_write_section_table_section(config,buf,section_table); |
155 | |
168 | |
156 | ERROR6: |
169 | ERROR6: |
|
|
170 | psiconv_buffer_free(buf_texted); |
|
|
171 | ERROR5: |
157 | psiconv_free_paragraph_layout(base_para); |
172 | psiconv_free_paragraph_layout(base_para); |
158 | ERROR5: |
173 | ERROR4: |
159 | psiconv_free_character_layout(base_char); |
174 | psiconv_free_character_layout(base_char); |
160 | ERROR4: |
|
|
161 | psiconv_buffer_free(extra_buf); |
|
|
162 | ERROR3: |
175 | ERROR3: |
163 | free(entry); |
176 | free(entry); |
164 | ERROR2: |
177 | ERROR2: |
165 | psiconv_list_free(section_table); |
178 | psiconv_list_free(section_table); |
166 | ERROR1: |
179 | ERROR1: |
167 | return res; |
180 | return res; |
168 | } |
181 | } |
|
|
182 | |
|
|
183 | int psiconv_write_word_file(const psiconv_config config, |
|
|
184 | psiconv_buffer buf,psiconv_word_f value) |
|
|
185 | { |
|
|
186 | int res; |
|
|
187 | psiconv_section_table_section section_table; |
|
|
188 | psiconv_section_table_entry entry; |
|
|
189 | psiconv_u32 section_table_id; |
|
|
190 | |
|
|
191 | if (!value) { |
|
|
192 | psiconv_warn(config,0,0,"Null Word file"); |
|
|
193 | return -PSICONV_E_GENERATE; |
|
|
194 | } |
|
|
195 | |
|
|
196 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
|
|
197 | res = -PSICONV_E_NOMEM; |
|
|
198 | goto ERROR1; |
|
|
199 | } |
|
|
200 | |
|
|
201 | if (!(entry = malloc(sizeof(*entry)))) { |
|
|
202 | res = -PSICONV_E_NOMEM; |
|
|
203 | goto ERROR2; |
|
|
204 | } |
|
|
205 | |
|
|
206 | section_table_id = psiconv_buffer_unique_id(); |
|
|
207 | if ((res = psiconv_write_offset(config,buf,section_table_id))) |
|
|
208 | goto ERROR3; |
|
|
209 | |
|
|
210 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
|
|
211 | entry->offset = psiconv_buffer_unique_id(); |
|
|
212 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
213 | goto ERROR3; |
|
|
214 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
215 | goto ERROR3; |
|
|
216 | if ((res=psiconv_write_application_id_section(config,buf, |
|
|
217 | PSICONV_ID_WORD,"Word.app"))) |
|
|
218 | goto ERROR3; |
|
|
219 | |
|
|
220 | entry->id = PSICONV_ID_WORD_STATUS_SECTION; |
|
|
221 | entry->offset = psiconv_buffer_unique_id(); |
|
|
222 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
223 | goto ERROR3; |
|
|
224 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
225 | goto ERROR3; |
|
|
226 | if ((res = psiconv_write_word_status_section(config,buf,value->status_sec))) |
|
|
227 | goto ERROR3; |
|
|
228 | |
|
|
229 | entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
|
|
230 | entry->offset = psiconv_buffer_unique_id(); |
|
|
231 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
232 | goto ERROR3; |
|
|
233 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
234 | goto ERROR3; |
|
|
235 | if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
|
|
236 | goto ERROR3; |
|
|
237 | |
|
|
238 | entry->id = PSICONV_ID_WORD_STYLES_SECTION; |
|
|
239 | entry->offset = psiconv_buffer_unique_id(); |
|
|
240 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
241 | goto ERROR3; |
|
|
242 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
243 | goto ERROR3; |
|
|
244 | if ((res = psiconv_write_word_styles_section(config,buf,value->styles_sec))) |
|
|
245 | goto ERROR3; |
|
|
246 | |
|
|
247 | entry->id = PSICONV_ID_TEXT_SECTION; |
|
|
248 | entry->offset = psiconv_buffer_unique_id(); |
|
|
249 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
250 | goto ERROR3; |
|
|
251 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
252 | goto ERROR3; |
|
|
253 | if ((res = psiconv_write_text_section(config,buf,value->paragraphs))) |
|
|
254 | goto ERROR3; |
|
|
255 | |
|
|
256 | entry->id = PSICONV_ID_LAYOUT_SECTION; |
|
|
257 | entry->offset = psiconv_buffer_unique_id(); |
|
|
258 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
259 | goto ERROR3; |
|
|
260 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
261 | goto ERROR3; |
|
|
262 | if ((res = psiconv_write_styled_layout_section(config,buf,value->paragraphs, |
|
|
263 | value->styles_sec))) |
|
|
264 | goto ERROR3; |
|
|
265 | |
|
|
266 | if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
|
|
267 | goto ERROR3; |
|
|
268 | |
|
|
269 | res = psiconv_write_section_table_section(config,buf,section_table); |
|
|
270 | |
|
|
271 | ERROR3: |
|
|
272 | free(entry); |
|
|
273 | ERROR2: |
|
|
274 | psiconv_list_free(section_table); |
|
|
275 | ERROR1: |
|
|
276 | return res; |
|
|
277 | } |
|
|
278 | |
|
|
279 | int psiconv_write_sketch_file(const psiconv_config config, |
|
|
280 | psiconv_buffer buf,psiconv_sketch_f value) |
|
|
281 | { |
|
|
282 | int res; |
|
|
283 | psiconv_section_table_section section_table; |
|
|
284 | psiconv_section_table_entry entry; |
|
|
285 | psiconv_u32 section_table_id; |
|
|
286 | |
|
|
287 | if (!value) { |
|
|
288 | psiconv_warn(config,0,0,"Null Sketch file"); |
|
|
289 | return -PSICONV_E_GENERATE; |
|
|
290 | } |
|
|
291 | |
|
|
292 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
|
|
293 | res = -PSICONV_E_NOMEM; |
|
|
294 | goto ERROR1; |
|
|
295 | } |
|
|
296 | |
|
|
297 | if (!(entry = malloc(sizeof(*entry)))) { |
|
|
298 | res = -PSICONV_E_NOMEM; |
|
|
299 | goto ERROR2; |
|
|
300 | } |
|
|
301 | |
|
|
302 | section_table_id = psiconv_buffer_unique_id(); |
|
|
303 | if ((res = psiconv_write_offset(config,buf,section_table_id))) |
|
|
304 | goto ERROR3; |
|
|
305 | |
|
|
306 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
|
|
307 | entry->offset = psiconv_buffer_unique_id(); |
|
|
308 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
309 | goto ERROR3; |
|
|
310 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
311 | goto ERROR3; |
|
|
312 | if ((res=psiconv_write_application_id_section(config,buf, |
|
|
313 | PSICONV_ID_SKETCH,"Paint.app"))) |
|
|
314 | goto ERROR3; |
|
|
315 | |
|
|
316 | entry->id = PSICONV_ID_SKETCH_SECTION; |
|
|
317 | entry->offset = psiconv_buffer_unique_id(); |
|
|
318 | if ((res = psiconv_list_add(section_table,entry))) |
|
|
319 | goto ERROR3; |
|
|
320 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
|
|
321 | goto ERROR3; |
|
|
322 | if ((res = psiconv_write_sketch_section(config,buf,value->sketch_sec))) |
|
|
323 | goto ERROR3; |
|
|
324 | |
|
|
325 | if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
|
|
326 | goto ERROR3; |
|
|
327 | res = psiconv_write_section_table_section(config,buf,section_table); |
|
|
328 | |
|
|
329 | ERROR3: |
|
|
330 | free(entry); |
|
|
331 | ERROR2: |
|
|
332 | psiconv_list_free(section_table); |
|
|
333 | ERROR1: |
|
|
334 | return res; |
|
|
335 | } |