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

  1. /* Copyright (c) 1991 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)readobj2.c 2.1 11/12/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  readobj2.c - routines for reading in object descriptions.
  9.  */
  10.  
  11. #include  "standard.h"
  12.  
  13. #include  "object.h"
  14.  
  15. #include  "otypes.h"
  16.  
  17. #include  <ctype.h>
  18.  
  19. extern char  *fgetword();
  20.  
  21. OBJREC  *objblock[MAXOBJBLK];        /* our objects */
  22. OBJECT  nobjects = 0;            /* # of objects */
  23. int newobject() {return(0);}
  24.  
  25.  
  26. readobj(input, callback)    /* read in an object file or stream */
  27. char  *input;
  28. int  (*callback)();
  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, callback);
  57.         } else {                /* object */
  58.             ungetc(c, infp);
  59.             getobject(input, infp, callback);
  60.         }
  61.     }
  62.     if (input[0] == '!')
  63.         pclose(infp);
  64.     else
  65.         fclose(infp);
  66. }
  67.  
  68.  
  69. getobject(name, fp, f)            /* read the next object */
  70. char  *name;
  71. FILE  *fp;
  72. int  (*f)();
  73. {
  74.     char  sbuf[MAXSTR];
  75.     OBJREC  thisobj;
  76.                     /* get modifier */
  77.     fgetword(sbuf, MAXSTR, fp);
  78.     thisobj.omod = OVOID;
  79.                     /* get type */
  80.     fgetword(sbuf, MAXSTR, fp);
  81.     if (!strcmp(sbuf, ALIASID))
  82.         thisobj.otype = -1;
  83.     else if ((thisobj.otype = otype(sbuf)) < 0) {
  84.         sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
  85.         error(USER, errmsg);
  86.     }
  87.                     /* get identifier */
  88.     fgetword(sbuf, MAXSTR, fp);
  89.     thisobj.oname = sbuf;
  90.                     /* get arguments */
  91.     if (thisobj.otype == -1) {
  92.         fscanf(fp, "%*s");
  93.         return;
  94.     }
  95.     if (readfargs(&thisobj.oargs, fp) <= 0) {
  96.         sprintf(errmsg, "(%s): bad arguments", name);
  97.         objerror(&thisobj, USER, errmsg);
  98.     }
  99.     thisobj.os = NULL;
  100.     thisobj.lastrno = -1;
  101.                     /* call function */
  102.     (*f)(&thisobj);
  103.                     /* free memory */
  104.     if (thisobj.os != NULL)
  105.         switch (thisobj.otype) {
  106.         case OBJ_CONE:
  107.         case OBJ_CUP:
  108.         case OBJ_CYLINDER:
  109.         case OBJ_TUBE:
  110.         case OBJ_RING:
  111.             freecone(&thisobj);
  112.             break;
  113.         case OBJ_FACE:
  114.             freeface(&thisobj);
  115.             break;
  116.         case OBJ_INSTANCE:
  117.             freeinstance(&thisobj);
  118.             break;
  119.         }
  120.     freefargs(&thisobj.oargs);
  121. }
  122.