/[public]/psiconv/trunk/lib/psiconv/generate_common.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/generate_common.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 73 Revision 89
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
28static 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? */
30int psiconv_write_header_section(psiconv_buffer buf,psiconv_u32 uid1, 34int 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 (!(entry = malloc(sizeof(*entry))))
55 return -PSICONV_E_NOMEM;
56
57 if ((res = psiconv_write_u8(buf,2 * sizeof(psiconv_u32) * 58 if ((res = psiconv_write_u8(buf,2 * psiconv_list_length(value))))
58 psiconv_list_length(value)))) 59 return res;
59 goto ERROR;
60 for (i = 0; i < psiconv_list_length(value); i++) { 60 for (i = 0; i < psiconv_list_length(value); i++) {
61 if (!(entry = psiconv_list_get(value,i))) { 61 if (!(entry = psiconv_list_get(value,i))) {
62 psiconv_warn(0,psiconv_list_length(buf),"Massive memory corruption"); 62 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
63 res = -PSICONV_E_NOMEM; 63 return -PSICONV_E_NOMEM;
64 goto ERROR;
65 } 64 }
66 if ((res = psiconv_write_u32(buf,entry->id))) 65 if ((res = psiconv_write_u32(buf,entry->id)))
67 goto ERROR; 66 return res;
68 if ((res = psiconv_write_u32(buf,entry->offset))) 67 if ((res = psiconv_write_offset(buf,entry->offset)))
69 goto ERROR;
70 }
71 res = -PSICONV_E_OK;
72ERROR:
73 free(entry);
74 return res; 68 return res;
69 }
70 return -PSICONV_E_OK;
75} 71}
76 72
77int psiconv_write_application_id_section(psiconv_buffer buf,psiconv_u32 id, 73int psiconv_write_application_id_section(psiconv_buffer buf,psiconv_u32 id,
78 const psiconv_string_t text) 74 const psiconv_string_t text)
79{ 75{
90 psiconv_buffer extra_buf; 86 psiconv_buffer extra_buf;
91 int i,j; 87 int i,j;
92 psiconv_paragraph paragraph; 88 psiconv_paragraph paragraph;
93 89
94 if (!value) { 90 if (!value) {
95 psiconv_warn(0,psiconv_list_length(buf),"Null text section"); 91 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section");
96 return -PSICONV_E_GENERATE; 92 return -PSICONV_E_GENERATE;
97 } 93 }
98 94
95 if (psiconv_list_length(value)) {
99 if (!(extra_buf = psiconv_new_buffer())) 96 if (!(extra_buf = psiconv_buffer_new()))
100 return -PSICONV_E_NOMEM; 97 return -PSICONV_E_NOMEM;
98 for (i = 0; i < psiconv_list_length(value); i++) {
99 if (!(paragraph = psiconv_list_get(value,i))) {
100 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
101 res = -PSICONV_E_OTHER;
102 goto ERROR;
103 }
104 for (j = 0; j < strlen(paragraph->text); j++)
105 if ((res = psiconv_write_u8(extra_buf,paragraph->text[j])))
106 goto ERROR;
107 psiconv_write_u8(extra_buf,0x06);
108 }
109 if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf))))
110 goto ERROR;
111 res = psiconv_buffer_concat(buf,extra_buf);
112 } else
113 /* Hack: empty text sections are just not allowed */
114 return psiconv_write_u16(buf,0x0602);
115
116ERROR:
117 psiconv_buffer_free(extra_buf);
118 return res;
119}
120
121int psiconv_write_layout_section(psiconv_buffer buf,
122 const psiconv_text_and_layout value,
123 const psiconv_word_styles_section styles,
124 int with_styles)
125{
126 typedef struct psiconv_paragraph_type_list_s
127 {
128 psiconv_character_layout character;
129 psiconv_paragraph_layout paragraph;
130 psiconv_u8 style;
131 psiconv_u8 nr;
132 } *psiconv_paragraph_type_list;
133 psiconv_list paragraph_type_list; /* Of psiconv_paragraph_type_list_s */
134 psiconv_paragraph_type_list paragraph_type;
135 struct psiconv_paragraph_type_list_s new_type;
136 psiconv_buffer buf_types,buf_elements,buf_inlines;
137 psiconv_paragraph paragraph;
138 psiconv_in_line_layout in_line;
139 psiconv_word_style style;
140 psiconv_character_layout para_charlayout;
141 int i,j,para_type,nr_of_inlines=0,res,ptl_length,pel_length,thislen,paralen;
142
143 if (!value) {
144 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section");
145 return -PSICONV_E_GENERATE;
146 }
147
148 if (!(paragraph_type_list = psiconv_list_new(sizeof(new_type)))) {
149 res = -PSICONV_E_NOMEM;
150 goto ERROR1;
151 }
152
153 if (!(buf_types = psiconv_buffer_new())) {
154 res = -PSICONV_E_NOMEM;
155 goto ERROR2;
156 }
157
158 if (!(buf_elements = psiconv_buffer_new())) {
159 res = -PSICONV_E_NOMEM;
160 goto ERROR3;
161 }
162
163 if (!(buf_inlines = psiconv_buffer_new())) {
164 res = -PSICONV_E_NOMEM;
165 goto ERROR4;
166 }
167
101 for (i = 0; i < psiconv_list_length(value); i++) { 168 for (i = 0; i < psiconv_list_length(value); i++) {
102 if (!(paragraph = psiconv_list_get(value,i))) { 169 if (!(paragraph = psiconv_list_get(value,i))) {
103 psiconv_warn(0,psiconv_list_length(buf),"Massive memory corruption"); 170 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
104 res = -PSICONV_E_OTHER; 171 res = -PSICONV_E_OTHER;
105 goto ERROR; 172 goto ERROR5;
106 } 173 }
107 for (j = 0; j < strlen(paragraph->text); j++)
108 if ((res = psiconv_write_u8(extra_buf,paragraph->text[j]))) 174 if ((res = psiconv_write_u32(buf_elements,strlen(paragraph->text)+1)))
175 goto ERROR5;
176
177 if (psiconv_list_length(paragraph->in_lines) > 1) {
178 /* Inline layouts, so we generate a paragraph element and inline
179 elements */
180 if ((res = psiconv_write_u8(buf_elements,0x00)))
109 goto ERROR; 181 goto ERROR5;
110 psiconv_write_u8(extra_buf,0x06); 182 if (!(style = psiconv_get_style(styles,paragraph->base_style))) {
183 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
184 res = -PSICONV_E_GENERATE;
185 goto ERROR5;
186 }
187 if ((res = psiconv_write_paragraph_layout_list(buf_elements,
188 paragraph->base_paragraph,
189 style->paragraph)))
190 goto ERROR5;
191 if (with_styles)
192 if ((res = psiconv_write_u8(buf_elements,paragraph->base_style)))
193 goto ERROR5;
194 if ((res = psiconv_write_u32(buf_elements,
195 psiconv_list_length(paragraph->in_lines))))
196 goto ERROR5;
197
198 /* Generate the inlines. NB: Against what are all settings relative?!? */
199 paralen = 0;
200 for (j = 0; j < psiconv_list_length(paragraph->in_lines); j++) {
201 nr_of_inlines ++;
202 if (!(in_line = psiconv_list_get(paragraph->in_lines,j))) {
203 psiconv_warn(0,psiconv_buffer_length(buf),
204 "Massive memory corruption");
205 res = -PSICONV_E_OTHER;
206 goto ERROR5;
207 }
208 if ((res = psiconv_write_u8(buf_inlines,0x00)))
209 goto ERROR5;
210 thislen = in_line->length;
211 paralen += thislen;
212 /* If this is the last in_line, we need to make sure that the
213 complete length of all inlines equals the text length */
214 if (j == psiconv_list_length(paragraph->in_lines)-1) {
215 if (paralen > strlen(paragraph->text)+1) {
216 res = -PSICONV_E_GENERATE;
217 goto ERROR5;
218 }
219 thislen += strlen(paragraph->text)+1-paralen;
220 }
221 if ((res = psiconv_write_u32(buf_inlines,thislen)))
222 goto ERROR5;
223 if ((res = psiconv_write_character_layout_list(buf_inlines,
224 in_line->layout,
225 style->character)))
226 goto ERROR5;
227 }
228 } else {
229 /* No inline layouts (or only 1), so we generate a paragraph type list */
230 para_type = 0;
231 /* Set para_charlayout to the correct character-level layout */
232 if (psiconv_list_length(paragraph->in_lines) == 0)
233 para_charlayout = paragraph->base_character;
234 else {
235 if (!(in_line = psiconv_list_get(paragraph->in_lines,0))) {
236 psiconv_warn(0,psiconv_buffer_length(buf),
237 "Massive memory corruption");
238 res = -PSICONV_E_OTHER;
239 goto ERROR5;
240 }
241 para_charlayout = in_line->layout;
242 }
243 for (j = 0; j < psiconv_list_length(paragraph_type_list); j++) {
244 if (!(paragraph_type = psiconv_list_get(paragraph_type_list,j))) {
245 psiconv_warn(0,psiconv_buffer_length(buf),
246 "Massive memory corruption");
247 res = -PSICONV_E_OTHER;
248 goto ERROR5;
249 }
250 if ((paragraph->base_style == paragraph_type->style) &&
251 !psiconv_compare_character_layout(para_charlayout,
252 paragraph_type->character) &&
253 !psiconv_compare_paragraph_layout(paragraph->base_paragraph,
254 paragraph_type->paragraph)) {
255 para_type = paragraph_type->nr;
256 break;
257 }
258 }
259 if (!para_type) {
260 /* We need to add a new entry */
261 para_type = new_type.nr = j+1;
262 /* No need to copy them, we won't change them anyway */
263 new_type.paragraph = paragraph->base_paragraph;
264 new_type.character = para_charlayout;
265 new_type.style = paragraph->base_style;
266 paragraph_type = &new_type;
267 if ((res = psiconv_list_add(paragraph_type_list,paragraph_type)))
268 goto ERROR5;
269 if ((res = psiconv_write_u32(buf_types,paragraph_type->nr)))
270 goto ERROR5;
271 if (!(style = psiconv_get_style(styles,paragraph_type->style))) {
272 psiconv_warn(0,psiconv_buffer_length(buf),"Unknown style");
273 res = -PSICONV_E_GENERATE;
274 goto ERROR5;
275 }
276 if ((res = psiconv_write_paragraph_layout_list(buf_types,
277 paragraph_type->paragraph,style->paragraph)))
278 goto ERROR5;
279 if (with_styles)
280 if ((res = psiconv_write_u8(buf_types,paragraph_type->style)))
281 goto ERROR5;
282 if ((res = psiconv_write_character_layout_list(buf_types,
283 paragraph_type->character,style->character)))
284 goto ERROR5;
285 }
286 if ((res = psiconv_write_u8(buf_elements,para_type)))
287 goto ERROR5;
111 } 288 }
112 if ((res = psiconv_write_X(buf,psiconv_list_length(extra_buf)))) 289 }
290
291 /* HACK: special case: no paragraphs at all. We need to improvize. */
292 if (!psiconv_list_length(value)) {
293 if ((res = psiconv_write_u32(buf_types,1)))
294 goto ERROR5;
295 if ((res = psiconv_write_u32(buf_types,0)))
296 goto ERROR5;
297 if (with_styles)
298 if ((res = psiconv_write_u8(buf_types,0)))
299 goto ERROR5;
300 if ((res = psiconv_write_u32(buf_types,0)))
301 goto ERROR5;
302
303 if ((res = psiconv_write_u32(buf_elements,1)))
304 goto ERROR5;
305 if ((res = psiconv_write_u8(buf_elements,1)))
306 goto ERROR5;
307 pel_length = 1;
308 ptl_length = 1;
309 } else {
310 pel_length = psiconv_list_length(value);
311 ptl_length = psiconv_list_length(paragraph_type_list);
312 }
313
314 /* Now append everything */
315 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000)))
113 goto ERROR; 316 goto ERROR5;
114 res = psiconv_list_concat(buf,extra_buf); 317 if ((res = psiconv_write_u8(buf, ptl_length)))
318 goto ERROR5;
319 if ((res = psiconv_buffer_concat(buf,buf_types)))
320 goto ERROR5;
321 if ((res = psiconv_write_u32(buf,pel_length)))
322 goto ERROR5;
323 if ((res = psiconv_buffer_concat(buf,buf_elements)))
324 goto ERROR5;
325 if ((res = psiconv_write_u32(buf,nr_of_inlines)))
326 goto ERROR5;
327 res = psiconv_buffer_concat(buf,buf_inlines);
115 328
116ERROR: 329ERROR5:
117 psiconv_free_buffer(extra_buf); 330 psiconv_buffer_free(buf_inlines);
331ERROR4:
332 psiconv_buffer_free(buf_elements);
333ERROR3:
334 psiconv_buffer_free(buf_types);
335ERROR2:
336 psiconv_list_free(paragraph_type_list);
337ERROR1:
338 return res;
118} 339}
340
341int psiconv_write_styled_layout_section(psiconv_buffer buf,
342 psiconv_text_and_layout result,
343 psiconv_word_styles_section styles)
344{
345 return psiconv_write_layout_section(buf,result,styles,1);
346}
347
348int psiconv_write_styleless_layout_section(psiconv_buffer buf,
349 const psiconv_text_and_layout value,
350 const psiconv_character_layout base_char,
351 const psiconv_paragraph_layout base_para)
352{
353 int res = 0;
354 psiconv_word_styles_section styles_section;
355
356 if (!(styles_section = malloc(sizeof(*styles_section))))
357 goto ERROR1;
358 if (!(styles_section->normal = malloc(sizeof(*styles_section->normal))))
359 goto ERROR2;
360 if (!(styles_section->normal->character =
361 psiconv_clone_character_layout(base_char)))
362 goto ERROR3;
363 if (!(styles_section->normal->paragraph =
364 psiconv_clone_paragraph_layout(base_para)))
365 goto ERROR4;
366 styles_section->normal->hotkey = 0;
367 if (!(styles_section->normal->name = strdup("")))
368 goto ERROR5;
369 if (!(styles_section->styles = psiconv_list_new(sizeof(
370 struct psiconv_word_style_s))))
371 goto ERROR6;
372
373 res = psiconv_write_layout_section(buf,value,styles_section,0);
374 psiconv_free_word_styles_section(styles_section);
375 return res;
376
377ERROR6:
378 free(styles_section->normal->name);
379ERROR5:
380 psiconv_free_paragraph_layout(styles_section->normal->paragraph);
381ERROR4:
382 psiconv_free_character_layout(styles_section->normal->character);
383ERROR3:
384 free(styles_section->normal);
385ERROR2:
386 free(styles_section);
387ERROR1:
388 if (!res)
389 return -PSICONV_E_NOMEM;
390 else
391 return res;
392}

Legend:
Removed from v.73  
changed lines
  Added in v.89

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26