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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 80 - (hide annotations)
Wed Dec 27 02:12:23 2000 UTC (23 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 11840 byte(s)
(Frodo) Many changes. Layout sections now work!

* Added list_empty, list_replace
* Added relocation to buffers, removed base address
* Added buffer_resolve, buffer_add_reference, buffer_add_target,
  psiconv_unique_id functions
* Modifified other buffer functions to work with references
* Added comments to buffer.h
* Added write_offset function
* Added reference support in functions where needed
* Corrected extra/rewrite error message
* Corrected numerous bugs to make layouts work.

1 frodo 73 /*
2     generate_common.c - Part of psiconv, a PSION 5 file formats converter
3     Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>
4    
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
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9    
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     GNU General Public License for more details.
14    
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18     */
19    
20     #include "config.h"
21     #include "compat.h"
22     #include <stdlib.h>
23     #include <string.h>
24    
25     #include "generate_routines.h"
26     #include "error.h"
27    
28 frodo 79 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);
32 frodo 73
33     /* Maybe use a psiconv_header_section variable instead? */
34     int psiconv_write_header_section(psiconv_buffer buf,psiconv_u32 uid1,
35     psiconv_u32 uid2, psiconv_u32 uid3)
36     {
37     int res;
38     if ((res = psiconv_write_u32(buf,uid1)))
39     return res;
40     if ((res = psiconv_write_u32(buf,uid2)))
41     return res;
42     if ((res = psiconv_write_u32(buf,uid3)))
43     return res;
44     return psiconv_write_u32(buf,psiconv_checkuid(uid1,uid2,uid3));
45     }
46    
47     int psiconv_write_section_table_section(psiconv_buffer buf,
48     const psiconv_section_table_section value)
49     {
50     int res,i;
51     psiconv_section_table_entry entry;
52    
53     if (!value) {
54 frodo 79 psiconv_warn(0,psiconv_buffer_length(buf),"Null section table section");
55 frodo 73 return -PSICONV_E_GENERATE;
56     }
57    
58 frodo 76 if ((res = psiconv_write_u8(buf,2 * psiconv_list_length(value))))
59     return res;
60 frodo 73 for (i = 0; i < psiconv_list_length(value); i++) {
61     if (!(entry = psiconv_list_get(value,i))) {
62 frodo 79 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
63 frodo 76 return -PSICONV_E_NOMEM;
64 frodo 73 }
65     if ((res = psiconv_write_u32(buf,entry->id)))
66 frodo 76 return res;
67 frodo 80 if ((res = psiconv_write_offset(buf,entry->offset)))
68 frodo 76 return res;
69 frodo 73 }
70 frodo 76 return -PSICONV_E_OK;
71 frodo 73 }
72    
73     int psiconv_write_application_id_section(psiconv_buffer buf,psiconv_u32 id,
74     const psiconv_string_t text)
75     {
76     int res;
77     if ((res = psiconv_write_u32(buf,id)))
78     return res;
79     return psiconv_write_string(buf,text);
80     }
81    
82     int psiconv_write_text_section(psiconv_buffer buf,
83     const psiconv_text_and_layout value)
84     {
85     int res;
86     psiconv_buffer extra_buf;
87     int i,j;
88     psiconv_paragraph paragraph;
89    
90     if (!value) {
91 frodo 79 psiconv_warn(0,psiconv_buffer_length(buf),"Null text section");
92 frodo 73 return -PSICONV_E_GENERATE;
93     }
94    
95 frodo 80 if (!(extra_buf = psiconv_buffer_new()))
96 frodo 73 return -PSICONV_E_NOMEM;
97     for (i = 0; i < psiconv_list_length(value); i++) {
98     if (!(paragraph = psiconv_list_get(value,i))) {
99 frodo 79 psiconv_warn(0,psiconv_buffer_length(buf),"Massive memory corruption");
100 frodo 73 res = -PSICONV_E_OTHER;
101     goto ERROR;
102     }
103     for (j = 0; j < strlen(paragraph->text); j++)
104     if ((res = psiconv_write_u8(extra_buf,paragraph->text[j])))
105     goto ERROR;
106     psiconv_write_u8(extra_buf,0x06);
107     }
108 frodo 79 if ((res = psiconv_write_X(buf,psiconv_buffer_length(extra_buf))))
109 frodo 73 goto ERROR;
110 frodo 79 res = psiconv_buffer_concat(buf,extra_buf);
111 frodo 73
112     ERROR:
113 frodo 79 psiconv_buffer_free(extra_buf);
114 frodo 75 return res;
115 frodo 73 }
116 frodo 79
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 frodo 80 if (!(buf_types = psiconv_buffer_new())) {
149 frodo 79 res = -PSICONV_E_NOMEM;
150     goto ERROR2;
151     }
152    
153 frodo 80 if (!(buf_elements = psiconv_buffer_new())) {
154 frodo 79 res = -PSICONV_E_NOMEM;
155     goto ERROR3;
156     }
157    
158 frodo 80 if (!(buf_inlines = psiconv_buffer_new())) {
159 frodo 79 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 frodo 80 if ((res = psiconv_write_u8(buf_elements,0x00)))
176 frodo 79 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 frodo 80 if ((res = psiconv_write_character_layout_list(buf_inlines,
206 frodo 79 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 frodo 80 if ((res = psiconv_write_u16(buf,with_styles?0x0001:0x0000)))
262 frodo 79 goto ERROR5;
263     if ((res = psiconv_write_u8(buf,psiconv_list_length(paragraph_type_list))))
264     goto ERROR5;
265     if ((res = psiconv_buffer_concat(buf,buf_types)))
266     goto ERROR5;
267     if ((res = psiconv_write_u32(buf,psiconv_list_length(value))))
268     goto ERROR5;
269     if ((res = psiconv_buffer_concat(buf,buf_elements)))
270     goto ERROR5;
271     if ((res = psiconv_write_u32(buf,nr_of_inlines)))
272     goto ERROR5;
273     res = psiconv_buffer_concat(buf,buf_inlines);
274    
275     ERROR5:
276     psiconv_buffer_free(buf_inlines);
277     ERROR4:
278     psiconv_buffer_free(buf_elements);
279     ERROR3:
280     psiconv_buffer_free(buf_types);
281     ERROR2:
282     psiconv_list_free(paragraph_type_list);
283     ERROR1:
284     return res;
285     }
286    
287     int psiconv_write_styled_layout_section(psiconv_buffer buf,
288     psiconv_text_and_layout result,
289     psiconv_word_styles_section styles)
290     {
291     return psiconv_write_layout_section(buf,result,styles,1);
292     }
293    
294     int psiconv_write_styleless_layout_section(psiconv_buffer buf,
295     const psiconv_text_and_layout value,
296     const psiconv_character_layout base_char,
297     const psiconv_paragraph_layout base_para)
298     {
299     int res = 0;
300     psiconv_word_styles_section styles_section;
301    
302     if (!(styles_section = malloc(sizeof(*styles_section))))
303     goto ERROR1;
304     if (!(styles_section->normal = malloc(sizeof(*styles_section->normal))))
305     goto ERROR2;
306     if (!(styles_section->normal->character =
307     psiconv_clone_character_layout(base_char)))
308     goto ERROR3;
309     if (!(styles_section->normal->paragraph =
310     psiconv_clone_paragraph_layout(base_para)))
311     goto ERROR4;
312     styles_section->normal->hotkey = 0;
313     if (!(styles_section->normal->name = strdup("")))
314     goto ERROR5;
315     if (!(styles_section->styles = psiconv_list_new(sizeof(
316     struct psiconv_word_style_s))))
317     goto ERROR6;
318    
319     res = psiconv_write_layout_section(buf,value,styles_section,0);
320     psiconv_free_word_styles_section(styles_section);
321     return res;
322    
323     ERROR6:
324     free(styles_section->normal->name);
325     ERROR5:
326     psiconv_free_paragraph_layout(styles_section->normal->paragraph);
327     ERROR4:
328     psiconv_free_character_layout(styles_section->normal->character);
329     ERROR3:
330     free(styles_section->normal);
331     ERROR2:
332     free(styles_section);
333     ERROR1:
334     if (!res)
335     return -PSICONV_E_NOMEM;
336     else
337     return res;
338     }

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