int STREAMReadBytes(STREAM *S, char *Buffer, int Buffsize) { char *ptr=NULL; int bytes=0, result=0, total=0; ptr=Buffer; if (S->InStart >= S->InEnd) { result=STREAMReadCharsToBuffer(S); if (S->InStart >= S->InEnd) { if (result==STREAM_CLOSED) return(EOF); if (result==STREAM_TIMEOUT) return(STREAM_TIMEOUT); if (result==STREAM_DATA_ERROR) return(STREAM_DATA_ERROR); } } while (total < Buffsize) { total+=STREAMTransferBytesOut(S, Buffer+total, Buffsize-total); bytes=S->InEnd - S->InStart; if (bytes < 1) { //in testing, the best way to prevent doing constant checking for new bytes, //and so filling up the buffer, was to only check for new bytes if //we didn't have enough to satisfy another read like the one we just had //We must check for '< 1' rather than '-1' because result=FDCheckForBytes(S->in_fd); if (result ==-1) { if (total==0) total=EOF; break; } if (result < 1) break; result=STREAMReadCharsToBuffer(S); if (result < 1) { if (total > 0) return(total); else return(result); } } } return(total); }
int STREAMPeekChar(STREAM *S) { int result; if (S->InStart >= S->InEnd) { result=STREAMReadCharsToBuffer(S); if (result < 1) return(result); } return(* (S->InputBuff + S->InStart)); }
int STREAMCountWaitingBytes(STREAM *S) { int read_result=0, result; if (! S) return(0); if (S->State & SS_EMBARGOED) return(0); result=FDCheckForBytes(S->in_fd); if (result > 0) read_result=STREAMReadCharsToBuffer(S); else if (result < 0) read_result=STREAM_CLOSED; result=S->InEnd - S->InStart; if (result > 0) return(result); if (read_result==STREAM_CLOSED) return(EOF); if (read_result==STREAM_DATA_ERROR) return(EOF); return(0); }
char *STREAMReadToTerminator(char *Buffer, STREAM *S,unsigned char Term) { int result, len=0, bytes_read=0; char *RetStr=NULL, *end, *ptr; RetStr=Buffer; while (1) { if (S->InEnd > S->InStart) { //memchr is wicked fast, so use it ptr=memchr(S->InputBuff+S->InStart,Term,S->InEnd - S->InStart); if (ptr) len=(ptr+1)-(S->InputBuff+S->InStart); else len=S->InEnd-S->InStart; //Get length of RetStr, because SetStrLen might realloc it RetStr=SetStrLen(RetStr,bytes_read + len); len=STREAMTransferBytesOut(S, RetStr+bytes_read , len); bytes_read+=len; *(RetStr+bytes_read)='\0'; //Only return if we found the terminator (using memchr, above) if (ptr) return(RetStr); } result=STREAMReadCharsToBuffer(S); if ((result != STREAM_NODATA) && (S->InStart >= S->InEnd)) { if (bytes_read==0) { DestroyString(RetStr); return(NULL); } return(RetStr); } } //impossible to get here! return(NULL); }
int STREAMCheckForWaitingChar(STREAM *S,unsigned char check_char) { int read_result=0, result, trash; char *found_char; if (! S) return(0); if (S->State & SS_EMBARGOED) return(0); result=FDCheckForBytes(S->in_fd); if (result > 0) read_result=STREAMReadCharsToBuffer(S); else if (result < 0) read_result=STREAM_CLOSED; if (S->InStart < S->InEnd) { found_char=memchr(S->InputBuff + S->InStart,check_char,S->InEnd - S->InStart); if (found_char > 0) return(TRUE); } if (read_result==STREAM_CLOSED) return(EOF); if (read_result==STREAM_DATA_ERROR) return(EOF); return(FALSE); }
int STREAMPeekBytes(STREAM *S, char *Buffer, int Buffsize) { int len=0, result=0; if (S->InStart >= S->InEnd) { result=STREAMReadCharsToBuffer(S); if (S->InStart >= S->InEnd) { if (result==STREAM_CLOSED) return(EOF); if (result==STREAM_TIMEOUT) return(STREAM_TIMEOUT); if (result==STREAM_DATA_ERROR) return(STREAM_DATA_ERROR); } } len=S->InEnd-S->InStart; if (len > Buffsize) len=Buffsize; if (len > 0) memcpy(Buffer,S->InputBuff + S->InStart,len); return(len); }