File: parser.c

package info (click to toggle)
hercules 3.13-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,392 kB
  • sloc: ansic: 175,124; sh: 8,792; makefile: 760; perl: 149
file content (150 lines) | stat: -rw-r--r-- 4,245 bytes parent folder | download | duplicates (5)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* PARSER.C     (c) Copyright Leland Lucius, 2001                    */
/*              Simple parameter parser                              */

#include "hstdinc.h"

#define _PARSER_C_
#define _HUTIL_DLL_
#include "hercules.h"
#include "parser.h"

#if !defined( NULL )
#define NULL 0
#endif

/*

    NAME
            parser - parse parameter strings

    SYNOPSIS
            #include "parser.h"

            int parser( PARSER *pp, char *str, void *res )

    DESCRIPTION
            The parser() function breaks the str parameter into a keyword
            and value using the "=" as a delimiter.  The pp table is then
            scanned for the keyword.  If found and the table entry specifies
            a format string, then parser() will use sscanf() to store the
            value at the location specifed by res.

            The PARSER table entries consist of a string pointer that
            designates the keyword and a string pointer that designates the
            format of the associated value.  The format may be set to NULL
            if the keyword does not take a value.  The table must be
            terminated with an entry that specifies NULL for the keyword
            string.

    RETURN VALUE
            If no errors are detected then the returned value will be the
            index+1 of the matching table entry.  The res argument will be
            updated only when a format string is specified.

            If an entry couldn't be found, zero will be returned and res
            will remain untouched.

            If an entry is found, but an error is detected while processing
            the str parameter then a negative value is returned.  This value
            will be the index-1 of the matching table entry.

    EXAMPLE
            #include "parser.h"
            #include <stdlib.h>

            PARSER ptab[] =
            {
                { "switchkey", NULL },
                { "numkey", "%d" },
                { "strkey", "%s" },
            };

            int main( int argc, char *argv[] )
            {
                int rc;
                union
                {
                    int num;
                    char str[ 80 ];
                } res;

                while( --argc )
                {
                    rc = parser( &ptab[0], argv[ argc ], &res );
                    printf( "parser() rc = %d\n", rc );
                    if( rc < 0 )
                    {
                        printf( "error parsing keyword: %s\n",
                            ptab[ abs( rc  1 ) ].key );
                    }
                    else if( rc == 0 )
                    {
                        printf( "unrecognized keyword: %s\n",
                            argv[ argc ] );
                    }
                    else
                    {
                        switch( rc )
                        {
                            case 1:
                                printf( "found switchkey\n" );
                            break;
                            case 2:
                                printf( "numkey value is: %d\n", res.num );
                            break;
                            case 3:
                                printf( "strkey value is: %s\n", res.str );
                            break;
                        }
                    }
                }
            }

    SEE ALSO
            sscanf(3)

*/

DLL_EXPORT int
parser( PARSER *pp, char *str, void *res )
{
    int ndx;
    char *key;
    char *val;

    ndx = 1;
    key = strtok( str, "=" );
    val = strtok( NULL, "=" );

    while( pp->key != NULL )
    {
        if( strcasecmp( key, pp->key ) == 0 )
        {
            if( pp->fmt == NULL )
            {
                if( val != NULL )
                {
                    return( -ndx );
                }
            }
            else
            {
                if( val == NULL )
                {
                    return( -ndx );
                }

                if( sscanf( val, pp->fmt, res ) != 1 )
                {
                    return( -ndx );
                }
            }

            return( ndx );
        }
        pp++;
        ndx++;
    }

    return( 0 );
}