File: getvar.c

package info (click to toggle)
icmake 6.22-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,148 kB
  • ctags: 1,042
  • sloc: ansic: 9,241; makefile: 1,134; sh: 235; asm: 126
file content (90 lines) | stat: -rw-r--r-- 2,849 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
\funcref{getvar}{UNS16 getvar (\params)}
    {
        {FILE} {*f} {binary makefile to read from}
        {BIN\_HEADER\_} {*headerp} {pointer to header information}
        {VAR\_} {**var} {address of pointer to {\em VAR\_} structs}
    }
    {number of read variables, or --1 upon failure}
    {xrealloc(), error(), initvar()}
    {getstring(), readheader()}
    {getvar.c}
    {
        Function {\em getvar()} attempts to read the variables defined in a
        binary makefile. Argument {\em headerp} is expected to point to a {\em
        BIN\_HEADER\_} struct filled with header information.

        {\em var} is the address of a pointer to {\em VAR\_} structures. {\em
        getvar()} reallocates the pointer as necessary; therefore, {\em $*$var}
        must point to allocated memory or must be {\em NULL}. For each created
        variable of the type {\bf list} or {\bf string}, the {\em vu.i} field
        is set to {\em NULL} to reflect that the variable is not (yet) in use.
        The {\em count} field is set to 1, reflecting one user of the attached
        memory block.

        When no error occurs, {\em getvar()} returns the number of read
        variables and restores the file pointer {\em f} to the location prior
        to reading. When an error occurs, {\em --1} is returned and the file
        pointer is not repositioned.
    }

Example:
{\footnotesize
    \begin{verbatim}
        // 'f' is assumed to be the opened file,
        UNS16
            nvar,
            i;
        VAR_
            *var = NULL;
        BIN_HEADER_
            *headerp;

        headerp = readheader (f);
        if ( (nvar = getvar (f, headerp, &var)) == -1 )
            error ("cannot get variables from binary makefile");
        for (i = 0; i < nvar; i++)
        {
            printf ("Variable %d is a ", i);
            if (var [i].type & e_str)
                puts ("string");
            else if (var [i].type & e_int)
                puts ("integer);
            .
            .. etcetera
            .
        }
    \end{verbatim}
} % end footnotesize
*/

#include "icrssdef.h"

UNS16 getvar (FILE *f, BIN_HEADER_ *headerp, VAR_ **var)
{
    register
        nvar = 0;
    INT32
        curoffs;

    if (headerp->offset[1] == headerp->offset[2])
        return (0);

    curoffs = ftell (f);
    if (fseek (f, headerp->offset[1], SEEK_SET))
        return ( (UNS16) -1);

    while (ftell (f) < headerp->offset[2])
    {
        *var = xrealloc (*var, (nvar + 1) * sizeof (VAR_));
        if (! fread (*var + nvar, sizeof (VAR_), 1, f) )
            error ("cannot read in variables");
        if ((*var) [nvar].type > e_list)
            error ("bad variable type (var #%d)\n", nvar + 1);
        (*var)[nvar] = initvar ((*var)[nvar]);
        nvar++;
    }

    fseek (f, curoffs, SEEK_SET);
    return (nvar);
}