Back to index

radiance  4R0+20100331
fgetval.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: fgetval.c,v 2.8 2004/03/28 20:33:12 schorsch Exp $";
00003 #endif
00004 /*
00005  * Read white space separated values from stream
00006  *
00007  *  External symbols declared in rtio.h
00008  */
00009 
00010 #include  "rtio.h"
00011 
00012 #include  <stdlib.h>
00013 #include  <math.h>
00014 #include  <ctype.h>
00015 
00016 int
00017 fgetval(fp, ty, vp)                /* get specified data word */
00018 register FILE *fp;
00019 int    ty;
00020 void   *vp;   /* type depends on ty */
00021 {
00022        char   wrd[64];
00023        register char *cp;
00024        register int  c;
00025                                    /* elide comments (# to EOL) */
00026        do {
00027               while ((c = getc(fp)) != EOF && isspace(c))
00028                      ;
00029               if (c == '#')
00030                      while ((c = getc(fp)) != EOF && c != '\n')
00031                             ;
00032        } while (c == '\n');
00033        if (c == EOF)
00034               return(EOF);
00035                                    /* get word */
00036        cp = wrd;
00037        do {
00038               *cp++ = c;
00039               if (cp - wrd >= sizeof(wrd))
00040                      return(0);
00041        } while ((c = getc(fp)) != EOF && !isspace(c) && c != '#');
00042        if (c != EOF)
00043               ungetc(c, fp);
00044        *cp = '\0';
00045        switch (ty) {               /* check and convert it */
00046        case 'h':                   /* short */
00047               if (!isint(wrd))
00048                      return(0);
00049               *(short *)vp = c = atoi(wrd);
00050               if (*(short *)vp != c)
00051                      return(0);
00052               return(1);
00053        case 'i':                   /* integer */
00054               if (!isint(wrd))
00055                      return(0);
00056               *(int *)vp = atoi(wrd);
00057               return(1);
00058        case 'l':                   /* long */
00059               if (!isint(wrd))
00060                      return(0);
00061               *(long *)vp = atol(wrd);
00062               return(1);
00063        case 'f':                   /* float */
00064               if (!isflt(wrd))
00065                      return(0);
00066               *(float *)vp = atof(wrd);
00067               return(1);
00068        case 'd':                   /* double */
00069               if (!isflt(wrd))
00070                      return(0);
00071               *(double *)vp = atof(wrd);
00072               return(1);
00073        case 's':                   /* string */
00074               strcpy(vp, wrd);
00075               return(1);
00076        default:                    /* unsupported type */
00077               return(0);
00078        }
00079 }