… | |
… | |
23 | #include <string.h> |
23 | #include <string.h> |
24 | |
24 | |
25 | #include "generate_routines.h" |
25 | #include "generate_routines.h" |
26 | #include "error.h" |
26 | #include "error.h" |
27 | |
27 | |
|
|
28 | static int psiconv_write_layout_section(psiconv_buffer buf, |
|
|
29 | const psiconv_text_and_layout value, |
|
|
30 | const psiconv_word_styles_section styles, |
|
|
31 | int with_styles); |
28 | |
32 | |
29 | /* Maybe use a psiconv_header_section variable instead? */ |
33 | /* Maybe use a psiconv_header_section variable instead? */ |
30 | int psiconv_write_header_section(psiconv_buffer buf,psiconv_u32 uid1, |
34 | int psiconv_write_header_section(psiconv_buffer buf,psiconv_u32 uid1, |
31 | psiconv_u32 uid2, psiconv_u32 uid3) |
35 | psiconv_u32 uid2, psiconv_u32 uid3) |
32 | { |
36 | { |
… | |
… | |
45 | { |
49 | { |
46 | int res,i; |
50 | int res,i; |
47 | psiconv_section_table_entry entry; |
51 | psiconv_section_table_entry entry; |
48 | |
52 | |
49 | if (!value) { |
53 | if (!value) { |
50 | psiconv_warn(0,psiconv_list_length(buf),"Null section table section"); |
54 | psiconv_warn(0,psiconv_buffer_length(buf),"Null section table section"); |
51 | return -PSICONV_E_GENERATE; |
55 | return -PSICONV_E_GENERATE; |
52 | } |
56 | } |
53 | |
57 | |
54 | if ((res = psiconv_write_u8(buf,2 * psiconv_list_length(value)))) |
58 | if ((res = psiconv_write_u8(buf,2 * psiconv_list_length(value)))) |
55 | return res; |
59 | return res; |
56 | for (i = 0; i < psiconv_list_length(value); i++) { |
60 | for (i = 0; i < psiconv_list_length(value); i++) { |
57 | if (!(entry = psiconv_list_get(value,i))) { |
61 | if (!(entry = psiconv_list_get(value,i))) { |
58 | psiconv_warn(0,psiconv_list_length(buf),"Massive memory corruption"); |
62 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
59 | return -PSICONV_E_NOMEM; |
63 | return -PSICONV_E_NOMEM; |
60 | } |
64 | } |
61 | if ((res = psiconv_write_u32(buf,entry->id))) |
65 | if ((res = psiconv_write_u32(buf,entry->id))) |
62 | return res; |
66 | return res; |
63 | if ((res = psiconv_write_u32(buf,entry->offset))) |
67 | if ((res = psiconv_write_u32(buf,entry->offset))) |
… | |
… | |
82 | psiconv_buffer extra_buf; |
86 | psiconv_buffer extra_buf; |
83 | int i,j; |
87 | int i,j; |
84 | psiconv_paragraph paragraph; |
88 | psiconv_paragraph paragraph; |
85 | |
89 | |
86 | if (!value) { |
90 | if (!value) { |
87 | psiconv_warn(0,psiconv_list_length(buf),"Null text section"); |
91 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
88 | return -PSICONV_E_GENERATE; |
92 | return -PSICONV_E_GENERATE; |
89 | } |
93 | } |
90 | |
94 | |
91 | if (!(extra_buf = psiconv_new_buffer())) |
95 | if (!(extra_buf = psiconv_buffer_new(0))) |
92 | return -PSICONV_E_NOMEM; |
96 | return -PSICONV_E_NOMEM; |
93 | for (i = 0; i < psiconv_list_length(value); i++) { |
97 | for (i = 0; i < psiconv_list_length(value); i++) { |
94 | if (!(paragraph = psiconv_list_get(value,i))) { |
98 | if (!(paragraph = psiconv_list_get(value,i))) { |
95 | psiconv_warn(0,psiconv_list_length(buf),"Massive memory corruption"); |
99 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
96 | res = -PSICONV_E_OTHER; |
100 | res = -PSICONV_E_OTHER; |
97 | goto ERROR; |
101 | goto ERROR; |
98 | } |
102 | } |
99 | for (j = 0; j < strlen(paragraph->text); j++) |
103 | for (j = 0; j < strlen(paragraph->text); j++) |
100 | if ((res = psiconv_write_u8(extra_buf,paragraph->text[j]))) |
104 | if ((res = psiconv_write_u8(extra_buf,paragraph->text[j]))) |
101 | goto ERROR; |
105 | goto ERROR; |
102 | psiconv_write_u8(extra_buf,0x06); |
106 | psiconv_write_u8(extra_buf,0x06); |
103 | } |
107 | } |
104 | if ((res = psiconv_write_X(buf,psiconv_list_length(extra_buf)))) |
108 | if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf)))) |
105 | goto ERROR; |
109 | goto ERROR; |
106 | res = psiconv_list_concat(buf,extra_buf); |
110 | res = psiconv_buffer_concat(buf,extra_buf); |
107 | |
111 | |
108 | ERROR: |
112 | ERROR: |
109 | psiconv_free_buffer(extra_buf); |
113 | psiconv_buffer_free(extra_buf); |
110 | return res; |
114 | return res; |
111 | } |
115 | } |
|
|
116 | |
|
|
117 | int psiconv_write_layout_section(psiconv_buffer buf, |
|
|
118 | const psiconv_text_and_layout value, |
|
|
119 | const psiconv_word_styles_section styles, |
|
|
120 | int with_styles) |
|
|
121 | { |
|
|
122 | typedef struct psiconv_paragraph_type_list_s |
|
|
123 | { |
|
|
124 | psiconv_character_layout character; |
|
|
125 | psiconv_paragraph_layout paragraph; |
|
|
126 | psiconv_u8 style; |
|
|
127 | psiconv_u8 nr; |
|
|
128 | } *psiconv_paragraph_type_list; |
|
|
129 | psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */ |
|
|
130 | psiconv_paragraph_type_list paragraph_type; |
|
|
131 | struct psiconv_paragraph_type_list_s new_type; |
|
|
132 | psiconv_buffer buf_types,buf_elements,buf_inlines; |
|
|
133 | psiconv_paragraph paragraph; |
|
|
134 | psiconv_in_line_layout in_line; |
|
|
135 | psiconv_word_style style; |
|
|
136 | int i,j,para_type,nr_of_inlines=0,res; |
|
|
137 | |
|
|
138 | if (!value) { |
|
|
139 | psiconv_warn(0,psiconv_buffer_length(buf),"Null text section"); |
|
|
140 | return -PSICONV_E_GENERATE; |
|
|
141 | } |
|
|
142 | |
|
|
143 | if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) { |
|
|
144 | res = -PSICONV_E_NOMEM; |
|
|
145 | goto ERROR1; |
|
|
146 | } |
|
|
147 | |
|
|
148 | if (!(buf_types = psiconv_buffer_new(0))) { |
|
|
149 | res = -PSICONV_E_NOMEM; |
|
|
150 | goto ERROR2; |
|
|
151 | } |
|
|
152 | |
|
|
153 | if (!(buf_elements = psiconv_buffer_new(0))) { |
|
|
154 | res = -PSICONV_E_NOMEM; |
|
|
155 | goto ERROR3; |
|
|
156 | } |
|
|
157 | |
|
|
158 | if (!(buf_inlines = psiconv_buffer_new(0))) { |
|
|
159 | res = -PSICONV_E_NOMEM; |
|
|
160 | goto ERROR4; |
|
|
161 | } |
|
|
162 | |
|
|
163 | for (i = 0; i < psiconv_list_length(value); i++) { |
|
|
164 | if (!(paragraph = psiconv_list_get(value,i))) { |
|
|
165 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
|
|
166 | res = -PSICONV_E_OTHER; |
|
|
167 | goto ERROR5; |
|
|
168 | } |
|
|
169 | if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1))) |
|
|
170 | goto ERROR5; |
|
|
171 | |
|
|
172 | if (psiconv_list_length(paragraph->in_lines)) { |
|
|
173 | /* Inline layouts, so we generate a paragraph element and inline |
|
|
174 | elements */ |
|
|
175 | if ((res = psiconv_write_u32(buf_elements,0x00))) |
|
|
176 | goto ERROR5; |
|
|
177 | if (!(style = psiconv_get_style(styles,paragraph->base_style))) { |
|
|
178 | psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); |
|
|
179 | res = -PSICONV_E_GENERATE; |
|
|
180 | goto ERROR5; |
|
|
181 | } |
|
|
182 | if ((res = psiconv_write_paragraph_layout_list(buf_elements, |
|
|
183 | paragraph->base_paragraph, |
|
|
184 | style->paragraph))) |
|
|
185 | goto ERROR5; |
|
|
186 | if (with_styles) |
|
|
187 | if ((res = psiconv_write_u8(buf_elements,paragraph->base_style))) |
|
|
188 | goto ERROR5; |
|
|
189 | if ((res = psiconv_write_u32(buf_elements, |
|
|
190 | psiconv_list_length(paragraph->in_lines)))) |
|
|
191 | goto ERROR5; |
|
|
192 | |
|
|
193 | /* Generate the inlines. NB: Against what are all settings relative?!? */ |
|
|
194 | for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) { |
|
|
195 | nr_of_inlines ++; |
|
|
196 | if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) { |
|
|
197 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
|
|
198 | res = -PSICONV_E_OTHER; |
|
|
199 | goto ERROR5; |
|
|
200 | } |
|
|
201 | if ((res = psiconv_write_u8(buf_inlines,0x00))) |
|
|
202 | goto ERROR5; |
|
|
203 | if ((res = psiconv_write_u32(buf_inlines,in_line->length))) |
|
|
204 | goto ERROR5; |
|
|
205 | if ((res = psiconv_write_character_layout_list(buf_elements, |
|
|
206 | in_line->layout, |
|
|
207 | style->character))) |
|
|
208 | goto ERROR5; |
|
|
209 | } |
|
|
210 | } else { |
|
|
211 | /* No inline layouts, so we generate a paragraph type list */ |
|
|
212 | para_type = 0; |
|
|
213 | for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) { |
|
|
214 | if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) { |
|
|
215 | psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption"); |
|
|
216 | res = -PSICONV_E_OTHER; |
|
|
217 | goto ERROR5; |
|
|
218 | } |
|
|
219 | if ((paragraph->base_style == paragraph_type->style) && |
|
|
220 | !psiconv_compare_character_layout(paragraph->base_character, |
|
|
221 | paragraph_type->character) && |
|
|
222 | !psiconv_compare_paragraph_layout(paragraph->base_paragraph, |
|
|
223 | paragraph_type->paragraph)) { |
|
|
224 | para_type = paragraph_type->nr; |
|
|
225 | break; |
|
|
226 | } |
|
|
227 | } |
|
|
228 | if (!para_type) { |
|
|
229 | /* We need to add a new entry */ |
|
|
230 | para_type = new_type.nr = j+1; |
|
|
231 | /* No need to copy them, we won't change them anyway */ |
|
|
232 | new_type.paragraph = paragraph->base_paragraph; |
|
|
233 | new_type.character = paragraph->base_character; |
|
|
234 | new_type.style = paragraph->base_style; |
|
|
235 | paragraph_type = &new_type; |
|
|
236 | if ((res = psiconv_list_add(paragraph_type_list,paragraph_type))) |
|
|
237 | goto ERROR5; |
|
|
238 | if ((res = psiconv_write_u32(buf_types,paragraph_type->nr))) |
|
|
239 | goto ERROR5; |
|
|
240 | if (!(style = psiconv_get_style(styles,paragraph_type->style))) { |
|
|
241 | psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style"); |
|
|
242 | res = -PSICONV_E_GENERATE; |
|
|
243 | goto ERROR5; |
|
|
244 | } |
|
|
245 | if ((res = psiconv_write_paragraph_layout_list(buf_types, |
|
|
246 | paragraph_type->paragraph,style->paragraph))) |
|
|
247 | goto ERROR5; |
|
|
248 | if (with_styles) |
|
|
249 | if ((res = psiconv_write_u8(buf_types,paragraph_type->style))) |
|
|
250 | goto ERROR5; |
|
|
251 | if ((res = psiconv_write_character_layout_list(buf_types, |
|
|
252 | paragraph_type->character,style->character))) |
|
|
253 | goto ERROR5; |
|
|
254 | } |
|
|
255 | if ((res = psiconv_write_u8(buf_elements,para_type))) |
|
|
256 | goto ERROR5; |
|
|
257 | } |
|
|
258 | } |
|
|
259 | |
|
|
260 | /* Now append everything */ |
|
|
261 | if ((res = psiconv_write_u8(buf,with_styles?0x00:0x01))) |
|
|
262 | goto ERROR5; |
|
|
263 | if ((res = psiconv_write_u8(buf,0x00))) |
|
|
264 | goto ERROR5; |
|
|
265 | if ((res = psiconv_write_u8(buf,psiconv_list_length(paragraph_type_list)))) |
|
|
266 | goto ERROR5; |
|
|
267 | if ((res = psiconv_buffer_concat(buf,buf_types))) |
|
|
268 | goto ERROR5; |
|
|
269 | if ((res = psiconv_write_u32(buf,psiconv_list_length(value)))) |
|
|
270 | goto ERROR5; |
|
|
271 | if ((res = psiconv_buffer_concat(buf,buf_elements))) |
|
|
272 | goto ERROR5; |
|
|
273 | if ((res = psiconv_write_u32(buf,nr_of_inlines))) |
|
|
274 | goto ERROR5; |
|
|
275 | res = psiconv_buffer_concat(buf,buf_inlines); |
|
|
276 | |
|
|
277 | ERROR5: |
|
|
278 | psiconv_buffer_free(buf_inlines); |
|
|
279 | ERROR4: |
|
|
280 | psiconv_buffer_free(buf_elements); |
|
|
281 | ERROR3: |
|
|
282 | psiconv_buffer_free(buf_types); |
|
|
283 | ERROR2: |
|
|
284 | psiconv_list_free(paragraph_type_list); |
|
|
285 | ERROR1: |
|
|
286 | return res; |
|
|
287 | } |
|
|
288 | |
|
|
289 | int psiconv_write_styled_layout_section(psiconv_buffer buf, |
|
|
290 | psiconv_text_and_layout result, |
|
|
291 | psiconv_word_styles_section styles) |
|
|
292 | { |
|
|
293 | return psiconv_write_layout_section(buf,result,styles,1); |
|
|
294 | } |
|
|
295 | |
|
|
296 | int psiconv_write_styleless_layout_section(psiconv_buffer buf, |
|
|
297 | const psiconv_text_and_layout value, |
|
|
298 | const psiconv_character_layout base_char, |
|
|
299 | const psiconv_paragraph_layout base_para) |
|
|
300 | { |
|
|
301 | int res = 0; |
|
|
302 | psiconv_word_styles_section styles_section; |
|
|
303 | |
|
|
304 | if (!(styles_section = malloc(sizeof(*styles_section)))) |
|
|
305 | goto ERROR1; |
|
|
306 | if (!(styles_section->normal = malloc(sizeof(*styles_section->normal)))) |
|
|
307 | goto ERROR2; |
|
|
308 | if (!(styles_section->normal->character = |
|
|
309 | psiconv_clone_character_layout(base_char))) |
|
|
310 | goto ERROR3; |
|
|
311 | if (!(styles_section->normal->paragraph = |
|
|
312 | psiconv_clone_paragraph_layout(base_para))) |
|
|
313 | goto ERROR4; |
|
|
314 | styles_section->normal->hotkey = 0; |
|
|
315 | if (!(styles_section->normal->name = strdup(""))) |
|
|
316 | goto ERROR5; |
|
|
317 | if (!(styles_section->styles = psiconv_list_new(sizeof( |
|
|
318 | struct psiconv_word_style_s)))) |
|
|
319 | goto ERROR6; |
|
|
320 | |
|
|
321 | res = psiconv_write_layout_section(buf,value,styles_section,0); |
|
|
322 | psiconv_free_word_styles_section(styles_section); |
|
|
323 | return res; |
|
|
324 | |
|
|
325 | ERROR6: |
|
|
326 | free(styles_section->normal->name); |
|
|
327 | ERROR5: |
|
|
328 | psiconv_free_paragraph_layout(styles_section->normal->paragraph); |
|
|
329 | ERROR4: |
|
|
330 | psiconv_free_character_layout(styles_section->normal->character); |
|
|
331 | ERROR3: |
|
|
332 | free(styles_section->normal); |
|
|
333 | ERROR2: |
|
|
334 | free(styles_section); |
|
|
335 | ERROR1: |
|
|
336 | if (!res) |
|
|
337 | return -PSICONV_E_NOMEM; |
|
|
338 | else |
|
|
339 | return res; |
|
|
340 | } |