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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 184 - (hide annotations)
Tue Jan 6 20:15:01 2004 UTC (20 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 14850 byte(s)
(Frodo) Unicode transition

Note: this commit breaks psiconv. The programs in the extra directory should
work properly.

  * Change all datastructures to use unicode for character encodings
  * Added psiconv_error function
  * Called psiconv_error at places where a warning was stupid
  * Added psiconv_progress in all generate functions
  * Added lev parameter to all generate functions
  * Removed general.h from CVS (we have general.h.in, after all)
  * Probably other stuff I forgot

1 frodo 2 /*
2     parse_word.c - Part of psiconv, a PSION 5 file formats converter
3 frodo 63 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl>
4 frodo 2
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 frodo 71 #include "compat.h"
22    
23 frodo 2 #include <stdlib.h>
24 frodo 71
25 frodo 2 #include "parse_routines.h"
26 frodo 71 #include "error.h"
27 frodo 2
28 frodo 142 #ifdef DMALLOC
29     #include <dmalloc.h>
30     #endif
31    
32 frodo 168 int psiconv_parse_word_status_section(const psiconv_config config,
33     const psiconv_buffer buf, int lev,
34 frodo 2 psiconv_u32 off, int *length,
35     psiconv_word_status_section *result)
36     {
37     int res=0;
38     int len=0;
39     psiconv_u32 temp;
40     int leng;
41    
42 frodo 168 psiconv_progress(config,lev+1,off,"Going to read the word status section");
43 frodo 64 if (!(*result = malloc(sizeof(**result))))
44     goto ERROR1;
45 frodo 2
46 frodo 168 psiconv_progress(config,lev+2,off+len,
47 frodo 2 "Going to read the initial byte (%02x expected)",0x02);
48 frodo 168 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
49 frodo 64 if (res)
50     goto ERROR2;
51 frodo 2 if (temp != 0x02) {
52 frodo 168 psiconv_warn(config,lev+2,off+len,
53 frodo 64 "Word status section initial byte unknown value (ignored)");
54 frodo 168 psiconv_debug(config,lev+2,off+len,"Initial byte: %02x",temp);
55 frodo 2 }
56     len ++;
57    
58 frodo 168 psiconv_progress(config,lev+2,off+len,
59 frodo 2 "Going to read the first byte of display flags");
60 frodo 168 temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
61 frodo 64 if (res)
62     goto ERROR2;
63 frodo 2
64     (*result)->show_tabs = temp&0x01 ? psiconv_bool_true : psiconv_bool_false;
65 frodo 168 psiconv_debug(config,lev+2,off+len,"Show tabs: %02x",(*result)->show_tabs);
66 frodo 2 (*result)->show_spaces = temp&0x02 ? psiconv_bool_true : psiconv_bool_false;
67 frodo 168 psiconv_debug(config,lev+2,off+len,"Show spaces: %02x",(*result)->show_spaces);
68 frodo 2 (*result)->show_paragraph_ends = temp &0x04 ? psiconv_bool_true :
69     psiconv_bool_false;
70 frodo 168 psiconv_debug(config,lev+2,off+len,"Show paragraph ends: %02x",
71 frodo 2 (*result)->show_paragraph_ends);
72     (*result)->show_line_breaks = temp & 0x08 ? psiconv_bool_true :
73     psiconv_bool_false;
74 frodo 168 psiconv_debug(config,lev+2,off+len,"Show line breaks: %02x",
75 frodo 2 (*result)->show_line_breaks);
76     (*result)->show_hard_minus = temp & 0x20 ? psiconv_bool_true :
77     psiconv_bool_false;
78 frodo 168 psiconv_debug(config,lev+2,off+len,"Show hard minus: %02x",
79 frodo 2 (*result)->show_hard_minus);
80     (*result)->show_hard_space = temp & 0x40 ? psiconv_bool_true :
81     psiconv_bool_false;
82 frodo 168 psiconv_debug(config,lev+2,off+len,"Show hard space: %02x",
83 frodo 2 (*result)->show_hard_space);
84     if (temp & 0x90) {
85 frodo 168 psiconv_warn(config,lev+2,off+len,"Word status section first byte of display "
86 frodo 64 "flags contains unknown flags (ignored)");
87 frodo 168 psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0x90);
88 frodo 2 }
89     len ++;
90    
91 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read second byte of display flags");
92     temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
93 frodo 64 if (res)
94     goto ERROR2;
95 frodo 2
96     (*result)->show_full_pictures = temp & 0x01 ? psiconv_bool_true :
97     psiconv_bool_false;
98 frodo 168 psiconv_debug(config,lev+2,off+len,"Show full pictures: %02x",
99 frodo 2 (*result)->show_full_pictures);
100     (*result)->show_full_graphs = temp & 0x02 ? psiconv_bool_true :
101     psiconv_bool_false;
102 frodo 168 psiconv_debug(config,lev+2,off+len,"Show full graphs: %02x",
103 frodo 2 (*result)->show_full_graphs);
104     if (temp & 0xfc) {
105 frodo 168 psiconv_warn(config,lev+2,off+len,"Word status section second byte of display "
106 frodo 64 "flags contains unknown flags (ignored)");
107 frodo 168 psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0xfc);
108 frodo 2 }
109     len ++;
110    
111 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read top toolbar setting");
112     if ((res = psiconv_parse_bool(config,buf,lev+2,off+len,&leng,
113 frodo 64 &(*result)->show_top_toolbar)))
114     goto ERROR2;
115 frodo 2 len += leng;
116    
117 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read side toolbar setting");
118     if ((res = psiconv_parse_bool(config,buf,lev+2,off+len,&leng,
119 frodo 64 &(*result)->show_side_toolbar)))
120     goto ERROR2;
121 frodo 2 len += leng;
122    
123 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read operational flags");
124     temp = psiconv_read_u8(config,buf,lev+2,off+len,&res);
125 frodo 64 if (res)
126     goto ERROR2;
127 frodo 2 (*result)->fit_lines_to_screen = temp & 0x08 ? psiconv_bool_true :
128     psiconv_bool_false;
129 frodo 168 psiconv_debug(config,lev+2,off+len,"Fit lines to screen: %02x",
130 frodo 2 (*result)->fit_lines_to_screen);
131     if (temp & 0xf7) {
132 frodo 168 psiconv_warn(config,lev+2,off+len,"Word status section operational flags "
133 frodo 64 "contains unknown flags (ignored)");
134 frodo 168 psiconv_debug(config,lev+2,off+len,"Unknown flags: %02x",temp & 0xfc);
135 frodo 2 }
136     len ++;
137    
138 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read cursor position");
139     (*result)->cursor_position = psiconv_read_u32(config,buf,lev+2,off + len,&res);
140 frodo 64 if (res)
141     goto ERROR2;
142 frodo 168 psiconv_debug(config,lev+2,off+len,"Cursor position: %08x",
143 frodo 2 (*result)->cursor_position);
144     len += 0x04;
145    
146 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read display size");
147     (*result)->display_size = psiconv_read_u32(config,buf,lev+2,off + len,&res);
148 frodo 64 if (res)
149     goto ERROR2;
150 frodo 168 psiconv_debug(config,lev+2,off+len,"Display size: %08x",
151 frodo 82 (*result)->display_size);
152 frodo 2 len += 0x04;
153    
154     if (length)
155     *length = len;
156    
157 frodo 168 psiconv_progress(config,lev,off+len-1,
158 frodo 2 "End of word status section (total length: %08x)", len);
159 frodo 64 return 0;
160 frodo 2
161 frodo 64 ERROR2:
162     free (*result);
163     ERROR1:
164 frodo 184 psiconv_error(config,lev+1,off,"Reading of Word Status Section failed");
165 frodo 64 if (length)
166     *length = 0;
167     if (!res)
168     return -PSICONV_E_NOMEM;
169     else
170     return res;
171 frodo 2 }
172    
173 frodo 168 int psiconv_parse_word_styles_section(const psiconv_config config,
174     const psiconv_buffer buf, int lev,
175 frodo 2 psiconv_u32 off, int *length,
176     psiconv_word_styles_section *result)
177     {
178     int res=0;
179     int len=0;
180 frodo 64 int leng,i,nr,j;
181 frodo 2 psiconv_word_style style;
182     psiconv_u32 temp;
183    
184 frodo 168 psiconv_progress(config,lev+1,off,"Going to read the word styles section");
185 frodo 64 if (!(*result = malloc(sizeof(**result))))
186     goto ERROR1;
187 frodo 2
188 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read style normal");
189 frodo 64 if (!(style = malloc(sizeof(*style))))
190     goto ERROR2;
191 frodo 2 style->name = NULL;
192 frodo 64 if (!(style->paragraph = psiconv_basic_paragraph_layout()))
193     goto ERROR2_1;
194 frodo 168 psiconv_progress(config,lev+3,off+len,"Going to read the paragraph codes");
195     if ((res = psiconv_parse_paragraph_layout_list(config,buf,lev+3,off+len,&leng,
196 frodo 64 style->paragraph)))
197     goto ERROR2_2;
198 frodo 2 len += leng;
199 frodo 168 psiconv_progress(config,lev+3,off+len,"Going to read the character codes");
200 frodo 64 if (!(style->character = psiconv_basic_character_layout()))
201     goto ERROR2_2;
202 frodo 168 if ((res = psiconv_parse_character_layout_list(config,buf,lev+3,off+len,&leng,
203 frodo 64 style->character)))
204     goto ERROR2_3;
205 frodo 2 len += leng;
206 frodo 168 psiconv_progress(config,lev+3,off+len,"Going to read the hotkey");
207     temp = psiconv_read_u32(config,buf,lev+3,off+len,&res);
208 frodo 64 if (res)
209     goto ERROR2_3;
210 frodo 2 if (temp & 0xffffff00) {
211 frodo 168 psiconv_warn(config,lev+3,off+len,
212 frodo 64 "Normal style hotkey has unknown value (ignored)");
213 frodo 168 psiconv_debug(config,lev+3,off+len,"Hotkey value %08x",temp);
214 frodo 2 temp = 0;
215     }
216 frodo 64 style->hotkey = temp & 0x000000ff;
217 frodo 2 if ((style->hotkey >= 32) && (style->hotkey < 127))
218 frodo 168 psiconv_debug(config,lev+3,off+len,"Hotkey: %c",style->hotkey);
219 frodo 2 else if (style->hotkey == 0x00)
220 frodo 168 psiconv_debug(config,lev+3,off+len,"Hotkey: <none>");
221 frodo 2 else
222 frodo 168 psiconv_debug(config,lev+3,off+len,"Hotkey: %02x",style->hotkey);
223 frodo 2 (*result)->normal = style;
224     len += 0x04;
225    
226 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read hotkeys list");
227 frodo 64 if (!((*result)->styles = psiconv_list_new(sizeof(*style))))
228     goto ERROR3;
229     if (!(style = malloc(sizeof(*style)))) {
230     goto ERROR3_1;
231     }
232    
233 frodo 168 psiconv_progress(config,lev+3,off+len,"Going to read the number of entries");
234     nr = psiconv_read_u8(config,buf,lev+3,off+len,&res);
235 frodo 64 if (res)
236     goto ERROR3_2;
237 frodo 2 len ++;
238 frodo 168 psiconv_debug(config,lev+3,off+len,"Nummer of hotkeys: %02x",nr);
239 frodo 2 for (i = 0; i < nr; i ++) {
240 frodo 168 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
241 frodo 64 if (res)
242     goto ERROR3_2;
243 frodo 2 if (temp & 0xffffff00) {
244 frodo 168 psiconv_warn(config,lev+3,off+len,"Style hotkey has unknown value (ignored)");
245     psiconv_debug(config,lev+3,off+len,"Hotkey %d value %08x",i,temp);
246 frodo 2 temp = 0;
247     }
248 frodo 64 style->hotkey = temp & 0x000000ff;
249 frodo 2 if ((style->hotkey >= 32) && (style->hotkey < 127))
250 frodo 168 psiconv_debug(config,lev+3,off+len,"Hotkey %d: %c",i,style->hotkey);
251 frodo 2 else if (style->hotkey == 0x00)
252 frodo 168 psiconv_debug(config,lev+3,off+len,"Hotkey %d: <none>",i);
253 frodo 2 else
254 frodo 168 psiconv_debug(config,lev+3,off+len,"Hotkey %d: %02x",i,style->hotkey);
255 frodo 64 if ((res = psiconv_list_add((*result)->styles,style)))
256     goto ERROR3_2;
257 frodo 2 len += 0x04;
258     }
259     free(style);
260    
261 frodo 168 psiconv_progress(config,lev+2,off+len,"Going to read all other styles");
262     psiconv_progress(config,lev+2,off+len,"Going to read the number of styles");
263     nr = psiconv_read_u8(config,buf,lev+3,off+len,&res);
264 frodo 64 if (res)
265     goto ERROR4;
266 frodo 2 if (nr != psiconv_list_length((*result)->styles)) {
267 frodo 168 psiconv_warn(config,lev+3,off+len,"Number of styles and hotkeys do not match");
268     psiconv_debug(config,lev+3,off+len,"%d hotkeys, %d styles",
269 frodo 2 psiconv_list_length((*result)->styles), nr);
270     }
271     len ++;
272    
273     for (i = 0; i < nr; i++) {
274 frodo 168 psiconv_progress(config,lev+2,off+len,"Next style: %d",i);
275 frodo 2 if (i >= psiconv_list_length((*result)->styles)) {
276 frodo 64 if (!(style = malloc(sizeof(*style))))
277     goto ERROR5;
278 frodo 2 style->hotkey = 0;
279 frodo 151 if (psiconv_list_add((*result)->styles,style)) {
280 frodo 64 free(style);
281     goto ERROR5;
282     }
283 frodo 168 psiconv_debug(config,lev+3,off+len,"New entry added in list");
284 frodo 2 free(style);
285     }
286 frodo 64 if (!(style = psiconv_list_get((*result)->styles,i)))
287     goto ERROR5;
288 frodo 168 psiconv_progress(config,lev+3,off+len,"Going to read the style name");
289     style->name = psiconv_read_string(config,buf,lev+3,off+len,&leng,&res);
290 frodo 64 if (res)
291     goto ERROR5;
292 frodo 2 len += leng;
293 frodo 168 psiconv_progress(config,lev+3,off+len,
294 frodo 2 "Going to read whether this style is built-in");
295 frodo 168 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
296 frodo 64 if (res)
297     goto ERROR6;
298    
299 frodo 2 if (temp == PSICONV_ID_STYLE_BUILT_IN) {
300     style->built_in = psiconv_bool_true;
301 frodo 168 psiconv_debug(config,lev+3,off+len,"Built-in style");
302 frodo 2 } else if (temp == PSICONV_ID_STYLE_REMOVABLE) {
303     style->built_in = psiconv_bool_false;
304 frodo 168 psiconv_debug(config,lev+3,off+len,"Removable style");
305 frodo 2 } else {
306 frodo 168 psiconv_warn(config,lev+3,off+len,
307 frodo 64 "Word styles section unknown style id (treated as built-in)");
308 frodo 168 psiconv_debug(config,lev+3,off+len,"Unknown id: %08x",temp);
309 frodo 2 style->built_in = psiconv_bool_false;
310     }
311     len += 4;
312 frodo 168 psiconv_progress(config,lev+3,off+len,"Going to read outline level");
313     style->outline_level = psiconv_read_u32(config,buf,lev+3,off+len,&res);
314 frodo 64 if (res)
315     goto ERROR6;
316 frodo 168 psiconv_debug(config,lev+3,off+len,"Outline Level: %08x", style->outline_level);
317 frodo 2 len += 4;
318 frodo 168 psiconv_progress(config,lev+3,off+len,"Going to read the character codes");
319 frodo 70 if (!(style->character = psiconv_clone_character_layout((*result)->normal->character)))
320 frodo 64 goto ERROR6;
321 frodo 168 if ((res = psiconv_parse_character_layout_list(config,buf,lev+3,off+len,&leng,
322 frodo 64 style->character)))
323     goto ERROR7;
324 frodo 2 len += leng;
325 frodo 168 psiconv_progress(config,lev+3,off+len,"Going to read the paragraph codes");
326 frodo 70 if (!(style->paragraph = psiconv_clone_paragraph_layout((*result)->normal->paragraph)))
327 frodo 64 goto ERROR7;
328 frodo 168 if ((res = psiconv_parse_paragraph_layout_list(config,buf,lev+3,off+len,&leng,
329 frodo 64 style->paragraph)))
330     goto ERROR8;
331 frodo 2 len += leng;
332     }
333    
334 frodo 168 psiconv_progress(config,lev+2,off+len,"Reading trailing bytes");
335 frodo 83 for (i = 0; i < psiconv_list_length((*result)->styles); i++) {
336 frodo 168 temp = psiconv_read_u8(config,buf,lev+3,off+len,&res);
337 frodo 82 if (res)
338     goto ERROR4;
339     if (temp != 0xff) {
340 frodo 168 psiconv_warn(config,lev+3,off+len,"Unknown trailing style byte");
341     psiconv_debug(config,lev+3,off+len,"Trailing byte: %02x expected, read %02x",
342 frodo 82 0xff,temp);
343     } else
344 frodo 168 psiconv_debug(config,lev+3,off+len,"Read trailing byte 0xff");
345 frodo 82 len++;
346     }
347    
348 frodo 2 if (length)
349     *length = len;
350    
351 frodo 168 psiconv_progress(config,lev,off+len-1,
352 frodo 2 "End of word styles section (total length: %08x)", len);
353    
354     return res;
355 frodo 64
356    
357     ERROR3_2:
358     free(style);
359     ERROR3_1:
360     psiconv_list_free((*result)->styles);
361     goto ERROR3;
362    
363     ERROR2_3:
364     psiconv_free_character_layout(style->character);
365     ERROR2_2:
366     psiconv_free_paragraph_layout(style->paragraph);
367     ERROR2_1:
368     free (style);
369     goto ERROR2;
370    
371     ERROR8:
372     psiconv_free_paragraph_layout(style->paragraph);
373     ERROR7:
374     psiconv_free_character_layout(style->character);
375     ERROR6:
376     free(style->name);
377     ERROR5:
378     for (j = 0; j < i ;j++) {
379     if (!(style = psiconv_list_get((*result)->styles,j))) {
380 frodo 184 psiconv_error(config,lev+1,off,"Massive memory corruption");
381 frodo 64 goto ERROR4;
382     }
383     psiconv_free_character_layout(style->character);
384     psiconv_free_paragraph_layout(style->paragraph);
385     free(style->name);
386     }
387     ERROR4:
388     psiconv_list_free((*result)->styles);
389     ERROR3:
390     psiconv_free_word_style((*result)->normal);
391     ERROR2:
392     free (*result);
393     ERROR1:
394 frodo 184 psiconv_error(config,lev+1,off,"Reading of Word Status Section failed");
395 frodo 64 if (length)
396     *length = 0;
397     if (!res)
398     return -PSICONV_E_NOMEM;
399     else
400     return res;
401 frodo 2 }
402    
403    
404 frodo 64

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