void read_delimited(istream& is, string& s) /* PURPOSE: Read a "" delimited string (including spaces) from a stream. Embedded " and \ are quoted by \ */ { char ch; s = ""; is >> ch; if (ch != '"') { is.clear(ios::failbit); return; } const int BUFSIZE = 80; char buffer[BUFSIZE]; int i = 0; for(;;) { ch = is.get(); if (is.fail()) return; // expect closing " if (ch == '"') { buffer[i] = 0; s += buffer; return; } if (ch == '\\') { ch = is.get(); // \ is escape for embedded " if (is.fail()) return; // expect escapee } buffer[i] = ch; i++; if (i >= BUFSIZE - 1) { buffer[i] = 0; s += buffer; i = 0; } } }
bool Point3Dot::load(istream &in){ coords.resize(3); int start = in.tellg(); for(int i = 0; i < 3; i++){ in >> coords[i]; if(in.fail()){ in.clear(); in.seekg(start+1); //????????? Why that one return false; } } in >> theta; if(in.fail()){ in.clear(); in.seekg(start+1); //????????? Why that one return false; } in >> phi; if(in.fail()){ in.clear(); in.seekg(start+1); //????????? Why that one return false; } int typeInt; in >> typeInt; type = (Type)typeInt; if(in.fail()){ in.clear(); in.seekg(start+1); //????????? Why that one return false; } return true; }
bool fcnn::internal::read_comment(istream &is, string &s) { if (!is.good() || is.eof()) return false; char c; s.clear(); c = is.peek(); if (c != '#') return false; is.get(c); start_line: do { is.get(c); } while ((is.good() && !is.eof()) && ((c == ' ') || (c == '\t'))); if (is.eof()) return true; if (is.fail()) return false; while ((is) && (c != '\n')) { s += c; is.get(c); } if (is.eof()) return true; if (is.fail()) return false; if (c == '\n') { if (is.peek() == '#') { s += c; goto start_line; } else return true; } return true; }
void JStringManager::MergeFile ( istream& input, const JBoolean debug ) { JUInt format; input >> format; if (format != kASCIIFormat) { return; } JString id; while (1) { input >> ws; while (!input.eof() && input.peek() == '#') // peek() at eof sets fail() { JIgnoreLine(input); input >> ws; } if (input.eof() || input.fail()) { break; } id = JReadUntilws(input); if (debug) { cout << id << endl; } if (input.eof() || input.fail()) { break; } JString* s = new JString; assert( s != NULL ); input >> *s; if (input.eof() || input.fail()) { delete s; break; } if (CanOverride(id)) { SetElement(id, s, JPtrArrayT::kDelete); } else if (!SetNewElement(id, s)) { delete s; } } }
MStatus CVData::readBinary ( istream& in ) { in.read( (char*) &_intData, sizeof(_intData) ); if ( !in.fail() ) { in.read( (char*) &_doubleData, sizeof(_doubleData) ); } else { return MS::kFailure; } return in.fail() ? MS::kFailure : MS::kSuccess; }
void Point::read(istream& is) { char ch; is >> ch; if (is.fail()) return; if (ch != '(') { is.clear(ios::failbit); return; } is >> _x; is >> ch; if (is.fail()) return; if (ch != ',') { is.clear(ios::failbit); return; } is >> _y; is >> ch; if (is.fail()) return; if (ch != ')') { is.clear(ios::failbit); return; } }
void Simulation::checkpoint (istream& stream, int checkpointNum) { util::checkpoint::header (stream); util::CommandLine::staticCheckpoint (stream); Population::staticCheckpoint (stream); Surveys & stream; Global::simulationTime & stream; Global::timeStep & stream; simPeriodEnd & stream; totalSimDuration & stream; phase & stream; (*_population) & stream; // read last, because other loads may use random numbers: util::random::checkpoint (stream, checkpointNum); // Check scenario.xml and checkpoint files correspond: int oldWUID(workUnitIdentifier); util::Checksum oldCksum(cksum); workUnitIdentifier & stream; cksum & stream; if (workUnitIdentifier != oldWUID || cksum != oldCksum) throw util::checkpoint_error ("mismatched checkpoint"); stream.ignore (numeric_limits<streamsize>::max()-1); // skip to end of file if (stream.gcount () != 0) { ostringstream msg; msg << "Checkpointing file has " << stream.gcount() << " bytes remaining." << endl; throw util::checkpoint_error (msg.str()); } else if (stream.fail()) throw util::checkpoint_error ("stream read error"); }
// The read specialization takes in each number and stuffs it into the array. int GA1DArrayAlleleGenome<float>::read(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(ios::badbit | is.rdstate()); return 1; } return 0; }
void end_of_loop(istream& ist, char term, const string& message) { if(ist.fail()) { ist.clear(); char ch; if(ist >> ch && ch==term) return; error(message); }
void // Loads pairs of words to find distances for. LoadPairs(istream& in, vector<pairdata> &results, const bool normCase) { pairdata temp; cerr << "Reading in pairs from wordlist" << endl; string lang = getLang(); while (!in.eof()){ if (in.fail()) { throw Exception("Error Reading Input File.. Make sure it is in the correct format. Exiting\n"); } string tempstring1 = ""; string tempstring2 = ""; in >> tempstring1 >> tempstring2; if (tempstring1 == "---END.OF.DOCUMENT---") break; if ((tempstring1.length() > MAX_WORDLEN) || (tempstring2.length() > MAX_WORDLEN)) { throw Exception("A Word was too long. Exiting\n"); } if (normCase) { temp.word1 = downstring(tempstring1,lang); temp.word2 = downstring(tempstring2,lang); } else { temp.word1 = tempstring1; temp.word2 = tempstring2; } temp.distance = 0.0; results.push_back(temp); } }
streamsize cb::transfer(istream &in, ostream &out, streamsize length, SmartPointer<TransferCallback> callback) { char buffer[BUFFER_SIZE]; streamsize total = 0; while (!in.fail() && !out.fail()) { in.read(buffer, length ? min(length, BUFFER_SIZE) : BUFFER_SIZE); streamsize bytes = in.gcount(); out.write(buffer, bytes); total += bytes; if (!callback.isNull() && !callback->transferCallback(bytes)) break; if (length) { length -= bytes; if (!length) break; } } out.flush(); if (out.fail() || length) THROW("Transfer failed"); return total; }
bool SquareMaze::SquareMazeNode::readXY (istream& inData,int *x,int *y) { char ch; // skip leading spaces, which are legit while (1) { inData.get(ch); if (ch!=' ') { break; } } if (ch!='(') { // not valid - probably a newline return false; } inData >> *x; if (inData.fail()) { inData.clear(); cout << "WARNING: Solution seems to have a bad format\n"; // a bit of random, defensive programming inData.ignore(100000,'\n'); // make sure we don't just keep reading the same thing return false; } inData.get(ch); inData >> *y; inData.get(ch); return true; }
void run(exploder & e, istream& in) { // any larger, and we may try to write to a multi_frame that never has enough space static const int BUF_SIZE = multi_frame::MAX_SIZE - frame::FRAME_SIZE; scoped_array<char> buffer(new char[BUF_SIZE]); ios::sync_with_stdio(false); // makes a big difference on buffered i/o for (int line = 1; !in.eof(); ++line) { in.getline(buffer.get(), BUF_SIZE - 1); // leave 1 for us to inject back the newline if (buffer[0] == '\0') continue; if (in.fail()) // line was too long? { cerr << "Skipping line <" << line << ">: line is probably too long" << endl; in.clear(); // clear state in.ignore(numeric_limits<streamsize>::max(), '\n'); continue; } buffer[in.gcount() - 1] = '\n'; // inject back the newline buffer[in.gcount()] = '\0'; e << buffer.get(); } }
static double read_double (istream &is) { is.clear(); double v; is >> v; if (is.fail()) throw SpecialException("number expected"); return v; }
void fillStdVect (vector<student> &studentlist, istream &in_put) { while (!in_put.fail()) { student* s = new student; in_put >> *s; if (!in_put.fail()) { studentlist.push_back(*s); } else { delete s; } } }
/*------------------------------------------------------------------------------ * Add a configuration line *----------------------------------------------------------------------------*/ void Config :: read ( istream & is ) throw ( Exception ) { char line[LINE_SIZE]; unsigned int num; for ( num = 0; !is.fail() && !is.eof(); ++num ) { is.getline( line, LINE_SIZE); if ( is.eof() ) { break; } else if ( is.fail() ) { throw Exception( __FILE__, __LINE__, "line too long", num); } addLine( line); } }
static inline void checkStream (const istream& in) { if (in.fail()) { cerr << "Fatal error: stream failed!" << endl; exit(-1); } }
void ProductParser::parseCommonProduct(istream& is, bool& error, string& errorMsg, int& lineno) { lineno++; string myline; getline(is, myline); myline = trim(myline); if(myline.size() == 0) { error = true; errorMsg = "Unable to find a product name"; return; } prodName_ = myline; lineno++; getline(is, myline); if(is.fail()) { error = true; errorMsg = "Expected another line with the price"; return; } stringstream ss1(myline); ss1 >> price_; if( ss1.fail() ) { error = true; errorMsg = "Unable to read price"; return; } lineno++; getline(is, myline); if(is.fail()) { error = true; errorMsg = "Expected another line with the quantity"; return; } stringstream ss2(myline); ss2 >> qty_; if( ss2.fail() ) { error = true; errorMsg = "Unable to read quantity"; } }
float ReadFloat( istream &in ){ float tmp = 0; in.read( reinterpret_cast<char*>(&tmp), sizeof(tmp) ); if (in.fail()) throw runtime_error("premature end of stream"); return tmp; }
short ReadShort( istream& in ){ short tmp = 0; in.read( (char*)&tmp, 2 ); if (in.fail()) throw runtime_error("premature end of stream"); return tmp; }
byte ReadByte( istream& in ){ byte tmp = 0; in.read( (char*)&tmp, 1 ); if (in.fail()) throw runtime_error("premature end of stream"); return tmp; }
unsigned int ReadUInt( istream& in ){ unsigned int tmp = 0; in.read( (char*)&tmp, 4 ); if (in.fail()) throw runtime_error("premature end of stream"); return tmp; }
int RobotPathGenome::read(istream & is) { for(int i = 0; i < n; i++) { is >> *(list[i]); } return is.fail() ? 1 : 0; }
//ShortString void NifStream( ShortString & val, istream& in, const NifInfo & info ) { byte len = ReadByte( in ); char * buffer = new char[len]; in.read( buffer, len ); if (in.fail()) throw runtime_error("premature end of stream"); val.str = buffer; delete [] buffer; };
void readEntireFile(istream & is, vector<string> & lines) { lines.clear(); while (true) { string line; getline(is, line); if (is.fail()) break; lines.push_back(line); } }
bool isbadinput(istream& in) { if (in.fail()) { cout << "Bad input\n"; in.clear(); in.ignore(); return true; } return false; }
/** Reads an employee record from a file. @param e filled with the employee @param in the stream to read from */ void read_employee(Employee& e, istream& in) { string line; getline(in, line); if (in.fail()) return; string name = line.substr(0, 30); double salary = string_to_double(line.substr(30, 10)); e = Employee(name, salary); }
void GLibXMLAdapter::read(istream &stream) { typedef void (*start_element_func_t)(GMarkupParseContext *, const gchar *, const gchar **, const gchar **, gpointer data, GError **error); typedef void (*end_element_func_t)(GMarkupParseContext *, const gchar *, gpointer, GError **); typedef void (*text_func_t)(GMarkupParseContext *, const gchar *, gsize, gpointer, GError **); GMarkupParser parser = { (start_element_func_t)&GLibXMLAdapter::startElement, (end_element_func_t)&GLibXMLAdapter::endElement, (text_func_t)&GLibXMLAdapter::text, 0, // passthrough 0, // error }; GMarkupParseContext *context = g_markup_parse_context_new(&parser, (GMarkupParseFlags)0, (void *)this, 0); if (!context) THROW("Failed to create XML parser."); try { while (!stream.eof() && !stream.fail()) { char buf[BUFFER_SIZE]; stream.read(buf, BUFFER_SIZE); streamsize count = stream.gcount(); GError *gerror = 0; if (count) { g_markup_parse_context_parse(context, buf, count, &gerror); if (error.get()) { Exception e(*error); error = 0; throw; } if (gerror) THROW("Parse failed " << g_quark_to_string(gerror->domain) << ": " << gerror->code << ": " << gerror->message); } } } catch (const Exception &e) { int line; int column; g_markup_parse_context_get_position(context, &line, &column); g_free(context); throw Exception(e.getMessage(), FileLocation(getFilename(), line - 1, column), e); } }
bool Point2Do::load(istream &in){ coords.resize(3); int start = in.tellg(); for(int i = 0; i < 2; i++){ in >> coords[i]; if(in.fail()){ in.clear(); in.seekg(start+1); //????????? Why that one return false; } } in >> theta; if(in.fail()){ in.clear(); in.seekg(start+1); //????????? Why that one return false; } return true; }
void fillGrdVect (vector<grades> &gradelist, istream &inGrades) { grades g; while (!inGrades.fail()) { cout << endl << "G1 " << g; inGrades >> g; cout << endl << "G2 " << g; if (!inGrades.fail()) { gradelist.push_back(g); cout << endl << "G3 " << gradelist.back(); } cout << endl << "G4 " << g << endl; } }