/* Get ray contribution from previous file */ static int get_contrib(DCOLOR cnt, FILE *finp) { COLOR fv; COLR cv; switch (outfmt) { case 'a': return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3); case 'f': if (getbinary(fv, sizeof(fv[0]), 3, finp) != 3) return(0); copycolor(cnt, fv); return(1); case 'd': return(getbinary(cnt, sizeof(cnt[0]), 3, finp) == 3); case 'c': if (getbinary(cv, sizeof(cv), 1, finp) != 1) return(0); colr_color(fv, cv); copycolor(cnt, fv); return(1); default: error(INTERNAL, "botched output format"); } return(0); /* pro forma return */ }
/* Get a vector from stdin */ int getvec(FVECT vec) { float vf[3]; double vd[3]; char buf[32]; int i; switch (inpfmt) { case 'a': /* ascii */ for (i = 0; i < 3; i++) { if (fgetword(buf, sizeof(buf), stdin) == NULL || !isflt(buf)) return(-1); vec[i] = atof(buf); } break; case 'f': /* binary float */ if (getbinary((char *)vf, sizeof(float), 3, stdin) != 3) return(-1); VCOPY(vec, vf); break; case 'd': /* binary double */ if (getbinary((char *)vd, sizeof(double), 3, stdin) != 3) return(-1); VCOPY(vec, vd); break; default: error(CONSISTENCY, "botched input format"); } return(0); }
/* Allocate and load a matrix from the given input (or stdin if NULL) */ CMATRIX * cm_load(const char *inspec, int nrows, int ncols, int dtype) { const int ROWINC = 2048; FILE *fp = stdin; CMATRIX *cm; if (inspec == NULL) inspec = "<stdin>"; else if (inspec[0] == '!') { fp = popen(inspec+1, "r"); if (fp == NULL) { sprintf(errmsg, "cannot start command '%s'", inspec); error(SYSTEM, errmsg); } } else if ((fp = fopen(inspec, "r")) == NULL) { sprintf(errmsg, "cannot open file '%s'", inspec); error(SYSTEM, errmsg); } #ifdef getc_unlocked flockfile(fp); #endif if (dtype != DTascii) SET_FILE_BINARY(fp); /* doesn't really work */ if (!dtype | !ncols) { /* expecting header? */ char *err = cm_getheader(&dtype, &nrows, &ncols, fp); if (err != NULL) error(USER, err); if (ncols <= 0) error(USER, "unspecified number of columns"); } switch (dtype) { case DTascii: case DTfloat: case DTdouble: break; default: error(USER, "unexpected data type in cm_load()"); } if (nrows <= 0) { /* don't know length? */ int guessrows = 147; /* usually big enough */ if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) { long startpos = ftell(fp); if (fseek(fp, 0L, SEEK_END) == 0) { long endpos = ftell(fp); long elemsiz = 3*(dtype==DTfloat ? sizeof(float) : sizeof(double)); if ((endpos - startpos) % (ncols*elemsiz)) { sprintf(errmsg, "improper length for binary file '%s'", inspec); error(USER, errmsg); } guessrows = (endpos - startpos)/(ncols*elemsiz); if (fseek(fp, startpos, SEEK_SET) < 0) { sprintf(errmsg, "fseek() error on file '%s'", inspec); error(SYSTEM, errmsg); } nrows = guessrows; /* we're confident */ } } cm = cm_alloc(guessrows, ncols); } else cm = cm_alloc(nrows, ncols); if (cm == NULL) /* XXX never happens */ return(NULL); if (dtype == DTascii) { /* read text file */ int maxrow = (nrows > 0 ? nrows : 32000); int r, c; for (r = 0; r < maxrow; r++) { if (r >= cm->nrows) /* need more space? */ cm = cm_resize(cm, cm->nrows+ROWINC); for (c = 0; c < ncols; c++) { COLORV *cv = cm_lval(cm,r,c); if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) { if ((nrows <= 0) & (r > 0) & !c) { cm = cm_resize(cm, maxrow=r); break; } else goto EOFerror; } } } while ((c = getc(fp)) != EOF) if (!isspace(c)) { sprintf(errmsg, "unexpected data at end of ascii input '%s'", inspec); error(WARNING, errmsg); break; } } else { /* read binary file */ if (sizeof(COLOR) == cm_elem_size[dtype]) { int nread = 0; do { /* read all we can */ nread += getbinary(cm->cmem + 3*nread, sizeof(COLOR), cm->nrows*cm->ncols - nread, fp); if (nrows <= 0) { /* unknown length */ if (nread == cm->nrows*cm->ncols) /* need more space? */ cm = cm_resize(cm, cm->nrows+ROWINC); else if (nread && !(nread % cm->ncols)) /* seem to be done */ cm = cm_resize(cm, nread/cm->ncols); else /* ended mid-row */ goto EOFerror; } else if (nread < cm->nrows*cm->ncols) goto EOFerror; } while (nread < cm->nrows*cm->ncols); } else if (dtype == DTdouble) { double dc[3]; /* load from double */ COLORV *cvp = cm->cmem; int n = nrows*ncols; if (n <= 0) goto not_handled; while (n--) { if (getbinary(dc, sizeof(double), 3, fp) != 3) goto EOFerror; copycolor(cvp, dc); cvp += 3; } } else /* dtype == DTfloat */ { float fc[3]; /* load from float */ COLORV *cvp = cm->cmem; int n = nrows*ncols; if (n <= 0) goto not_handled; while (n--) { if (getbinary(fc, sizeof(float), 3, fp) != 3) goto EOFerror; copycolor(cvp, fc); cvp += 3; } } if (fgetc(fp) != EOF) { sprintf(errmsg, "unexpected data at end of binary input '%s'", inspec); error(WARNING, errmsg); } } if (fp != stdin) { if (inspec[0] != '!') fclose(fp); else if (pclose(fp)) { sprintf(errmsg, "error running command '%s'", inspec); error(WARNING, errmsg); } } #ifdef getc_unlocked else funlockfile(fp); #endif return(cm); EOFerror: sprintf(errmsg, "unexpected EOF reading %s", inspec); error(USER, errmsg); not_handled: error(INTERNAL, "unhandled data size or length in cm_load()"); return(NULL); /* gratis return */ }
int get_ia_stat(char *hostname, char *username ) { #ifdef NESSIE int connsid, iter, ival, initOpsCompl, newOpsCompl, updated; TIMESTAMP timeStamp, timeStamp2; char expprocReply[ 122 ]; gettimeofday( &timeStamp, NULL); initOpsCompl = getStatOpsCompl(); ival = talk2Acq( hostname, username, READACQHW, "", &expprocReply[ 0 ], sizeof( expprocReply ) ); if (ival < 0) return( -1 ); else connsid = ival; ival = prepare_reply_socket( connsid ); if (ival != 0) { close( connsid ); return( -1 ); } ival = getascii( connsid, &expprocReply[ 0 ], sizeof( expprocReply ) - 1 ); close( connsid ); updated = 0; for (iter = 0; iter < 50; iter++) { /* increased from 10 to 50, July 1997 */ usleep2( 20000 ); /* 20 ms, 0.02 s */ getStatTimeStamp( &timeStamp2 ); newOpsCompl = getStatOpsCompl(); if (cmpTimeStamp( &timeStamp2, &timeStamp ) > 0 && newOpsCompl != initOpsCompl) { updated = 1; break; } } if (updated == 0) return( -1 ); else return( 0 ); #else int connsid, ival; #if defined(WINBRIDGE) && defined(DEBUG) fprintf(stdout, "DEBUG jgw: socket.c get_ia_stat calling poke_acqproc\n"); fprintf(stdout, "DEBUG jgw: socket.c get_ia_stat hostname = %s\n", hostname); fprintf(stdout, "DEBUG jgw: socket.c get_ia_stat username = %s\n", username); fprintf(stdout, "DEBUG jgw: socket.c get_ia_stat cmd = %d\n", READACQHW); #endif ival = poke_acqproc(hostname, username, READACQHW, "1,24,"); if (ival < 0) { return( -1 ); } else connsid = ival; ival = prepare_reply_socket( connsid ); if (ival != 0) { return( -1 ); } memset( &shlk_block, 0, sizeof( struct ia_stat ) ); ival = getbinary( connsid, &shlk_block, sizeof( struct ia_stat ) ); close( connsid ); if (ival > 0) return( 0 ); else return( -1 ); #endif }
int serv_result(void) /* get next server result and process it */ { static char *buf = NULL; static int bufsiz = 0; MSGHEAD msg; /* read message header */ if (getbinary(&msg, sizeof(MSGHEAD), 1, stdin) != 1) goto readerr; if (msg.nbytes > 0) { /* get the message body */ if (msg.nbytes > bufsiz) { if (buf == NULL) buf = (char *)malloc(bufsiz=msg.nbytes); else buf = (char *)realloc((void *)buf, bufsiz=msg.nbytes); if (buf == NULL) error(SYSTEM, "out of memory in serv_result"); } if (getbinary(buf, 1, msg.nbytes, stdin) != msg.nbytes) goto readerr; } switch (msg.type) { /* process results */ case DS_BUNDLE: if (msg.nbytes < sizeof(PACKHEAD) || msg.nbytes != packsiz(((PACKHEAD *)buf)->nr)) error(INTERNAL, "bad display packet from server"); disp_bundle((PACKHEAD *)buf); break; case DS_ADDHOLO: if (msg.nbytes < sizeof(HDGRID)+2) error(INTERNAL, "bad holodeck record from server"); add_holo((HDGRID *)buf, buf+sizeof(HDGRID), buf+sizeof(HDGRID)+strlen(buf+sizeof(HDGRID))+1); break; case DS_OUTSECT: do_outside = 1; goto noargs; case DS_EYESEP: if (msg.nbytes <= 1 || (eyesepdist = atof(buf)) <= FTINY) error(INTERNAL, "bad eye separation from server"); break; case DS_STARTIMM: case DS_ENDIMM: if (!(imm_mode = (msg.type==DS_STARTIMM))) dev_flush(); #ifdef DEBUG { time_t tnow = time(NULL); if (msg.type==DS_STARTIMM) tadd += tnow - tmodesw; else timm += tnow - tmodesw; tmodesw = tnow; } #endif goto noargs; case DS_ACKNOW: case DS_SHUTDOWN: goto noargs; default: error(INTERNAL, "unrecognized result from server process"); } return(msg.type); /* return message type */ noargs: if (msg.nbytes) { sprintf(errmsg, "unexpected body with server message %d", msg.type); error(INTERNAL, errmsg); } return(msg.type); readerr: if (feof(stdin)) error(SYSTEM, "server process died"); error(SYSTEM, "error reading from server process"); return -1; }