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

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

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