Back to index

radiance  4R0+20100331
readfargs.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: readfargs.c,v 2.10 2003/07/27 22:12:01 schorsch Exp $";
00003 #endif
00004 /*
00005  * Allocate, read and free object arguments
00006  */
00007 
00008 #include "copyright.h"
00009 
00010 #include "standard.h"
00011 
00012 #include "object.h"
00013 
00014 
00015 
00016 int
00017 readfargs(fa, fp)           /* read function arguments from stream */
00018 register FUNARGS  *fa;
00019 FILE  *fp;
00020 {
00021 #define getstr(s)    (fgetword(s,sizeof(s),fp)!=NULL)
00022 #define getint(s)    (getstr(s) && isint(s))
00023 #define getflt(s)    (getstr(s) && isflt(s))
00024        char  sbuf[MAXSTR];
00025        register int  n, i;
00026 
00027        if (!getint(sbuf) || (n = atoi(sbuf)) < 0)
00028               return(0);
00029        if ( (fa->nsargs = n) ) {
00030               fa->sarg = (char **)malloc(n*sizeof(char *));
00031               if (fa->sarg == NULL)
00032                      return(-1);
00033               for (i = 0; i < fa->nsargs; i++) {
00034                      if (!getstr(sbuf))
00035                             return(0);
00036                      fa->sarg[i] = savestr(sbuf);
00037               }
00038        } else
00039               fa->sarg = NULL;
00040        if (!getint(sbuf) || (n = atoi(sbuf)) < 0)
00041               return(0);
00042 #ifdef  IARGS
00043        if (fa->niargs = n) {
00044               fa->iarg = (long *)malloc(n*sizeof(long));
00045               if (fa->iarg == NULL)
00046                      return(-1);
00047               for (i = 0; i < n; i++) {
00048                      if (!getint(sbuf))
00049                             return(0);
00050                      fa->iarg[i] = atol(sbuf);
00051               }
00052        } else
00053               fa->iarg = NULL;
00054 #else
00055        if (n != 0)
00056               return(0);
00057 #endif
00058        if (!getint(sbuf) || (n = atoi(sbuf)) < 0)
00059               return(0);
00060        if ( (fa->nfargs = n) ) {
00061               fa->farg = (RREAL *)malloc(n*sizeof(RREAL));
00062               if (fa->farg == NULL)
00063                      return(-1);
00064               for (i = 0; i < n; i++) {
00065                      if (!getflt(sbuf))
00066                             return(0);
00067                      fa->farg[i] = atof(sbuf);
00068               }
00069        } else
00070               fa->farg = NULL;
00071        return(1);
00072 #undef getflt
00073 #undef getint
00074 #undef getstr
00075 }
00076 
00077 
00078 void
00079 freefargs(fa)               /* free object arguments */
00080 register FUNARGS  *fa;
00081 {
00082        register int  i;
00083 
00084        if (fa->nsargs) {
00085               for (i = 0; i < fa->nsargs; i++)
00086                      freestr(fa->sarg[i]);
00087               free((void *)fa->sarg);
00088               fa->sarg = NULL;
00089               fa->nsargs = 0;
00090        }
00091 #ifdef  IARGS
00092        if (fa->niargs) {
00093               free((void *)fa->iarg);
00094               fa->iarg = NULL;
00095               fa->niargs = 0;
00096        }
00097 #endif
00098        if (fa->nfargs) {
00099               free((void *)fa->farg);
00100               fa->farg = NULL;
00101               fa->nfargs = 0;
00102        }
00103 }