Revision: | 2.8 |
Committed: | Sun Mar 28 20:33:12 2004 UTC (21 years, 1 month ago) by schorsch |
Content type: | text/plain |
Branch: | MAIN |
CVS Tags: | rad5R4, rad5R2, rad4R2P2, rad5R0, rad5R1, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1, rad5R3 |
Changes since 2.7: | +2 -2 lines |
Log Message: | Continued ANSIfication, and other fixes and clarifications. |
# | User | Rev | Content |
---|---|---|---|
1 | greg | 2.1 | #ifndef lint |
2 | schorsch | 2.8 | static const char RCSid[] = "$Id: fgetval.c,v 2.7 2003/06/27 22:27:45 greg 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 | greg | 2.7 | #include <stdlib.h> |
13 | greg | 2.1 | #include <math.h> |
14 | #include <ctype.h> | ||
15 | |||
16 | int | ||
17 | fgetval(fp, ty, vp) /* get specified data word */ | ||
18 | register FILE *fp; | ||
19 | int ty; | ||
20 | schorsch | 2.8 | void *vp; /* type depends on ty */ |
21 | greg | 2.1 | { |
22 | char wrd[64]; | ||
23 | register char *cp; | ||
24 | register int c; | ||
25 | /* elide comments (# to EOL) */ | ||
26 | do { | ||
27 | while ((c = getc(fp)) != EOF && isspace(c)) | ||
28 | ; | ||
29 | if (c == '#') | ||
30 | while ((c = getc(fp)) != EOF && c != '\n') | ||
31 | ; | ||
32 | } while (c == '\n'); | ||
33 | if (c == EOF) | ||
34 | return(EOF); | ||
35 | /* get word */ | ||
36 | cp = wrd; | ||
37 | do { | ||
38 | *cp++ = c; | ||
39 | if (cp - wrd >= sizeof(wrd)) | ||
40 | return(0); | ||
41 | } while ((c = getc(fp)) != EOF && !isspace(c) && c != '#'); | ||
42 | if (c != EOF) | ||
43 | ungetc(c, fp); | ||
44 | *cp = '\0'; | ||
45 | switch (ty) { /* check and convert it */ | ||
46 | case 'h': /* short */ | ||
47 | greg | 2.2 | if (!isint(wrd)) |
48 | greg | 2.1 | return(0); |
49 | *(short *)vp = c = atoi(wrd); | ||
50 | if (*(short *)vp != c) | ||
51 | return(0); | ||
52 | return(1); | ||
53 | case 'i': /* integer */ | ||
54 | greg | 2.2 | if (!isint(wrd)) |
55 | greg | 2.1 | return(0); |
56 | *(int *)vp = atoi(wrd); | ||
57 | return(1); | ||
58 | case 'l': /* long */ | ||
59 | greg | 2.2 | if (!isint(wrd)) |
60 | greg | 2.1 | return(0); |
61 | *(long *)vp = atol(wrd); | ||
62 | return(1); | ||
63 | case 'f': /* float */ | ||
64 | greg | 2.2 | if (!isflt(wrd)) |
65 | greg | 2.1 | return(0); |
66 | *(float *)vp = atof(wrd); | ||
67 | return(1); | ||
68 | case 'd': /* double */ | ||
69 | greg | 2.2 | if (!isflt(wrd)) |
70 | greg | 2.1 | return(0); |
71 | *(double *)vp = atof(wrd); | ||
72 | return(1); | ||
73 | case 's': /* string */ | ||
74 | strcpy(vp, wrd); | ||
75 | return(1); | ||
76 | default: /* unsupported type */ | ||
77 | return(0); | ||
78 | } | ||
79 | } |