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