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