| 1 | 
greg | 
2.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 | 
  | 
  | 
        char    wrd[64]; | 
| 33 | 
  | 
  | 
        register char   *cp; | 
| 34 | 
  | 
  | 
        register int    c; | 
| 35 | 
  | 
  | 
                                        /* elide comments (# to EOL) */ | 
| 36 | 
  | 
  | 
        do { | 
| 37 | 
  | 
  | 
                while ((c = getc(fp)) != EOF && isspace(c)) | 
| 38 | 
  | 
  | 
                        ; | 
| 39 | 
  | 
  | 
                if (c == '#') | 
| 40 | 
  | 
  | 
                        while ((c = getc(fp)) != EOF && c != '\n') | 
| 41 | 
  | 
  | 
                                ; | 
| 42 | 
  | 
  | 
        } while (c == '\n'); | 
| 43 | 
  | 
  | 
        if (c == EOF) | 
| 44 | 
  | 
  | 
                return(EOF); | 
| 45 | 
  | 
  | 
                                        /* get word */ | 
| 46 | 
  | 
  | 
        cp = wrd; | 
| 47 | 
  | 
  | 
        do { | 
| 48 | 
  | 
  | 
                *cp++ = c; | 
| 49 | 
  | 
  | 
                if (cp - wrd >= sizeof(wrd)) | 
| 50 | 
  | 
  | 
                        return(0); | 
| 51 | 
  | 
  | 
        } while ((c = getc(fp)) != EOF && !isspace(c) && c != '#'); | 
| 52 | 
  | 
  | 
        if (c != EOF) | 
| 53 | 
  | 
  | 
                ungetc(c, fp); | 
| 54 | 
  | 
  | 
        *cp = '\0'; | 
| 55 | 
  | 
  | 
        switch (ty) {                   /* check and convert it */ | 
| 56 | 
  | 
  | 
        case 'h':                       /* short */ | 
| 57 | 
greg | 
2.2 | 
                if (!isint(wrd)) | 
| 58 | 
greg | 
2.1 | 
                        return(0); | 
| 59 | 
  | 
  | 
                *(short *)vp = c = atoi(wrd); | 
| 60 | 
  | 
  | 
                if (*(short *)vp != c) | 
| 61 | 
  | 
  | 
                        return(0); | 
| 62 | 
  | 
  | 
                return(1); | 
| 63 | 
  | 
  | 
        case 'i':                       /* integer */ | 
| 64 | 
greg | 
2.2 | 
                if (!isint(wrd)) | 
| 65 | 
greg | 
2.1 | 
                        return(0); | 
| 66 | 
  | 
  | 
                *(int *)vp = atoi(wrd); | 
| 67 | 
  | 
  | 
                return(1); | 
| 68 | 
  | 
  | 
        case 'l':                       /* long */ | 
| 69 | 
greg | 
2.2 | 
                if (!isint(wrd)) | 
| 70 | 
greg | 
2.1 | 
                        return(0); | 
| 71 | 
  | 
  | 
                *(long *)vp = atol(wrd); | 
| 72 | 
  | 
  | 
                return(1); | 
| 73 | 
  | 
  | 
        case 'f':                       /* float */ | 
| 74 | 
greg | 
2.2 | 
                if (!isflt(wrd)) | 
| 75 | 
greg | 
2.1 | 
                        return(0); | 
| 76 | 
  | 
  | 
                *(float *)vp = atof(wrd); | 
| 77 | 
  | 
  | 
                return(1); | 
| 78 | 
  | 
  | 
        case 'd':                       /* double */ | 
| 79 | 
greg | 
2.2 | 
                if (!isflt(wrd)) | 
| 80 | 
greg | 
2.1 | 
                        return(0); | 
| 81 | 
  | 
  | 
                *(double *)vp = atof(wrd); | 
| 82 | 
  | 
  | 
                return(1); | 
| 83 | 
  | 
  | 
        case 's':                       /* string */ | 
| 84 | 
  | 
  | 
                strcpy(vp, wrd); | 
| 85 | 
  | 
  | 
                return(1); | 
| 86 | 
  | 
  | 
        default:                        /* unsupported type */ | 
| 87 | 
  | 
  | 
                return(0); | 
| 88 | 
  | 
  | 
        } | 
| 89 | 
  | 
  | 
} |