-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIBPARSE.C
303 lines (240 loc) · 6.63 KB
/
LIBPARSE.C
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* LIBPARSE.C -- File Parser Routines
Written October 1987 by Craig Finseth
Copyright 1987,8,9,90,1,2,3 by Craig A. Finseth
*/
#include "lib.h"
char *Parse__Token(); /* char *cptr, struct parse_data *pptr */
/* ------------------------------------------------------------ */
/* Open the input file and initialize the structure variables. Return
0 on success or -1 on error. */
int
Parse_Open(pptr, fname)
struct parse_data *pptr;
char *fname;
{
if (pptr->p_length < 1) return(-1);
if (strequ(fname, "")) pptr->p_fptr = stdin;
else if ((pptr->p_fptr = fopen(fname, "r")) == NULL) return(-1);
pptr->p_line = 0;
xstrcpy(pptr->p_fname, fname);
if (xisgray(pptr->p_separator)) {
pptr->p_separator = SP;
pptr->p_trim = TRUE;
}
return(0);
}
/* ------------------------------------------------------------ */
/* Close the file and finish reading. */
void
Parse_Close(pptr)
struct parse_data *pptr;
{
if (pptr->p_fptr != NULL && pptr->p_fptr != stdin)
fclose(pptr->p_fptr);
pptr->p_fptr = NULL;
}
/* ------------------------------------------------------------ */
/* Parse the line according to the description. Refer to PARSE.DOC
for a complete description of this function. */
int
Parse_A_Line(pptr, tokens, num_tokens)
struct parse_data *pptr;
char *tokens[];
int num_tokens;
{
char *cptr;
char *cptr2;
char *begptr;
char *endptr;
int cnt;
char flushbuf[BUFFSIZE];
if (pptr->p_fptr == NULL) return(0);
do {
/* read a line */
cptr = fgets(pptr->p_buffer, pptr->p_length, pptr->p_fptr);
pptr->p_line++;
if (cptr == NULL) { /* cannot separate EOF and error */
Parse_Close(pptr);
return(0);
}
/* if no newline, flush the rest of the line */
if (*sindex(pptr->p_buffer, NL) == NUL) {
do {
cptr2 = fgets(flushbuf, sizeof(flushbuf),
pptr->p_fptr);
if (cptr2 == NULL) {
Parse_Close(pptr);
break;
}
} while (*sindex(flushbuf, NL) == NUL);
}
/* check for blank and blank/comment lines */
for (cptr = pptr->p_buffer; *cptr; cptr++) {
if (*cptr == pptr->p_comment) {
*cptr = NUL; /* mark as no data */
break;
}
if (*cptr == NL) { /* at end of line */
*cptr = NUL;
break;
}
if (!xisgray(*cptr)) break; /* we have data */
}
if (*cptr == NUL) *pptr->p_buffer = NUL;
} while (*pptr->p_buffer == NUL);
/* parse the line */
if (num_tokens <= 0) return(0); /* what else can we do? */
cptr = pptr->p_buffer;
for (cnt = 0; cnt < num_tokens; cnt++) {
if (xisgray(pptr->p_separator)) {
if (pptr->p_trim) { while (xisgray(*cptr)) cptr++; }
if (*cptr == NUL || *cptr == pptr->p_comment)
return(cnt);
}
else {
if (*cptr == NUL || *cptr == pptr->p_comment)
return(cnt);
if (pptr->p_trim) { while (xiswhite(*cptr)) cptr++; }
}
tokens[cnt] = cptr;
begptr = cptr;
cptr = Parse__Token(begptr, pptr);
if (cptr == NULL) return(cnt - 1);
if (pptr->p_trim) { /* trim trailing whitespace */
for (endptr = begptr + strlen(begptr) - 1;
endptr > begptr && xisgray(*endptr); ) {
*endptr-- = NUL;
}
}
}
if (cptr != NULL) tokens[num_tokens - 1] = cptr; /* get all the rest */
return(num_tokens);
}
/* ------------------------------------------------------------ */
/* Parse the supplied string according to the description. Refer to
PARSE.DOC for a complete description of this function. */
int
Parse_A_String(pptr, tokens, num_tokens, str)
struct parse_data *pptr;
char *tokens[];
int num_tokens;
char *str;
{
char *cptr;
char *begptr;
char *endptr;
int cnt;
if (num_tokens <= 0 || pptr->p_length < 1)
return(0); /* what else can we do? */
strncpy(pptr->p_buffer, str, pptr->p_length);
pptr->p_buffer[pptr->p_length - 1] = NUL;
if (xisgray(pptr->p_separator)) { /* normalize as Parse_Open */
pptr->p_separator = SP;
pptr->p_trim = TRUE;
}
cptr = pptr->p_buffer;
for (cnt = 0; cnt < num_tokens; cnt++) {
if (xisgray(pptr->p_separator)) {
if (pptr->p_trim) { while (xisgray(*cptr)) cptr++; }
if (*cptr == NUL || *cptr == pptr->p_comment)
return(cnt);
}
else {
if (*cptr == NUL || *cptr == pptr->p_comment)
return(cnt);
if (pptr->p_trim) { while (xiswhite(*cptr)) cptr++; }
}
tokens[cnt] = cptr;
begptr = cptr;
cptr = Parse__Token(begptr, pptr);
if (cptr == NULL) return(cnt - 1);
if (pptr->p_trim) { /* trim trailing whitespace */
for (endptr = begptr + strlen(begptr) - 1;
endptr > begptr && xisgray(*endptr); ) {
*endptr-- = NUL;
}
}
}
if (cptr != NULL) tokens[num_tokens - 1] = cptr; /* get all the rest */
return(num_tokens);
}
/* ------------------------------------------------------------ */
/* Return the current file name. */
char *
Parse_File(pptr)
struct parse_data *pptr;
{
return(pptr->p_fname);
}
/* ------------------------------------------------------------ */
/* Read and return a line, buf don't parse it. Return 0 on success or
-1 on error. */
int Parse_Get_Line(pptr, buf, bufsize)
struct parse_data *pptr;
char *buf;
int bufsize;
{
char *cptr;
char flushbuf[BUFFSIZE];
/* read a line */
cptr = fgets(buf, bufsize, pptr->p_fptr);
pptr->p_line++;
if (cptr == NULL) { /* cannot separate EOF and error */
Parse_Close(pptr);
return(-1);
}
/* if no newline, flush the rest of the line */
if (*sindex(buf, NL) == NUL) {
do {
cptr = fgets(flushbuf, sizeof(flushbuf), pptr->p_fptr);
if (cptr == NULL) {
Parse_Close(pptr);
break;
}
} while (*sindex(flushbuf, NL) == NUL);
}
TrimNL(buf);
return(0);
}
/* ------------------------------------------------------------ */
/* Return the current line number. */
int
Parse_Line(pptr)
struct parse_data *pptr;
{
return(pptr->p_line);
}
/* ------------------------------------------------------------ */
/* Parse the next token from the string. Return NULL when you have
run out of tokens. */
char *
Parse__Token(cptr, pptr)
char *cptr;
struct parse_data *pptr;
{
FLAG token;
if (xisgray(pptr->p_separator)) {
token = FALSE;
while (*cptr && *cptr != pptr->p_comment &&
!xisgray(*cptr)) {
cptr++;
token = TRUE;
}
}
else {
token = TRUE;
while (*cptr && *cptr != pptr->p_comment && *cptr != NL &&
*cptr != pptr->p_separator) {
cptr++;
}
}
if (!token || !*cptr || *cptr == pptr->p_comment) {
*cptr = NUL;
return(NULL);
}
else {
*cptr = NUL;
return(cptr + 1);
}
}
/* end of LIBPARSE.C -- File Parser Routines */