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

  1. /* Copyright (c) 1986 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)readobj.c 2.1 11/12/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  readobj.c - routines for reading in object descriptions.
  9.  *
  10.  *     7/28/85
  11.  */
  12.  
  13. #include  "standard.h"
  14.  
  15. #include  "object.h"
  16.  
  17. #include  "otypes.h"
  18.  
  19. #include  <ctype.h>
  20.  
  21. extern char  *fgetword(), *strcpy();
  22.  
  23. OBJREC  *objblock[MAXOBJBLK];        /* our objects */
  24. OBJECT  nobjects = 0;            /* # of objects */
  25.  
  26.  
  27. readobj(input)            /* read in an object file or stream */
  28. char  *input;
  29. {
  30.     FILE  *popen();
  31.     char  *fgets(), *fgetline();
  32.     FILE  *infp;
  33.     char  buf[512];
  34.     register int  c;
  35.  
  36.     if (input == NULL) {
  37.         infp = stdin;
  38.         input = "standard input";
  39.     } else if (input[0] == '!') {
  40.         if ((infp = popen(input+1, "r")) == NULL) {
  41.             sprintf(errmsg, "cannot execute \"%s\"", input);
  42.             error(SYSTEM, errmsg);
  43.         }
  44.     } else if ((infp = fopen(input, "r")) == NULL) {
  45.         sprintf(errmsg, "cannot open scene file \"%s\"", input);
  46.         error(SYSTEM, errmsg);
  47.     }
  48.     while ((c = getc(infp)) != EOF) {
  49.         if (isspace(c))
  50.             continue;
  51.         if (c == '#') {                /* comment */
  52.             fgets(buf, sizeof(buf), infp);
  53.         } else if (c == '!') {            /* command */
  54.             ungetc(c, infp);
  55.             fgetline(buf, sizeof(buf), infp);
  56.             readobj(buf);
  57.         } else {                /* object */
  58.             ungetc(c, infp);
  59.             getobject(input, infp);
  60.         }
  61.     }
  62.     if (input[0] == '!')
  63.         pclose(infp);
  64.     else
  65.         fclose(infp);
  66. }
  67.  
  68.  
  69. getobject(name, fp)            /* read the next object */
  70. char  *name;
  71. FILE  *fp;
  72. {
  73.     OBJECT  obj;
  74.     char  sbuf[MAXSTR];
  75.     int  rval;
  76.     register OBJREC  *objp;
  77.  
  78.     if ((obj = newobject()) == OVOID)
  79.         error(SYSTEM, "out of object space");
  80.     objp = objptr(obj);
  81.                     /* get modifier */
  82.     strcpy(sbuf, "EOF");
  83.     fgetword(sbuf, MAXSTR, fp);
  84.     if (!strcmp(sbuf, VOIDID))
  85.         objp->omod = OVOID;
  86.     else if ((objp->omod = modifier(sbuf)) == OVOID) {
  87.         sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf);
  88.         error(USER, errmsg);
  89.     }
  90.                     /* get type */
  91.     strcpy(sbuf, "EOF");
  92.     fgetword(sbuf, MAXSTR, fp);
  93.     if (!strcmp(sbuf, ALIASID))
  94.         objp->otype = -1;
  95.     else if ((objp->otype = otype(sbuf)) < 0) {
  96.         sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
  97.         error(USER, errmsg);
  98.     }
  99.                     /* get identifier */
  100.     sbuf[0] = '\0';
  101.     fgetword(sbuf, MAXSTR, fp);
  102.     objp->oname = savqstr(sbuf);
  103.                     /* get arguments */
  104.     if (objp->otype == -1) {
  105.         register OBJECT  alias;
  106.         strcpy(sbuf, "EOF");
  107.         fgetword(sbuf, MAXSTR, fp);
  108.         if ((alias = modifier(sbuf)) == OVOID) {
  109.             sprintf(errmsg,
  110.             "(%s): bad reference \"%s\" for %s \"%s\"",
  111.                     name, sbuf, ALIASID, objp->oname);
  112.             error(USER, errmsg);
  113.         }
  114.         objp->otype = objptr(alias)->otype;
  115.         copystruct(&objp->oargs, &objptr(alias)->oargs);
  116.     } else if ((rval = readfargs(&objp->oargs, fp)) == 0) {
  117.         sprintf(errmsg, "(%s): bad arguments", name);
  118.         objerror(objp, USER, errmsg);
  119.     } else if (rval < 0) {
  120.         sprintf(errmsg, "(%s): error reading scene", name);
  121.         error(SYSTEM, errmsg);
  122.     }
  123.                     /* initialize */
  124.     objp->os = NULL;
  125.     objp->lastrno = -1;
  126.  
  127.     insertobject(obj);        /* add to global structure */
  128. }
  129.  
  130.  
  131. int
  132. newobject()                /* get a new object */
  133. {
  134.     register int  i;
  135.  
  136.     if ((nobjects & 077) == 0) {        /* new block */
  137.         errno = 0;
  138.         i = nobjects >> 6;
  139.         if (i >= MAXOBJBLK)
  140.             return(OVOID);
  141.         objblock[i] = (OBJREC *)bmalloc(0100*sizeof(OBJREC));
  142.         if (objblock[i] == NULL)
  143.             return(OVOID);
  144.     }
  145.     return(nobjects++);
  146. }
  147.