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