void ImageType::copyHeader(ofstream& imageDestination, ifstream& imageSource) { char line[COMMENT_LENGTH]; imageSource.getline(line, COMMENT_LENGTH); imageDestination << line << '\n'; char c; imageSource.get(c); while (c == '#') { imageSource.unget(); imageSource.getline(line, COMMENT_LENGTH); imageDestination << line << '\n'; imageSource.get(c); } imageSource.unget(); int width; int height; int maxvalue; imageSource >> width >> height >> maxvalue; imageDestination << width << ' ' << height << '\n'; imageDestination << maxvalue << '\n'; }
bool countFrequency(ifstream& infile, int *frequency, String& str) { for (int i = 0; i < maxCode; i++)//init with 0 { frequency[i] = 0; } char letter = infile.get(); if (infile.eof()) { cout << "\n\aFile is empty!"; return 1; } else { infile.unget(); } while (!infile.eof()) { letter = infile.get(); if (letter < 10)//not letter or symbol { continue; } concat(str, {&letter, 1}); frequency[(int) letter]++; } return 0; }
bool POS_get(ifstream& in, ifstream& in2, ofstream& out) { // one line at a time string def, line, line2, pos; vector<string> defs; getline(in, line); if (line == "THIS_IS_THE_END") { return false; } // get the word in english string eng = engword(in2); //line = switch_quotes(line); pos_body(pos, def, defs, line, eng, out); char c; while (1) { c = in.get(); if (c == '2') { getline(in, line2); line.erase(0, 2); pos_body(pos, def, defs, line2, eng, out); } else { in.unget(); break; } } return true; }
void getVarsDeclaration(ifstream &file) { //var ja foi consumido string token; //---------------------------- file >> token; // "name?" file.putback(token.length()); // retorna "name?" if (token==";") { cout << "expected: variable identifier" <<endl; exit(0); } while (token!=";" && file.good()) { file >> token; // name file >> token; // separador ou terminador if (token!="," && token!=";") { cout << "expected: variable identifier separator or declaration terminator" <<endl; exit(0); } file.unget(); //putback the ; } }
void getStatements(ifstream &file) { string token,tmp; tmp=Peek(file); if (tmp=="}" || tmp==")") return; while (true) { file >> token; if (token=="}") { file.unget(); return; } if (token=="var") getVarsDeclaration(file); else { //token eh nome tmp=token; file >> token; // pega o segundo token depois do nome putback(token,file); // retorna ponteiro para o nome do file.putback(1); while (file.peek()==' '); putback(tmp,file); //retorna ponteiro para antes do token if (token=="(") getFuncCall(file); cout << endl; } expect(";",file); } }
Map<string,Vector<string> > analyzeSourceText(ifstream &infile,int order) { Map<string,Vector<string> > markovModel; Vector<string> followingCharFrequence; char ch; while(infile.get(ch)) { string currentSeed=""; for(int i=0; i<order; i++) { currentSeed+=ch; infile.get(ch); } if(!markovModel.containsKey(currentSeed)) { Vector<string> copy=followingCharFrequence; markovModel.put(currentSeed,copy); } string followedCharS=""; followedCharS+=ch; markovModel[currentSeed].add(followedCharS); for(int i=0; i<order; i++) { infile.unget(); } } return markovModel; }
void PulaBrancoComentario(ifstream &ifile) { char c; string line; while (!ifile.eof()) { c = ifile.get(); while ((c==' ') && (!ifile.eof())) c = ifile.get(); if (!ifile.eof()) { if (c=='#') getline(ifile,line); else {ifile.unget(); break; } } } }
//function that finds if the chars following match a target symbol bool followingChars(string target, char currentChar, token curToken) { for(int x = 0; x < target.length(); x++) { if(currentChar == target[x]) { curToken.appendChar(currentChar); currentChar = filestream.get(); } else { curToken.clearData(); while(x != 0) { filestream.unget(); x--; } return false; } } filestream.unget(); return true; }
//function that finds if the chars following match a target symbol bool followingChars(string target, char currentChar) { for(int x = 0; x < target.length(); x++) { if(currentChar == target[x]) { calcTextAppend(currentChar); currentChar = filestream.get(); } else { calcTextClear(); while(x != 0) { filestream.unget(); x--; } return false; } } filestream.unget(); return true; }
/* A function that reads the map data file and imports all of the arcs into the graph structure. */ void getArcs(PathfinderGraph& graph, ifstream& mapData){ while(true){ if(mapData.get() == EOF) break; else mapData.unget(); string line; getline(mapData, line); TokenScanner scanner(line); scanner.ignoreWhitespace(); string start = scanner.nextToken(); string finish = scanner.nextToken(); scanner.scanNumbers(); string next = scanner.nextToken(); double cost = stringToReal(next); graph.addArc(start, finish, cost); graph.addArc(finish, start, cost); } }
ref_loc_t RefSeq::LoadNextSeq(ifstream &fin) { char ch[1000]; char c; string s; if(param.gz_ref) return 0; fin>>c; if(fin.eof()) return 0; string::iterator z=_seq.begin(); _length=0; fin>>_name; fin.getline(ch, 1000); while(!fin.eof()) { fin>>c; if(fin.eof()) break; fin.unget(); if(c=='>') break; fin>>s; if(_length+s.size()>=param.max_dbseq_size) { param.max_dbseq_size+=param.append_dbseq_size; _seq.resize(param.max_dbseq_size); z=_seq.begin()+_length; //cout<<"_seq size: "<<param.max_dbseq_size<<endl; } copy(s.begin(), s.end(), z); z+=s.size(); _length+=s.size(); } return _length; }
//this function finds the next token and outputs it token calcLex() { curToken.clearData(); char currentChar; while(1) //infinite loop, breaks through a return statement { //get the next char! currentChar = filestream.get(); //used to ignore whitespace as a token while( currentChar == ' ' || currentChar == '\t' || currentChar == '\n') { currentChar = filestream.get(); }; //if the current char is the end of a file, it returns the end of file symbol if(currentChar == EOF) { curToken.type(endOfFileSym); return curToken; } //this is the comment logic: if the next chars are /*... if(followingChars("/*", currentChar, curToken)) { //then while it isn't ending the line, or finding the end of comment sentinel... while(currentChar != '\n' && !followingChars("*/", currentChar, curToken)) { //move onto the next character currentChar = filestream.get(); } //move to the next character after the comment or line has ended //hop back up to the top of the while loop continue; } //if the chars ":=" are found, return the assignment symbol if(followingChars(":=", currentChar, curToken)) { curToken.type(assignSym); curToken.data(":="); return curToken; } //if the chars "read" are found, return the read symbol if(followingChars("read", currentChar, curToken)) { curToken.type(readSym); curToken.data("read"); return curToken; } //if the chars "write" are found, return the write symbol if(followingChars("write", currentChar, curToken)) { curToken.type(writeSym); curToken.data("write"); return curToken; } //if this is put above the followingChars, it adds a duplicate letter. curToken.appendChar(currentChar); //checks for identifier strings only starting with letters and underscores if( isalpha(currentChar) || (currentChar == '_') ) { while( isalnum(currentChar = filestream.get()) || (currentChar == '_')) { curToken.appendChar(currentChar); } //once the currentChar falls off the end of the identifier string, it must hop back to read the next one filestream.unget(); //returns the identifier int curToken.type(identifier); return curToken; } //if the current char is a number if(isdigit(currentChar)) { while ( isdigit(currentChar = filestream.get()) || currentChar == '.') { if (currentChar == '.') { curToken.appendChar(currentChar); currentChar = filestream.get(); if (!isdigit(currentChar)) { curToken.type(numConstError); return curToken; } while (isdigit(currentChar)) { curToken.appendChar(currentChar); currentChar = filestream.get(); } filestream.unget(); curToken.type(numConst); return curToken; } curToken.appendChar(currentChar); } filestream.unget(); curToken.type(numConst); return curToken; } if(currentChar == '+') { curToken.type(addOp); curToken.data("+"); return curToken; } if(currentChar == '-') { curToken.type(subOp); curToken.data("-"); return curToken; } if(currentChar == '*') { curToken.type(multOp); curToken.data("*"); return curToken; } if(currentChar == '/') { curToken.type(divOp); curToken.data("/"); return curToken; } if(currentChar == '(') { curToken.type(leftParen); curToken.data("("); return curToken; } if(currentChar == ')') { curToken.type(rightParen); curToken.data(")"); return curToken; } curToken.type(unknownError); return curToken; } curToken.type(unknownError); //this should never get here return curToken; }
//this function finds the next token and outputs it int calcLex() { char currentChar; while(1) //infinite loop, breaks through a return statement { //clear out the token info from last time calcTextClear(); //get the next char! currentChar = filestream.get(); //used to ignore whitespace as a token while( currentChar == ' ' || currentChar == '\t' || currentChar == '\n') { currentChar = filestream.get(); }; //if the current char is the end of a file, it returns the end of file symbol if(currentChar == EOF) { return endOfFileSym; } //this is the comment logic: if the next chars are /*... if(followingChars("/*", currentChar)) { //then while it isn't ending the line, or finding the end of comment sentinel... while(currentChar != '\n' && !followingChars("*/", currentChar)) { //move onto the next character currentChar = filestream.get(); } //move to the next character after the comment or line has ended //hop back up to the top of the while loop continue; } //if the chars ":=" are found, return the assignment symbol if(followingChars(":=", currentChar)) { return assignSym; } //if the chars "read" are found, return the read symbol if(followingChars("read", currentChar)) { return readSym; } //if the chars "write" are found, return the write symbol if(followingChars("write", currentChar)) { return writeSym; } //this line must go below the followingChars checking, because otherwise you get double chars in your token info calcTextAppend(currentChar); //checks for identifier strings, must only use letters if( (currentChar >= 'A' && currentChar <= 'Z') || (currentChar >= 'a' && currentChar <= 'z')) { //messy line, but gets new char and checks between capital letters and lowercase letters on the ascii table while(((currentChar = filestream.get()) >= 'A' && currentChar <= 'Z') || (currentChar >= 'a' && currentChar <= 'z')) { calcTextAppend(currentChar); } //once the currentChar falls off the end of the identifier string, it must hop back to read the next one filestream.unget(); //returns the identifier int return identifier; } //if the current char is a number if((currentChar >= '0' && currentChar <= '9')) { while ( ((currentChar = filestream.get()) >= '0' && currentChar <= '9') || currentChar == '.') { if (currentChar == '.') { calcTextAppend(currentChar); currentChar = filestream.get(); if (currentChar < '0' || currentChar > '9') { return 10; } while (currentChar >= '0' && currentChar <= '9') { calcTextAppend(currentChar); currentChar = filestream.get(); } filestream.unget(); return numConst; } calcTextAppend(currentChar); } filestream.unget(); return numConst; } if(currentChar == '+' || currentChar == '-') { return addOp; } if(currentChar == '*' || currentChar == '/') { return multOp; } if(currentChar == '(') { return leftParen; } if(currentChar == ')') { return rightParen; } return currentChar; //scanner doesn't know what this char is, throw it away } return endOfFileSym; //this should never be returned from here }
void putback(string str,ifstream &file) { for (int c=0;c<str.length();c++) file.unget(); }
int yylex() { int c; while (1) { yytextclear(); int c; while ((c = fin.get()) == ' ' || c == '\t' || c == '\n') continue; if (c == '/') { if (fin.peek() == '*') { fin.get(); while (c = fin.get()) { if ((c == '*' && fin.peek() == '/') || c == '\n') break; } fin.get(); continue; } else yytext[0]='/'; yytext[1]=0; return DIVOP; //MULOP be * | / } if (c == EOF){ return EOFSY; } if (c == ':' && fin.peek() == '=') { yytext[0]=c; yytext[1]=fin.get(); yytext[2]=0; return ASSIGNOPP; } if (c == '('){ yytext[0] = '('; yytext[1]=0; return LPAREN; } if (c == ')'){ yytext[0] = ')'; yytext[1]=0; return RPAREN; } if (c == '+'){ yytext[0] = '+'; yytext[1]=0; return ADDOP; //ADDOP can be + | - } if (c == '-'){ yytext[0] = '-'; yytext[1]=0; return SUBOP; //ADDOP can be + | - } if (c == '*'){ yytext[0] = '*'; yytext[1]=0; return MULOP; //MULOP can be * | / } if (c >= '0' && c <= '9') { int dec = 0; yytext[0] = c; int i = 1; while (((c = fin.get()) >= '0' && c <= '9') || (c == '.')) { if (c == '.') dec++; if (dec == 2) throw exception(); yytext[i] = c; i++; } yytext[i]=0; fin.unget(); return NUMCONST; } else { yytext[0] = toupper(c); int i = 1; while ((c = fin.get()) != ' ' && c != '\t' && c != '\n' && c != EOF && c != ':' && c != '(' && c != ')' && c != '+' && c != '-' && c != '*' && c != '/') { yytext[i] = toupper(c); i++; } yytext[i] = 0; fin.unget(); if (_stricmp(yytext, "read") == 0) { return READSY; } if (_stricmp(yytext, "write") == 0) { return WRITESY; } return ID; } } }