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))) { |
|
|
234 | res = -PSICONV_E_NOMEM; |
190 | goto ERROR6; |
235 | goto ERROR6; |
|
|
236 | } |
191 | |
237 | |
192 | if ((psiconv_list_length(paragraph->in_lines) > 1) || |
238 | if ((psiconv_list_length(paragraph->in_lines) > 1) || |
193 | ((psiconv_list_length(paragraph->in_lines) == 1) && |
239 | ((psiconv_list_length(paragraph->in_lines) == 1) && |
194 | (in_line->object != NULL))) { |
240 | (in_line->object != NULL))) { |
195 | /* Inline layouts, or an object, so we generate a paragraph element |
241 | /* Inline layouts, or an object, so we generate a paragraph element |
196 | and inline elements */ |
242 | and inline elements */ |
197 | if ((res = psiconv_write_u8(buf_elements,0x00))) |
243 | if ((res = psiconv_write_u8(config,buf_elements,lev+1,0x00))) |
198 | goto ERROR6; |
244 | goto ERROR6; |
199 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
245 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
200 | psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); |
246 | psiconv_error(config,lev+1,0,"Unknown style"); |
201 | res = -PSICONV_E_GENERATE; |
247 | res = -PSICONV_E_GENERATE; |
202 | goto ERROR6; |
248 | goto ERROR6; |
203 | } |
249 | } |
204 | if ((res = psiconv_write_paragraph_layout_list(buf_elements, |
250 | if ((res = psiconv_write_paragraph_layout_list(config,buf_elements,lev+1, |
205 | paragraph->base_paragraph, |
251 | paragraph->base_paragraph, |
206 | style->paragraph))) |
252 | style->paragraph))) |
207 | goto ERROR6; |
253 | goto ERROR6; |
208 | if (with_styles) |
254 | if (with_styles) |
209 | if ((res = psiconv_write_u8(buf_elements,paragraph->base_style))) |
255 | if ((res = psiconv_write_u8(config,buf_elements,lev+1,paragraph->base_style))) |
210 | goto ERROR6; |
256 | goto ERROR6; |
211 | if ((res = psiconv_write_u32(buf_elements, |
257 | if ((res = psiconv_write_u32(config,buf_elements,lev+1, |
212 | psiconv_list_length(paragraph->in_lines)))) |
258 | psiconv_list_length(paragraph->in_lines)))) |
213 | goto ERROR6; |
259 | goto ERROR6; |
214 | |
260 | |
215 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
261 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
216 | paralen = 0; |
262 | paralen = 0; |
217 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
263 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
218 | nr_of_inlines ++; |
264 | nr_of_inlines ++; |
219 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
265 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
220 | psiconv_warn(0,psiconv_buffer_length(buf), |
266 | psiconv_error(config,lev,0,"Massive memory corruption"); |
221 | "Massive memory corruption"); |
|
|
222 | res = -PSICONV_E_OTHER; |
267 | res = -PSICONV_E_NOMEM; |
223 | goto ERROR6; |
268 | goto ERROR6; |
224 | } |
269 | } |
225 | if ((res = psiconv_write_u8(buf_inlines,in_line->object?0x01:0x00))) |
270 | if ((res = psiconv_write_u8(config,buf_inlines,lev+1,in_line->object?0x01:0x00))) |
226 | goto ERROR6; |
271 | goto ERROR6; |
227 | thislen = in_line->length; |
272 | thislen = in_line->length; |
228 | paralen += thislen; |
273 | paralen += thislen; |
229 | /* If this is the last in_line, we need to make sure that the |
274 | /* If this is the last in_line, we need to make sure that the |
230 | complete length of all inlines equals the text length */ |
275 | complete length of all inlines equals the text length */ |
231 | if (j == psiconv_list_length(paragraph->in_lines)-1) { |
276 | if (j == psiconv_list_length(paragraph->in_lines)-1) { |
232 | if (paralen > strlen(paragraph->text)+1) { |
277 | if (paralen > psiconv_unicode_strlen(paragraph->text)+1) { |
233 | res = -PSICONV_E_GENERATE; |
278 | res = -PSICONV_E_GENERATE; |
234 | goto ERROR6; |
279 | goto ERROR6; |
235 | } |
280 | } |
236 | thislen += strlen(paragraph->text)+1-paralen; |
281 | thislen += psiconv_unicode_strlen(paragraph->text)+1-paralen; |
237 | } |
282 | } |
238 | if ((res = psiconv_write_u32(buf_inlines,thislen))) |
283 | if ((res = psiconv_write_u32(config,buf_inlines,lev+1,thislen))) |
239 | goto ERROR6; |
284 | goto ERROR6; |
240 | if ((res = psiconv_write_character_layout_list(buf_inlines, |
285 | if ((res = psiconv_write_character_layout_list(config,buf_inlines,lev+1, |
241 | in_line->layout, |
286 | in_line->layout, |
242 | style->character))) |
287 | style->character))) |
243 | goto ERROR6; |
288 | goto ERROR6; |
244 | if (in_line->object) { |
289 | if (in_line->object) { |
245 | if ((res = psiconv_write_u32(buf_inlines,PSICONV_ID_OBJECT))) |
290 | if ((res = psiconv_write_u32(config,buf_inlines,lev+1,PSICONV_ID_OBJECT))) |
246 | goto ERROR6; |
291 | goto ERROR6; |
247 | obj_id = psiconv_buffer_unique_id(); |
292 | obj_id = psiconv_buffer_unique_id(); |
248 | if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id))) |
293 | if ((res = psiconv_buffer_add_reference(buf_inlines,obj_id))) |
249 | goto ERROR6; |
294 | goto ERROR6; |
250 | if ((res = psiconv_buffer_add_target(buf_objects,obj_id))) |
295 | if ((res = psiconv_buffer_add_target(buf_objects,obj_id))) |
251 | goto ERROR6; |
296 | goto ERROR6; |
252 | if ((res = psiconv_write_embedded_object_section(buf_objects, |
297 | if ((res = psiconv_write_embedded_object_section(config,buf_objects,lev+1, |
253 | in_line->object))) |
298 | in_line->object))) |
254 | goto ERROR6; |
299 | goto ERROR6; |
255 | if ((res = psiconv_write_length(buf_inlines,in_line->object_width))) |
300 | if ((res = psiconv_write_length(config,buf_inlines,lev+1,in_line->object_width))) |
256 | goto ERROR6; |
301 | goto ERROR6; |
257 | if ((res = psiconv_write_length(buf_inlines,in_line->object_height))) |
302 | if ((res = psiconv_write_length(config,buf_inlines,lev+1,in_line->object_height))) |
258 | goto ERROR6; |
303 | goto ERROR6; |
259 | } |
304 | } |
260 | } |
305 | } |
261 | } else { |
306 | } else { |
262 | /* No inline layouts (or only 1), so we generate a paragraph type list */ |
307 | /* No inline layouts (or only 1), so we generate a paragraph type list */ |
… | |
… | |
264 | /* Set para_charlayout to the correct character-level layout */ |
309 | /* Set para_charlayout to the correct character-level layout */ |
265 | if (psiconv_list_length(paragraph->in_lines) == 0) |
310 | if (psiconv_list_length(paragraph->in_lines) == 0) |
266 | para_charlayout = paragraph->base_character; |
311 | para_charlayout = paragraph->base_character; |
267 | else { |
312 | else { |
268 | if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { |
313 | if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) { |
269 | psiconv_warn(0,psiconv_buffer_length(buf), |
314 | psiconv_error(config,lev,0,"Massive memory corruption"); |
270 | "Massive memory corruption"); |
|
|
271 | res = -PSICONV_E_OTHER; |
315 | res = -PSICONV_E_NOMEM; |
272 | goto ERROR6; |
316 | goto ERROR6; |
273 | } |
317 | } |
274 | para_charlayout = in_line->layout; |
318 | para_charlayout = in_line->layout; |
275 | } |
319 | } |
276 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
320 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
277 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
321 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
278 | psiconv_warn(0,psiconv_buffer_length(buf), |
322 | psiconv_error(config,lev,0,"Massive memory corruption"); |
279 | "Massive memory corruption"); |
|
|
280 | res = -PSICONV_E_OTHER; |
323 | res = -PSICONV_E_NOMEM; |
281 | goto ERROR6; |
324 | goto ERROR6; |
282 | } |
325 | } |
283 | if ((paragraph->base_style == paragraph_type->style) && |
326 | if ((paragraph->base_style == paragraph_type->style) && |
284 | !psiconv_compare_character_layout(para_charlayout, |
327 | !psiconv_compare_character_layout(para_charlayout, |
285 | paragraph_type->character) && |
328 | paragraph_type->character) && |
… | |
… | |
297 | new_type.character = para_charlayout; |
340 | new_type.character = para_charlayout; |
298 | new_type.style = paragraph->base_style; |
341 | new_type.style = paragraph->base_style; |
299 | paragraph_type = &new_type; |
342 | paragraph_type = &new_type; |
300 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
343 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
301 | goto ERROR6; |
344 | goto ERROR6; |
302 | if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) |
345 | if ((res = psiconv_write_u32(config,buf_types,lev+1,paragraph_type->nr))) |
303 | goto ERROR6; |
346 | goto ERROR6; |
304 | if (!(style = psiconv_get_style(styles,paragraph_type->style))) { |
347 | if (!(style = psiconv_get_style(styles,paragraph_type->style))) { |
305 | psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); |
348 | psiconv_error(config,lev,0,"Unknown style"); |
306 | res = -PSICONV_E_GENERATE; |
349 | res = -PSICONV_E_GENERATE; |
307 | goto ERROR6; |
350 | goto ERROR6; |
308 | } |
351 | } |
309 | if ((res = psiconv_write_paragraph_layout_list(buf_types, |
352 | if ((res = psiconv_write_paragraph_layout_list(config,buf_types,lev+1, |
310 | paragraph_type->paragraph,style->paragraph))) |
353 | paragraph_type->paragraph,style->paragraph))) |
311 | goto ERROR6; |
354 | goto ERROR6; |
312 | if (with_styles) |
355 | if (with_styles) |
313 | if ((res = psiconv_write_u8(buf_types,paragraph_type->style))) |
356 | if ((res = psiconv_write_u8(config,buf_types,lev+1,paragraph_type->style))) |
314 | goto ERROR6; |
357 | goto ERROR6; |
315 | if ((res = psiconv_write_character_layout_list(buf_types, |
358 | if ((res = psiconv_write_character_layout_list(config,buf_types,lev+1, |
316 | paragraph_type->character,style->character))) |
359 | paragraph_type->character,style->character))) |
317 | goto ERROR6; |
360 | goto ERROR6; |
318 | } |
361 | } |
319 | if ((res = psiconv_write_u8(buf_elements,para_type))) |
362 | if ((res = psiconv_write_u8(config,buf_elements,lev+1,para_type))) |
320 | goto ERROR6; |
363 | goto ERROR6; |
321 | } |
364 | } |
322 | } |
365 | } |
323 | |
366 | |
324 | /* HACK: special case: no paragraphs at all. We need to improvize. */ |
367 | /* HACK: special case: no paragraphs at all. We need to improvize. */ |
325 | if (!psiconv_list_length(value)) { |
368 | if (!psiconv_list_length(value)) { |
326 | if ((res = psiconv_write_u32(buf_types,1))) |
369 | if ((res = psiconv_write_u32(config,buf_types,lev+1,1))) |
327 | goto ERROR6; |
370 | goto ERROR6; |
328 | if ((res = psiconv_write_u32(buf_types,0))) |
371 | if ((res = psiconv_write_u32(config,buf_types,lev+1,0))) |
329 | goto ERROR6; |
372 | goto ERROR6; |
330 | if (with_styles) |
373 | if (with_styles) |
331 | if ((res = psiconv_write_u8(buf_types,0))) |
374 | if ((res = psiconv_write_u8(config,buf_types,lev+1,0))) |
332 | goto ERROR6; |
375 | goto ERROR6; |
333 | if ((res = psiconv_write_u32(buf_types,0))) |
376 | if ((res = psiconv_write_u32(config,buf_types,lev+1,0))) |
334 | goto ERROR6; |
377 | goto ERROR6; |
335 | |
378 | |
336 | if ((res = psiconv_write_u32(buf_elements,1))) |
379 | if ((res = psiconv_write_u32(config,buf_elements,lev+1,1))) |
337 | goto ERROR6; |
380 | goto ERROR6; |
338 | if ((res = psiconv_write_u8(buf_elements,1))) |
381 | if ((res = psiconv_write_u8(config,buf_elements,lev+1,1))) |
339 | goto ERROR6; |
382 | goto ERROR6; |
340 | pel_length = 1; |
383 | pel_length = 1; |
341 | ptl_length = 1; |
384 | ptl_length = 1; |
342 | } else { |
385 | } else { |
343 | pel_length = psiconv_list_length(value); |
386 | pel_length = psiconv_list_length(value); |
344 | ptl_length = psiconv_list_length(paragraph_type_list); |
387 | ptl_length = psiconv_list_length(paragraph_type_list); |
345 | } |
388 | } |
346 | |
389 | |
347 | /* Now append everything */ |
390 | /* Now append everything */ |
348 | if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000))) |
391 | if ((res = psiconv_write_u16(config,buf,lev+1,with_styles?0x0001:0x0000))) |
349 | goto ERROR6; |
392 | goto ERROR6; |
350 | if ((res = psiconv_write_u8(buf, ptl_length))) |
393 | if ((res = psiconv_write_u8(config,buf,lev+1, ptl_length))) |
351 | goto ERROR6; |
394 | goto ERROR6; |
352 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
395 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
353 | goto ERROR6; |
396 | goto ERROR6; |
354 | if ((res = psiconv_write_u32(buf,pel_length))) |
397 | if ((res = psiconv_write_u32(config,buf,lev+1,pel_length))) |
355 | goto ERROR6; |
398 | goto ERROR6; |
356 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
399 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
357 | goto ERROR6; |
400 | goto ERROR6; |
358 | if ((res = psiconv_write_u32(buf,nr_of_inlines))) |
401 | if ((res = psiconv_write_u32(config,buf,lev+1,nr_of_inlines))) |
359 | goto ERROR6; |
402 | goto ERROR6; |
360 | if ((res = psiconv_buffer_concat(buf,buf_inlines))) |
403 | if ((res = psiconv_buffer_concat(buf,buf_inlines))) |
361 | goto ERROR6; |
404 | goto ERROR6; |
362 | if ((res = psiconv_buffer_concat(buf,buf_objects))) |
405 | if ((res = psiconv_buffer_concat(buf,buf_objects))) |
363 | goto ERROR6; |
406 | goto ERROR6; |
… | |
… | |
371 | ERROR3: |
414 | ERROR3: |
372 | psiconv_buffer_free(buf_types); |
415 | psiconv_buffer_free(buf_types); |
373 | ERROR2: |
416 | ERROR2: |
374 | psiconv_list_free(paragraph_type_list); |
417 | psiconv_list_free(paragraph_type_list); |
375 | ERROR1: |
418 | ERROR1: |
|
|
419 | if (res) |
|
|
420 | psiconv_error(config,lev,0,"Writing of layout section failed"); |
|
|
421 | else |
|
|
422 | psiconv_progress(config,lev,0,"End of layout section"); |
376 | return res; |
423 | return res; |
377 | } |
424 | } |
378 | |
425 | |
379 | int psiconv_write_styled_layout_section(psiconv_buffer buf, |
426 | int psiconv_write_styled_layout_section(const psiconv_config config, |
|
|
427 | psiconv_buffer buf,int lev, |
380 | psiconv_text_and_layout result, |
428 | psiconv_text_and_layout result, |
381 | psiconv_word_styles_section styles) |
429 | psiconv_word_styles_section styles) |
382 | { |
430 | { |
|
|
431 | int res; |
|
|
432 | |
|
|
433 | psiconv_progress(config,lev,0,"Writing styled layout section"); |
383 | return psiconv_write_layout_section(buf,result,styles,1); |
434 | res = psiconv_write_layout_section(config,buf,lev+1,result,styles,1); |
|
|
435 | if (res) |
|
|
436 | psiconv_error(config,lev,0,"Writing of styles layout section failed"); |
|
|
437 | else |
|
|
438 | psiconv_progress(config,lev,0,"End of styled layout section"); |
|
|
439 | return res; |
384 | } |
440 | } |
385 | |
441 | |
386 | int psiconv_write_styleless_layout_section(psiconv_buffer buf, |
442 | int psiconv_write_styleless_layout_section(const psiconv_config config, |
|
|
443 | psiconv_buffer buf,int lev, |
387 | const psiconv_text_and_layout value, |
444 | const psiconv_text_and_layout value, |
388 | const psiconv_character_layout base_char, |
445 | const psiconv_character_layout base_char, |
389 | const psiconv_paragraph_layout base_para) |
446 | const psiconv_paragraph_layout base_para) |
390 | { |
447 | { |
391 | int res = 0; |
448 | int res = 0; |
392 | psiconv_word_styles_section styles_section; |
449 | psiconv_word_styles_section styles_section; |
393 | |
450 | |
|
|
451 | psiconv_progress(config,lev,0,"Writing styleless layout section"); |
394 | if (!(styles_section = malloc(sizeof(*styles_section)))) |
452 | if (!(styles_section = malloc(sizeof(*styles_section)))) |
395 | goto ERROR1; |
453 | goto ERROR1; |
396 | if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) |
454 | if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) |
397 | goto ERROR2; |
455 | goto ERROR2; |
398 | if (!(styles_section->normal->character = |
456 | if (!(styles_section->normal->character = |
… | |
… | |
400 | goto ERROR3; |
458 | goto ERROR3; |
401 | if (!(styles_section->normal->paragraph = |
459 | if (!(styles_section->normal->paragraph = |
402 | psiconv_clone_paragraph_layout(base_para))) |
460 | psiconv_clone_paragraph_layout(base_para))) |
403 | goto ERROR4; |
461 | goto ERROR4; |
404 | styles_section->normal->hotkey = 0; |
462 | styles_section->normal->hotkey = 0; |
405 | if (!(styles_section->normal->name = strdup(""))) |
463 | if (!(styles_section->normal->name = psiconv_unicode_empty_string())) |
406 | goto ERROR5; |
464 | goto ERROR5; |
407 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
465 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
408 | struct psiconv_word_style_s)))) |
466 | struct psiconv_word_style_s)))) |
409 | goto ERROR6; |
467 | goto ERROR6; |
410 | |
468 | |
411 | res = psiconv_write_layout_section(buf,value,styles_section,0); |
469 | res = psiconv_write_layout_section(config,buf,lev+1,value,styles_section,0); |
412 | psiconv_free_word_styles_section(styles_section); |
470 | psiconv_free_word_styles_section(styles_section); |
|
|
471 | psiconv_progress(config,lev,0,"End of styleless layout section"); |
413 | return res; |
472 | return res; |
414 | |
473 | |
415 | ERROR6: |
474 | ERROR6: |
416 | free(styles_section->normal->name); |
475 | free(styles_section->normal->name); |
417 | ERROR5: |
476 | ERROR5: |
… | |
… | |
421 | ERROR3: |
480 | ERROR3: |
422 | free(styles_section->normal); |
481 | free(styles_section->normal); |
423 | ERROR2: |
482 | ERROR2: |
424 | free(styles_section); |
483 | free(styles_section); |
425 | ERROR1: |
484 | ERROR1: |
|
|
485 | psiconv_error(config,lev,0,"Writing of styleless layout section failed"); |
426 | if (!res) |
486 | if (!res) |
427 | return -PSICONV_E_NOMEM; |
487 | return -PSICONV_E_NOMEM; |
428 | else |
488 | else |
429 | return res; |
489 | return res; |
430 | } |
490 | } |
431 | |
491 | |
432 | |
492 | |
433 | int psiconv_write_embedded_object_section(psiconv_buffer buf, |
493 | int psiconv_write_embedded_object_section(const psiconv_config config, |
|
|
494 | psiconv_buffer buf,int lev, |
434 | const psiconv_embedded_object_section value) |
495 | const psiconv_embedded_object_section value) |
435 | { |
496 | { |
436 | int res; |
497 | int res; |
437 | psiconv_u32 display_id,icon_id,table_id; |
498 | psiconv_u32 display_id,icon_id,table_id; |
438 | psiconv_buffer extra_buf; |
499 | psiconv_buffer extra_buf; |
439 | |
500 | |
|
|
501 | psiconv_progress(config,lev,0,"Writing embedded object section"); |
440 | if (!value) { |
502 | if (!value) { |
441 | psiconv_warn(0,psiconv_buffer_length(buf),"Null Object"); |
503 | psiconv_error(config,lev,0,"Null Object"); |
442 | res = -PSICONV_E_GENERATE; |
504 | res = -PSICONV_E_GENERATE; |
443 | goto ERROR1; |
505 | goto ERROR1; |
444 | } |
506 | } |
445 | |
507 | |
446 | if (!(extra_buf = psiconv_buffer_new())) { |
508 | if (!(extra_buf = psiconv_buffer_new())) { |
… | |
… | |
449 | } |
511 | } |
450 | |
512 | |
451 | display_id = psiconv_buffer_unique_id(); |
513 | display_id = psiconv_buffer_unique_id(); |
452 | icon_id = psiconv_buffer_unique_id(); |
514 | icon_id = psiconv_buffer_unique_id(); |
453 | table_id = psiconv_buffer_unique_id(); |
515 | table_id = psiconv_buffer_unique_id(); |
454 | if ((res = psiconv_write_u8(buf,0x06))) |
516 | if ((res = psiconv_write_u8(config,buf,lev+1,0x06))) |
455 | goto ERROR2; |
517 | goto ERROR2; |
456 | if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_DISPLAY_SECTION))) |
518 | if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_DISPLAY_SECTION))) |
457 | goto ERROR2; |
519 | goto ERROR2; |
458 | if ((res = psiconv_buffer_add_reference(buf,display_id))) |
520 | if ((res = psiconv_buffer_add_reference(buf,display_id))) |
459 | goto ERROR2; |
521 | goto ERROR2; |
460 | if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_ICON_SECTION))) |
522 | if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_ICON_SECTION))) |
461 | goto ERROR2; |
523 | goto ERROR2; |
462 | if ((res = psiconv_buffer_add_reference(buf,icon_id))) |
524 | if ((res = psiconv_buffer_add_reference(buf,icon_id))) |
463 | goto ERROR2; |
525 | goto ERROR2; |
464 | if ((res = psiconv_write_u32(buf,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION))) |
526 | if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_OBJECT_SECTION_TABLE_SECTION))) |
465 | goto ERROR2; |
527 | goto ERROR2; |
466 | if ((res = psiconv_buffer_add_reference(buf,table_id))) |
528 | if ((res = psiconv_buffer_add_reference(buf,table_id))) |
467 | goto ERROR2; |
529 | goto ERROR2; |
468 | |
530 | |
469 | if ((res = psiconv_buffer_add_target(buf,display_id))) |
531 | if ((res = psiconv_buffer_add_target(buf,display_id))) |
470 | goto ERROR2; |
532 | goto ERROR2; |
471 | if ((res = psiconv_write_object_display_section(buf,value->display))) |
533 | if ((res = psiconv_write_object_display_section(config,buf,lev+1,value->display))) |
472 | goto ERROR2; |
534 | goto ERROR2; |
473 | if ((res = psiconv_buffer_add_target(buf,icon_id))) |
535 | if ((res = psiconv_buffer_add_target(buf,icon_id))) |
474 | goto ERROR2; |
536 | goto ERROR2; |
475 | if ((res = psiconv_write_object_icon_section(buf,value->icon))) |
537 | if ((res = psiconv_write_object_icon_section(config,buf,lev+1,value->icon))) |
476 | goto ERROR2; |
538 | goto ERROR2; |
477 | if ((res = psiconv_buffer_add_target(buf,table_id))) |
539 | if ((res = psiconv_buffer_add_target(buf,table_id))) |
478 | goto ERROR2; |
540 | goto ERROR2; |
479 | switch(value->object->type) { |
541 | switch(value->object->type) { |
480 | case psiconv_word_file: |
542 | case psiconv_word_file: |
481 | if ((res = psiconv_write_word_file(extra_buf, |
543 | if ((res = psiconv_write_word_file(config,extra_buf,lev+1, |
482 | (psiconv_word_f) value->object->file))) |
544 | (psiconv_word_f) value->object->file))) |
483 | goto ERROR2; |
545 | goto ERROR2; |
484 | break; |
546 | break; |
485 | /* |
|
|
486 | case psiconv_sketch_file: |
547 | case psiconv_sketch_file: |
487 | if ((res = psiconv_write_sketch_file(extra_buf, |
548 | if ((res = psiconv_write_sketch_file(config,extra_buf,lev+1, |
488 | (psiconv_sketch_f) value->object->file))) |
549 | (psiconv_sketch_f) value->object->file))) |
489 | goto ERROR2; |
550 | goto ERROR2; |
490 | break; |
551 | break; |
|
|
552 | /* |
491 | case psiconv_sheet_file: |
553 | case psiconv_sheet_file: |
492 | if ((res = psiconv_write_sheet_file(extra_buf, |
554 | if ((res = psiconv_write_sheet_file(config,extra_buf,lev+1, |
493 | (psiconv_sheet_f) value->object->file))) |
555 | (psiconv_sheet_f) value->object->file))) |
494 | goto ERROR2; |
556 | goto ERROR2; |
495 | break; |
557 | break; |
496 | */ |
558 | */ |
497 | default: |
559 | default: |
498 | psiconv_warn(0,psiconv_buffer_length(buf), |
560 | psiconv_error(config,lev,0,"Unknown or unsupported object type"); |
499 | "Unknown or unsupported object type"); |
|
|
500 | res = -PSICONV_E_GENERATE; |
561 | res = -PSICONV_E_GENERATE; |
501 | goto ERROR2; |
562 | goto ERROR2; |
502 | } |
563 | } |
503 | |
564 | |
504 | if ((res = psiconv_buffer_resolve(extra_buf))) |
565 | if ((res = psiconv_buffer_resolve(extra_buf))) |
505 | goto ERROR2; |
566 | goto ERROR2; |
506 | if ((res = psiconv_buffer_concat(buf,extra_buf))) |
567 | if ((res = psiconv_buffer_concat(buf,extra_buf))) |
507 | goto ERROR2; |
568 | goto ERROR2; |
|
|
569 | psiconv_buffer_free(extra_buf); |
508 | |
570 | |
|
|
571 | psiconv_progress(config,lev,0,"End of embedded object section"); |
509 | return 0; |
572 | return 0; |
510 | |
573 | |
511 | ERROR2: |
574 | ERROR2: |
512 | psiconv_buffer_free(extra_buf); |
575 | psiconv_buffer_free(extra_buf); |
513 | ERROR1: |
576 | ERROR1: |
|
|
577 | psiconv_error(config,lev,0,"Writing of embedded object section failed"); |
514 | return res; |
578 | return res; |
515 | } |
579 | } |
516 | |
580 | |
517 | |
581 | |
518 | int psiconv_write_object_display_section(psiconv_buffer buf, |
582 | int psiconv_write_object_display_section(const psiconv_config config, |
|
|
583 | psiconv_buffer buf,int lev, |
519 | const psiconv_object_display_section value) |
584 | const psiconv_object_display_section value) |
520 | { |
585 | { |
521 | int res; |
586 | int res; |
522 | |
587 | |
|
|
588 | psiconv_progress(config,lev,0,"Writing object display section"); |
|
|
589 | |
523 | if (!value) { |
590 | if (!value) { |
524 | psiconv_warn(0,psiconv_buffer_length(buf),"Null Object Display Section"); |
591 | psiconv_error(config,lev,0,"Null Object Display Section"); |
525 | res = -PSICONV_E_GENERATE; |
592 | res = -PSICONV_E_GENERATE; |
526 | goto ERROR1; |
593 | goto ERROR1; |
527 | } |
594 | } |
528 | |
595 | |
529 | if ((res = psiconv_write_u8(buf,value->show_icon?0x00:0x01))) |
596 | if ((res = psiconv_write_u8(config,buf,lev+1,value->show_icon?0x00:0x01))) |
530 | goto ERROR1; |
597 | goto ERROR1; |
531 | if ((res = psiconv_write_length(buf,value->width))) |
598 | if ((res = psiconv_write_length(config,buf,lev+1,value->width))) |
532 | goto ERROR1; |
599 | goto ERROR1; |
533 | if ((res = psiconv_write_length(buf,value->height))) |
600 | if ((res = psiconv_write_length(config,buf,lev+1,value->height))) |
534 | goto ERROR1; |
601 | goto ERROR1; |
535 | if ((res = psiconv_write_u32(buf,0x00000000))) |
602 | if ((res = psiconv_write_u32(config,buf,lev+1,0x00000000))) |
536 | goto ERROR1; |
603 | goto ERROR1; |
|
|
604 | |
|
|
605 | psiconv_progress(config,lev,0,"End of object display section"); |
537 | |
606 | |
538 | return 0; |
607 | return 0; |
539 | |
608 | |
540 | ERROR1: |
609 | ERROR1: |
|
|
610 | psiconv_error(config,lev,0,"Writing of object display section failed"); |
541 | return res; |
611 | return res; |
542 | } |
612 | } |
543 | |
613 | |
544 | int psiconv_write_object_icon_section(psiconv_buffer buf, |
614 | int psiconv_write_object_icon_section(const psiconv_config config, |
|
|
615 | psiconv_buffer buf,int lev, |
545 | const psiconv_object_icon_section value) |
616 | const psiconv_object_icon_section value) |
546 | { |
617 | { |
547 | int res; |
618 | int res; |
548 | |
619 | |
|
|
620 | psiconv_progress(config,lev,0,"Writing object icon section"); |
|
|
621 | |
549 | if (!value) { |
622 | if (!value) { |
550 | psiconv_warn(0,psiconv_buffer_length(buf),"Null Object Icon Section"); |
623 | psiconv_error(config,lev,0,"Null Object Icon Section"); |
551 | res = -PSICONV_E_GENERATE; |
624 | res = -PSICONV_E_GENERATE; |
552 | goto ERROR1; |
625 | goto ERROR1; |
553 | } |
626 | } |
554 | |
627 | |
555 | if ((res = psiconv_write_string(buf,value->icon_name))) |
628 | if ((res = psiconv_write_string(config,buf,lev+1,value->icon_name))) |
556 | goto ERROR1; |
629 | goto ERROR1; |
557 | if ((res = psiconv_write_length(buf,value->icon_width))) |
630 | if ((res = psiconv_write_length(config,buf,lev+1,value->icon_width))) |
558 | goto ERROR1; |
631 | goto ERROR1; |
559 | if ((res = psiconv_write_length(buf,value->icon_height))) |
632 | if ((res = psiconv_write_length(config,buf,lev+1,value->icon_height))) |
560 | goto ERROR1; |
633 | goto ERROR1; |
561 | |
634 | |
|
|
635 | psiconv_progress(config,lev,0,"End of object icon section"); |
562 | return 0; |
636 | return 0; |
563 | |
637 | |
564 | ERROR1: |
638 | ERROR1: |
|
|
639 | psiconv_error(config,lev,0,"Writing of object icon section failed"); |
565 | return res; |
640 | return res; |
566 | } |
641 | } |