1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.3 |
static const char RCSid[] = "$Id$"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Read white space separated values from stream |
6 |
greg |
2.3 |
* |
7 |
|
|
* External symbols declared in standard.h |
8 |
|
|
*/ |
9 |
|
|
|
10 |
greg |
2.4 |
#include "copyright.h" |
11 |
greg |
2.1 |
|
12 |
|
|
#include <stdio.h> |
13 |
|
|
|
14 |
|
|
#include <math.h> |
15 |
|
|
|
16 |
greg |
2.3 |
#include <stdlib.h> |
17 |
|
|
|
18 |
|
|
#include <string.h> |
19 |
|
|
|
20 |
greg |
2.1 |
#include <ctype.h> |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
int |
25 |
|
|
fgetval(fp, ty, vp) /* get specified data word */ |
26 |
|
|
register FILE *fp; |
27 |
|
|
int ty; |
28 |
|
|
char *vp; |
29 |
|
|
{ |
30 |
|
|
char wrd[64]; |
31 |
|
|
register char *cp; |
32 |
|
|
register int c; |
33 |
|
|
/* elide comments (# to EOL) */ |
34 |
|
|
do { |
35 |
|
|
while ((c = getc(fp)) != EOF && isspace(c)) |
36 |
|
|
; |
37 |
|
|
if (c == '#') |
38 |
|
|
while ((c = getc(fp)) != EOF && c != '\n') |
39 |
|
|
; |
40 |
|
|
} while (c == '\n'); |
41 |
|
|
if (c == EOF) |
42 |
|
|
return(EOF); |
43 |
|
|
/* get word */ |
44 |
|
|
cp = wrd; |
45 |
|
|
do { |
46 |
|
|
*cp++ = c; |
47 |
|
|
if (cp - wrd >= sizeof(wrd)) |
48 |
|
|
return(0); |
49 |
|
|
} while ((c = getc(fp)) != EOF && !isspace(c) && c != '#'); |
50 |
|
|
if (c != EOF) |
51 |
|
|
ungetc(c, fp); |
52 |
|
|
*cp = '\0'; |
53 |
|
|
switch (ty) { /* check and convert it */ |
54 |
|
|
case 'h': /* short */ |
55 |
greg |
2.2 |
if (!isint(wrd)) |
56 |
greg |
2.1 |
return(0); |
57 |
|
|
*(short *)vp = c = atoi(wrd); |
58 |
|
|
if (*(short *)vp != c) |
59 |
|
|
return(0); |
60 |
|
|
return(1); |
61 |
|
|
case 'i': /* integer */ |
62 |
greg |
2.2 |
if (!isint(wrd)) |
63 |
greg |
2.1 |
return(0); |
64 |
|
|
*(int *)vp = atoi(wrd); |
65 |
|
|
return(1); |
66 |
|
|
case 'l': /* long */ |
67 |
greg |
2.2 |
if (!isint(wrd)) |
68 |
greg |
2.1 |
return(0); |
69 |
|
|
*(long *)vp = atol(wrd); |
70 |
|
|
return(1); |
71 |
|
|
case 'f': /* float */ |
72 |
greg |
2.2 |
if (!isflt(wrd)) |
73 |
greg |
2.1 |
return(0); |
74 |
|
|
*(float *)vp = atof(wrd); |
75 |
|
|
return(1); |
76 |
|
|
case 'd': /* double */ |
77 |
greg |
2.2 |
if (!isflt(wrd)) |
78 |
greg |
2.1 |
return(0); |
79 |
|
|
*(double *)vp = atof(wrd); |
80 |
|
|
return(1); |
81 |
|
|
case 's': /* string */ |
82 |
|
|
strcpy(vp, wrd); |
83 |
|
|
return(1); |
84 |
|
|
default: /* unsupported type */ |
85 |
|
|
return(0); |
86 |
|
|
} |
87 |
|
|
} |