void readline (istream &cin, string &s) { while (cin) { char c; size_t n=0; while (cin && cin.get(c) && (c!=10) && (c!=13)) { if (c==9) { int nn = 8 - (n%8); for (int i=0 ; i<nn ; i++) s+=' ', n++; } else { s+=c, n++; } } if (!cin) return; if (c==10) { if (cin.get(c) && (c!=13)) { cin.unget(); } return; } if (c==13) { if (cin.get(c) && (c!=10)) { cin.unget(); } return; } } }
//--------------------------------------------------------------------------- unique_ptr<Expression> ExpressionParser::parseSingleExpression(istream& input, ExpressionType lastExpression, Environment& environment) { // read harriet::skipWhiteSpace(input); char a = input.get(); if(!input.good()) return nullptr; // other single letter operators if(a == '(') return make_unique<OpeningPharentesis>(); if(a == ')') return make_unique<ClosingPharentesis>(); if(a == '+') return make_unique<PlusOperator>(); if(a == '-') { if(lastExpression==ExpressionType::TBinaryOperator || lastExpression==ExpressionType::TUnaryOperator || lastExpression==ExpressionType::TOpeningPharentesis) return make_unique<UnaryMinusOperator>(); else return make_unique<MinusOperator>(); } if(a == '*') return make_unique<MultiplicationOperator>(); if(a == '/') return make_unique<DivisionOperator>(); if(a == '%') return make_unique<ModuloOperator>(); if(a == '^') return make_unique<ExponentiationOperator>(); if(a == '&') return make_unique<AndOperator>(); if(a == '|') return make_unique<OrOperator>(); if(a=='>' && input.peek()!='=') return make_unique<GreaterOperator>(); if(a=='<' && input.peek()!='=') return make_unique<LessOperator>(); if(a=='!' && input.peek()!='=') return make_unique<NotOperator>(); if(a=='=' && input.peek()!='=') return make_unique<AssignmentOperator>(); // check for string char b = input.get(); if(a=='"') { string result; while(b!='"' && a!='\\') { if(!input.good()) throw harriet::Exception{"unterminated string expression"}; result.push_back(b); a = b; b = input.get(); } return make_unique<StringValue>(result); } // check for two signed letters if(input.good()) { if(a=='=' && b=='=') return make_unique<EqualOperator>(); if(a=='>' && b=='=') return make_unique<GreaterEqualOperator>(); if(a=='<' && b=='=') return make_unique<LessEqualOperator>(); if(a=='!' && b=='=') return make_unique<NotEqualOperator>(); input.unget(); } else { input.clear(); } // check for a number input.unget(); if(isdigit(a)) { int32_t intNum; input >> intNum; if(input.peek()=='.' && input.good()) { float floatNum; input >> floatNum; return make_unique<FloatValue>(floatNum+intNum); } else { return make_unique<IntegerValue>(intNum);
void Fasta_reader::read(istream & input, vector<Fasta_entry> & seqs, bool short_names, bool degap) const throw (Exception) { if (!input) { throw IOException ("Fasta_reader::read. Failed to open file"); } char c = input.get(); while(c==' ' || c=='\n') { c = input.get(); } if(c=='>') { input.unget(); this->read_fasta(input,seqs,short_names,degap); } else if(c=='@') { input.unget(); this->read_fastq(input,seqs); } else if(c=='#') { input.unget(); this->read_graph(input,seqs,short_names); } else { Log_output::write_out("Input file format unrecognized. Only FASTA and FASTQ formats supported. Exiting.\n\n",0); exit(1); } map<string,int> copy_num; for(int i=0;i<(int)seqs.size();i++) { string name = seqs.at(i).name; map<string,int>::iterator it = copy_num.find(name); if(it != copy_num.end()) { it->second++; stringstream ss; ss<<"."<<it->second; name.append(ss.str()); Log_output::write_out("Warning: duplicate names found. '"+seqs.at(i).name+"' is renamed '"+name+"'.\n",2); seqs.at(i).name = name; } else { copy_num.insert( pair<string,int>(name,0) ); } } }
int readDataLine(istream &f, strings_t &line, int &expected) { int obtained = 0; while (f.good()) { int c = skipBlank(f); if (c == '\n' || c == '\r') break; string s; f >> s; if (! s.empty()) { line.push_back(s); obtained += 1; } } int c = f.get(); if (c == '\r' && f.get() != '\n') f.unget(); if (obtained > 0) { if (expected <= 0) expected = obtained; else if (expected > 0 && expected != obtained) { cerr << "ERROR: expecting " << expected << " columns in data file." << endl; exit(10); } } else skipSpace(f); return obtained; }
void SkipWS( istream& is) { if( is.eof()) throw GDLIOException( "End of file encountered. "+ StreamInfo( &is)); char c; do { c = is.get(); if ( (is.rdstate() & ifstream::failbit ) != 0 ) { if ( (is.rdstate() & ifstream::eofbit ) != 0 ) throw GDLIOException( "End of file encountered. "+ StreamInfo( &is)); if ( (is.rdstate() & ifstream::badbit ) != 0 ) throw GDLIOException( "Error reading stream. "+ StreamInfo( &is)); is.clear(); return ; } } while( c == ' ' || c == '\t' || c == '\n'); is.unget(); }
void Polygon::read(istream &stream) { flags = readString(stream); readWS(stream); int c = stream.get(); if (c != ')') THROW("missing ')' on Polygon"); readWS(stream); c = stream.get(); if (c != '(') THROW("missing '(' on Polygon"); do { readWS(stream); c = stream.get(); if (c != '[') break; Point p; stream >> p; points.push_back(p); readWS(stream); c = stream.get(); if (c != ']') THROW("missing ']' on Polygon point"); } while (true); stream.unget(); }
void Object::readWS(istream &stream) const { int c; do { c = stream.get(); if (c == -1) return; } while (isspace(c)); stream.unget(); }
static int skipSpace(istream &f) { int c = f.get(); while (f.good() && isspace(c)) c = f.get(); if (! f.eof()) f.unget(); return c; }
static int skipBlank(istream &f) { int c = f.get(); while (f.good() && isspace(c) && c!='\n' && c!='\r') c = f.get(); if (! f.eof()) f.unget(); return c; }
void SendFormattedInputToOutput(istream &input, ostream &output) { string line; while (getline(input, line)) { output << RemoveExtraSpaces(line); input.unget(); if (input.get() == '\n') { output << endl; } } }
vector<tag> getHtml(istream& input, int htmlLines){ vector<tag> html; stack<tag> holdTag; for (size_t i = 0; i < htmlLines; i++) { tag temp; input.ignore(LINESIZE, '<'); if (input.peek() == '/') { tag hold = holdTag.top(); holdTag.pop(); if (holdTag.empty()) { html.push_back(hold); }else{ holdTag.top().children.push_back(hold); } input.ignore(LINESIZE, '\n'); }else{ holdTag.push(tag()); input >> holdTag.top().tagName; if(holdTag.top().tagName.back() == '>'){ holdTag.top().tagName.erase(holdTag.top().tagName.end()-1); input.unget(); //remove the last char if the > char is found there } while(input.peek() != '>'){ string key, val; input.get(); getline(input, key, ' '); input.ignore(LINESIZE, '\"'); getline(input, val, '\"'); holdTag.top().attributes.insert(make_pair(key, val)); } } } return html; }
// no skip of WS const string ReadComplexElement(istream& is) { SkipWS( is); string buf; char c = is.get(); if ( (is.rdstate() & ifstream::failbit ) != 0 ) { if ( (is.rdstate() & ifstream::eofbit ) != 0 ) throw GDLIOException( "End of file encountered. "+ StreamInfo( &is)); if ( (is.rdstate() & ifstream::badbit ) != 0 ) throw GDLIOException( "Error reading stream. "+ StreamInfo( &is)); is.clear(); return buf; } bool brace = (c == '('); if( !brace) { is.unget(); return ReadElement( is); } buf.push_back( c); for(;;) { c = is.get(); if ( (is.rdstate() & ifstream::failbit ) != 0 ) { if ( (is.rdstate() & ifstream::badbit ) != 0 ) throw GDLIOException( "Error reading line. "+ StreamInfo( &is)); is.clear(); return buf; } if( c == '\n') return buf; buf.push_back( c); if( c == ')') return buf; } }
// EOFまたは終了インジゲータが検出されるまでistからvに整数を読み取る void fill_vector(istream& ist, vector<int>& v, char terminator){ int i = 0; while(ist >> i) v.push_back(i); if(ist.eof()) return; // OK: EOFが検出された if(ist.fail()){ // できるだけ後始末をし、問題を報告する ist.clear(); // ストリームの状態をクリアし、終了インジゲータを調査できるようにする char c; ist >> c; // 文字を読み取る(終了インジゲータでありますように) if(c != terminator){ // 予想外の文字 ist.unget(); ist.clear(ios_base::failbit); // 状態をfailに設定する } }
TokenType ReadDecNumber (istream& is, string& str, Coordinates& xy) { State st = is_digit; TokenType T = int_const_dec; bool point = false; bool exp = false; while (st == is_digit) { st = GetState(is.peek()); if (st == is_point) { if (point) throw Error("wrong float number", pos.first, pos.second); point = true; T = float_const; str = str + GetSymb(is,xy); st = GetState(is.peek()); if (st == is_point) { is.unget(); --xy.first; str.resize(str.size()-1); return int_const_dec; } } if (is.peek() == 'e' || is.peek() == 'E') { if (exp) throw Error("wrong float number", pos.first, pos.second); exp = true; T = float_const; str = str + GetSymb(is, xy); st = GetState(is.peek()); if (st == is_plus || st == is_minus) { str = str + GetSymb(is, xy); st = GetState(is.peek()); } } st = GetState(is.peek()); if (st == is_digit) str = str + GetSymb(is, xy); if (IsSyntaxError(st) || st == is_letter) throw Error("syntax error", pos.first, pos.second); } return T; }
void fill_vector(istream& ist, vector<int>& v, char terminator) // read integers from ist into v until we reach eof() or terminator { int i = 0; while (ist >> i) v.push_back(i); if (ist.eof()) return; // fine: we found the end of file // not good() and not bad() and not eof(), ist must be fail() ist.clear(); // clear stream state char c; ist>>c; // read a character, hopefully terminator if (c != terminator) { // ouch: not the terminator, so we must fail ist.unget(); // maybe my caller can use that character ist.clear(ios_base::failbit); // set the state to fail() } }
void fillVector(vector<int> &v, istream &ist, char terminator) { int x; while(ist >> x) v.push_back(x); if(ist.bad()) error("Some unusual error occurred, stream is in bad state."); if(ist.eof()) return; if(ist.fail()) { ist.clear(); // clear stream state char c; ist >> c; if(c == terminator) { cout << "found terminator\n"; return; } ist.unget(); ist.clear(ios_base::failbit); // set the state to fail }
// function to strip out white space in front of next token. Modifies // the provided stream accordingly and returns 1 if whitespace was // skipped, 0 otherwise int skip_ws(istream & is) { char ch; int flag=0; // isspace gets all possible whitespace while( isspace(ch=is.get()) ) { // remember we got whitespace flag=1; // if it is a newline, we'd be on the next line if (ch=='\n') __linenum++; } // sneakily, the last get "ate up" the first non-WS character. The // token will want that back! is.unget(); return flag; }
int Object::readInt(istream &stream) { readWS(stream); int c = stream.get(); int sign = 1; if (c == '-') sign = -1; else if (!isdigit(c)) THROW("Expected number"); int x = 0; do { x = 10 * x + c - '0'; c = stream.get(); if (!isdigit(c)) { stream.unget(); break; } } while (!stream.fail()); return x * sign; }
void EndlineFilter::applyFilter(istream & inStream, ostream & outStream) { while (inStream.good()) { char currentLetter; currentLetter = inStream.get(); while (inStream.good()) { if (currentLetter != '\n') { outStream << currentLetter; currentLetter = inStream.get(); } else { outStream << "<br>" << " "; currentLetter = inStream.get(); } } inStream.unget(); } };
bool CSVSimpleParser::multiplatformgetline ( istream& is, string& str ) { //nothing new to read. if(is.eof()) return false; str.clear(); str.reserve(2048); char c; bool tdlm = false; while(is.get(c)) { if (c == _textDelimiter) { tdlm = !tdlm; str.push_back(c); continue; } //Carriage return Windows and mac if(c=='\r') { //Check if the next character is \n and remove it. if(is.get(c) && c != '\n') { is.unget(); } if (!tdlm) break; } else if(c=='\n' && !tdlm) { break; } //Push the character str.push_back(c); } //End of line reading. return true; }
// function to strip out (a sequence of) comments in front of next // token. Modifies the provided stream accordingly and returns 1 if at // least one comment was skipped, 0 otherwise int skip_comm(istream &is) { char ch; ch = is.get(); // does the next character indicate the start of another comment? if (ch != '#') { // if not ... we need to give that character back; it could well // be the beginning of the next token is.unget(); return 0; } // ... but if it is a comment, just keep going to the end of line. while ( (ch=is.get())!='\n' ); // since the comment ends at the end of a line, the line number goes up! __linenum++; return 1; }
void translateStream(istream& input, ostream& output){ char word[255]; char translated_word[255]; int i = 0; word[0] = input.get(); if (input.bad() || input.eof()) return; if (!isalpha(word[0]) && !isdigit(word[0])){ translated_word[0] = word[0]; translated_word[1] = '\0'; } else { for (i = 1; isalpha(word[i-1]) || isdigit(word[i-1]); i++){ word[i] = input.get(); } input.unget(); word[i-1] = '\0'; translateWord(word, translated_word); } output << translated_word; translateStream(input, output); }
// helper function - reads one line, does error checking const string ReadElement(istream& is) { SkipWS( is); string buf; char c; for(;;) { c = is.get(); // int cc = c; // cout << "ReadEl: " << cc << " " << c << ":" << endl; if ( (is.rdstate() & ifstream::failbit ) != 0 ) { if ( (is.rdstate() & ifstream::badbit ) != 0 ) throw GDLIOException( "Error reading line. "+ StreamInfo( &is)); is.clear(); return buf; } if( c == '\n') return buf; if( c == ' ' || c == '\t') { is.unget(); return buf; } buf.push_back( c); } if( !is.good()) throw GDLIOException( "Error reading stream. "+StreamInfo( &is)); return buf; // // old version (read full line which is then split - does not work with // // different types on the same line) // if( is.eof()) // throw GDLIOException( "End of file encountered. "+ // StreamInfo( &is)); // string retStr; // getline( is, retStr); // if ( (is.rdstate() & ifstream::failbit ) != 0 ) // { // if ( (is.rdstate() & ifstream::eofbit ) != 0 ) // throw GDLIOException( "End of file encountered. "+ // StreamInfo( &is)); // if ( (is.rdstate() & ifstream::badbit ) != 0 ) // throw GDLIOException( "Error reading line. "+ // StreamInfo( &is)); // is.clear(); // return ""; // } // if( !is.good()) // throw GDLIOException( "Error reading line. "+StreamInfo( &is)); // cout << "Read line: " << retStr << endl; // return retStr; }
string GetInChI(istream& is) { string prefix("InChI="); string result; enum statetype {before_inchi, match_inchi, unquoted, quoted}; statetype state = before_inchi; char ch, lastch=0, qch=0; size_t split_pos = 0; bool inelement=false, afterelement=false; while((ch=is.get())!=EOF) { if(state==before_inchi) { if(ch>=0 && !isspace(ch)) { if(ch==prefix[0]) { result += ch; state = match_inchi; qch = lastch; } } lastch = ch; } else if(ch=='<') { // Ignore the content of any <...> elements // But a second consecutive <...> element terminates an unquoted InChI if(afterelement && state==unquoted) return result; inelement=true; } else if(inelement) { if(afterelement) { //Now reading after a <...> inserted in the InChI string //Neglect whitespace, but any other character reverts to normal InChI parsing if(ch<0 || !isspace(ch)) { is.unget(); afterelement=false; inelement=false; } } else { if(ch=='>') afterelement=true; //look for whitespace after end of element } } else if(ch>=0 && isspace(ch)) { if(state==unquoted) return result; } else if(isnic(ch)) { if(ch==qch && state!=match_inchi) return result; if(split_pos!=0) result.erase(split_pos); split_pos = result.size(); } else { result += ch; if(state==match_inchi) { if(prefix.compare(0,result.size(),result)==0) //true if correct { if(result.size()==prefix.size()) state = isnic(qch)&& qch!='>' ? quoted : unquoted; } else { is.unget(); //It may be the start of a real "InChI=" result.erase(); state = before_inchi; } } } } return result; }
int getMyNum(istream& is,char delim,bool & emptyFlag,bool & delimFlag,bool allowWS=false) // deliminator is '/' or '\n' { stringstream ss; bool foundFirst=false; delimFlag=false; //ensure it starts out FALSE char c='0'; int myNum=-99; // init number is>>ws; // built in skip leading whitespace //while(is.get(c) && !(delimFlag=delimFound(c,delim)) && c!=sysEOL)//c!=delim) while(is.get(c) && !(delimFlag=delimFound(c,delim)) && c!=sysEOL)//c!=delim) { //cout<<":"<<c<<":"<<endl; //cout<<"!"<<is.tellg()<<"!"<<endl; if((!foundFirst)&& c=='-') //check for a leading negative { ss<<c; //first char can b negative (-) foundFirst=true; } else if(!isIntChar(c)) { if(!( allowWS && isWhiteSpaceChar(c))) // TODO could be combined w/ above if? { //TODO do not strip \n is.ignore(256,delim); // fails for >256 chars, okay by me is.setstate(ios::failbit); delimFlag=true; } } else { foundFirst=true; //set found first, if no negative and ss<<c; } } // get size /* ss.seekg(0,ios::end); int size=ss.tellg(); ss.seekg(0,ios::beg); */ int size=ss.str().size(); if( size==0 ||(size=1 && ss.str()=="-")) // check for empty, or single '-' { emptyFlag=true; } ss>> myNum; //stuff result into an int /* if(is.fail()&& !allowWS) // kluge 12 3/345 doesnt => 12 { cout <<"magic3"<<endl; is.unget(); is.putback(delim); } */ if(delim != sysEOL && is.peek()==sysEOL) //kluge for 88/<LF>, otherwise grabs next line { cout <<"Magic2"<<endl; is.unget(); } if(delim== sysEOL || c== sysEOL) // put back EOL or (c == sysEOL) kinda duplication //if(c== sysEOL&&(emptyFlag&&delim==sysEOL)) // put back EOL or (c == sysEOL) kinda duplication { //cout<<"putting LF back"<<endl; is.unget(); } //kluge if( emptyFlag && delim == sysEOL) // check for empty, or single '-' for case of 7 <lf>, is.get(c) processed before delimFlag=delimFound... { is.unget(); delimFlag=true; cout <<"Magic set flag"<<endl; myNum=1; } is.clear(); //clear all flags return myNum; }
// Gets the next token in the input stream (is) void Token::Get(istream &is) { // remember if we have built the table for lexical analysis yet ... static short table_built=0; // the lexical analysis table; static makes sure it remains even // after the current call completes. static int DFA[MAXSTATES][256]; // if we have not built the table yet, do so ... if (!table_built) { int snum; // "iterator" for state number int inp; // "iterator" for input characters. // default for all states is ERROR (-1) for (snum=0; snum<MAXSTATES; snum++) for(inp=0; inp<256; inp++) DFA[snum][inp]=-1; // transitions into state 1 for(inp='A'; inp<='Z'; inp++) DFA[0][inp]=DFA[1][inp]=1; for(inp='a'; inp<='z'; inp++) DFA[0][inp]=DFA[1][inp]=1; for(inp='0'; inp<='9'; inp++) DFA[1][inp]=1; // transitions into state 2 for(inp='0'; inp<='9'; inp++) DFA[0][inp]=DFA[2][inp]=2; // transitions into state 3 DFA[2][(int) '.']=3; // transitions into state 4 for(inp='0'; inp<='9'; inp++) DFA[3][inp]=DFA[4][inp]=4; /* transitions into state 5 */ DFA[0][(int)'+']=DFA[0][(int)'-']=5; /* transitions into state 6 */ DFA[0][(int)'*']=DFA[0][(int)'/']=6; /* transitions into state 7 */ DFA[0][(int)'<']=DFA[0][(int)'>']=7; /* transitions into state 8 */ DFA[7][(int)'=']=DFA[9][(int)'=']=8; /* transitions into state 9 */ DFA[0][(int)'=']=9; /* transitions into state 10 */ DFA[0][(int)'(']=10; /* transitions into state 11 */ DFA[0][(int)')']=11; /* transitions into state 12 */ DFA[0][(int)'&']=12; /* transitions into state 13 */ DFA[12][(int)'&']=13; /* transitions into state 14 */ DFA[0][(int)'|']=14; /* transitions into state 15 */ DFA[14][(int)'|']=15; /* transitions into state 16 */ DFA[0][(int)';']=16; /* transitions into state 17 */ DFA[0][(int)'[']=17; /* transitions into state 18 */ DFA[0][(int)']']=18; /* transitions into state 19 */ DFA[0][(int)',']=19; // nopw that we've built the tabel, make sure the next call // doesn't waste time rebuilding it! table_built=1; } // if the string for tiken value alread has storage, delete it; it // might not be enough for this token, or it might be too much for // this token. if (_value) delete _value; // certainly won't need token of more than 75 chars (76 covers // terminating '\0') _value=new char[76]; // keep skipping whitepace and/or comments as long as you can while(skip_comm(is) || (skip_ws(is)) ); /* note "Tricky" semicolon here */ // if we are at the end of the stream, return EOF. if (!is) { _type=EOF_TOK; return; } int prev_state, // state we were in before current iteration curr_state; // current state char ch; // next character in input prev_state=curr_state=0; // before we start, we are in state 0 int index=0; // position within value string we are currently at // as long as the next state is not an error state ... while(curr_state!=-1) { // get next character ch=is.get(); // put it in the token (may be removed later) _value[index++]=ch; // back up current (valid) state prev_state=curr_state; // get the next state curr_state=DFA[curr_state][(int)ch]; } // getting here means that the current state is INVALID as a result // of reading the next character. So we need to: // // (1) remove the character from the token value string ... _value[--index]=0; // ... and (2) place it back in the input stream (as it could be the // beginning of something else is.unget(); // update the token's line number _line_num=__linenum; // get the type of token, based on the state ... _type=state2type[prev_state]; // ... one catch; if it is an ID, it could really be a reserved word if (_type==ID) // go through all the reserverd words (see reserved[] above) for (int tt=0; tt<=ELSE-VOID; tt++) { // if it matches a reserved word, then that is its type! if (!strcmp(reserved[tt], _value) ) { _type=(TokenType) (tt+VOID); break; } } }
void readQuotedString(istream & is, string & str) { str = ""; char ch; while (is.get(ch) && isspace(ch)) { /* Empty */ } if (is.fail()) return; if (ch == '\'' || ch == '"') { char delim = ch; while (is.get(ch) && ch != delim) { if (is.fail()) error("Unterminated string"); if (ch == '\\') { if (!is.get(ch)) error("Unterminated string"); if (isdigit(ch) || ch == 'x') { int maxDigits = 3; int base = 8; if (ch == 'x') { base = 16; maxDigits = 2; } int result = 0; int digit = 0; for (int i = 0; i < maxDigits && ch != delim; i++) { if (isdigit(ch)) { digit = ch - '0'; } else if (base == 16 && isxdigit(ch)) { digit = toupper(ch) - 'A' + 10; } else { break; } result = base * result + digit; if (!is.get(ch)) error("Unterminated string"); } ch = char(result); is.unget(); } else { switch (ch) { case 'a': ch = '\a'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case 'v': ch = '\v'; break; case '"': ch = '"'; break; case '\'': ch = '\''; break; case '\\': ch = '\\'; break; } } } str += ch; } } else { str += ch; int endTrim = 0; while (is.get(ch) && STRING_DELIMITERS.find(ch) == string::npos) { str += ch; if (!isspace(ch)) endTrim = str.length(); } if (is) is.unget(); str = str.substr(0, endTrim); } }