inline void readVectorData(std::istream& is, std::vector<T>& data) { data.clear(); while (is) { T candidate; is >> candidate; if (is.rdstate() & std::ios::failbit) { is.clear(is.rdstate() & ~std::ios::failbit); is >> ignoreWhitespace; char dummy; is >> dummy; if (dummy == '/') { is >> ignoreLine; break; } else if (dummy == '-') { // "comment test"
int GA3DBinaryStringGenome::read(std::istream & is) { static char c; unsigned int i=0, j=0, k=0; do{ is >> c; if(isdigit(c)){ gene(i++, j, k, ((c == '0') ? 0 : 1)); if(i >= nx){ i=0; j++; } if(j >= ny){ j=0; k++; } } } while(!is.fail() && !is.eof() && k < nz); _evaluated = gaFalse; if(is.eof() && ((k < nz) || // didn't get some lines (j < ny && j != 0) || // didn't get some lines (i < nx && i != 0))){ // didn't get some lines GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF); is.clear(std::ios::badbit | is.rdstate()); return 1; } return 0; }
void set(std::istream& in, std::ostream& out, std::ostream& err) { if (not activated) { using namespace std; in_buf = cin.rdbuf(); out_buf = cout.rdbuf(); err_buf = cerr.rdbuf(); in_state = cin.rdstate(); out_state = cin.rdstate(); err_state = cerr.rdstate(); cin.rdbuf(in.rdbuf()); cout.rdbuf(out.rdbuf()); cerr.rdbuf(err.rdbuf()); cin.exceptions(ios_base::goodbit); cout.exceptions(ios_base::goodbit); cerr.exceptions(ios_base::goodbit); cin.clear(in.rdstate()); cout.clear(out.rdstate()); cerr.clear(err.rdstate()); } }
bool checkFileState(std::istream& unbuiltFile, std::istream& builtFile) { // check for the stream state flags if (unbuiltFile.eof()) { if (! builtFile.eof()) { throw runtime_error("unbuilt file contains more data than unbuilt"); } } if (builtFile.eof()) { if (! unbuiltFile.eof()) { throw runtime_error("built file contains more data than unbuilt"); } } if (unbuiltFile.rdstate()!=0 && !unbuiltFile.eof()) { throw runtime_error("Unbuilt file has error state"); } if (builtFile.rdstate()!=0 && ! builtFile.eof()) { throw runtime_error("Built file has error state"); } return (builtFile.eof() || unbuiltFile.eof()); }
std::vector<Point> Calibrator::loadPoints(std::istream &in) { std::vector<Point> result; for(;;) { double x, y; in >> x >> y; if (in.rdstate()) { // break if any error break; } result.push_back(Point(x, y)); } return result; }
/*! * \brief Check for a failure during the last stream input. * * If the last stream input did not succeed, throws an exception with * details on the stream position where this occurred. */ void check_failedload(std::istream &is) { if (is.fail()) { std::ios::iostate state = is.rdstate(); is.clear(); std::ostringstream sout; sout << "Failure loading object at position " << is.tellg() << ", next line:" << std::endl; std::string s; getline(is, s); sout << s; is.clear(state); throw load_error(sout.str()); } }
xml_lmi::dom_parser::dom_parser(std::istream const& is) { try { error_context_ = "Unable to parse xml stream: "; if(0 != is.rdstate()) { throw std::runtime_error("Stream state is not 'good'."); } std::string s; istream_to_string(is, s); parser_.reset(new DomParser(s.c_str(), 1 + s.size())); } catch(std::exception const& e) { fatal_error() << error_context_ << e.what() << LMI_FLUSH; } }
bool VersionedFileHeader::read_known( VFH_Type i_type, std::istream &io_istream ) { // save stream state and position streampos pos = io_istream.tellg(); ios_base::iostate state = io_istream.rdstate(); if ( !read_raw( io_istream ) || m_magicNumber!=(isMSBF()?g_versioned_headers_list[i_type].magic_number_MSBF:g_versioned_headers_list[i_type].magic_number_LSBF) ) { // reading failed, rewind to starting point and reset error flags io_istream.seekg( pos ); io_istream.setstate( state ); m_magicNumber = m_version = 0; m_isMSBF = MSBF_PROCESSOR(); return false; } return true; }
bool VersionedFileHeader::read_unknown( std::istream &io_istream, VFH_Type &o_id ) { // save stream state and position streampos pos = io_istream.tellg(); ios_base::iostate state = io_istream.rdstate(); bool isMSBF_; // from magic number if ( !read_raw( io_istream ) || !typeFromMagicNumber(m_magicNumber, o_id, isMSBF_) || isMSBF_!=isMSBF() ) { // reading failed, rewind to starting point and reset error flags io_istream.seekg( pos ); io_istream.setstate( state ); return false; } return true; }
void Date::read(std::istream& in) { int in_day { }, in_month { }, in_year { }; char sep1 { }, sep2 { }; in >> in_day >> sep1 >> in_month >> sep2 >> in_year; if (isValidDate(1,1,1)) //should be (isValidYear(in_day)) std::swap(in_day, in_year); if (in_year >= 0 && in_year < 100) in_year += 2000; if (!in.fail() && sep1 == sep2 && (sep1 == '.' || sep1 == '/' || sep1 == '-') && isValidDate(in_year, in_month, in_day)) { year = in_year; month = in_month; day = in_day; } else { in.setstate(std::ios::failbit | in.rdstate()); } }
int GA1DArrayAlleleGenome<char>::read(std::istream & is) { unsigned int i = 0; char c; do { is.get(c); if(!is.fail()) { gene(i++, c); } } while(!is.fail() && !is.eof() && i < nx); if(is.eof() && i < nx) { GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF); is.clear(std::ios::badbit | is.rdstate()); return 1; } return 0; }
/// The read specialization takes in each number and stuffs it into the array. template <> int GA1DArrayAlleleGenome<float>::read(std::istream & is) { unsigned int i = 0; float val; do { is >> val; if(!is.fail()) { gene(i++, val); } } while(!is.fail() && !is.eof() && i < nx); if(is.eof() && i < nx) { GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF); is.clear(std::ios::badbit | is.rdstate()); return 1; } return 0; }
std::ostream& operator<<(std::ostream& out, const ostrstream& x) { out << static_cast<strstreambuf*>(x.rdbuf())->str(); return out; } class istrstream : public std::istream { strstreambuf sb; public: istrstream(char* buf) : sb(buf) { } template<class T> istrstream& operator>>(T& x) { std::istream in(&sb); in.copyfmt(*this); in >> x; setstate(in.rdstate()); return *this; } }; } int main() { using namespace std; using namespace ch21; char buf[1024]; ostrstream out(buf, 1024); out << "foo bar " << 12345; cout << out << endl; istrstream in(buf);
int32_t vtkReadBinaryData(std::istream& in, T* data, int32_t numTuples, int32_t numComp) { if (numTuples == 0 || numComp == 0) { // nothing to read here. return 1; } size_t numBytesToRead = static_cast<size_t>(numTuples) * static_cast<size_t>(numComp) * sizeof(T); size_t numRead = 0; // Cast our pointer to a pointer that std::istream will take char* chunkptr = reinterpret_cast<char*>(data); numRead = 0; // Now start reading the data in chunks if needed. size_t chunkSize = DEFAULT_BLOCKSIZE; // Sanity check the chunk size to make sure it is not any larger than the chunk of data we are about to read if (numBytesToRead < DEFAULT_BLOCKSIZE) { chunkSize = numBytesToRead; } size_t master_counter = 0; size_t bytes_read = 0; // Now chunk through the file reading up chunks of data that can actually be // read in a single read. DEFAULT_BLOCKSIZE will control this. while(1) { in.read(chunkptr, chunkSize); bytes_read = in.gcount(); chunkptr = chunkptr + bytes_read; master_counter += bytes_read; if (numBytesToRead - master_counter < chunkSize) { chunkSize = numBytesToRead - master_counter; } if (master_counter >= numBytesToRead) { break; } if (in.good()) { //std::cout << "all data read successfully." << in.gcount() << std::endl; } if ((in.rdstate() & std::ifstream::failbit) != 0) { std::cout << "FAIL " << in.gcount() << " could be read. Needed " << chunkSize << " total bytes read = " << master_counter << std::endl; return -12020; } if ((in.rdstate() & std::ifstream::eofbit) != 0) { std::cout << "EOF " << in.gcount() << " could be read. Needed " << chunkSize << " total bytes read = " << master_counter << std::endl; return -12021; } if ((in.rdstate() & std::ifstream::badbit) != 0) { std::cout << "BAD " << in.gcount() << " could be read. Needed " << chunkSize << " total bytes read = " << master_counter << std::endl; return -12021; } } return 0; }