home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / RADIANCE / SRC / COMMON / READFARG.C < prev    next >
C/C++ Source or Header  |  1993-10-07  |  2KB  |  112 lines

  1. /* Copyright (c) 1991 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)readfargs.c 2.2 12/19/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  * Allocate, read and free object arguments
  9.  */
  10.  
  11. #include <stdio.h>
  12.  
  13. #include "fvect.h"
  14.  
  15. #include "object.h"
  16.  
  17.  
  18. extern char  *savestr(), *malloc(), *fgetword();
  19. #ifndef atof
  20. extern double  atof();
  21. #endif
  22. extern int  atoi();
  23. extern long  atol();
  24.  
  25. #ifdef  MEMHOG
  26. extern char  *bmalloc();
  27. #else
  28. #define  bmalloc    malloc
  29. #endif
  30.  
  31.  
  32. readfargs(fa, fp)        /* read function arguments from stream */
  33. register FUNARGS  *fa;
  34. FILE  *fp;
  35. {
  36. #define getstr()    (fgetword(sbuf,MAXSTR,fp)!=NULL)
  37. #define getint()    (getstr() && isint(sbuf))
  38. #define getflt()    (getstr() && isflt(sbuf))
  39.     char  sbuf[MAXSTR];
  40.     register int  n, i;
  41.  
  42.     if (!getint() || (n = atoi(sbuf)) < 0)
  43.         return(0);
  44.     if (fa->nsargs = n) {
  45.         fa->sarg = (char **)bmalloc(n*sizeof(char *));
  46.         if (fa->sarg == NULL)
  47.             return(-1);
  48.         for (i = 0; i < fa->nsargs; i++) {
  49.             if (!getstr())
  50.                 return(0);
  51.             fa->sarg[i] = savestr(sbuf);
  52.         }
  53.     } else
  54.         fa->sarg = NULL;
  55.     if (!getint() || (n = atoi(sbuf)) < 0)
  56.         return(0);
  57. #ifdef  IARGS
  58.     if (fa->niargs = n) {
  59.         fa->iarg = (long *)bmalloc(n*sizeof(long));
  60.         if (fa->iarg == NULL)
  61.             return(-1);
  62.         for (i = 0; i < n; i++) {
  63.             if (!getint())
  64.                 return(0);
  65.             fa->iarg[i] = atol(sbuf);
  66.         }
  67.     } else
  68.         fa->iarg = NULL;
  69. #else
  70.     if (n != 0)
  71.         return(0);
  72. #endif
  73.     if (!getint() || (n = atoi(sbuf)) < 0)
  74.         return(0);
  75.     if (fa->nfargs = n) {
  76.         fa->farg = (FLOAT *)bmalloc(n*sizeof(FLOAT));
  77.         if (fa->farg == NULL)
  78.             return(-1);
  79.         for (i = 0; i < n; i++) {
  80.             if (!getflt())
  81.                 return(0);
  82.             fa->farg[i] = atof(sbuf);
  83.         }
  84.     } else
  85.         fa->farg = NULL;
  86.     return(1);
  87. #undef getflt
  88. #undef getint
  89. #undef getstr
  90. }
  91.  
  92.  
  93. #ifndef  MEMHOG
  94. freefargs(fa)            /* free object arguments */
  95. register FUNARGS  *fa;
  96. {
  97.     register int  i;
  98.  
  99.     if (fa->nsargs) {
  100.         for (i = 0; i < fa->nsargs; i++)
  101.             freestr(fa->sarg[i]);
  102.         free((char *)fa->sarg);
  103.     }
  104. #ifdef  IARGS
  105.     if (fa->niargs)
  106.         free((char *)fa->iarg);
  107. #endif
  108.     if (fa->nfargs)
  109.         free((char *)fa->farg);
  110. }
  111. #endif
  112.