1 | /* |
1 | /* |
2 | generate_common.c - Part of psiconv, a PSION 5 file formats converter |
2 | generate_common.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 2000-2004 Frodo Looijaard <frodol@dds.nl> |
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 int psiconv_write_layout_section(psiconv_buffer buf, |
32 | static int psiconv_write_layout_section(const psiconv_config config, |
|
|
33 | psiconv_buffer buf, int lev, |
33 | const psiconv_text_and_layout value, |
34 | const psiconv_text_and_layout value, |
34 | const psiconv_word_styles_section styles, |
35 | const psiconv_word_styles_section styles, |
35 | int with_styles); |
36 | int with_styles); |
36 | |
37 | |
37 | /* Maybe use a psiconv_header_section variable instead? */ |
38 | /* Maybe use a psiconv_header_section variable instead? */ |
38 | int psiconv_write_header_section(psiconv_buffer buf,psiconv_u32 uid1, |
39 | int psiconv_write_header_section(const psiconv_config config, |
|
|
40 | psiconv_buffer buf,int lev,psiconv_u32 uid1, |
39 | psiconv_u32 uid2, psiconv_u32 uid3) |
41 | psiconv_u32 uid2, psiconv_u32 uid3) |
40 | { |
42 | { |
41 | int res; |
43 | int res; |
|
|
44 | psiconv_progress(config,lev,0,"Writing header section"); |
42 | if ((res = psiconv_write_u32(buf,uid1))) |
45 | if ((res = psiconv_write_u32(config,buf,lev+1,uid1))) |
|
|
46 | goto ERROR; |
|
|
47 | if ((res = psiconv_write_u32(config,buf,lev+1,uid2))) |
|
|
48 | goto ERROR; |
|
|
49 | if ((res = psiconv_write_u32(config,buf,lev+1,uid3))) |
|
|
50 | goto ERROR; |
|
|
51 | if ((res = psiconv_write_u32(config,buf,lev+1, |
|
|
52 | psiconv_checkuid(uid1,uid2,uid3)))) |
|
|
53 | goto ERROR; |
|
|
54 | psiconv_progress(config,lev,0,"End of header section"); |
|
|
55 | return 0; |
|
|
56 | |
|
|
57 | ERROR: |
|
|
58 | psiconv_error(config,lev,0,"Writing of header section failed"); |
43 | return res; |
59 | return res; |
44 | if ((res = psiconv_write_u32(buf,uid2))) |
|
|
45 | return res; |
|
|
46 | if ((res = psiconv_write_u32(buf,uid3))) |
|
|
47 | return res; |
|
|
48 | return psiconv_write_u32(buf,psiconv_checkuid(uid1,uid2,uid3)); |
|
|
49 | } |
60 | } |
50 | |
61 | |
51 | int psiconv_write_section_table_section(psiconv_buffer buf, |
62 | int psiconv_write_section_table_section(const psiconv_config config, |
|
|
63 | psiconv_buffer buf,int lev, |
52 | const psiconv_section_table_section value) |
64 | const psiconv_section_table_section value) |
53 | { |
65 | { |
54 | int res,i; |
66 | int res,i; |
55 | psiconv_section_table_entry entry; |
67 | psiconv_section_table_entry entry; |
56 | |
68 | |
|
|
69 | psiconv_progress(config,lev,0,"Writing section table section"); |
57 | if (!value) { |
70 | if (!value) { |
58 | psiconv_warn(0,psiconv_buffer_length(buf),"Null section table section"); |
71 | psiconv_error(config,lev,0,"Null section table section"); |
59 | return -PSICONV_E_GENERATE; |
72 | res = -PSICONV_E_GENERATE; |
|
|
73 | goto ERROR; |
60 | } |
74 | } |
61 | |
75 | |
62 | if ((res = psiconv_write_u8(buf,2 * psiconv_list_length(value)))) |
76 | if ((res = psiconv_write_u8(config,buf,lev+1,2 * psiconv_list_length(value)))) |
63 | return res; |
77 | goto ERROR; |
64 | for (i = 0; i < psiconv_list_length(value); i++) { |
78 | for (i = 0; i < psiconv_list_length(value); i++) { |
65 | if (!(entry = psiconv_list_get(value,i))) { |
79 | if (!(entry = psiconv_list_get(value,i))) { |
66 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
80 | psiconv_error(config,lev+1,0,"Massive memory corruption"); |
67 | return -PSICONV_E_NOMEM; |
81 | res = -PSICONV_E_NOMEM; |
|
|
82 | goto ERROR; |
68 | } |
83 | } |
69 | if ((res = psiconv_write_u32(buf,entry->id))) |
84 | if ((res = psiconv_write_u32(config,buf,lev+1,entry->id))) |
70 | return res; |
85 | goto ERROR; |
71 | if ((res = psiconv_write_offset(buf,entry->offset))) |
86 | if ((res = psiconv_write_offset(config,buf,lev+1,entry->offset))) |
72 | return res; |
87 | goto ERROR; |
73 | } |
88 | } |
|
|
89 | psiconv_progress(config,lev,0,"End of section table section"); |
74 | return -PSICONV_E_OK; |
90 | return -PSICONV_E_OK; |
75 | } |
|
|
76 | |
91 | |
|
|
92 | ERROR: |
|
|
93 | psiconv_error(config,lev,0,"Writing of section table section failed"); |
|
|
94 | return res; |
|
|
95 | } |
|
|
96 | |
77 | int psiconv_write_application_id_section(psiconv_buffer buf,psiconv_u32 id, |
97 | int psiconv_write_application_id_section(const psiconv_config config, |
|
|
98 | psiconv_buffer buf,int lev,psiconv_u32 id, |
78 | const psiconv_string_t text) |
99 | const psiconv_string_t text) |
79 | { |
100 | { |
80 | int res; |
101 | int res; |
|
|
102 | psiconv_progress(config,lev,0,"Writing application id section"); |
81 | if ((res = psiconv_write_u32(buf,id))) |
103 | if ((res = psiconv_write_u32(config,buf,lev+1,id))) |
|
|
104 | goto ERROR; |
|
|
105 | if ((res = psiconv_write_string(config,buf,lev+1,text))) |
|
|
106 | goto ERROR; |
|
|
107 | psiconv_progress(config,lev,0,"End of application id section"); |
|
|
108 | return 0; |
|
|
109 | ERROR: |
|
|
110 | psiconv_error(config,lev,0,"Writing of application id section failed"); |
82 | return res; |
111 | return res; |
83 | return psiconv_write_string(buf,text); |
|
|
84 | } |
112 | } |
85 | |
113 | |
86 | int psiconv_write_text_section(psiconv_buffer buf, |
114 | int psiconv_write_text_section(const psiconv_config config, |
|
|
115 | psiconv_buffer buf,int lev, |
87 | const psiconv_text_and_layout value) |
116 | const psiconv_text_and_layout value) |
88 | { |
117 | { |
89 | int res; |
118 | int res; |
90 | psiconv_buffer extra_buf; |
119 | psiconv_buffer extra_buf = NULL; |
91 | int i,j; |
120 | int i,j; |
92 | psiconv_paragraph paragraph; |
121 | psiconv_paragraph paragraph; |
93 | |
122 | |
|
|
123 | psiconv_progress(config,lev,0,"Writing text section"); |
94 | if (!value) { |
124 | if (!value) { |
95 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
125 | psiconv_error(config,lev+1,0,"Null text section"); |
96 | return -PSICONV_E_GENERATE; |
126 | res = -PSICONV_E_GENERATE; |
|
|
127 | goto ERROR; |
97 | } |
128 | } |
98 | |
129 | |
99 | if (psiconv_list_length(value)) { |
130 | if (psiconv_list_length(value)) { |
100 | if (!(extra_buf = psiconv_buffer_new())) |
131 | if (!(extra_buf = psiconv_buffer_new())) { |
101 | return -PSICONV_E_NOMEM; |
132 | res = -PSICONV_E_NOMEM; |
|
|
133 | goto ERROR; |
|
|
134 | } |
102 | for (i = 0; i < psiconv_list_length(value); i++) { |
135 | for (i = 0; i < psiconv_list_length(value); i++) { |
103 | if (!(paragraph = psiconv_list_get(value,i))) { |
136 | if (!(paragraph = psiconv_list_get(value,i))) { |
104 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
137 | psiconv_error(config,lev+1,0,"Massive memory corruption"); |
105 | res = -PSICONV_E_OTHER; |
138 | res = -PSICONV_E_NOMEM; |
106 | goto ERROR; |
139 | goto ERROR; |
107 | } |
140 | } |
108 | for (j = 0; j < strlen(paragraph->text); j++) |
141 | for (j = 0; j < psiconv_unicode_strlen(paragraph->text); j++) |
109 | if ((res = psiconv_write_u8(extra_buf,paragraph->text[j]))) |
142 | if ((res = psiconv_unicode_write_char(config,extra_buf,lev+1, |
|
|
143 | paragraph->text[j]))) |
110 | goto ERROR; |
144 | goto ERROR; |
111 | psiconv_write_u8(extra_buf,0x06); |
145 | psiconv_unicode_write_char(config,extra_buf,lev+1,0x06); |
112 | } |
146 | } |
113 | if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf)))) |
147 | if ((res = psiconv_write_X(config,buf,lev+1,psiconv_buffer_length(extra_buf)))) |
114 | goto ERROR; |
148 | goto ERROR; |
115 | res = psiconv_buffer_concat(buf,extra_buf); |
149 | res = psiconv_buffer_concat(buf,extra_buf); |
116 | } else |
150 | } else |
117 | /* Hack: empty text sections are just not allowed */ |
151 | /* Hack: empty text sections are just not allowed */ |
118 | return psiconv_write_u16(buf,0x0602); |
152 | if ((res = psiconv_write_u16(config,buf,lev+1,0x0602))) |
|
|
153 | goto ERROR; |
|
|
154 | |
|
|
155 | psiconv_progress(config,lev,0,"End of text section"); |
|
|
156 | return 0; |
119 | |
157 | |
120 | ERROR: |
158 | ERROR: |
|
|
159 | if (extra_buf) |
121 | psiconv_buffer_free(extra_buf); |
160 | psiconv_buffer_free(extra_buf); |
|
|
161 | psiconv_error(config,lev,0,"Writing of text section failed"); |
122 | return res; |
162 | return res; |
123 | } |
163 | } |
124 | |
164 | |
125 | int psiconv_write_layout_section(psiconv_buffer buf, |
165 | int psiconv_write_layout_section(const psiconv_config config, |
|
|
166 | psiconv_buffer buf,int lev, |
126 | const psiconv_text_and_layout value, |
167 | const psiconv_text_and_layout value, |
127 | const psiconv_word_styles_section styles, |
168 | const psiconv_word_styles_section styles, |
128 | int with_styles) |
169 | int with_styles) |
129 | { |
170 | { |
130 | typedef struct psiconv_paragraph_type_list_s |
171 | typedef struct psiconv_paragraph_type_list_s |
… | |
… | |
143 | psiconv_in_line_layout in_line = NULL; |
184 | psiconv_in_line_layout in_line = NULL; |
144 | psiconv_word_style style; |
185 | psiconv_word_style style; |
145 | psiconv_character_layout para_charlayout; |
186 | psiconv_character_layout para_charlayout; |
146 | int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen; |
187 | int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen; |
147 | |
188 | |
|
|
189 | psiconv_progress(config,lev,0,"Writing layout section"); |
148 | if (!value) { |
190 | if (!value) { |
149 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
191 | psiconv_error(config,lev,0,"Null text section"); |
150 | return -PSICONV_E_GENERATE; |
192 | res = -PSICONV_E_GENERATE; |
|
|
193 | goto ERROR1; |
151 | } |
194 | } |
152 | |
195 | |
153 | if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) { |
196 | if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) { |
154 | res = -PSICONV_E_NOMEM; |
197 | res = -PSICONV_E_NOMEM; |
155 | goto ERROR1; |
198 | goto ERROR1; |
… | |
… | |
175 | goto ERROR5; |
218 | goto ERROR5; |
176 | } |
219 | } |
177 | |
220 | |
178 | for (i = 0; i < psiconv_list_length(value); i++) { |
221 | for (i = 0; i < psiconv_list_length(value); i++) { |
179 | if (!(paragraph = psiconv_list_get(value,i))) { |
222 | if (!(paragraph = psiconv_list_get(value,i))) { |
180 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
223 | psiconv_error(config,lev+1,0,"Massive memory corruption"); |
181 | res = -PSICONV_E_OTHER; |
224 | res = -PSICONV_E_NOMEM; |
182 | goto ERROR6; |
225 | goto ERROR6; |
183 | } |
226 | } |
184 | if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) |
227 | if ((res = psiconv_write_u32(config,buf_elements,lev+1, |
|
|
228 | psiconv_unicode_strlen(paragraph->text)+1))) |
185 | goto ERROR6; |
229 | goto ERROR6; |
186 | |
230 | |
187 | /* We need it for the next if-statement */ |
231 | /* We need it for the next if-statement */ |
188 | if (psiconv_list_length(paragraph->in_lines) == 1) |
232 | if (psiconv_list_length(paragraph->in_lines) == 1) |
189 | if (!(in_line = psiconv_list_get(paragraph->in_lines,1))) |
233 | if (!(in_line = psiconv_list_get(paragraph->in_lines,1))) |
… | |
… | |
192 | if ((psiconv_list_length(paragraph->in_lines) > 1) || |
236 | if ((psiconv_list_length(paragraph->in_lines) > 1) || |
193 | ((psiconv_list_length(paragraph->in_lines) == 1) && |
237 | ((psiconv_list_length(paragraph->in_lines) == 1) && |
194 | (in_line->object != NULL))) { |
238 | (in_line->object != NULL))) { |
195 | /* Inline layouts, or an object, so we generate a paragraph element |
239 | /* Inline layouts, or an object, so we generate a paragraph element |
196 | and inline elements */ |
240 | and inline elements */ |
197 | if ((res = psiconv_write_u8(buf_elements,0x00))) |
241 | if ((res = psiconv_write_u8(config,buf_elements,lev+1,0x00))) |
198 | goto ERROR6; |
242 | goto ERROR6; |
199 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
243 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
200 | psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); |
244 | psiconv_error(config,lev+1,0,"Unknown style"); |
201 | res = -PSICONV_E_GENERATE; |
245 | res = -PSICONV_E_GENERATE; |
202 | goto ERROR6; |
246 | goto ERROR6; |
203 | } |
247 | } |
204 | if ((res = psiconv_write_paragraph_layout_list(buf_elements, |
248 | if ((res = psiconv_write_paragraph_layout_list(config,buf_elements,lev+1, |
205 | paragraph->base_paragraph, |
249 | paragraph->base_paragraph, |
206 | style->paragraph))) |
250 | style->paragraph))) |
207 | goto ERROR6; |
251 | goto ERROR6; |
208 | if (with_styles) |
252 | if (with_styles) |
209 | if ((res = psiconv_write_u8(buf_elements,paragraph->base_style))) |
253 | if ((res = psiconv_write_u8(config,buf_elements,lev+1,paragraph->base_style))) |
210 | goto ERROR6; |
254 | goto ERROR6; |
211 | if ((res = psiconv_write_u32(buf_elements, |
255 | if ((res = psiconv_write_u32(config,buf_elements,lev+1, |
212 | psiconv_list_length(paragraph->in_lines)))) |
256 | psiconv_list_length(paragraph->in_lines)))) |
213 | goto ERROR6; |
257 | goto ERROR6; |
214 | |
258 | |
215 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
259 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
216 | paralen = 0; |
260 | paralen = 0; |
217 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
261 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
218 | nr_of_inlines ++; |
262 | nr_of_inlines ++; |
219 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
263 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
220 | psiconv_warn(0,psiconv_buffer_length(buf), |
264 | psiconv_error(config,lev,0,"Massive memory corruption"); |
221 | "Massive memory corruption"); |
|
|
222 | res = -PSICONV_E_OTHER; |
265 | res = -PSICONV_E_NOMEM; |
223 | goto ERROR6; |
266 | goto ERROR6; |
224 | } |
267 | } |
225 | if ((res = psiconv_write_u8(buf_inlines,in_line->object?0x01:0x00))) |
268 | if ((res = psiconv_write_u8(config,buf_inlines,lev+1,in_line->object?0x01:0x00))) |
226 | goto ERROR6; |
269 | goto ERROR6; |
227 | thislen = in_line->length; |
270 | thislen = in_line->length; |
228 | paralen += thislen; |
271 | paralen += thislen; |
229 | /* If this is the last in_line, we need to make sure that the |
272 | /* If this is the last in_line, we need to make sure that the |
230 | complete length of all inlines equals the text length */ |
273 | complete length of all inlines equals the text length */ |
231 | if (j == psiconv_list_length(paragraph->in_lines)-1) { |
274 | if (j == psiconv_list_length(paragraph->in_lines)-1) { |
232 | if (paralen > strlen(paragraph->text)+1) { |
275 | if (paralen > psiconv_unicode_strlen(paragraph->text)+1) { |
233 | res = -PSICONV_E_GENERATE; |
276 | res = -PSICONV_E_GENERATE; |
234 | goto ERROR6; |
277 | goto ERROR6; |
235 | } |
278 | } |
236 | thislen += strlen(paragraph->text)+1-paralen; |
279 | thislen += psiconv_unicode_strlen(paragraph->text)+1-paralen; |
237 | } |
280 | } |
238 | if ((res = psiconv_write_u32(buf_inlines,thislen))) |
281 | if ((res = psiconv_write_u32(config,buf_inlines,lev+1,thislen))) |
239 | goto ERROR6; |
282 | goto ERROR6; |
240 | if ((res = psiconv_write_character_layout_list(buf_inlines, |
283 | if ((res = psiconv_write_character_layout_list(config,buf_inlines,lev+1, |
241 | in_line->layout, |
284 | in_line->layout, |
242 | style->character))) |
285 | style->character))) |
243 | goto ERROR6; |
286 | goto ERROR6; |
244 | if (in_line->object) { |
287 | if (in_line->object) { |
245 | if ((res = psiconv_write_u32(buf_inlines,PSICONV_ID_OBJECT))) |
288 | if ((res = psiconv_write_u32(config,buf_inlines,lev+1,PSICONV_ID_OBJECT))) |
246 | goto ERROR6; |
289 | goto ERROR6; |
247 | obj_id = psiconv_buffer_unique_id(); |
290 | obj_id = psiconv_buffer_unique_id(); |
248 | if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id))) |
291 | if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id))) |
249 | goto ERROR6; |
292 | goto ERROR6; |
250 | if ((res = psiconv_buffer_add_target(buf_objects,obj_id))) |
293 | if ((res = psiconv_buffer_add_target(buf_objects,obj_id))) |
251 | goto ERROR6; |
294 | goto ERROR6; |
252 | if ((res = psiconv_write_embedded_object_section(buf_objects, |
295 | if ((res = psiconv_write_embedded_object_section(config,buf_objects,lev+1, |
253 | in_line->object))) |
296 | in_line->object))) |
254 | goto ERROR6; |
297 | goto ERROR6; |
255 | if ((res = psiconv_write_length(buf_inlines,in_line->object_width))) |
298 | if ((res = psiconv_write_length(config,buf_inlines,lev+1,in_line->object_width))) |
256 | goto ERROR6; |
299 | goto ERROR6; |
257 | if ((res = psiconv_write_length(buf_inlines,in_line->object_height))) |
300 | if ((res = psiconv_write_length(config,buf_inlines,lev+1,in_line->object_height))) |
258 | goto ERROR6; |
301 | goto ERROR6; |
259 | } |
302 | } |
260 | } |
303 | } |
261 | } else { |
304 | } else { |
262 | /* No inline layouts (or only 1), so we generate a paragraph type list */ |
305 | /* No inline layouts (or only 1), so we generate a paragraph type list */ |
… | |
… | |
264 | /* Set para_charlayout to the correct character-level layout */ |
307 | /* Set para_charlayout to the correct character-level layout */ |
265 | if (psiconv_list_length(paragraph->in_lines) == 0) |
308 | if (psiconv_list_length(paragraph->in_lines) == 0) |
266 | para_charlayout = paragraph->base_character; |
309 | para_charlayout = paragraph->base_character; |
267 | else { |
310 | else { |
268 | if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { |
311 | if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { |
269 | psiconv_warn(0,psiconv_buffer_length(buf), |
312 | psiconv_error(config,lev,0,"Massive memory corruption"); |
270 | "Massive memory corruption"); |
|
|
271 | res = -PSICONV_E_OTHER; |
313 | res = -PSICONV_E_NOMEM; |
272 | goto ERROR6; |
314 | goto ERROR6; |
273 | } |
315 | } |
274 | para_charlayout = in_line->layout; |
316 | para_charlayout = in_line->layout; |
275 | } |
317 | } |
276 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
318 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
277 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
319 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
278 | psiconv_warn(0,psiconv_buffer_length(buf), |
320 | psiconv_error(config,lev,0,"Massive memory corruption"); |
279 | "Massive memory corruption"); |
|
|
280 | res = -PSICONV_E_OTHER; |
321 | res = -PSICONV_E_NOMEM; |
281 | goto ERROR6; |
322 | goto ERROR6; |
282 | } |
323 | } |
283 | if ((paragraph->base_style == paragraph_type->style) && |
324 | if ((paragraph->base_style == paragraph_type->style) && |
284 | !psiconv_compare_character_layout(para_charlayout, |
325 | !psiconv_compare_character_layout(para_charlayout, |
285 | paragraph_type->character) && |
326 | paragraph_type->character) && |
… | |
… | |
297 | new_type.character = para_charlayout; |
338 | new_type.character = para_charlayout; |
298 | new_type.style = paragraph->base_style; |
339 | new_type.style = paragraph->base_style; |
299 | paragraph_type = &new_type; |
340 | paragraph_type = &new_type; |
300 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
341 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
301 | goto ERROR6; |
342 | goto ERROR6; |
302 | if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) |
343 | if ((res = psiconv_write_u32(config,buf_types,lev+1,paragraph_type->nr))) |
303 | goto ERROR6; |
344 | goto ERROR6; |
304 | if (!(style = psiconv_get_style(styles,paragraph_type->style))) { |
345 | if (!(style = psiconv_get_style(styles,paragraph_type->style))) { |
305 | psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); |
346 | psiconv_error(config,lev,0,"Unknown style"); |
306 | res = -PSICONV_E_GENERATE; |
347 | res = -PSICONV_E_GENERATE; |
307 | goto ERROR6; |
348 | goto ERROR6; |
308 | } |
349 | } |
309 | if ((res = psiconv_write_paragraph_layout_list(buf_types, |
350 | if ((res = psiconv_write_paragraph_layout_list(config,buf_types,lev+1, |
310 | paragraph_type->paragraph,style->paragraph))) |
351 | paragraph_type->paragraph,style->paragraph))) |
311 | goto ERROR6; |
352 | goto ERROR6; |
312 | if (with_styles) |
353 | if (with_styles) |
313 | if ((res = psiconv_write_u8(buf_types,paragraph_type->style))) |
354 | if ((res = psiconv_write_u8(config,buf_types,lev+1,paragraph_type->style))) |
314 | goto ERROR6; |
355 | goto ERROR6; |
315 | if ((res = psiconv_write_character_layout_list(buf_types, |
356 | if ((res = psiconv_write_character_layout_list(config,buf_types,lev+1, |
316 | paragraph_type->character,style->character))) |
357 | paragraph_type->character,style->character))) |
317 | goto ERROR6; |
358 | goto ERROR6; |
318 | } |
359 | } |
319 | if ((res = psiconv_write_u8(buf_elements,para_type))) |
360 | if ((res = psiconv_write_u8(config,buf_elements,lev+1,para_type))) |
320 | goto ERROR6; |
361 | goto ERROR6; |
321 | } |
362 | } |
322 | } |
363 | } |
323 | |
364 | |
324 | /* HACK: special case: no paragraphs at all. We need to improvize. */ |
365 | /* HACK: special case: no paragraphs at all. We need to improvize. */ |
325 | if (!psiconv_list_length(value)) { |
366 | if (!psiconv_list_length(value)) { |
326 | if ((res = psiconv_write_u32(buf_types,1))) |
367 | if ((res = psiconv_write_u32(config,buf_types,lev+1,1))) |
327 | goto ERROR6; |
368 | goto ERROR6; |
328 | if ((res = psiconv_write_u32(buf_types,0))) |
369 | if ((res = psiconv_write_u32(config,buf_types,lev+1,0))) |
329 | goto ERROR6; |
370 | goto ERROR6; |
330 | if (with_styles) |
371 | if (with_styles) |
331 | if ((res = psiconv_write_u8(buf_types,0))) |
372 | if ((res = psiconv_write_u8(config,buf_types,lev+1,0))) |
332 | goto ERROR6; |
373 | goto ERROR6; |
333 | if ((res = psiconv_write_u32(buf_types,0))) |
374 | if ((res = psiconv_write_u32(config,buf_types,lev+1,0))) |
334 | goto ERROR6; |
375 | goto ERROR6; |
335 | |
376 | |
336 | if ((res = psiconv_write_u32(buf_elements,1))) |
377 | if ((res = psiconv_write_u32(config,buf_elements,lev+1,1))) |
337 | goto ERROR6; |
378 | goto ERROR6; |
338 | if ((res = psiconv_write_u8(buf_elements,1))) |
379 | if ((res = psiconv_write_u8(config,buf_elements,lev+1,1))) |
339 | goto ERROR6; |
380 | goto ERROR6; |
340 | pel_length = 1; |
381 | pel_length = 1; |
341 | ptl_length = 1; |
382 | ptl_length = 1; |
342 | } else { |
383 | } else { |
343 | pel_length = psiconv_list_length(value); |
384 | pel_length = psiconv_list_length(value); |
344 | ptl_length = psiconv_list_length(paragraph_type_list); |
385 | ptl_length = psiconv_list_length(paragraph_type_list); |
345 | } |
386 | } |
346 | |
387 | |
347 | /* Now append everything */ |
388 | /* Now append everything */ |
348 | if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) |
389 | if ((res = psiconv_write_u16(config,buf,lev+1,with_styles?0x0001:0x0000))) |
349 | goto ERROR6; |
390 | goto ERROR6; |
350 | if ((res = psiconv_write_u8(buf, ptl_length))) |
391 | if ((res = psiconv_write_u8(config,buf,lev+1, ptl_length))) |
351 | goto ERROR6; |
392 | goto ERROR6; |
352 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
393 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
353 | goto ERROR6; |
394 | goto ERROR6; |
354 | if ((res = psiconv_write_u32(buf,pel_length))) |
395 | if ((res = psiconv_write_u32(config,buf,lev+1,pel_length))) |
355 | goto ERROR6; |
396 | goto ERROR6; |
356 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
397 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
357 | goto ERROR6; |
398 | goto ERROR6; |
358 | if ((res = psiconv_write_u32(buf,nr_of_inlines))) |
399 | if ((res = psiconv_write_u32(config,buf,lev+1,nr_of_inlines))) |
359 | goto ERROR6; |
400 | goto ERROR6; |
360 | if ((res = psiconv_buffer_concat(buf,buf_inlines))) |
401 | if ((res = psiconv_buffer_concat(buf,buf_inlines))) |
361 | goto ERROR6; |
402 | goto ERROR6; |
362 | if ((res = psiconv_buffer_concat(buf,buf_objects))) |
403 | if ((res = psiconv_buffer_concat(buf,buf_objects))) |
363 | goto ERROR6; |
404 | goto ERROR6; |
… | |
… | |
371 | ERROR3: |
412 | ERROR3: |
372 | psiconv_buffer_free(buf_types); |
413 | psiconv_buffer_free(buf_types); |
373 | ERROR2: |
414 | ERROR2: |
374 | psiconv_list_free(paragraph_type_list); |
415 | psiconv_list_free(paragraph_type_list); |
375 | ERROR1: |
416 | ERROR1: |
|
|
417 | if (res) |
|
|
418 | psiconv_error(config,lev,0,"Writing of layout section failed"); |
|
|
419 | else |
|
|
420 | psiconv_progress(config,lev,0,"End of layout section"); |
376 | return res; |
421 | return res; |
377 | } |
422 | } |
378 | |
423 | |
379 | int psiconv_write_styled_layout_section(psiconv_buffer buf, |
424 | int psiconv_write_styled_layout_section(const psiconv_config config, |
|
|
425 | psiconv_buffer buf,int lev, |
380 | psiconv_text_and_layout result, |
426 | psiconv_text_and_layout result, |
381 | psiconv_word_styles_section styles) |
427 | psiconv_word_styles_section styles) |
382 | { |
428 | { |
|
|
429 | int res; |
|
|
430 | |
|
|
431 | psiconv_progress(config,lev,0,"Writing styled layout section"); |
383 | return psiconv_write_layout_section(buf,result,styles,1); |
432 | res = psiconv_write_layout_section(config,buf,lev+1,result,styles,1); |
|
|
433 | if (res) |
|
|
434 | psiconv_error(config,lev,0,"Writing of styles layout section failed"); |
|
|
435 | else |
|
|
436 | psiconv_progress(config,lev,0,"End of styled layout section"); |
|
|
437 | return res; |
384 | } |
438 | } |
385 | |
439 | |
386 | int psiconv_write_styleless_layout_section(psiconv_buffer buf, |
440 | int psiconv_write_styleless_layout_section(const psiconv_config config, |
|
|
441 | psiconv_buffer buf,int lev, |
387 | const psiconv_text_and_layout value, |
442 | const psiconv_text_and_layout value, |
388 | const psiconv_character_layout base_char, |
443 | const psiconv_character_layout base_char, |
389 | const psiconv_paragraph_layout base_para) |
444 | const psiconv_paragraph_layout base_para) |
390 | { |
445 | { |
391 | int res = 0; |
446 | int res = 0; |
392 | psiconv_word_styles_section styles_section; |
447 | psiconv_word_styles_section styles_section; |
393 | |
448 | |
|
|
449 | psiconv_progress(config,lev,0,"Writing styleless layout section"); |
394 | if (!(styles_section = malloc(sizeof(*styles_section)))) |
450 | if (!(styles_section = malloc(sizeof(*styles_section)))) |
395 | goto ERROR1; |
451 | goto ERROR1; |
396 | if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) |
452 | if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) |
397 | goto ERROR2; |
453 | goto ERROR2; |
398 | if (!(styles_section->normal->character = |
454 | if (!(styles_section->normal->character = |
… | |
… | |
400 | goto ERROR3; |
456 | goto ERROR3; |
401 | if (!(styles_section->normal->paragraph = |
457 | if (!(styles_section->normal->paragraph = |
402 | psiconv_clone_paragraph_layout(base_para))) |
458 | psiconv_clone_paragraph_layout(base_para))) |
403 | goto ERROR4; |
459 | goto ERROR4; |
404 | styles_section->normal->hotkey = 0; |
460 | styles_section->normal->hotkey = 0; |
405 | if (!(styles_section->normal->name = strdup(""))) |
461 | if (!(styles_section->normal->name = psiconv_unicode_empty_string())) |
406 | goto ERROR5; |
462 | goto ERROR5; |
407 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
463 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
408 | struct psiconv_word_style_s)))) |
464 | struct psiconv_word_style_s)))) |
409 | goto ERROR6; |
465 | goto ERROR6; |
410 | |
466 | |
411 | res = psiconv_write_layout_section(buf,value,styles_section,0); |
467 | res = psiconv_write_layout_section(config,buf,lev+1,value,styles_section,0); |
412 | psiconv_free_word_styles_section(styles_section); |
468 | psiconv_free_word_styles_section(styles_section); |
|
|
469 | psiconv_progress(config,lev,0,"End of styleless layout section"); |
413 | return res; |
470 | return res; |
414 | |
471 | |
415 | ERROR6: |
472 | ERROR6: |
416 | free(styles_section->normal->name); |
473 | free(styles_section->normal->name); |
417 | ERROR5: |
474 | ERROR5: |
… | |
… | |
421 | ERROR3: |
478 | ERROR3: |
422 | free(styles_section->normal); |
479 | free(styles_section->normal); |
423 | ERROR2: |
480 | ERROR2: |
424 | free(styles_section); |
481 | free(styles_section); |
425 | ERROR1: |
482 | ERROR1: |
|
|
483 | psiconv_error(config,lev,0,"Writing of styleless layout section failed"); |
426 | if (!res) |
484 | if (!res) |
427 | return -PSICONV_E_NOMEM; |
485 | return -PSICONV_E_NOMEM; |
428 | else |
486 | else |
429 | return res; |
487 | return res; |
430 | } |
488 | } |
431 | |
489 | |
432 | |
490 | |
433 | int psiconv_write_embedded_object_section(psiconv_buffer buf, |
491 | int psiconv_write_embedded_object_section(const psiconv_config config, |
|
|
492 | psiconv_buffer buf,int lev, |
434 | const psiconv_embedded_object_section value) |
493 | const psiconv_embedded_object_section value) |
435 | { |
494 | { |
436 | int res; |
495 | int res; |
437 | psiconv_u32 display_id,icon_id,table_id; |
496 | psiconv_u32 display_id,icon_id,table_id; |
438 | psiconv_buffer extra_buf; |
497 | psiconv_buffer extra_buf; |
439 | |
498 | |
|
|
499 | psiconv_progress(config,lev,0,"Writing embedded object section"); |
440 | if (!value) { |
500 | if (!value) { |
441 | psiconv_warn(0,psiconv_buffer_length(buf),"Null Object"); |
501 | psiconv_error(config,lev,0,"Null Object"); |
442 | res = -PSICONV_E_GENERATE; |
502 | res = -PSICONV_E_GENERATE; |
443 | goto ERROR1; |
503 | goto ERROR1; |
444 | } |
504 | } |
445 | |
505 | |
446 | if (!(extra_buf = psiconv_buffer_new())) { |
506 | if (!(extra_buf = psiconv_buffer_new())) { |
… | |
… | |
449 | } |
509 | } |
450 | |
510 | |
451 | display_id = psiconv_buffer_unique_id(); |
511 | display_id = psiconv_buffer_unique_id(); |
452 | icon_id = psiconv_buffer_unique_id(); |
512 | icon_id = psiconv_buffer_unique_id(); |
453 | table_id = psiconv_buffer_unique_id(); |
513 | table_id = psiconv_buffer_unique_id(); |
454 | if ((res = psiconv_write_u8(buf,0x06))) |
514 | if ((res = psiconv_write_u8(config,buf,lev+1,0x06))) |
455 | goto ERROR2; |
515 | goto ERROR2; |
456 | if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_DISPLAY_SECTION))) |
516 | if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_DISPLAY_SECTION))) |
457 | goto ERROR2; |
517 | goto ERROR2; |
458 | if ((res = psiconv_buffer_add_reference(buf,display_id))) |
518 | if ((res = psiconv_buffer_add_reference(buf,display_id))) |
459 | goto ERROR2; |
519 | goto ERROR2; |
460 | if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_ICON_SECTION))) |
520 | if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_ICON_SECTION))) |
461 | goto ERROR2; |
521 | goto ERROR2; |
462 | if ((res = psiconv_buffer_add_reference(buf,icon_id))) |
522 | if ((res = psiconv_buffer_add_reference(buf,icon_id))) |
463 | goto ERROR2; |
523 | goto ERROR2; |
464 | if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION))) |
524 | if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION))) |
465 | goto ERROR2; |
525 | goto ERROR2; |
466 | if ((res = psiconv_buffer_add_reference(buf,table_id))) |
526 | if ((res = psiconv_buffer_add_reference(buf,table_id))) |
467 | goto ERROR2; |
527 | goto ERROR2; |
468 | |
528 | |
469 | if ((res = psiconv_buffer_add_target(buf,display_id))) |
529 | if ((res = psiconv_buffer_add_target(buf,display_id))) |
470 | goto ERROR2; |
530 | goto ERROR2; |
471 | if ((res = psiconv_write_object_display_section(buf,value->display))) |
531 | if ((res = psiconv_write_object_display_section(config,buf,lev+1,value->display))) |
472 | goto ERROR2; |
532 | goto ERROR2; |
473 | if ((res = psiconv_buffer_add_target(buf,icon_id))) |
533 | if ((res = psiconv_buffer_add_target(buf,icon_id))) |
474 | goto ERROR2; |
534 | goto ERROR2; |
475 | if ((res = psiconv_write_object_icon_section(buf,value->icon))) |
535 | if ((res = psiconv_write_object_icon_section(config,buf,lev+1,value->icon))) |
476 | goto ERROR2; |
536 | goto ERROR2; |
477 | if ((res = psiconv_buffer_add_target(buf,table_id))) |
537 | if ((res = psiconv_buffer_add_target(buf,table_id))) |
478 | goto ERROR2; |
538 | goto ERROR2; |
479 | switch(value->object->type) { |
539 | switch(value->object->type) { |
480 | case psiconv_word_file: |
540 | case psiconv_word_file: |
481 | if ((res = psiconv_write_word_file(extra_buf, |
541 | if ((res = psiconv_write_word_file(config,extra_buf,lev+1, |
482 | (psiconv_word_f) value->object->file))) |
542 | (psiconv_word_f) value->object->file))) |
483 | goto ERROR2; |
543 | goto ERROR2; |
484 | break; |
544 | break; |
485 | /* |
|
|
486 | case psiconv_sketch_file: |
545 | case psiconv_sketch_file: |
487 | if ((res = psiconv_write_sketch_file(extra_buf, |
546 | if ((res = psiconv_write_sketch_file(config,extra_buf,lev+1, |
488 | (psiconv_sketch_f) value->object->file))) |
547 | (psiconv_sketch_f) value->object->file))) |
489 | goto ERROR2; |
548 | goto ERROR2; |
490 | break; |
549 | break; |
|
|
550 | /* |
491 | case psiconv_sheet_file: |
551 | case psiconv_sheet_file: |
492 | if ((res = psiconv_write_sheet_file(extra_buf, |
552 | if ((res = psiconv_write_sheet_file(config,extra_buf,lev+1, |
493 | (psiconv_sheet_f) value->object->file))) |
553 | (psiconv_sheet_f) value->object->file))) |
494 | goto ERROR2; |
554 | goto ERROR2; |
495 | break; |
555 | break; |
496 | */ |
556 | */ |
497 | default: |
557 | default: |
498 | psiconv_warn(0,psiconv_buffer_length(buf), |
558 | psiconv_error(config,lev,0,"Unknown or unsupported object type"); |
499 | "Unknown or unsupported object type"); |
|
|
500 | res = -PSICONV_E_GENERATE; |
559 | res = -PSICONV_E_GENERATE; |
501 | goto ERROR2; |
560 | goto ERROR2; |
502 | } |
561 | } |
503 | |
562 | |
504 | if ((res = psiconv_buffer_resolve(extra_buf))) |
563 | if ((res = psiconv_buffer_resolve(extra_buf))) |
505 | goto ERROR2; |
564 | goto ERROR2; |
506 | if ((res = psiconv_buffer_concat(buf,extra_buf))) |
565 | if ((res = psiconv_buffer_concat(buf,extra_buf))) |
507 | goto ERROR2; |
566 | goto ERROR2; |
|
|
567 | psiconv_buffer_free(extra_buf); |
508 | |
568 | |
|
|
569 | psiconv_progress(config,lev,0,"End of embedded object section"); |
509 | return 0; |
570 | return 0; |
510 | |
571 | |
511 | ERROR2: |
572 | ERROR2: |
512 | psiconv_buffer_free(extra_buf); |
573 | psiconv_buffer_free(extra_buf); |
513 | ERROR1: |
574 | ERROR1: |
|
|
575 | psiconv_error(config,lev,0,"Writing of embedded object section failed"); |
514 | return res; |
576 | return res; |
515 | } |
577 | } |
516 | |
578 | |
517 | |
579 | |
518 | int psiconv_write_object_display_section(psiconv_buffer buf, |
580 | int psiconv_write_object_display_section(const psiconv_config config, |
|
|
581 | psiconv_buffer buf,int lev, |
519 | const psiconv_object_display_section value) |
582 | const psiconv_object_display_section value) |
520 | { |
583 | { |
521 | int res; |
584 | int res; |
522 | |
585 | |
|
|
586 | psiconv_progress(config,lev,0,"Writing object display section"); |
|
|
587 | |
523 | if (!value) { |
588 | if (!value) { |
524 | psiconv_warn(0,psiconv_buffer_length(buf),"Null Object Display Section"); |
589 | psiconv_error(config,lev,0,"Null Object Display Section"); |
525 | res = -PSICONV_E_GENERATE; |
590 | res = -PSICONV_E_GENERATE; |
526 | goto ERROR1; |
591 | goto ERROR1; |
527 | } |
592 | } |
528 | |
593 | |
529 | if ((res = psiconv_write_u8(buf,value->show_icon?0x00:0x01))) |
594 | if ((res = psiconv_write_u8(config,buf,lev+1,value->show_icon?0x00:0x01))) |
530 | goto ERROR1; |
595 | goto ERROR1; |
531 | if ((res = psiconv_write_length(buf,value->width))) |
596 | if ((res = psiconv_write_length(config,buf,lev+1,value->width))) |
532 | goto ERROR1; |
597 | goto ERROR1; |
533 | if ((res = psiconv_write_length(buf,value->height))) |
598 | if ((res = psiconv_write_length(config,buf,lev+1,value->height))) |
534 | goto ERROR1; |
599 | goto ERROR1; |
535 | if ((res = psiconv_write_u32(buf,0x00000000))) |
600 | if ((res = psiconv_write_u32(config,buf,lev+1,0x00000000))) |
536 | goto ERROR1; |
601 | goto ERROR1; |
|
|
602 | |
|
|
603 | psiconv_progress(config,lev,0,"End of object display section"); |
537 | |
604 | |
538 | return 0; |
605 | return 0; |
539 | |
606 | |
540 | ERROR1: |
607 | ERROR1: |
|
|
608 | psiconv_error(config,lev,0,"Writing of object display section failed"); |
541 | return res; |
609 | return res; |
542 | } |
610 | } |
543 | |
611 | |
544 | int psiconv_write_object_icon_section(psiconv_buffer buf, |
612 | int psiconv_write_object_icon_section(const psiconv_config config, |
|
|
613 | psiconv_buffer buf,int lev, |
545 | const psiconv_object_icon_section value) |
614 | const psiconv_object_icon_section value) |
546 | { |
615 | { |
547 | int res; |
616 | int res; |
548 | |
617 | |
|
|
618 | psiconv_progress(config,lev,0,"Writing object icon section"); |
|
|
619 | |
549 | if (!value) { |
620 | if (!value) { |
550 | psiconv_warn(0,psiconv_buffer_length(buf),"Null Object Icon Section"); |
621 | psiconv_error(config,lev,0,"Null Object Icon Section"); |
551 | res = -PSICONV_E_GENERATE; |
622 | res = -PSICONV_E_GENERATE; |
552 | goto ERROR1; |
623 | goto ERROR1; |
553 | } |
624 | } |
554 | |
625 | |
555 | if ((res = psiconv_write_string(buf,value->icon_name))) |
626 | if ((res = psiconv_write_string(config,buf,lev+1,value->icon_name))) |
556 | goto ERROR1; |
627 | goto ERROR1; |
557 | if ((res = psiconv_write_length(buf,value->icon_width))) |
628 | if ((res = psiconv_write_length(config,buf,lev+1,value->icon_width))) |
558 | goto ERROR1; |
629 | goto ERROR1; |
559 | if ((res = psiconv_write_length(buf,value->icon_height))) |
630 | if ((res = psiconv_write_length(config,buf,lev+1,value->icon_height))) |
560 | goto ERROR1; |
631 | goto ERROR1; |
561 | |
632 | |
|
|
633 | psiconv_progress(config,lev,0,"End of object icon section"); |
562 | return 0; |
634 | return 0; |
563 | |
635 | |
564 | ERROR1: |
636 | ERROR1: |
|
|
637 | psiconv_error(config,lev,0,"Writing of object icon section failed"); |
565 | return res; |
638 | return res; |
566 | } |
639 | } |