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