-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.c
360 lines (303 loc) · 8.35 KB
/
scanner.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <stdio.h>
#include <stdlib.h> /* for exit, malloc, realloc */
#include <ctype.h> /* for isspace */
#include <string.h> /* for strdup */
#include "scanner.h"
static void *allocateMsg(size_t size,char *where);
static void *reallocateMsg(void *s,size_t size,char *where);
/* VERSION 1.3
*
* scanner.c - a collection of input routines for C
* - written by John C. Lusth
* - in general, the functions return what is implied by their names
*
* readInt(FILE *)
* - wrapper for fscanf(fp,"%d")
* - returns 0 if end of file, but feof will subsequently return true
* - usage example: int x = readInt(stdin);
* readReal(FILE *)
* - wrapper for fscanf(fp,"%lf")
* - returns a double
* - returns 0.0 if read fails due to end of file,
* but feof will subsequently return true
* - usage example: double x = readReal(stdin);
* readChar(FILE *)
* - wrapper for fscanf(fp," %c")
* - returns a non-whitespace character
* - returns EOF if end of file; feof will subsequently return true
* - usage example: char x = readChar(stdin);
* readRawChar(FILE *fp)
* - wrapper for fscanf(fp,"%c")
* - returns a character, whether whitespace or non-whitespace
* - returns EOF if end of file; feof will subsequently return true
* - usage example: char x = readRawChar(stdin);
* readToken(FILE *fp)
* - safe version of fscanf(fp,"%s")
* - returns a malloc'd string
* the caller should free the returned string
* - returns 0 if end of file; feof will subsequently return true
* - usage example: char *x = readToken(stdin);
* readString(FILE *fp)
* - reads in a double quoted string
* - returns a malloc'd string; the quotation marks are not included
* the caller should free the returned string
* - returns 0 if end of file; feof will subsequently return true
* - usage example: char *x = readString(stdin);
* readLine(FILE *fp)
* - reads in a line or remainder of a line
* - returns a malloc'd string; the newline is not included
* the caller should free the returned string
* - returns 0 if end of file; feof will subsequently return true
* - usage example: char *x = readLine(stdin);
* stringPending(FILE *fp)
* - returns true (non-zero) if the next non-whitespace character
* is a double quote
* - it consumes all the whitespace up to that non-whitespace character
*/
static void skipWhiteSpace(FILE *);
static char convertEscapedChar(int);
/********** public functions **********************/
int
readInt(FILE *fp)
{
int x,result;
result = fscanf(fp,"%d",&x);
if (result == EOF)
{
return 0;
}
if (result == 0)
{
fprintf(stderr,"SCAN ERROR: attempt to read an integer failed\n");
fprintf(stderr,"offending character was <%c>\n",fgetc(fp));
exit(1);
}
return x;
}
double
readReal(FILE *fp)
{
int result;
double x;
result = fscanf(fp," %lf",&x);
if (result == EOF)
{
return 0.0;
}
if (result == 0)
{
fprintf(stderr,"SCAN ERROR: attempt to read a real number failed\n");
fprintf(stderr,"offending character was <%c>\n",fgetc(fp));
exit(2);
}
return x;
}
char
readChar(FILE *fp)
{
int result;
char x;
result = fscanf(fp," %c",&x);
if (result == EOF)
{
return EOF;
}
if (result == 0)
{
fprintf(stderr,"SCAN ERROR: attempt to read a non-whitespace character failed\n");
fprintf(stderr,"offending character was <%c>\n",fgetc(fp));
exit(2);
}
return x;
}
char
readRawChar(FILE *fp)
{
int result;
char x;
result = fscanf(fp,"%c",&x);
if (result == EOF)
{
return EOF;
}
if (result == 0)
{
fprintf(stderr,"SCAN ERROR: attempt to read a raw character failed\n");
fprintf(stderr,"offending character was <%c>\n",fgetc(fp));
exit(2);
}
return x;
}
char *
readString(FILE *fp)
{
int ch,index;
char *buffer;
int size = 512;
/* advance to the double quote */
skipWhiteSpace(fp);
if (feof(fp)) return 0;
ch = fgetc(fp);
if (ch == EOF) return 0;
/* allocate the buffer */
buffer = allocateMsg(size,"readString");
if (ch != '\"')
{
fprintf(stderr,"SCAN ERROR: attempt to read a string failed\n");
fprintf(stderr,"first character was <%c>\n",ch);
exit(4);
}
/* toss the double quote, skip to the next character */
ch = fgetc(fp);
/* initialize the buffer index */
index = 0;
/* collect characters until the closing double quote */
while (ch != '\"')
{
if (ch == EOF)
{
fprintf(stderr,"SCAN ERROR: attempt to read a string failed\n");
fprintf(stderr,"no closing double quote\n");
exit(6);
}
if (index > size - 2)
{
++size;
buffer = reallocateMsg(buffer,size,"readString");
}
if (ch == '\\')
{
ch = fgetc(fp);
if (ch == EOF)
{
fprintf(stderr,"SCAN ERROR: attempt to read a string failed\n");
fprintf(stderr,"escaped character missing\n");
exit(6);
}
buffer[index] = convertEscapedChar(ch);
}
else
buffer[index] = ch;
++index;
ch = fgetc(fp);
}
buffer[index] = '\0';
return buffer;
}
char *
readToken(FILE *fp)
{
int ch,index;
char *buffer;
int size = 80;
skipWhiteSpace(fp);
ch = fgetc(fp);
if (ch == EOF) return 0;
buffer = allocateMsg(size,"readToken");
index = 0;
while (!isspace(ch))
{
if (ch == EOF) break;
if (index > size - 2)
{
++size;
buffer = reallocateMsg(buffer,size,"readToken");
}
buffer[index] = ch;
++index;
ch = fgetc(fp);
}
/* push back the character that got us out of this loop */
ungetc(ch,fp);
if (index > 0) //there is something in the buffer
clearerr(fp); //so force the read to be good
buffer[index] = '\0';
return buffer;
}
char *
readLine(FILE *fp)
{
int ch,index;
char *buffer;
int size = 512;
ch = fgetc(fp);
if (ch == EOF) return 0;
buffer = allocateMsg(size,"readLine");
index = 0;
while (ch != '\n')
{
if (ch == EOF) break;
if (index > size - 2)
{
++size;
buffer = reallocateMsg(buffer,size,"readLine");
}
buffer[index] = ch;
++index;
ch = fgetc(fp);
}
if (index > 0) //there is something in the buffer
clearerr(fp); //so force the read to be good
buffer[index] = '\0';
return buffer;
}
int
stringPending(FILE *fp)
{
int ch,result = 0;
skipWhiteSpace(fp);
ch = fgetc(fp);
if (ch == EOF) return 0;
if (ch == '\"') result = 1;
ungetc(ch,fp);
return result;
}
/********** private functions **********************/
static void
skipWhiteSpace(FILE *fp)
{
int ch;
/* read chars until a non-whitespace character is encountered */
while ((ch = fgetc(fp)) != EOF && isspace(ch))
continue;
/* a non-space character got us out of the loop, so push it back */
if (ch != EOF) ungetc(ch,fp);
}
static char
convertEscapedChar(int ch)
{
switch (ch)
{
case 'n': return '\n';
case 't': return '\t';
case '"': return '\"';
case '\\': return '\\';
}
return ch;
}
void *
allocateMsg(size_t size,char *where)
{
char *s;
s = malloc(size);
if (s == 0)
{
fprintf(stderr,"%s: could not allocate string, out of memory\n",
where);
exit(3);
}
return s;
}
static void *
reallocateMsg(void *s,size_t size,char *where)
{
char *t;
t = realloc(s,size);
if (t == 0)
{
fprintf(stderr,"%s: could not reallocate string, out of memory\n",
where);
exit(3);
}
return t;
}