File: input.c

package info (click to toggle)
hsc 0.916-2
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 2,584 kB
  • ctags: 2,277
  • sloc: ansic: 17,375; makefile: 396
file content (237 lines) | stat: -rw-r--r-- 5,032 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * This source code is part of hsc, a html-preprocessor,
 * Copyright (C) 1995-1998  Thomas Aglassinger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
/*
 * hsclib/input.c
 *
 * basic functions for parsing input
 *
 * updated: 16-May-1997
 * created: 29-Jul-1995
 */

#include <ctype.h>

#include "hsclib/inc_base.h"

/*
 *---------------------------
 * overloaded methods for
 * INFILE class
 *---------------------------
 */

/*
 * hsc_whtspc
 *
 * decides if an char is a white-space
 *
 * params: ch...char to test
 * result: TRUE, if ch is a 'normal' ch
 *
 * NOTE: this function is used as is_ws-methode
 *       for the INFILE class
 */
BOOL hsc_whtspc(int ch)
{
    if ((ch == ' ')
        || (ch == '\n')
#if (!defined MSDOS)  /* HSC_INTO */
        || (ch == '\r')
#endif
        || (ch == '\t')
        )
    {
        return TRUE;
    }
    else
        return FALSE;
}

/*
 * hsc_normch
 *
 * decides if an char is an 'normal' char
 *
 * params: ch...char to test
 * result: TRUE, if ch is a 'normal' ch
 *
 * NOTE: this function is used as is_nc-methode
 *       for the INFILE class
 */
BOOL hsc_normch(int ch)
{
    if (((ch >= '0') && (ch <= '9'))
        || ((ch >= 'a') && (ch <= 'z'))
        || ((ch >= 'A') && (ch <= 'Z'))
        || (ch == '_') || (ch == '-') || (ch == '.')
        )
    {
        return TRUE;
    }
    else
        return FALSE;
}

/*
 * hsc_normch_tagid
 *
 * decides if an char is an 'normal' char for tagnames
 *
 * params: ch...char to test
 * result: TRUE, if ch is a 'normal' ch
 *
 * NOTE: this function is used as is_nc-methode
 *       for the infile class
 */
BOOL hsc_normch_tagid(int ch)
{
    BOOL found = hsc_normch(ch);

    if (!found)
        if (strchr(HSC_TAGID, ch))
            found = TRUE;

    return (found);
}

/*
 *-------------------------------------
 * read a tag/attribute name from input file
 *-------------------------------------
 */

/*
 * infget_tagid
 *
 * read next word from input, but with a
 * different is_nc-methode that also handles
 * the id for hsc-tags (usually "$")
 */
STRPTR infget_tagid(HSCPRC * hp)
{
    INFILE *inpf = hp->inpf;
    STRPTR tagid = NULL;

    BOOL(*old_is_nc) (int ch);

    old_is_nc = inpf->is_nc;    /* remember old is_nc-methode */
    inpf->is_nc = hsc_normch_tagid;
    tagid = infgetw(inpf);      /* read tagid */
    if (!tagid)
        hsc_msg_eof(hp, "reading tag name");
    inpf->is_nc = old_is_nc;    /* remember old is_nc-methode */

    return (tagid);
}

/*
 * infget_attrid
 *
 * read next word from input, but with a
 * different is_nc-methode that also handles
 * the id for hsc-attribs (usually "$")
 */
STRPTR infget_attrid(HSCPRC * hp)
{
    INFILE *inpf = hp->inpf;
    STRPTR attrid = NULL;

    BOOL(*old_is_nc) (int ch);

    old_is_nc = inpf->is_nc;    /* remember old is_nc-methode */
    inpf->is_nc = hsc_normch_tagid;
    attrid = infgetw(inpf);     /* read attrid */
    if (!attrid)
        hsc_msg_eof(hp, "reading attribute name");
    inpf->is_nc = old_is_nc;    /* remember old is_nc-methode */

    return (attrid);
}

/*
 *-------------------------------------
 * parse simple chars/words
 *-------------------------------------
 */

/*
 * parse_wd
 *
 * check if a expected word really occured and
 * display error message if neccessary
 *
 * params: inpf.....input file to read char from
 *         expstr...expected word
 * result: TRUE if successful, FALSE if wrong char found
 */
BOOL parse_wd(HSCPRC * hp, STRPTR expstr)
{
    INFILE *inpf = hp->inpf;
    BOOL value = TRUE;

    if (expstr)
    {
        STRPTR nw = infgetw(inpf);

        /* check for expeted word */
        if (!nw || upstrcmp(nw, expstr))
        {
            if (!nw)
                nw = "<EOF>";

            hsc_message(hp, MSG_UNEXPT_CH,
                        "expected %q, found %q", expstr, nw);
            value = FALSE;
        }
    }
    else
    {
        panic("no data to expect");
    }

    return (value);
}

/*
 * parse_eq
 *
 * check for '='
 *
 * params: inpf...input file to read char from
 * result: -1 if successful, 0 if wrong char found
 */
BOOL parse_eq(HSCPRC * hp)
{
    return (parse_wd(hp, "="));
}

/*
 * parse_gt
 *
 * check for '>'
 *
 * params: inpf...input file to read char from
 * result: -1 if successful, 0 if wrong char found
 */
BOOL parse_gt(HSCPRC * hp)
{
    return (parse_wd(hp, ">"));
}