1 |
Major parsing routines are built according to this template: |
2 |
|
3 |
int psiconv_parse_TYPE (const psiconv_buffer buf, int lev, |
4 |
psiconv_u32 off, int *length, |
5 |
psiconv_TYPE *result) |
6 |
{ |
7 |
int res = 0; |
8 |
int len = 0; |
9 |
int leng; |
10 |
|
11 |
psiconv_progress(lev+1,off,"Going to parse TYPE"); |
12 |
if (!(*result = malloc(sizeof(**result)))) |
13 |
goto ERROR1; |
14 |
|
15 |
/* Example of calling some other parse function */ |
16 |
if ((res = psiconv_parse_TYPE2(buf,lev+2,off+len,&leng, |
17 |
&(*result)->FIELD2))) |
18 |
goto ERROR2; |
19 |
psiconv_debug(lev+2,off+len,"Some helpful message"); |
20 |
len += leng; |
21 |
|
22 |
/* Example of reading some bytes directly */ |
23 |
(*result)->FIELD1 = psiconv_read_u8(buf,lev+2,off+len,&res); |
24 |
if (!res) |
25 |
goto ERROR3; |
26 |
psiconv_debug(lev+2,off+len,"Some helpful message"); |
27 |
len ++; |
28 |
|
29 |
psiconv_debug(lev+2,off+len,"Some helpful message"); |
30 |
if (length) |
31 |
*length = len; |
32 |
psiconv_progress(lev+1,off+len-1,"End of TYPE (total length: %08x)", |
33 |
len); |
34 |
return 0; |
35 |
|
36 |
ERROR3: |
37 |
free ((*result)->FIELD2) |
38 |
ERROR2: |
39 |
free (*result); |
40 |
ERROR1: |
41 |
psiconv_warn(lev+1,off,"Reading of TYPE failed"); |
42 |
if (length) |
43 |
*length = 0; |
44 |
if (!res) |
45 |
return -PSICONV_E_NOMEM; |
46 |
else |
47 |
return res; |
48 |
} |
49 |
|
50 |
If something unexpected happens half-way, but you can recover from it |
51 |
(for example, an unexpected value in some well-defined field), call |
52 |
psiconv_warn, but do not return with an error. The rule is that if |
53 |
the result code of a procedure is not 0, you may assume that things |
54 |
are hopeless, that nothing was allocated, and that every field contains |
55 |
nonsense. |