[[nodiscard]] inline size_t read(std::istream& Stream, const range<char*>& Buffer) { { const auto Exceptions = Stream.exceptions(); Stream.exceptions(Exceptions & ~(Stream.failbit | Stream.eofbit)); SCOPE_EXIT{ Stream.exceptions(Exceptions); }; Stream.read(Buffer.data(), Buffer.size()); if (!Stream.bad() && Stream.eof()) Stream.clear(); } return Stream.gcount(); }
//Load a unit from the input stream Unit Unit::load(std::istream& in) { in.exceptions(std::istream::failbit | std::istream::eofbit); std::vector<int> levels(7); std::string iacode; std::for_each(levels.begin(), levels.end(), [&in](int& level) { in >> level; });
EMAlg::EMAlg( const Evidence &evidence, InfAlg &estep, std::istream &msteps_file ) : _evidence(evidence), _estep(estep), _msteps(), _iters(0), _lastLogZ(), _max_iters(MAX_ITERS_DEFAULT), _log_z_tol(LOG_Z_TOL_DEFAULT) { msteps_file.exceptions( std::istream::eofbit | std::istream::failbit | std::istream::badbit ); size_t num_msteps = -1; msteps_file >> num_msteps; _msteps.reserve(num_msteps); for( size_t i = 0; i < num_msteps; ++i ) _msteps.push_back( MaximizationStep( msteps_file, estep.fg() ) ); }
Error setBody(std::istream& is, const Filter& filter, std::streamsize buffSize = 128) { try { // set exception mask (required for proper reporting of errors) is.exceptions(std::istream::failbit | std::istream::badbit); // setup filtering stream for writing body boost::iostreams::filtering_ostream filteringStream ; // don't bother adding the filter if it is the NullOutputFilter if ( !boost::is_same<Filter, NullOutputFilter>::value ) filteringStream.push(filter, buffSize); // handle gzip if (contentEncoding() == kGzipEncoding) #ifdef _WIN32 // never gzip on win32 removeHeader("Content-Encoding"); #else // add gzip compressor on posix filteringStream.push(boost::iostreams::gzip_compressor(), buffSize); #endif // buffer to write to std::ostringstream bodyStream; filteringStream.push(bodyStream, buffSize); // copy input stream boost::iostreams::copy(is, filteringStream, buffSize); // set body body_ = bodyStream.str(); setContentLength(body_.length()); // return success return Success(); } catch(const std::exception& e) { Error error = systemError(boost::system::errc::io_error, ERROR_LOCATION); error.addProperty("what", e.what()); return error; } }
void RoomList::LoadFromStream(std::istream &in) { std::string ris; std::getline(in, ris); if (ris != "SERVER LIST") throw Net::NetExn("Invalid format: Missing preamble"); in.exceptions(std::istream::badbit | std::istream::failbit); bool foundScoreServer = false; for (;;) { int type; try { in >> type; } catch (std::istream::failure&) { // End of stream. break; } try { switch (type) { case 0: // Score server in >> scoreServer; foundScoreServer = true; break; case 1: // Room entry rooms.push_back(new Server()); in >> *(rooms.back()); break; case 2: // Room w/ ladder server. // Currently unused. break; case 8: // Public banner banners.push_back(new Banner()); in >> *(banners.back()); break; case 9: // Registered banner // Currently ignored. break; } // Consume trailing characters in line. std::getline(in, ris); } catch (std::istream::failure&) { throw Net::NetExn("Parse error"); } } // Verify data. if (!foundScoreServer) { throw Net::NetExn("No score server"); } if (rooms.empty()) { throw Net::NetExn("No rooms available"); } if (!banners.empty()) { curBanner = banners.front(); curBannerIdx = 0; } }