/[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 71 - (hide annotations)
Fri Dec 22 22:31:50 2000 UTC (23 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 13543 byte(s)
(Frodo) First generate routines! Reshuffled a few things to make it all work out

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

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