unsigned long long fstreamReadBig(std::ifstream &S, char* A, unsigned long long N) { unsigned long long C=0; for (unsigned long long ii=0; ii<N/fstream_Chunk_Max; ii++) { S.read(A+C,fstream_Chunk_Max); C+=S.gcount(); if (!S.good()) break; }; S.read(A+C,N%fstream_Chunk_Max); C+=S.gcount(); return C; };
void AudioStreamingServer::StreamFile(const std::shared_ptr<ISocket>& clientSocket, std::ifstream& file, HttpContentType contentType) const { file.seekg(0, file.end); int fileLength = file.tellg(); HttpResponse response(HTTP_1_1, OK, contentType, fileLength); SendResponseHeader(clientSocket, response); file.seekg(0, file.beg); char sendbuf[DEFAULT_BUFFER_SIZE] = { 0 }; file.read(sendbuf, DEFAULT_BUFFER_SIZE); while(file.gcount() > 0) { clientSocket->Send(sendbuf, file.gcount()); file.read(sendbuf, DEFAULT_BUFFER_SIZE); } }
void CopyData ( std::ifstream &fin, std::ofstream &fout ) { char line[704]; while (!fin.eof()) { fin.read(line, 704); fout.write(line, fin.gcount()); } }
MMFormatInput::MMFormatInput(std::ifstream &in, std::vector<uint32_t> &permIn) : MatrixInput(), in(in), curRow(0), curIndex(-1), already_read(false), perm(permIn) { //read header only char buf[500]; isBinary = false; in.getline(buf, 500); if ( strstr(buf, "pattern") != NULL ) { isBinary = true; } do { in.getline(buf, 500); if (in.gcount() >=500){ cerr << "FAILED FORMAT" << endl; exit(1); } } while (buf[0] == '%'); uint32_t nr, nc, nnz; sscanf( buf , "%d %d %d", &nr, &nc, &nnz); this->nnz = nnz; this->numRows = nr; this->numCols = nc; if (perm.size() == 0) { perm.resize( max(numRows, numCols) ); for (int i=0; i<perm.size(); i++) { perm[i] = i; } } }
std::string hash_file(HashFunc * hf, std::ifstream& file, std::streampos start) { size_t bs = 16384; char * b = new char [bs]; file.seekg (start, std::ios::beg); while (file) { file.read (b, bs); if (file) { hf->process_bytes(b, bs); } else if (file.eof()) { // file.gcount() returns an std::streamsize, which is the signed counterpart of std::size_t // it only returns a negative value in the constructors of std::strstreambuf, so we can // safely cast to size_t (taken from http://en.cppreference.com/w/cpp/io/streamsize) hf->process_bytes(b, (size_t)file.gcount()); } else { hf->reset(); } } delete[] b; return hf->str(); }
void Sha256File::getHash( std::ifstream& inFile, unsigned char* pHash ) { SHA256Context sha256; SHA256Init(&sha256); uint8_t* pMovableBuffer = pBuffer; // Ensure it is on a 64-bit boundary. INTPTR offs; if ((offs = reinterpret_cast<INTPTR>(pBuffer) & 7L)) pMovableBuffer += 8 - offs; unsigned int len; for (;;) { inFile.read( reinterpret_cast<char*>(pMovableBuffer), SHA_BUFFER_SIZE ); len = inFile.gcount(); if ( len == 0) break; SHA256Update (&sha256, pMovableBuffer, len); } SHA256Final (&sha256, pHash); }
bool load( std::ifstream& stream ) { stream.seekg( 0 ); stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) ); return (stream.gcount() == sizeof( header ) ); }
inline void fillBuffer(std::ifstream& file, std::streamsize size, boost::uint8_t*& buffer) { std::streamsize read_bytes = 0; unsigned int i = 0; while (read_bytes != size && ++i<100) { file.read((char*) buffer, size); read_bytes += file.gcount(); } if (read_bytes != size) throw PNMImageExcpetion("Unable to read pixel data properly"); }
bool getNextTriple(TripleEdge & triple) { if ( ! curbufleft ) { istr.read ( reinterpret_cast<char *>(B.get()), B.getN() * sizeof(TripleEdge) ); if ( istr.gcount() == 0 ) return false; assert ( istr.gcount() % sizeof(TripleEdge) == 0 ); curbufleft = istr.gcount() / sizeof(TripleEdge); assert ( curbufleft ); curtrip = B.get(); } triple = *(curtrip++); curbufleft -= 1; return true; }
void TestDataInputHandler::restoreData( char* m_buffer, uint64_t m_size ) { m_is.read( m_buffer, m_size ); uint64_t size = m_is.gcount(); if( size < m_size ) { std::ostringstream ostr; ostr << "TestDataInputHandler::restoreData(" << m_size << "): no more data ! gcount = " << size; std::cerr << ostr.str() << std::endl; throw(AccessByStringNotInitialized(ostr.str())); } }
std::string FileReader::readOpenStreamContents(const std::string &filePath, std::ifstream &inputFileStream, char *buffer) { std::stringstream stringBuilder; // Read input file stream contents while (!inputFileStream.eof()) { readFromInputFileStream(filePath, inputFileStream, buffer); stringBuilder << std::string(buffer, inputFileStream.gcount()); } return stringBuilder.str(); }
bool FileInputReader::isELFFile(std::ifstream& str) { char first_bytes[4]; str.read(first_bytes, sizeof(first_bytes)); if (str.gcount() != 4) // not enough characters for ELF magic return false; if ( (first_bytes[0] == ELFMAG0) and (first_bytes[1] == ELFMAG1) and (first_bytes[2] == ELFMAG2) and (first_bytes[3] == ELFMAG3)) return true; return false; }
/// read portion of file into internal buffer /// if a_crunch == true, crunch buffer before reading bool read(bool a_crunch = true) { if (!m_open || !m_file.is_open()) return false; if (a_crunch) m_buf.crunch(); while (true) { BOOST_ASSERT(m_buf.capacity() > 0); m_file.read(m_buf.wr_ptr(), m_buf.capacity()); int n = m_file.gcount(); if (n == 0) { if (m_file.good()) continue; if (m_file.eof()) return false; // this should never happen since we have set badbit throw io_error(errno, "Unexpected error reading ", m_fname); } m_buf.commit(n); return true; } }
/** Reads the pixel rows from the bitmap file. @param[in] FileStream The input file stream. @param[out] DestBuffer The buffer to write the pixels to. @param[in] FileHeader The bitmap file header. @param[in] InfoHeader The bitmap info header. @return One of #BitmapLoadStatus codes. */ static BitmapLoadStatus LoadPixels(std::ifstream& FileStream, std::vector<std::uint8_t>* DestBuffer, const BitmapFileHeader& FileHeader, const BitmapInfoHeader& InfoHeader) { FileStream.seekg(FileHeader.PixelArrayOffset); // Workaround for Paint.NET being stupid and generating broken images... if (InfoHeader.PixelDataSize != 0) { DestBuffer->resize(InfoHeader.PixelDataSize); } else { DestBuffer->resize(CalculateStride(InfoHeader) * InfoHeader.Height); } auto buffer = reinterpret_cast<char*>(&DestBuffer->at(0)); FileStream.read(buffer, InfoHeader.PixelDataSize); if (FileStream.gcount() != InfoHeader.PixelDataSize) { return UNEXPECTED_END_OF_FILE; } return LOAD_SUCCESS; }
/** * Reads a single particle from disk into the specified buffer. * @param data The location to read a single particle to. Must be at least m_layout.size() bytes. * @return True if a particle was read, false if EOF or the stream was never opened. */ virtual bool read_impl( char* data ) { if( m_particleCount == 0 ) { if( !m_fin.is_open() || m_fin.eof() ) return false; throw std::runtime_error( "The file \"" + m_filePath + "\" did not contain the number of particles it claimed" ); } m_zstream.avail_out = static_cast<uInt>( m_layout.size() ); m_zstream.next_out = reinterpret_cast<unsigned char*>(data); do { if(m_zstream.avail_in == 0) { m_fin.read(m_buffer, m_bufferSize); if( m_fin.fail() && m_bufferSize == 0 ) throw std::ios_base::failure( "Failed to read from file \"" + m_filePath + "\"" ); m_zstream.avail_in = static_cast<uInt>(m_fin.gcount()); m_zstream.next_in = reinterpret_cast<unsigned char*>(m_buffer); } int ret = inflate(&m_zstream, Z_SYNC_FLUSH); if(Z_OK != ret && Z_STREAM_END != ret) { std::stringstream ss; ss << "inflate() on file \"" << m_filePath << "\" "; ss << "with " << m_particleCount << " particles left failed:\n\t"; ss << zError(ret); throw std::runtime_error( ss.str() ); } } while(m_zstream.avail_out != 0); --m_particleCount; return true; }
void read_meta_chunk( detail::prt_int32 chunkLength ) { char channelName[32]; char valueName[32]; detail::prt_int32 valueType; m_fin.getline( channelName, 32, '\0' ); chunkLength -= m_fin.gcount(); m_fin.getline( valueName, 32, '\0' ); chunkLength -= m_fin.gcount(); m_fin.read( reinterpret_cast<char*>( &valueType ), 4u ); chunkLength -= 4; assert( chunkLength > 0 ); if( valueType < meta_types::type_string || valueType >= meta_types::type_last ) throw std::runtime_error( std::string() + "The data type specified for channel \"" + channelName + "\" metadata \"" + valueName + "\" in the input stream \"" + m_filePath + "\" is not valid." ); struct scoped_void_ptr { void* ptr; ~scoped_void_ptr() { operator delete( ptr ); } } buffer = { operator new( chunkLength ) }; // We've been subtracting the read bytes as we go, so this is the remaining number of bytes in the chunk. m_fin.read( static_cast<char*>( buffer.ptr ), chunkLength ); if( channelName[0] != '\0' && !detail::is_valid_channel_name( channelName ) ) { std::cerr << "Invalid channel name: \"" << channelName << "\" in metadata: \"" << valueName << "\"" << std::endl; return; } if( !detail::is_valid_channel_name( valueName ) ) { std::cerr << "Invalid metadata name: \"" << valueName << "\" in channel: \"" << channelName << "\"" << std::endl; return; } std::map< std::string, prt_meta_value >& metaValue = (channelName[0] == '\0') ? m_fileMetadata : m_channelMetadata[ std::string(channelName) ]; if( valueType == meta_types::type_string ) { #ifdef _WIN32 int wcharLength = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, static_cast<const char*>( buffer.ptr ), chunkLength, NULL, 0 ); if( wcharLength <= 0 ) { std::cerr << "Invalid string data in metadata: \"" << valueName << "\" in channel: \"" << channelName << "\"" << std::endl; return; } // In Windows we convert from UTF8 to UTF16LE in memory. std::vector<wchar_t> convertedString( wcharLength, L'\0' ); int r = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, static_cast<const char*>( buffer.ptr ), chunkLength, &convertedString.front(), wcharLength ); assert( r > 0 ); metaValue[ std::string(valueName) ].set_string( &convertedString.front(), static_cast<std::size_t>( chunkLength - 1 ) ); #else // In non-Windows we can work directly with UTF8. metaValue[ std::string(valueName) ].set_string( static_cast<char*>( buffer.ptr ), static_cast<std::size_t> ( chunkLength - 1 ) ); #endif } else { std::size_t basicSize = data_types::sizes[valueType]; // We've already verified that we have a valid index. assert( chunkLength >= basicSize && (chunkLength % basicSize) == 0 ); std::size_t arity = chunkLength / basicSize; metaValue[ std::string(valueName) ].set( static_cast<meta_types::option>( valueType ), arity, buffer.ptr ); } }
virtual size_t read(char* buf, size_t num) { std::unique_lock<std::mutex> lock(m_mutex); // Released when out of scope ensureStream(); m_stream.read (buf, num); return m_stream.gcount(); }
virtual size_t skip(size_t num) { std::unique_lock<std::mutex> lock(m_mutex); // Released when out of scope ensureStream(); m_stream.ignore(num); return m_stream.gcount(); }
void get_line(std::ifstream fin, char *buff, int buff_len, int &read_len) { fin.getline(buff, buff_len); read_len = fin.gcount(); buff[INPUT_LINE_SIZE - 1] = '\0'; }