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