--- ray/src/util/rtcontrib.c 2005/10/06 16:28:59 1.32 +++ ray/src/util/rtcontrib.c 2005/10/11 23:41:45 1.37 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.32 2005/10/06 16:28:59 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.37 2005/10/11 23:41:45 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -63,10 +63,13 @@ static void closestream(void *p) { STREAMOUT *sop = (STREAMOUT *)p; + int status; if (sop->reclen == CNT_PIPE) - pclose(sop->ofp); + status = pclose(sop->ofp); else - fclose(sop->ofp); + status = fclose(sop->ofp); + if (status) + error(SYSTEM, "error closing output stream"); free(p); } @@ -120,6 +123,7 @@ int inpfmt = 'a'; /* input format */ int outfmt = 'a'; /* output format */ int header = 1; /* output header? */ +int force_open = 0; /* truncate existing output? */ int xres = 0; /* horiz. output resolution */ int yres = 0; /* vert. output resolution */ @@ -263,7 +267,7 @@ main(int argc, char *argv[]) continue; } break; - case 'f': /* file or i/o format */ + case 'f': /* file or force or format */ if (!argv[i][2]) { char *fpath; if (i >= argc-2) break; @@ -278,6 +282,10 @@ main(int argc, char *argv[]) fcompile(fpath); continue; } + if (argv[i][2] == 'o') { + force_open++; + continue; + } setformat(argv[i]+2); continue; case 'e': /* expression */ @@ -699,15 +707,17 @@ getostream(const char *ospec, const char *mname, int b sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); if (sop == NULL) error(SYSTEM, "out of memory in getostream"); - sop->reclen = oname[0] == '!' ? CNT_PIPE : 0; + sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN; sop->ofp = NULL; /* open iff noopen==0 */ lep->data = (char *)sop; } if (!noopen && sop->ofp == NULL) { /* open output stream */ - int i; + long i; if (oname[0] == '!') /* output to command */ sop->ofp = popen(oname+1, "w"); - else + else if (!force_open && access(oname, F_OK) == 0) + errno = EEXIST; /* file exists */ + else /* else open it */ sop->ofp = fopen(oname, "w"); if (sop->ofp == NULL) { sprintf(errmsg, "cannot open '%s' for writing", oname); @@ -731,7 +741,8 @@ getostream(const char *ospec, const char *mname, int b } printresolu(sop->ofp); /* play catch-up */ - for (i = 0; i < lastdone; i++) { + for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone; + i--; ) { static const DCOLOR nocontrib = BLKCOLOR; put_contrib(nocontrib, sop->ofp); if (outfmt == 'a') @@ -749,6 +760,8 @@ getostream(const char *ospec, const char *mname, int b int getinp(char *buf, FILE *fp) { + double dv[3]; + float fv[3]; char *cp; int i; @@ -757,25 +770,35 @@ getinp(char *buf, FILE *fp) cp = buf; /* make sure we get 6 floats */ for (i = 0; i < 6; i++) { if (fgetword(cp, buf+127-cp, fp) == NULL) - return 0; + return -1; + if (i >= 3) + dv[i-3] = atof(cp); if ((cp = fskip(cp)) == NULL || *cp) - return 0; + return -1; *cp++ = ' '; } getc(fp); /* get/put eol */ *cp-- = '\0'; *cp = '\n'; + if (DOT(dv,dv) <= FTINY*FTINY) + return 0; /* dummy ray */ return strlen(buf); case 'f': if (fread(buf, sizeof(float), 6, fp) < 6) - return 0; + return -1; + memcpy(fv, buf+3*sizeof(float), 3*sizeof(float)); + if (DOT(fv,fv) <= FTINY*FTINY) + return 0; /* dummy ray */ return sizeof(float)*6; case 'd': if (fread(buf, sizeof(double), 6, fp) < 6) - return 0; + return -1; + memcpy(dv, buf+3*sizeof(double), 3*sizeof(double)); + if (DOT(dv,dv) <= FTINY*FTINY) + return 0; /* dummy ray */ return sizeof(double)*6; } error(INTERNAL, "botched input format"); - return 0; /* pro forma return */ + return -1; /* pro forma return */ } static float rparams[9]; /* traced ray parameters */ @@ -949,7 +972,8 @@ process_queue(void) } done_contrib(); /* sum up contributions & output */ lastdone = rtp->raynum; - free(rtp->buf); /* free up buffer space */ + if (rtp->buf != NULL) /* free up buffer space */ + free(rtp->buf); rt_unproc = rtp->next; free(rtp); /* done with this ray tree */ } @@ -1037,16 +1061,24 @@ trace_contribs(FILE *fin) int iblen; struct rtproc *rtp; /* loop over input */ - while ((iblen = getinp(inpbuf, fin)) > 0) { - if (lastray+1 < lastray || /* need reset? */ - queue_length() > 5*nrtprocs()) { + while ((iblen = getinp(inpbuf, fin)) >= 0) { + if (!iblen || /* need reset? */ + queue_length() > 10*nrtprocs() || + lastray+1 < lastray) { while (wait_rproc() != NULL) process_queue(); - lastdone = lastray = 0; + if (lastray+1 < lastray) + lastdone = lastray = 0; } rtp = get_rproc(); /* get avail. rtrace process */ - rtp->raynum = ++lastray; /* assign ray to it */ - writebuf(rtp->pd.w, inpbuf, iblen); + rtp->raynum = ++lastray; /* assign ray */ + if (iblen) { /* trace ray if valid */ + writebuf(rtp->pd.w, inpbuf, iblen); + } else { /* else bypass dummy ray */ + queue_raytree(rtp); /* empty tree */ + if ((yres <= 0) | (waitflush > 0)) + waitflush = 1; /* flush after this */ + } if (raysleft && !--raysleft) break; process_queue(); /* catch up with results */ @@ -1125,7 +1157,7 @@ recover_output(FILE *fin) if (oent->data != NULL) { sout = *(STREAMOUT *)oent->data; } else { - sout.reclen = 0; + sout.reclen = CNT_UNKNOWN; sout.ofp = NULL; } if (sout.ofp != NULL) { /* already open? */ @@ -1207,7 +1239,7 @@ recover_output(FILE *fin) lu_doall(&ofiletab, myseeko, &nvals); /* skip repeated input */ for (nvals = 0; nvals < lastout; nvals++) - if (getinp(oname, fin) <= 0) + if (getinp(oname, fin) < 0) error(USER, "unexpected EOF on input"); lastray = lastdone = (unsigned long)lastout; if (raysleft)