Card32 fileSniff(Card8 which) { IntX count = 0; Card32 value = 0; fileSeekAbsNotBuffered(which, 0); if (which == 1) filep = &file1; else filep = &file2; count = sysRead(filep->id, filep->buf, 4, filep->name); if (count == 0) value = 0xBADBAD; else { filep->next = filep->buf; value = *filep->next++; if (filep->next == filep->end) fillBuf(filep); value = value<<8 | *filep->next++; if (filep->next == filep->end) fillBuf(filep); value = value<<8 | *filep->next++; if (filep->next == filep->end) fillBuf(filep); value = value<<8 | *filep->next++; } fileSeekAbsNotBuffered(which, 0); return value; }
/* 1, 2, and 4 byte big-endian machine independent input */ void fileReadObject(IntX size, ...) { Int32 value; va_list ap; va_start(ap, size); if (file.end - file.next >= size) /* Request doesn't cross block boundary */ switch (size) { case 1: *va_arg(ap, Int8 *) = *file.next++; break; case 2: value = *file.next++; *va_arg(ap, Int16 *) = value<<8 | *file.next++; break; case 4: value = *file.next++; value = value<<8 | *file.next++; value = value<<8 | *file.next++; *va_arg(ap, Int32 *) = value<<8 | *file.next++; break; default: fatal(SPOT_MSG_BADREADSIZE, size); break; } else { /* Read across block boundary */ if (file.next == file.end) fillBuf(); switch (size) { case 1: *va_arg(ap, Int8 *) = *file.next++; break; case 2: value = *file.next++; if (file.next == file.end) fillBuf(); *va_arg(ap, Int16 *) = value<<8 | *file.next++; break; case 4: value = *file.next++; if (file.next == file.end) fillBuf(); value = value<<8 | *file.next++; if (file.next == file.end) fillBuf(); value = value<<8 | *file.next++; if (file.next == file.end) fillBuf(); *va_arg(ap, Int32 *) = value<<8 | *file.next++; break; default: fatal(SPOT_MSG_BADREADSIZE, size); break; } } va_end(ap); }
/* Supply specified number of bytes */ void fileReadBytes(Card8 which, Int32 count, Card8 *buf) { IntX size; IntX left; if (which == 1) filep = &file1; else filep = &file2; while (count > 0) { left = filep->end - filep->next; if (left == 0) { fillBuf(filep); left = filep->end - filep->next; } size = (left < count) ? left : count; memcpy(buf, filep->next, size); filep->next += size; buf += size; count -= size; } }
//============================================================================== // private //------------------------------------------------------------------------------ int StdioStream::fillGet() { if (!fillBuf()) { return EOF; } m_r--; return *m_p++; }
/* Peek at a character from the file without disturbing the read position. This will put the file into buffered mode. */ PUBLIC int mprPeekFileChar(MprFile *file) { MprBuf *bp; ssize len; assert(file); if (file == 0) { return MPR_ERR; } if (file->buf == 0) { file->buf = mprCreateBuf(ME_MAX_BUFFER, ME_MAX_BUFFER); } bp = file->buf; if (mprGetBufLength(bp) == 0) { len = fillBuf(file); if (len <= 0) { return -1; } } if (mprGetBufLength(bp) == 0) { return 0; } return ((uchar*) mprGetBufStart(bp))[0]; }
/* Get a character from the file. This will put the file into buffered mode. */ PUBLIC int mprGetFileChar(MprFile *file) { MprBuf *bp; ssize len; assert(file); if (file == 0) { return MPR_ERR; } if (file->buf == 0) { file->buf = mprCreateBuf(ME_MAX_BUFFER, ME_MAX_BUFFER); } bp = file->buf; if (mprGetBufLength(bp) == 0) { len = fillBuf(file); if (len <= 0) { return -1; } } if (mprGetBufLength(bp) == 0) { return 0; } file->pos++; return mprGetCharFromBuf(bp); }
/* Read a line from the file. This will put the file into buffered mode. Return NULL on eof. */ PUBLIC char *mprReadLine(MprFile *file, ssize maxline, ssize *lenp) { MprBuf *bp; MprFileSystem *fs; ssize size, len, nlen, consumed; cchar *eol, *newline, *start; char *result; assert(file); if (file == 0) { return NULL; } if (lenp) { *lenp = 0; } if (maxline <= 0) { maxline = ME_MAX_BUFFER; } fs = file->fileSystem; newline = fs->newline; if (file->buf == 0) { file->buf = mprCreateBuf(maxline, maxline); } bp = file->buf; result = NULL; size = 0; do { if (mprGetBufLength(bp) == 0) { if (fillBuf(file) <= 0) { return result; } } start = mprGetBufStart(bp); len = mprGetBufLength(bp); if ((eol = findNewline(start, newline, len, &nlen)) != 0) { len = eol - start; consumed = len + nlen; } else { consumed = len; } file->pos += (MprOff) consumed; if (lenp) { *lenp += len; } if ((result = mprRealloc(result, size + len + 1)) == 0) { return NULL; } memcpy(&result[size], start, len); size += len; result[size] = '\0'; mprAdjustBufStart(bp, consumed); } while (!eol); return result; }
uint64_t lpcmReader::read( unsigned char *buf, uint64_t len ) { int64_t avail = ct.now - bufPos; if( avail < len && ! ( state & _eoi ) ) { memmove( bigBuf, bigBuf + bufPos, avail ); counter<uint64_t> c( 0, avail, 0 ); fillBuf( 0, &c ); if( state & _eof ) { clearbits( state, _eof ); state |= _eoi; if( surplus && state & padded ) { int padding = alignment - surplus; memset( bigBuf + ct.now, 0, padding ); gcount += padding; ct.now += padding; surplus = 0; } else { gcount -= surplus; ct.now -= surplus; } } if( state & prepended ) { gcount += avail; avail = 0; clearbits( state, prepended ); } swap2dvd( bigBuf + avail, gcount, fmeta.data.stream_info.channels, fmeta.data.stream_info.bits_per_sample ); bufPos = 0; avail = ct.now; } else if( ! avail && state & _eoi ) { state |= _eof; memmove( bigBuf, bigBuf + bufPos, surplus ); } len = ( avail < len ) ? avail : len; memcpy( buf, bigBuf + bufPos, len ); bufPos += len; pos.now += len; return len; }
/* returns True on success */ Boolean getChar(char *achar) { if (havePutBack) { *achar = putBack; havePutBack = False; return(True); } if (curBufPos == curBufLen) fillBuf(); if (endOfFile) return (False); *achar = readBuf[curBufPos++]; return (True); }
unsigned char CEgIStream::PeekByte() { register unsigned char c; if ( mIsTied ) { if ( mPos != 0 ) c = *((unsigned char*) mNextPtr); } else if ( mPos < long(mBufPos + mStrLen) && mPos >= mBufPos ) c = *((unsigned char*) mNextPtr); else if ( noErr() ) { fillBuf(); if ( noErr() ) c = PeekByte(); else throwErr( cNoErr ); } return c; }
size_t FCGICache::read(void *buf, size_t len) { if(buf == NULL || len == 0) return 0; size_t rd = 0; /* 先从缓冲区内读取 */ if( _buf ) { if( _wpos - _rpos >= len ) { rd = len; } else { rd = _wpos - _rpos; } if( rd > 0 ) { memcpy(buf, _buf + _rpos, rd); _rpos += rd; } } /* 剩下的数据从文件中读取 */ if( rd < len ) { if( _file ) { // fseek(_file, _frpos, SEEK_SET); // size_t frd = fread(reinterpret_cast<byte*>(buf) + rd, 1, len - rd, _file); _file->seek(_frpos, WINFile::s_set); size_t frd = _file->read(reinterpret_cast<byte*>(buf) + rd, len - rd); _frpos += frd; rd += frd; } } /* 填充缓冲区 */ fillBuf(); return rd; }
//------------------------------------------------------------------------------ size_t StdioStream::fread(void* ptr, size_t size, size_t count) { uint8_t* dst = reinterpret_cast<uint8_t*>(ptr); size_t total = size*count; if (total == 0) { return 0; } size_t need = total; while (need > m_r) { memcpy(dst, m_p, m_r); dst += m_r; m_p += m_r; need -= m_r; if (!fillBuf()) { return (total - need)/size; } } memcpy(dst, m_p, need); m_r -= need; m_p += need; return count; }
/* Supply specified number of bytes */ void fileReadBytes(Int32 count, Card8 *buf) { while (count > 0) { IntX size; IntX left = file.end - file.next; if (left == 0) { fillBuf(); left = file.end - file.next; } size = (left < count) ? left : count; memcpy(buf, file.next, size); file.next += size; buf += size; count -= size; } }
unsigned char CEgIStream::GetByte() { register unsigned char c; if ( mIsTied ) { if ( mPos != 0 ) { c = *((unsigned char*) mNextPtr); mNextPtr++; mPos++; } else throwErr( cTiedEOS ); } else if ( mPos < long(mBufPos + mStrLen) && mPos >= mBufPos ) { c = *((unsigned char*) mNextPtr); mNextPtr++; mPos++; } else if ( noErr() ) { fillBuf(); c = GetByte(); } return c; }
//------------------------------------------------------------------------------ char* StdioStream::fgets(char* str, size_t num, size_t* len) { char* s = str; size_t n; if (num-- == 0) { return 0; } while (num) { if ((n = m_r) == 0) { if (!fillBuf()) { if (s == str) { return 0; } break; } n = m_r; } if (n > num) { n = num; } uint8_t* end = reinterpret_cast<uint8_t*>(memchr2(m_p, '\n', n)); if (end != 0) { n = ++end - m_p; memcpy(s, m_p, n); m_r -= n; m_p = end; s += n; break; } memcpy(s, m_p, n); m_r -= n; m_p += n; s += n; num -= n; } *s = 0; if (len) { *len = s - str; } return str; }
PUBLIC ssize mprReadFile(MprFile *file, void *buf, ssize size) { MprFileSystem *fs; MprBuf *bp; ssize bytes, totalRead; void *bufStart; assert(file); if (file == 0) { return MPR_ERR_BAD_HANDLE; } fs = file->fileSystem; bp = file->buf; if (bp == 0) { totalRead = fs->readFile(file, buf, size); } else { bufStart = buf; while (size > 0) { if (mprGetBufLength(bp) == 0) { bytes = fillBuf(file); if (bytes <= 0) { return -1; } } bytes = min(size, mprGetBufLength(bp)); memcpy(buf, mprGetBufStart(bp), bytes); mprAdjustBufStart(bp, bytes); buf = (void*) (((char*) buf) + bytes); size -= bytes; } totalRead = ((char*) buf - (char*) bufStart); } file->pos += (MprOff) totalRead; return totalRead; }
/* 1, 2, and 4 byte big-endian machine independent input */ void fileReadObject(Card8 which, IntX size, ...) { Int32 value; va_list ap; if (which == 1) filep = &file1; else filep = &file2; va_start(ap, size); if (filep->end - filep->next >= size) /* Request doesn't cross block boundary */ switch (size) { case 1: *va_arg(ap, Int8 *) = *filep->next++; break; case 2: value = *filep->next++; *va_arg(ap, Int16 *) = (Int16)(value<<8 | *filep->next++); break; case 4: value = *filep->next++; value = value<<8 | *filep->next++; value = value<<8 | *filep->next++; *va_arg(ap, Int32 *) = value<<8 | *filep->next++; break; default: fatal("bad input object size [%d]\n", size); break; } else { /* Read across block boundary */ if (filep->next == filep->end) fillBuf(filep); switch (size) { case 1: *va_arg(ap, Int8 *) = *filep->next++; break; case 2: value = *filep->next++; if (filep->next == filep->end) fillBuf(filep); *va_arg(ap, Int16 *) = (Int16)(value<<8 | *filep->next++); break; case 4: value = *filep->next++; if (filep->next == filep->end) fillBuf(filep); value = value<<8 | *filep->next++; if (filep->next == filep->end) fillBuf(filep); value = value<<8 | *filep->next++; if (filep->next == filep->end) fillBuf(filep); *va_arg(ap, Int32 *) = value<<8 | *filep->next++; break; default: fatal("bad input object size [%d]\n", size); break; } } va_end(ap); }
int main (int argc, char **argv) { int linePos = 0; int precedingNewlines = 0; unsigned char uChar[4]; fillBuf(); uChar[0] = getNextByte(); #ifdef _WIN32 // Work around newline replacement on Windows extern int fileno(FILE*); setmode(fileno(stdout), O_BINARY); #endif while (1) { for (int i = 1; i < 4; i++) { uChar[i] = 0x00; } if ((uChar[0] & 0b11100000) == 0b11100000) { uChar[1] = getNextByte(); uChar[2] = getNextByte(); if (printReplacement(uChar)) { fwrite(uChar, 3, 1, stdout); } precedingNewlines = 0; } else if (uChar[0] == '\r') { uChar[1] = getNextByte(); if (uChar[1] == '\n') { fwrite(&uChar[1], 1, 1, stdout); } precedingNewlines++; linePos = 0; } else { if (uChar[0] == '\n') { precedingNewlines++; /* Handle text wrapping */ uChar[0] = getNextByte(); if (linePos > 45 && uChar[0] != ' ' && uChar[0] != '\t' && uChar[0] != '\n') { fwrite(" ", 1, 1, stdout); } else { fwrite("\n", 1, 1, stdout); } linePos = 0; } if (uChar[0] == 0x91 || uChar[0] == 0x92) { fwrite("'", 1, 1, stdout); precedingNewlines = 0; } else if (uChar[0] == 0x93 || uChar[0] == 0x94) { fwrite("\"", 1, 1, stdout); precedingNewlines = 0; } else if ( (uChar[0] != ' ' && uChar[0] != '\t') || precedingNewlines < 2 || nextLineIndented()) { fwrite(uChar, 1, 1, stdout); if (uChar[0] != '\n') { precedingNewlines = 0; } else { precedingNewlines++; } } } linePos +=1; uChar[0] = getNextByte(); } return 0; }