📄 openfilechnl.3
字号:
As a performance optimization, when reading from a channel with the encoding\fBbinary\fR, the bytes are not converted to UTF-8 as they are read.Instead, they are stored in \fIreadObjPtr\fR's internal representation as abyte-array object. The string representation of this object will only beconstructed if it is needed (e.g., because of a call to\fBTcl_GetStringFromObj\fR). In this way, byte-oriented data can be readfrom a channel, manipulated by calling \fBTcl_GetByteArrayFromObj\fR andrelated functions, and then written to a channel without the expense of everconverting to or from UTF-8..PP\fBTcl_Read\fR is similar to \fBTcl_ReadChars\fR, except that it doesn't doencoding conversions, regardless of the channel's encoding. It is deprecatedand exists for backwards compatibility with non-internationalized Tclextensions. It consumes bytes from \fIchannel\fR and stores them in\fIreadBuf\fR, performing end-of-line translations on the way. The return valueof \fBTcl_Read\fR is the number of bytes, up to \fIbytesToRead\fR, written in\fIreadBuf\fR. The buffer produced by \fBTcl_Read\fR is not NULL terminated.Its contents are valid from the zeroth position up to and excluding theposition indicated by the return value. .PP\fBTcl_ReadRaw\fR is the same as \fBTcl_Read\fR but does notcompensate for stacking. While \fBTcl_Read\fR (and the other functionsin the API) always get their data from the topmost channel in thestack the supplied channel is part of, \fBTcl_ReadRaw\fR doesnot. Thus this function is \fBonly\fR usable for transformationalchannel drivers, i.e. drivers used in the middle of a stack ofchannels, to move data from the channel below into the transformation..SH "TCL_GETSOBJ AND TCL_GETS".PP\fBTcl_GetsObj\fR consumes bytes from \fIchannel\fR, converting the bytes toUTF-8 based on the channel's encoding, until a full line of input has beenseen. If the channel's encoding is \fBbinary\fR, each byte read from thechannel is treated as an individual Unicode character. All of thecharacters of the line except for the terminating end-of-line character(s)are appended to \fIlineObjPtr\fR's string representation. The end-of-linecharacter(s) are read and discarded..PPIf a line was successfully read, the return value is greater than or equalto zero and indicates the number of bytes stored in \fIlineObjPtr\fR. If anerror occurs, \fBTcl_GetsObj\fR returns \-1 and records a POSIX error codethat can be retrieved with \fBTcl_GetErrno\fR. \fBTcl_GetsObj\fR alsoreturns \-1 if the end of the file is reached; the \fBTcl_Eof\fR procedurecan be used to distinguish an error from an end-of-file condition..PPIf the channel is in nonblocking mode, the return value can also be \-1 ifno data was available or the data that was available did not contain anend-of-line character. When \-1 is returned, the \fBTcl_InputBlocked\fRprocedure may be invoked to determine if the channel is blocked becauseof input unavailability..PP\fBTcl_Gets\fR is the same as \fBTcl_GetsObj\fR except the resultingcharacters are appended to the dynamic string given by\fIlineRead\fR rather than a Tcl object..SH "TCL_UNGETS".PP\fBTcl_Ungets\fR is used to add data to the input queue of a channel,at either the head or tail of the queue. The pointer \fIinput\fR pointsto the data that is to be added. The length of the input to add is givenby \fIinputLen\fR. A non-zero value of \fIaddAtEnd\fR indicates that thedata is to be added at the end of queue; otherwise it will be added at thehead of the queue. If \fIchannel\fR has a "sticky" EOF set, no data will beadded to the input queue. \fBTcl_Ungets\fR returns \fIinputLen\fR or-1 if an error occurs..SH "TCL_WRITECHARS, TCL_WRITEOBJ, AND TCL_WRITE".PP\fBTcl_WriteChars\fR accepts \fIbytesToWrite\fR bytes of character data at\fIcharBuf\fR. The UTF-8 characters in the buffer are converted to thechannel's encoding and queued for output to \fIchannel\fR. If\fIbytesToWrite\fR is negative, \fBTcl_WriteChars\fR expects \fIcharBuf\fRto be NULL terminated and it outputs everything up to the NULL..PPData queued for output may not appear on the output device immediately, dueto internal buffering. If the data should appear immediately, call\fBTcl_Flush\fR after the call to \fBTcl_WriteChars\fR, or set the \fB\-buffering\fR option on the channel to \fBnone\fR. If you wish the datato appear as soon as a complete line is accepted for output, set the\fB\-buffering\fR option on the channel to \fBline\fR mode..PPThe return value of \fBTcl_WriteChars\fR is a count of how many bytes wereaccepted for output to the channel. This is either greater than zero toindicate success or \-1 to indicate that an error occurred. If an erroroccurs, \fBTcl_WriteChars\fR records a POSIX error code that may beretrieved with \fBTcl_GetErrno\fR..PPNewline characters in the output data are translated to platform-specificend-of-line sequences according to the \fB\-translation\fR option for thechannel. This is done even if the channel has no encoding..PP\fBTcl_WriteObj\fR is similar to \fBTcl_WriteChars\fR except itaccepts a Tcl object whose contents will be output to the channel. TheUTF-8 characters in \fIwriteObjPtr\fR's string representation are convertedto the channel's encoding and queued for output to \fIchannel\fR. As a performance optimization, when writing to a channel with the encoding\fBbinary\fR, UTF-8 characters are not converted as they are written.Instead, the bytes in \fIwriteObjPtr\fR's internal representation as abyte-array object are written to the channel. The byte-array representationof the object will be constructed if it is needed. In this way,byte-oriented data can be read from a channel, manipulated by calling\fBTcl_GetByteArrayFromObj\fR and related functions, and then written to achannel without the expense of ever converting to or from UTF-8..PP\fBTcl_Write\fR is similar to \fBTcl_WriteChars\fR except that it doesn't doencoding conversions, regardless of the channel's encoding. It isdeprecated and exists for backwards compatibility with non-internationalizedTcl extensions. It accepts \fIbytesToWrite\fR bytes of data at\fIbyteBuf\fR and queues them for output to \fIchannel\fR. If\fIbytesToWrite\fR is negative, \fBTcl_Write\fR expects \fIbyteBuf\fR to beNULL terminated and it outputs everything up to the NULL..PP\fBTcl_WriteRaw\fR is the same as \fBTcl_Write\fR but does notcompensate for stacking. While \fBTcl_Write\fR (and the otherfunctions in the API) always feed their input to the topmost channelin the stack the supplied channel is part of, \fBTcl_WriteRaw\fR doesnot. Thus this function is \fBonly\fR usable for transformationalchannel drivers, i.e. drivers used in the middle of a stack ofchannels, to move data from the transformation into the channel belowit..VE.SH TCL_FLUSH.PP\fBTcl_Flush\fR causes all of the buffered output data for \fIchannel\fRto be written to its underlying file or device as soon as possible.If the channel is in blocking mode, the call does not return untilall the buffered data has been sent to the channel or some error occurred.The call returns immediately if the channel is nonblocking; it startsa background flush that will write the buffered data to the channeleventually, as fast as the channel is able to absorb it..PPThe return value is normally \fBTCL_OK\fR.If an error occurs, \fBTcl_Flush\fR returns \fBTCL_ERROR\fR andrecords a POSIX error code that can be retrieved with \fBTcl_GetErrno\fR..SH TCL_SEEK.PP\fBTcl_Seek\fR moves the access point in \fIchannel\fR where subsequentdata will be read or written. Buffered output is flushed to the channel andbuffered input is discarded, prior to the seek operation..PP\fBTcl_Seek\fR normally returns the new access point.If an error occurs, \fBTcl_Seek\fR returns \-1 and records a POSIX errorcode that can be retrieved with \fBTcl_GetErrno\fR.After an error, the access point may or may not have been moved..SH TCL_TELL.PP\fBTcl_Tell\fR returns the current access point for a channel. The returnedvalue is \-1 if the channel does not support seeking..SH TCL_GETCHANNELOPTION.PP\fBTcl_GetChannelOption\fR retrieves, in \fIoptionValue\fR, the value of one ofthe options currently in effect for a channel, or a list of all options andtheir values. The \fIchannel\fR argument identifies the channel for whichto query an option or retrieve all options and their values.If \fIoptionName\fR is not NULL, it is the name of theoption to query; the option's value is copied to the Tcl dynamic stringdenoted by \fIoptionValue\fR. If\fIoptionName\fR is NULL, the function stores an alternating list of optionnames and their values in \fIoptionValue\fR, using a series of calls to\fBTcl_DStringAppendElement\fR. The various preexisting options andtheir possible values are described in the manual entry for the Tcl\fBfconfigure\fR command. Other options can be added by each channel type.These channel type specific options are described in the manual entry forthe Tcl command that creates a channel of that type; for example, theadditional options for TCP based channels are described in the manual entryfor the Tcl \fBsocket\fR command.The procedure normally returns \fBTCL_OK\fR. If an error occurs, it returns\fBTCL_ERROR\fR and calls \fBTcl_SetErrno\fR to store an appropriate POSIXerror code..SH TCL_SETCHANNELOPTION.PP\fBTcl_SetChannelOption\fR sets a new value \fInewValue\fRfor an option \fIoptionName\fR on \fIchannel\fR.The procedure normally returns \fBTCL_OK\fR. If an error occurs,it returns \fBTCL_ERROR\fR; in addition, if \fIinterp\fR is non-NULL,\fBTcl_SetChannelOption\fR leaves an error message in the interpreter's result..SH TCL_EOF.PP\fBTcl_Eof\fR returns a nonzero value if \fIchannel\fR encounteredan end of file during the last input operation..SH TCL_INPUTBLOCKED.PP\fBTcl_InputBlocked\fR returns a nonzero value if \fIchannel\fR is innonblocking mode and the last input operation returned less data thanrequested because there was insufficient data available.The call always returns zero if the channel is in blocking mode..SH TCL_INPUTBUFFERED.PP\fBTcl_InputBuffered\fR returns the number of bytes of input currentlybuffered in the internal buffers for a channel. If the channel is not openfor reading, this function always returns zero..SH TCL_OUTPUTBUFFERED.VS 8.4\fBTcl_OutputBuffered\fR returns the number of bytes of outputcurrently buffered in the internal buffers for a channel. If thechannel is not open for writing, this function always returns zero..VE.SH "PLATFORM ISSUES".PPThe handles returned from \fBTcl_GetChannelHandle\fR depend on theplatform and the channel type. On Unix platforms, the handle isalways a Unix file descriptor as returned from the \fBopen\fR systemcall. On Windows platforms, the handle is a file \fBHANDLE\fR whenthe channel was created with \fBTcl_OpenFileChannel\fR,\fBTcl_OpenCommandChannel\fR, or \fBTcl_MakeFileChannel\fR. Otherchannel types may return a different type of handle on Windowsplatforms. On the Macintosh platform, the handle is a file referencenumber as returned from \fBHOpenDF\fR..SH "SEE ALSO"DString(3), fconfigure(n), filename(n), fopen(3), Tcl_CreateChannel(3).SH KEYWORDSaccess point, blocking, buffered I/O, channel, channel driver, end of file,flush, input, nonblocking, output, read, seek, write
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -