static int readchar(Tcl_Channel f) { char c; if (Tcl_Read(f, &c, 1) != 1) { return (Tcl_Eof(f)) ? 0 : -1; } return c; }
static int ConnSend(Ns_Conn *conn, int nsend, Tcl_Channel chan, FILE *fp, int fd, off_t off) { size_t toread; int nread, status; char buf[IOBUFSZ]; /* * Even if nsend is 0, ensure all queued data (like HTTP response * headers) get flushed. */ if (nsend == 0) { Ns_WriteConn(conn, NULL, 0); } status = NS_OK; while (status == NS_OK && nsend > 0) { toread = (size_t) nsend; if (toread > sizeof(buf)) { toread = sizeof(buf); } if (chan != NULL) { nread = Tcl_Read(chan, buf, (int) toread); } else if (fp != NULL) { nread = fread(buf, 1, toread, fp); if (ferror(fp)) { nread = -1; } } else if (off < 0) { nread = read(fd, buf, toread); } else { #ifdef WIN32 nread = -1; #else nread = pread(fd, buf, toread, off); if (nread > 0) { off += (off_t) nread; } #endif } if (nread == -1) { status = NS_ERROR; } else if (nread == 0) { nsend = 0; /* NB: Silently ignore a truncated file. */ } else if ((status = Ns_WriteConn(conn, buf, nread)) == NS_OK) { nsend -= nread; } } return status; }
static int tclgd_channelGetchar (gdIOCtx * ctx) { unsigned char b[2]; tclgd_channelIOCtx *tctx; tctx = (tclgd_channelIOCtx *) ctx; Tcl_Read (tctx->channel, (char *)&b, 1); /* printf("tclgd_channelGetchar ctx %lx char %d\n", ctx, b[0]); */ return b[0]; }
static int tclgd_channelGetbuf (gdIOCtx * ctx, void *buf, int size) { tclgd_channelIOCtx *tctx; int readResult; tctx = (tclgd_channelIOCtx *) ctx; readResult = Tcl_Read (tctx->channel, buf, size); /* printf("tclgd_channelGetbuf ctx %lx buf %lx size %d result %d\n", ctx, buf, size, readResult); */ return readResult; }
static int GetByte( Tcl_Channel chan) /* The channel we read from. */ { char buffer; int size; size = Tcl_Read(chan, &buffer, 1); if (size <= 0) { return EOF; } else { return buffer; } }
void RtclSignalAction::TclChannelHandler(int mask) { char signum; Tcl_Read(fShuttleChn, (char*) &signum, sizeof(signum)); // FIXME_code: handle return code Tcl_SetVar2Ex(fpInterp, "Rutil_signum", NULL, Tcl_NewIntObj((int)signum), 0); // FIXME_code: handle return code if ((Tcl_Obj*)fpScript[(int)signum]) { Tcl_EvalObjEx(fpInterp, fpScript[(int)signum], TCL_EVAL_GLOBAL); // FIXME_code: handle return code } return; }
int tclcommand_readmd(ClientData dummy, Tcl_Interp *interp, int argc, char **argv) { char *row; int pos_row[3] = { -1 }, v_row[3] = { -1 }, #ifdef DIPOLES dip_row[3] = { -1 }, #endif f_row[3] = { -1 }; int av_pos = 0, av_v = 0, #ifdef DIPOLES av_dip=0, #endif #ifdef MASS av_mass=0, #endif #ifdef SHANCHEN av_solvation=0, #endif av_f = 0, #ifdef ELECTROSTATICS av_q = 0, #endif av_type = 0; int node, i; struct MDHeader header; Particle data; int tcl_file_mode; Tcl_Channel channel; if (argc != 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " <file>\"", (char *) NULL); return (TCL_ERROR); } if ((channel = Tcl_GetChannel(interp, argv[1], &tcl_file_mode)) == NULL) return (TCL_ERROR); /* tune channel to binary translation, e.g. none */ Tcl_SetChannelOption(interp, channel, "-translation", "binary"); Tcl_Read(channel, (char *)&header, sizeof(header)); /* check token */ if (strncmp(header.magic, MDMAGIC, 4) || header.n_rows < 0) { Tcl_AppendResult(interp, "data file \"", argv[1], "\" does not contain tcl MD data", (char *) NULL); return (TCL_ERROR); } if (!particle_node) build_particle_node(); /* parse rows */ row = (char*)malloc(header.n_rows*sizeof(char)); for (i = 0; i < header.n_rows; i++) { Tcl_Read(channel, (char *)&row[i], sizeof(char)); switch (row[i]) { case POSX: pos_row[0] = i; break; case POSY: pos_row[1] = i; break; case POSZ: pos_row[2] = i; break; case VX: v_row[0] = i; break; case VY: v_row[1] = i; break; case VZ: v_row[2] = i; break; #ifdef DIPOLES case MX: dip_row[0] = i; break; case MY: dip_row[1] = i; break; case MZ: dip_row[2] = i; break; #endif case FX: f_row[0] = i; break; case FY: f_row[1] = i; break; case FZ: f_row[2] = i; break; #ifdef MASS case MASSES: av_mass = 1; break; #endif #ifdef SHANCHEN case SOLVATION: av_solvation = 1; break; #endif #ifdef ELECTROSTATICS case Q: av_q = 1; break; #endif case TYPE: av_type = 1; break; } } /* *_row[0] tells if * data is completely available - * otherwise we ignore it */ if (pos_row[0] != -1 && pos_row[1] != -1 && pos_row[2] != -1) { av_pos = 1; } if (v_row[0] != -1 && v_row[1] != -1 && v_row[2] != -1) { av_v = 1; } if (f_row[0] != -1 && f_row[1] != -1 && f_row[2] != -1) { av_f = 1; } #ifdef DIPOLES if (dip_row[0] != -1 && dip_row[1] != -1 && dip_row[2] != -1) { av_dip = 1; } #endif while (!Tcl_Eof(channel)) { Tcl_Read(channel, (char *)&data.p.identity, sizeof(int)); if (data.p.identity == -1) break; /* printf("id=%d\n", data.identity); */ if (data.p.identity < 0) { Tcl_AppendResult(interp, "illegal data format in data file \"", argv[1], "\", perhaps wrong file?", (char *) NULL); free(row); return (TCL_ERROR); } for (i = 0; i < header.n_rows; i++) { switch (row[i]) { case POSX: Tcl_Read(channel, (char *)&data.r.p[0], sizeof(double)); break; case POSY: Tcl_Read(channel, (char *)&data.r.p[1], sizeof(double)); break; case POSZ: Tcl_Read(channel, (char *)&data.r.p[2], sizeof(double)); break; case VX: Tcl_Read(channel, (char *)&data.m.v[0], sizeof(double)); break; case VY: Tcl_Read(channel, (char *)&data.m.v[1], sizeof(double)); break; case VZ: Tcl_Read(channel, (char *)&data.m.v[2], sizeof(double)); break; case FX: Tcl_Read(channel, (char *)&data.f.f[0], sizeof(double)); break; case FY: Tcl_Read(channel, (char *)&data.f.f[1], sizeof(double)); break; case FZ: Tcl_Read(channel, (char *)&data.f.f[2], sizeof(double)); break; case MASSES: #ifdef MASS Tcl_Read(channel, (char *)&data.p.mass, sizeof(double)); break; #else { double dummy_mass; Tcl_Read(channel, (char *)&dummy_mass, sizeof(double)); break; } #endif #ifdef ELECTROSTATICS case Q: Tcl_Read(channel, (char *)&data.p.q, sizeof(double)); break; #endif #ifdef DIPOLES case MX: Tcl_Read(channel, (char *)&data.r.dip[0], sizeof(double)); break; case MY: Tcl_Read(channel, (char *)&data.r.dip[1], sizeof(double)); break; case MZ: Tcl_Read(channel, (char *)&data.r.dip[2], sizeof(double)); break; #endif case TYPE: Tcl_Read(channel, (char *)&data.p.type, sizeof(int)); break; } } node = (data.p.identity <= max_seen_particle) ? particle_node[data.p.identity] : -1; if (node == -1) { if (!av_pos) { Tcl_AppendResult(interp, "new particle without position data", (char *) NULL); free(row); return (TCL_ERROR); } } if (av_pos) place_particle(data.p.identity, data.r.p); #ifdef MASS if (av_mass) set_particle_mass(data.p.identity, data.p.mass); #endif #ifdef SHANCHEN if (av_solvation) set_particle_solvation(data.p.identity, data.p.solvation); #endif #ifdef ELECTROSTATICS if (av_q) set_particle_q(data.p.identity, data.p.q); #endif #ifdef DIPOLES if (av_dip) set_particle_dip(data.p.identity, data.r.dip); #endif if (av_v) set_particle_v(data.p.identity, data.m.v); if (av_f) set_particle_f(data.p.identity, data.f.f); if (av_type) set_particle_type(data.p.identity, data.p.type); } free(row); return TCL_OK; }
static int ReadPPMFileHeader( Tcl_Channel chan, /* Image file to read the header from. */ int *widthPtr, int *heightPtr, /* The dimensions of the image are returned * here. */ int *maxIntensityPtr) /* The maximum intensity value for the image * is stored here. */ { #define BUFFER_SIZE 1000 char buffer[BUFFER_SIZE], c; int i, numFields, type = 0; /* * Read 4 space-separated fields from the file, ignoring comments (any * line that starts with "#"). */ if (Tcl_Read(chan, &c, 1) != 1) { return 0; } i = 0; for (numFields = 0; numFields < 4; numFields++) { /* * Skip comments and white space. */ while (1) { while (isspace(UCHAR(c))) { if (Tcl_Read(chan, &c, 1) != 1) { return 0; } } if (c != '#') { break; } do { if (Tcl_Read(chan, &c, 1) != 1) { return 0; } } while (c != '\n'); } /* * Read a field (everything up to the next white space). */ while (!isspace(UCHAR(c))) { if (i < (BUFFER_SIZE-2)) { buffer[i] = c; i++; } if (Tcl_Read(chan, &c, 1) != 1) { goto done; } } if (i < (BUFFER_SIZE-1)) { buffer[i] = ' '; i++; } } done: buffer[i] = 0; /* * Parse the fields, which are: id, width, height, maxIntensity. */ if (strncmp(buffer, "P6 ", 3) == 0) { type = PPM; } else if (strncmp(buffer, "P5 ", 3) == 0) { type = PGM; } else { return 0; } if (sscanf(buffer+3, "%d %d %d", widthPtr, heightPtr, maxIntensityPtr) != 3) { return 0; } return type; }
static int FileReadPPM( Tcl_Interp *interp, /* Interpreter to use for reporting errors. */ Tcl_Channel chan, /* The image file, open for reading. */ CONST char *fileName, /* The name of the image file. */ Tcl_Obj *format, /* User-specified format string, or NULL. */ Tk_PhotoHandle imageHandle, /* The photo image to write into. */ int destX, int destY, /* Coordinates of top-left pixel in photo * image to be written to. */ int width, int height, /* Dimensions of block of photo image to be * written to. */ int srcX, int srcY) /* Coordinates of top-left pixel to be used in * image being read. */ { int fileWidth, fileHeight, maxIntensity; int nLines, nBytes, h, type, count; unsigned char *pixelPtr; Tk_PhotoImageBlock block; type = ReadPPMFileHeader(chan, &fileWidth, &fileHeight, &maxIntensity); if (type == 0) { Tcl_AppendResult(interp, "couldn't read raw PPM header from file \"", fileName, "\"", NULL); return TCL_ERROR; } if ((fileWidth <= 0) || (fileHeight <= 0)) { Tcl_AppendResult(interp, "PPM image file \"", fileName, "\" has dimension(s) <= 0", NULL); return TCL_ERROR; } if ((maxIntensity <= 0) || (maxIntensity >= 256)) { char buffer[TCL_INTEGER_SPACE]; sprintf(buffer, "%d", maxIntensity); Tcl_AppendResult(interp, "PPM image file \"", fileName, "\" has bad maximum intensity value ", buffer, NULL); return TCL_ERROR; } if ((srcX + width) > fileWidth) { width = fileWidth - srcX; } if ((srcY + height) > fileHeight) { height = fileHeight - srcY; } if ((width <= 0) || (height <= 0) || (srcX >= fileWidth) || (srcY >= fileHeight)) { return TCL_OK; } if (type == PGM) { block.pixelSize = 1; block.offset[0] = 0; block.offset[1] = 0; block.offset[2] = 0; } else { block.pixelSize = 3; block.offset[0] = 0; block.offset[1] = 1; block.offset[2] = 2; } block.offset[3] = 0; block.width = width; block.pitch = block.pixelSize * fileWidth; if (Tk_PhotoExpand(interp, imageHandle, destX + width, destY + height) != TCL_OK) { return TCL_ERROR; } if (srcY > 0) { Tcl_Seek(chan, (Tcl_WideInt)(srcY * block.pitch), SEEK_CUR); } nLines = (MAX_MEMORY + block.pitch - 1) / block.pitch; if (nLines > height) { nLines = height; } if (nLines <= 0) { nLines = 1; } nBytes = nLines * block.pitch; pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes); block.pixelPtr = pixelPtr + srcX * block.pixelSize; for (h = height; h > 0; h -= nLines) { if (nLines > h) { nLines = h; nBytes = nLines * block.pitch; } count = Tcl_Read(chan, (char *) pixelPtr, nBytes); if (count != nBytes) { Tcl_AppendResult(interp, "error reading PPM image file \"", fileName, "\": ", Tcl_Eof(chan) ? "not enough data" : Tcl_PosixError(interp), NULL); ckfree((char *) pixelPtr); return TCL_ERROR; } if (maxIntensity != 255) { unsigned char *p; for (p = pixelPtr; count > 0; count--, p++) { *p = (((int) *p) * 255)/maxIntensity; } } block.height = nLines; if (Tk_PhotoPutBlock(interp, imageHandle, &block, destX, destY, width, nLines, TK_PHOTO_COMPOSITE_SET) != TCL_OK) { ckfree((char *) pixelPtr); return TCL_ERROR; } destY += nLines; } ckfree((char *) pixelPtr); return TCL_OK; }
static int ReadOptionFile( Tcl_Interp *interp, /* Interpreter to use for reporting results. */ Tk_Window tkwin, /* Token for window: options are entered for * this window's main window. */ const char *fileName, /* Name of file containing options. */ int priority) /* Priority level to use for options in this * file, such as TK_USER_DEFAULT_PRIO or * TK_INTERACTIVE_PRIO. Must be between 0 and * TK_MAX_PRIO. */ { const char *realName; char *buffer; int result, bufferSize; Tcl_Channel chan; Tcl_DString newName; /* * Prevent file system access in a safe interpreter. */ if (Tcl_IsSafe(interp)) { Tcl_AppendResult(interp, "can't read options from a file in a", " safe interpreter", NULL); return TCL_ERROR; } realName = Tcl_TranslateFileName(interp, fileName, &newName); if (realName == NULL) { return TCL_ERROR; } chan = Tcl_OpenFileChannel(interp, realName, "r", 0); Tcl_DStringFree(&newName); if (chan == NULL) { Tcl_ResetResult(interp); Tcl_AppendResult(interp, "couldn't open \"", fileName, "\": ", Tcl_PosixError(interp), NULL); return TCL_ERROR; } /* * Compute size of file by seeking to the end of the file. This will * overallocate if we are performing CRLF translation. */ bufferSize = (int) Tcl_Seek(chan, (Tcl_WideInt) 0, SEEK_END); Tcl_Seek(chan, (Tcl_WideInt) 0, SEEK_SET); if (bufferSize < 0) { Tcl_AppendResult(interp, "error seeking to end of file \"", fileName, "\":", Tcl_PosixError(interp), NULL); Tcl_Close(NULL, chan); return TCL_ERROR; } buffer = ckalloc((unsigned) bufferSize+1); bufferSize = Tcl_Read(chan, buffer, bufferSize); if (bufferSize < 0) { Tcl_AppendResult(interp, "error reading file \"", fileName, "\":", Tcl_PosixError(interp), NULL); Tcl_Close(NULL, chan); return TCL_ERROR; } Tcl_Close(NULL, chan); buffer[bufferSize] = 0; result = AddFromString(interp, tkwin, buffer, priority); ckfree(buffer); return result; }