int readCha(string &res, stringstream &strIn) { res = ""; char ch; while (~(ch = strIn.get()) && (ch == ' ' || ch == '\n' || ch == '\t')) { ; } if (strIn.eof()) return END; if (ch == '\'') { res += ch; strIn >> ch; if (strIn.eof()) { strIn.putback(ch); return ERR; } if (ch == '\\') { res += ch; if ((ch = strIn.get()) == -1) return ERR; res += ch; if ((ch = strIn.get()) == -1 || ch != '\'') { return ERR; } res += ch; } else { res += ch; // cout << ch << endl; if ((ch = strIn.get()) == -1 || ch != '\'') { return ERR; } res += ch; } } else {
//checked int readInt(string &fres, stringstream &strIn, bool can = true) { if (can) fres = ""; string res = ""; char ch; while (~(ch = strIn.get()) && (ch == ' ' || ch == '\n' || ch == '\t')) { ; } if (strIn.eof()) return END; if (ch == '+' || ch == '-' || (ch >= '0' && ch <= '9')) { res += ch, fres += ch; } else { strIn.putback(ch); return ERR; } while (~(ch = strIn.get()) && (ch >= '0' && ch <= '9')) { res += ch, fres += ch; } int st = strIn.eof(); if (st) { if (isdigit(res[0]) || (int)res.size() > 1) return OKAY; else return ERR; } strIn.putback(ch); if ((int)res.size() == 1 && !isdigit(res[0])) { strIn.putback(res[0]); } return isdigit(res[0]) || (int)res.size() > 1 ? OKAY : ERR; }
int TablePaser::GetColumn(stringstream& ss_str, std::basic_istream<char>& in_put, string& column) { column = ""; if (ss_str.eof()) return -1; char cur_char[2]; cur_char[1] = 0; char separator = ' '; if (ss_str.get(cur_char[0]).eof()) return 1; if (cur_char[0] == ' ') return 1; else if (cur_char[0] == '"') separator = '"'; else column += string(cur_char); while (1) { if (ss_str.eof()) { if (separator == '"') { if (in_put.eof()) return 0; column += string("\n\0"); ss_str.clear(); in_put.getline(LineData, LINE_DATA_MAX); ss_str << LineData; } else return 0; } ss_str.getline(LineData, 4096, separator); column += LineData; //column.append(LineData); if (ss_str.eof()) continue; if (separator == ' ') return 1; else if (separator == '"') { if (ss_str.eof()) return 0; ss_str.get(cur_char[0]); if (ss_str.eof()) return 1; else if (cur_char[0] == ' ') return 1; else if (cur_char[0] != separator) separator = ' '; //column.push_back(cur_char); column += string(cur_char); } } return -1; }
void Logger::writeMessageStream(const int level, const string &scope, stringstream &stream) { std::string line; while(!stream.eof()) { getline(stream, line); writeMessage(level, scope, line); } }
// function for reading the nodeid from the stringstream. While it reads the // INTEGER it tests 3 various errors that can arrive from the user // I set an arbitrary return of -1 for an error occurrence so my function calls // know if it failed, if not it returns the desired input int get_nodeid (stringstream &ss, int lowerbound, int upperbound){ int i; ss >> i; // check for invalid number my making sure it was read, and also making sure // if the stored type into the variable matches and to see whether that value // was not actually written as a double and if a character follows the int if ((ss.fail() && !(ss.eof())) || ss.peek() == '.'){ if (ss.peek() != ' ' ){ cout << "Error: argument is not a number" <<endl; return -1; } } // checks the upper and lower bounds on the integer while making sure the // integer was for sure read and meets the integer type else if ((i < lowerbound || i > upperbound) && !ss.fail()) { cout << "Error: value " << i << " is out of permitted range " << lowerbound << "-" << upperbound << endl; return -1; } // this check tells us if an input was read else if (ss.fail()) { cout << "Error: missing argument" << endl; return -1; } // if no error raises, return the desired input return i; }
void get_l(stringstream& os, string& s, char del) { char t; while(!os.eof()) { os >> t; if(t==del) return; s += t; } }
bool parsePoint(stringstream &ss, string &analyst, double &value) { try { ss >> analyst; } catch (...) { return false; } if (ss.eof()) return false; try { ss >> value; } catch (...) { return false; } return true; }
void CIF::Parse(stringstream &in) { bool vv=false;//very verbose ? char lastc=' '; string block="";// Current block data while(!in.eof()) { stringstream mess; mess<<"CIF: Parsing:"<<in.tellg(); (*fpObjCrystInformUser)(mess.str()); while(!isgraph(in.peek()) && !in.eof()) in.get(lastc); if(in.eof()) break; if(vv) cout<<endl; if(in.peek()=='#') {//Comment string tmp; getline(in,tmp); if(block=="") mvComment.push_back(tmp); else mvData[block].mvComment.push_back(tmp); lastc='\r'; if(vv)cout<<"Comment:"<<tmp<<endl; continue; } if(in.peek()=='_') {//Tag string tag,value; in>>tag; // Convert all dots to underscores to cover much of DDL2 with this DDL1 parser. for (string::size_type pos = tag.find('.'); pos != string::npos; pos = tag.find('.', ++ pos)) tag.replace(pos, 1, 1, '_'); value=CIFReadValue(in,lastc); if(value==string("?")) continue;//useless mvData[block].mvItem[ci_string(tag.c_str())]=value; if(vv)cout<<"New Tag:"<<tag<<" ("<<value.size()<<"):"<<value<<endl; continue; } if((in.peek()=='d') || (in.peek()=='D')) {// Data string tmp; in>>tmp; block=tmp.substr(5); if(vv) cout<<endl<<endl<<"NEW BLOCK DATA: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ->"<<block<<endl<<endl<<endl; mvData[block]=CIFData(); continue; }
void FindFileNameInCommand(stringstream &ss, string &fname) { while(!ss.eof()) { ss>>fname; if( !(fname.find("-")!=string::npos || fname.find("--")!=string::npos) ) { return; } } }
/************************************************************************ * solveTheProblem * * Assumes the equations and variables have already been entered * * solves the problem, writing the solution in TEMP_FILE, * * including the tags and problem statements if the problem was * * not completely solved. * * Then it opens the file for reading, and returns the first line * * [does not close file] * ************************************************************************/ const char* solveTheProblem() { int k; if (isFirst) throw string("solveTheProblem called before initialization"); try { // reset the buffer to be empty resultBuffer.str(string()); if(resultBuffer.good()) { numsols->assign(canonvars->size(),HUGE_VAL); if (solveeqs(resultBuffer)) { // should we do checking of solution here? bool discrep = false; for (k = 0; k < canoneqf->size(); k++) { if (checksol((*canoneqf)[k],numsols,RELERR) > 1) { if (!discrep) { resultBuffer << "<DISCREPANCIES>" << endl; discrep = true; } resultBuffer << (*canoneqf)[k]->getInfix() << endl; } } // loop over equations to check dimchkeqf(resultBuffer); // end of "should we do checking of solution here?" } } else throw string("unable to create solution buffer"); } catch (string message) { throw message; } catch (...) { throw string("solveTheProblem went boom!!"); } // this would be a good place to insert checksol ? or after returning // solution, in solveMoreOfTheProblem ? try { resultBuffer.clear(); resultBuffer.seekg(0); int tk; try { tk = resultBuffer.eof(); } catch (...) { throw string("eof?!?"); } if (! tk) { string t; try { t = getaline(resultBuffer); } catch (...) { throw string("getaline fails???"); } try { sprintf(lzbfr, "%s", t.c_str()); } catch(...) { throw string("copy is wrong??"); } return lzbfr; } else return error[4]; } catch (string message) { throw message; } catch (...) { throw string("Read went bad!!"); } return error[3]; }
int main() { #ifndef ONLINE_JUDGE freopen("in_2289.txt","r",stdin); #endif while(scanf("%d%d",&n,&m)&&n) { memset(mat,false,sizeof(mat)); for(int i=0;i<n;++i) { scanf("%s",&buf); gets(buf); str.assign(buf); ss.clear(); ss<<str; int g; while(1) { ss>>g; mat[i][g]=true; if(ss.eof()) { break; } } } l=1; r=n; while(l<=r) { mid=(l+r)>>1; if(Check()) { r=mid-1; } else { l=mid+1; } } printf("%d\n",l); } return 0; }
int ParseLHCO::Define(stringstream &inputLine) { stringstream copy(inputLine.str()); if(Parse::Define(copy)) return 1; string block; inputLine >> block; if(block == "lhco_input") { string type, option; int val; inputLine >> option; LHCOInputs.push_back(new LHCOInfo(option)); while(!inputLine.eof()) { inputLine >> type; if(type != "MET") inputLine >> val; else val = -1; LHCOInputs.back() -> AddSubInput(type, val); }
void MesenMovie::ParseSettings(stringstream &data) { while(!data.eof()) { string line; std::getline(data, line); if(!line.empty()) { size_t index = line.find_first_of(' '); if(index != string::npos) { string name = line.substr(0, index); string value = line.substr(index + 1); if(name == "Cheat") { _cheats.push_back(value); } else { _settings[name] = value; } } } } }
/* * NOW WITH STRING STREAM SUPPORT! */ DataLine::DataLine(stringstream &inStream,int position) { this->position = position; while (inStream.peek()!='\n' && !inStream.eof()) { //keep reading unless the end is reached. string token = ""; inStream >> token; cout << token << endl; try { float f = std::stof(token); //<works //double d = std::stod(token); //cout << f << endl; theValues.push_back(f); } catch (...) { if (token != "") { cout << "Error on: " << token << endl; } } } }
// function for reading the resistance from the stringstream. While it reads the // DOUBLE it tests 3 various errors that can arrive from the user // I set an arbitrary return of -1 for an error occurrence so my function calls // know if it failed, if not it returns the desired input double get_resistance (stringstream &ss){ double i; ss >> i; // check for invalid number my making sure it was read, and also making sure // if the stored type into the variable matches and also checks to make sure // the input is not a double or if a character follows the integer if ((ss.fail() && !(ss.eof()))){ cout << "Error: argument is not a number" <<endl; return -1; } // checks to see if the resistance was negative else if (i < 0.0){ cout << "Error: invalid resistance (negative)" << endl; return -1; } // this check tells us if an input was read else if (ss.fail()) { cout << "Error: missing argument" << endl; return -1; } return i; }
bool parseSubscribeChange(stringstream &ss, ConsensusStat &stat, int &pct) { string statInput; try { ss >> statInput; } catch (...) { return false; } if (ss.eof()) return false; try { ss >> pct; } catch (...) { return false; } if (0 == statInput.compare("mean")) stat = MEAN; else if(0 == statInput.compare("median")) stat = MEDIAN; else if(0 == statInput.compare("max")) stat = MAX; else if(0 == statInput.compare("min")) stat = MIN; else if(0 == statInput.compare("stddev")) stat = STDDEV; else return false; return true; }
/************************************************************************ * solveMoreOfTheProblem * * reads one line from the opened solution file, * * if that line is empty, closes file, returns nil * * if not, returns it * * if EOF, closes file and returns nil * * I don't understand when is returns "So far so good" * ************************************************************************/ const char* solveMoreOfTheProblem() { try { if (! resultBuffer.eof()) { string t = getaline(resultBuffer); if (t.size() == 0) { resultBuffer.clear(); return error[4]; // if checksol after returning sol, here+below } // end of if line empty, which closes and returns else sprintf(lzbfr, "%s", t.c_str()); return lzbfr; } // end of not eof. else { resultBuffer.clear(); return error[4]; // if checksol after returning sol, here and above } } catch (string message) { resultBuffer.clear(); throw message; } catch (...) { resultBuffer.clear(); throw string("More went bad!!"); } return error[3]; }
int readFloat(string &res, stringstream &strIn) { // cout << strIn.str() << endl; res = ""; char ch; while (~(ch = strIn.get()) && (ch == ' ' || ch == '\n' || ch == '\t')) { ; } if (strIn.eof()) return END; if (ch != '.' && ch != '+' && ch != '-' && !(ch <= '9' && ch >= '0')) { return ERR; } if (ch == '.') { res += ch; if ((ch = strIn.get()) == -1 || !isdigit(ch)) { if (!strIn.eof()) { strIn.putback(ch); } strIn.putback(res[0]); return ERR; } else { // cout << res << endl; strIn.putback(ch); // cout << ch << endl; readInt(res, strIn, false); // cout << res << endl; if ((ch = strIn.get()) == -1 || ch != 'e') { if (!strIn.eof()) { strIn.putback(ch); } return OKAY; } else { res += ch; if ((ch = strIn.get()) == -1 || !(ch == '+' || ch == '-' || isdigit(ch))) { return ERR; } else { strIn.putback(ch); if (readInt(res, strIn, false) == ERR) return ERR; } } } } else if (isdigit(ch)) { strIn.putback(ch); readInt(res, strIn, false); if ((ch = strIn.get()) == -1 || ch != '.') { if (ch != 'e') { if (ch != -1) strIn.putback(ch); Revc(res, strIn); return ERR; } else { res += ch; if ((ch = strIn.get()) == -1 || !(ch == '+' || ch == '-' || isdigit(ch))) { return ERR; } else { strIn.putback(ch); if (readInt(res, strIn, false) == ERR) return ERR; } } } else { res += ch; if ((ch = strIn.get()) == -1) { return OKAY; } else if (isdigit(ch)) { strIn.putback(ch); readInt(res, strIn, false); if ((ch = strIn.get()) == -1 || ch != 'e') { if (!strIn.eof())strIn.putback(ch); return OKAY; } else { res += ch; if ((ch = strIn.get()) == -1 || !(ch == '+' || ch == '-' || isdigit(ch))) { return ERR; } else { strIn.putback(ch); if (readInt(res, strIn, false) == ERR) return ERR; } } } else if (ch == 'e') { res += ch; if ((ch = strIn.get()) == -1 || !(ch == '+' || ch == '-' || isdigit(ch))) { return ERR; } else { strIn.putback(ch); if (readInt(res, strIn, false) == ERR) return ERR; } } else { strIn.putback(ch); return OKAY; } } } else { } return OKAY; }