Back to index

radiance  4R0+20100331
readobj2.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: readobj2.c,v 2.10 2004/09/28 23:55:02 greg Exp $";
00003 #endif
00004 /*
00005  *  readobj2.c - routines for reading in object descriptions.
00006  */
00007 
00008 #include  <ctype.h>
00009 #include  <stdio.h>
00010 
00011 #include  "platform.h"
00012 #include  "rtprocess.h"
00013 #include  "rtmath.h"
00014 #include  "rtio.h"
00015 #include  "rterror.h"
00016 #include  "object.h"
00017 #include  "otypes.h"
00018 #include  "oconv.h"
00019 
00020 
00021 static void getobject2(char  *name, FILE  *fp, ro_cbfunc f);
00022 
00023 
00024 void
00025 readobj2(     /* read in an object file or stream */
00026        char  *input,
00027        ro_cbfunc callback
00028 )
00029 {
00030        char  *fgetline();
00031        FILE  *infp;
00032        char  buf[2048];
00033        register int  c;
00034 
00035        if (input == NULL) {
00036               infp = stdin;
00037               input = "standard input";
00038        } else if (input[0] == '!') {
00039               if ((infp = popen(input+1, "r")) == NULL) {
00040                      sprintf(errmsg, "cannot execute \"%s\"", input);
00041                      error(SYSTEM, errmsg);
00042               }
00043        } else if ((infp = fopen(input, "r")) == NULL) {
00044               sprintf(errmsg, "cannot open scene file \"%s\"", input);
00045               error(SYSTEM, errmsg);
00046        }
00047        while ((c = getc(infp)) != EOF) {
00048               if (isspace(c))
00049                      continue;
00050               if (c == '#') {                           /* comment */
00051                      fgets(buf, sizeof(buf), infp);
00052               } else if (c == '!') {                    /* command */
00053                      ungetc(c, infp);
00054                      fgetline(buf, sizeof(buf), infp);
00055                      readobj2(buf, callback);
00056               } else {                           /* object */
00057                      ungetc(c, infp);
00058                      getobject2(input, infp, callback);
00059               }
00060        }
00061        if (input[0] == '!')
00062               pclose(infp);
00063        else
00064               fclose(infp);
00065 }
00066 
00067 
00068 static void
00069 getobject2(                 /* read the next object */
00070        char  *name,
00071        FILE  *fp,
00072        ro_cbfunc f
00073 )
00074 {
00075        char  sbuf[MAXSTR];
00076        OBJREC  thisobj;
00077                                    /* get modifier */
00078        fgetword(sbuf, MAXSTR, fp);
00079        thisobj.omod = OVOID;
00080                                    /* get type */
00081        fgetword(sbuf, MAXSTR, fp);
00082        if ((thisobj.otype = otype(sbuf)) < 0) {
00083               sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
00084               error(USER, errmsg);
00085        }
00086                                    /* get identifier */
00087        fgetword(sbuf, MAXSTR, fp);
00088        thisobj.oname = sbuf;
00089                                    /* get arguments */
00090        if (thisobj.otype == MOD_ALIAS) {
00091               fscanf(fp, "%*s");
00092               return;
00093        }
00094        if (readfargs(&thisobj.oargs, fp) <= 0) {
00095               sprintf(errmsg, "(%s): bad arguments", name);
00096               objerror(&thisobj, USER, errmsg);
00097        }
00098        thisobj.os = NULL;
00099                                    /* call function */
00100        (*f)(&thisobj);
00101                                    /* free memory */
00102        freefargs(&thisobj.oargs);
00103        free_os(&thisobj);
00104 }