// -------------------------------------------------- int WSAClientSocket::read(void *p, int l) { int bytesRead=0; while (l) { int r = recv(sockNum, (char *)p, l, 0); if (r == SOCKET_ERROR) { // non-blocking sockets always fall through to here checkTimeout(true,false); }else if (r == 0) { throw EOFException("Closed on read"); }else { stats.add(Stats::BYTESIN,r); if (host.localIP()) stats.add(Stats::LOCALBYTESIN,r); updateTotals(r,0); bytesRead += r; l -= r; p = (char *)p+r; } } return bytesRead; }
std::string FileRead::readString() { char buffer[32]; // thats our read buffer std::string read = ""; // thats what we read so far size_t len = length(); while(true) // check that we can read as much as want { int maxread = std::min(sizeof(buffer), len - tell()); readRawBytes( buffer, maxread ); // read into buffer for(int i = 0; i < maxread; ++i) { if(buffer[i] == 0) { seek( tell() - maxread + i + 1); return read; } else { read += buffer[i]; // this might not be the most efficient way... } } // when we reached the end of file if(maxread < 32) break; } BOOST_THROW_EXCEPTION(EOFException(mFileName)); }
std::string read_line() { std::string::iterator siter = std::find(buffer.begin(), buffer.end(), '\n'); std::string rv(""); int bytes_read(0); fd_set fds; timeval tv; int sel_call(0); tv.tv_sec = 0; tv.tv_usec = 500000; FD_ZERO(&fds); FD_SET(sock, &fds); if (! connected && buffer != "") { rv = buffer; rv.erase(std::remove(rv.begin(), rv.end(), '\n'), rv.end()); rv.erase(std::remove(rv.begin(), rv.end(), '\r'), rv.end()); buffer = ""; return rv; } if (! connected && buffer == "") { throw EOFException(); } while (connected && siter == buffer.end()) { sel_call = select(0, &fds, NULL, &fds, &tv); if (0 == sel_call) continue; if (SOCKET_ERROR == sel_call) { connected = false; throw NetworkError(); } ZeroMemory(temporary_pool, 8192); bytes_read = recv(sock, (char*) temporary_pool, 8192, 0); if (SOCKET_ERROR == bytes_read || 0 == bytes_read) { connected = false; break; } buffer += std::string((const char*) temporary_pool, (const char*) temporary_pool + bytes_read); siter = std::find(buffer.begin(), buffer.end(), '\n'); } siter = std::find(buffer.begin(), buffer.end(), '\n'); rv = std::string(buffer.begin(), siter); if (siter != buffer.end()) siter += 1; buffer = std::string(siter, buffer.end()); return rv; }
QVariant Driver::parse(istream* inStream) { variant_t ret; result = &ret; if (inStream) { scanner.yyrestart(inStream); } if (parser.parse() != 0) { throw ErrorException(lastError); } if (!result) { throw EOFException(); } return *result; }
uint32_t FileRead::readUInt32() { check_file_open(); if ( length() - tell() < 4) { BOOST_THROW_EXCEPTION( EOFException(mFileName) ); } PHYSFS_uint32 ret; if(!PHYSFS_readULE32( reinterpret_cast<PHYSFS_file*>(mHandle), &ret)) { BOOST_THROW_EXCEPTION( PhysfsFileException(mFileName) ); } return ret; }
uint32_t FileRead::readRawBytes( char* target, std::size_t num_of_bytes ) { check_file_open(); PHYSFS_sint64 num_read = PHYSFS_read(reinterpret_cast<PHYSFS_file*> (mHandle), target, 1, num_of_bytes); // -1 indicates that reading was not possible if( num_read == -1) { BOOST_THROW_EXCEPTION( PhysfsFileException(mFileName) ); } if( num_read != (PHYSFS_sint64)num_of_bytes ) { BOOST_THROW_EXCEPTION ( EOFException(mFileName) ); } return num_read; }
// -------------------------------------------------- /// pにnlength分読み取る int CSocket::read(void *p, int nlength) { int bytesRead = 0; while (nlength) { if (m_nDataSize >= nlength) { // nlength以上読み込んだ memcpy(p, &m_ReadBuf[m_nPos], nlength); bytesRead += nlength; /* 次にreadが呼ばれる時用にバッファの位置と残りのサイズを保存しておく */ m_nPos += nlength; m_nDataSize -= nlength; break; } else if (m_nDataSize > 0) { // バッファに残っている部分を書き込む memcpy(p, &m_ReadBuf[m_nPos], m_nDataSize); p = (char *) p + m_nDataSize; // 書き込み位置更新 nlength -= m_nDataSize; bytesRead += m_nDataSize; } m_nPos = 0; m_nDataSize = 0; //int r = recv(sockNum, (char *)p, l, 0); int readed = recv(m_sock, m_ReadBuf, RBSIZE, 0); // ソケットから読み取る if (readed == SOCKET_ERROR) { // non-blocking sockets always fall through to here _checkTimeout(true); } else if (readed == 0) { throw EOFException("Closed on read"); } else { //updateTotals(r,0); //bytesRead += r; //l -= r; //p = (char *)p+r; m_nDataSize += readed; } } return bytesRead; }
int FDIOStream::read(void* ptr, int numbytes){ if(numbytes==0) return 0; char* bufferPointer = (char*)ptr; int totalRead = 0; while (totalRead < numbytes) { int numRead = ::read(fd, bufferPointer, numbytes-totalRead); if(numRead == 0){ throw EOFException(); } else if (numRead < 0) { perror("read error: "); fflush(stderr); throw IOException("Error on FDIO read"); } bufferPointer += numRead; totalRead += numRead; } return totalRead; }
void Lexer::nextChar() { ++c; if(c >= bufend) { if(eof) throw EOFException(); stream.read(buffer, BUFFER_SIZE); size_t bytes_read = stream.gcount(); c = buffer; bufend = buffer + bytes_read; // the following is a hack that appends an additional ' ' at the end of // the file to avoid problems when parsing symbols/elements and a sudden // EOF. This is faster than relying on unget and IMO also nicer. if(bytes_read == 0 || stream.eof()) { eof = true; *bufend = ' '; ++bufend; } } }
int FDIOStream::read(void* ptr, int numbytes){ if(numbytes==0) return 0; char* bufferPointer = (char*)ptr; int totalRead = 0; struct timeval timeout; fd_set fdSet; while (totalRead < numbytes) { FD_ZERO(&fdSet); FD_SET(fd, &fdSet); timeout.tv_sec = 1; timeout.tv_usec = 0; int select_result = select(FD_SETSIZE, &fdSet, NULL, NULL, &timeout); if ( select_result < 0) throw IOException("Select returned an error on read"); int numRead = 0; if (FD_ISSET(fd, &fdSet)) { numRead = ::read(fd, bufferPointer, numbytes-totalRead); if(numRead == 0){ throw EOFException(); } else if (numRead < 0) { perror("read error: "); fflush(stderr); throw IOException("Error on FDIO read"); } bufferPointer += numRead; totalRead += numRead; } } return totalRead; }
// Read exactly the specified number of bytes from the file handle into the // given buffer. This is based on the readn() function given on pages 51-2 of // _Effective TCP/IP Programming_ by Jon D. Snader. vpr::Uint32 FileHandleImplUNIX::readn_i(void* buffer, const vpr::Uint32 buffer_size, const vpr::Interval& timeout) { if ( vpr::Interval::NoTimeout != timeout ) { vprDEBUG(vprDBG_ALL,vprDBG_WARNING_LVL) << "Timeout not supported\n" << vprDEBUG_FLUSH; //TODO: InvalidArgumentException instead, but this will require // adding it as an acceptable exception to throw. throw IOException("Timeout not supported by readn.", VPR_LOCATION); } vpr::Uint32 bytes_read(0); size_t bytes_left = buffer_size; while ( bytes_left > 0 ) { vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL) << "[vpr::FileHandleImplUNIX::readn_i()] Reading " << bytes_left << " bytes from file handle " << mFdesc << std::endl << vprDEBUG_FLUSH; const ssize_t bytes = ::read(mFdesc, buffer, bytes_left); vprDEBUG_NEXT(vprDBG_ALL, vprDBG_HVERB_LVL) << "Read " << bytes << " bytes from file handle " << mFdesc << std::endl << vprDEBUG_FLUSH; // Read error. if ( bytes < 0 ) { // Restart the read process if we were interrupted by the OS. if ( EINTR == errno ) { continue; } // Restart the read process if socket is non-blocking and no // data was immediately available. else if ( EAGAIN == errno ) { continue; } // Otherwise, we have an error situation, so return failure status. else { std::ostringstream msg_stream; msg_stream << "Error reading from " << mName << ": " << strerror(errno); throw IOException(msg_stream.str(), VPR_LOCATION); } } // We have read EOF, so there is nothing more to read. At this point, // bytes_read contains an accurate count of the bytes read so far // (posisbly less than buffer_size). else if ( bytes == 0 ) { vprDEBUG(vprDBG_ALL, vprDBG_HVERB_LVL) << "[vpr::FileHandleImplUNIX::readn_i()] Read EOF with " << bytes_left << " bytes left to read from file handle " << mFdesc << " and " << bytes_read << " bytes read in total." << std::endl << vprDEBUG_FLUSH; std::ostringstream ss; ss << "Read EOF with " << bytes_left << " bytes left to read from file handle " << mFdesc << " and " << bytes_read << " bytes read in total."; throw EOFException(ss.str(), VPR_LOCATION); } else { buffer = (void*) ((char*) buffer + bytes); bytes_left -= bytes; bytes_read += bytes; } } return bytes_read; }
void Parser::reportEOF() { mErrorHandler.endOfFile(); throw EOFException(); }
void throwEOFException(const char *msg, int e) { if (msg == NULL && e == 0) msg = "premature end of file"; throw EOFException(msg, e); }