// 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; }
/// Read one value, whether it is numeric, string or text string CIFReadValue(stringstream &in,char &lastc) { bool vv=false;//very verbose ? string value(""); while(!isgraph(in.peek())) in.get(lastc); while(in.peek()=='#') {//discard these comments for now string tmp; getline(in,tmp); lastc='\r'; while(!isgraph(in.peek())) in.get(lastc); } if(in.peek()=='_') { if (vv) cout << "WARNING: Trying to read a value but found a new tag !" << endl; return value; } if(in.peek()==';') {//SemiColonTextField bool warning=!iseol(lastc); if(warning) cout<<"WARNING: Trying to read a SemiColonTextField but last char is not an end-of-line char !"<<endl; value=""; in.get(lastc); while(in.peek()!=';') { if (in.peek() == '_') { cout << "WARNING: Trying to read a SemiColonTextField but found a new tag !" << endl; warning = true; break; } string tmp; getline(in,tmp); value+=tmp+" "; } if (!warning) in.get(lastc); if(vv) cout<<"SemiColonTextField:"<<value<<endl; if(warning && !vv) cout<<"SemiColonTextField:"<<value<<endl; return value; } if((in.peek()=='\'') || (in.peek()=='\"')) {//QuotedString char delim; in.get(delim); value=""; while(!((lastc==delim)&&(!isgraph(in.peek()))) ) { in.get(lastc); value+=lastc; } if(vv) cout<<"QuotedString:"<<value<<endl; return value.substr(0,value.size()-1); } // If we got here, we have an ordinary value, numeric or unquoted string in>>value; if(vv) cout<<"NormalValue:"<<value<<endl; return value; }
int Util::trim(stringstream &ss) { while(ss.peek()==' ') ss.get(); return 0; }
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; }
Arbin<Pieza> leeArbol(stringstream& ss) { Arbin<Pieza> r; if(ss.peek() == 'T') { ss.get(); r = Arbin<Pieza>(leeArbol(ss), Pieza(), leeArbol(ss)); } else { r = Arbin<Pieza>(Arbin<Pieza>(), tablaPiezas.consulta(ss.get()), Arbin<Pieza>()); } }
/* * 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; } } } }
unsigned int getRule(stringstream& ss, S2Setmap& pt2base, V2Imap& goodman) { char c; ss >> c; assert(c == '('); string sym = ""; ss >> sym; unsigned int symI = sym2base[sym]; // printf("got symbol %s (%u)\n",sym.c_str(),symI); vector<unsigned int> kSyms; kSyms.push_back(symI); while(true) { c = ss.peek(); if(c == ' ') { //it's a space ss.ignore(1); //ignore that space } else if(c == '(') { //nonterminal //get child node index unsigned int index = getRule(ss,pt2base,goodman); kSyms.push_back(index); } else if (c == ')') { ss.ignore(1); //burn closing paren // printf("Finished Rule with sym %s and %lu kids\n",sym.c_str(),kSyms.size()-1); unsigned int index = 0; while(kSyms.size() > 3) { //try to add glue rule // printf("K = %lu\n",kSyms.size()); unsigned int r = kSyms.back(); kSyms.pop_back(); unsigned int l = kSyms.back(); kSyms.pop_back(); unsigned int glueI = nSym*2; vector<unsigned int> gluerule; gluerule.push_back(glueI);//glue symbol gluerule.push_back(l); gluerule.push_back(r); unsigned int glueindex = 0; V2Imap::iterator fter = goodman.find(gluerule); if(fter == goodman.end()) { //new node goodman[gluerule] = goodmanIndex; baseSym.push_back(glueI); glueindex = goodmanIndex; canL.push_back(false); canR.push_back(false); canL[l] = true; canR[r] = true; leftlook[l].push_back(make_pair(r,goodmanIndex)); bmap[make_pair(l,r)].insert(goodmanIndex); goodmanIndex++; } else { //seen it glueindex = fter->second; } kSyms.push_back(glueindex); } V2Imap::iterator fter = goodman.find(kSyms); if(fter == goodman.end()) { //new node goodman[kSyms] = goodmanIndex; baseSym.push_back(symI); index = goodmanIndex; canL.push_back(false); canR.push_back(false); goodmanIndex++; if(kSyms.size() == 3) { //add index -> l r // printf("BR : %u -> %u %u\n",index,kSyms[1],kSyms[2]); canL[kSyms[1]] = true; canR[kSyms[2]] = true; leftlook[kSyms[1]].push_back(make_pair(kSyms[2],index)); bmap[make_pair(kSyms[1],kSyms[2])].insert(index); } else { //one child //add index -> k umap[kSyms[1]].insert(index); //printf("UR : %u -> %u\n",index,kSyms[1]); } } else { //seen it index = fter->second; } return index; } else { //terminal string term = ""; while(ss.peek() != ')') { term += ss.get(); } // printf("got terminal %s\n",term.c_str()); ss.ignore(1); //burn closing paren unsigned int index = 0; if(term == "<>") { //nonterminal leaf index = sym2base[sym]; } else { //preterminal node //get index and add //printf("T:%s\n",term.c_str()); S2Setmap::iterator fter = pt2base.find(term); ptsyms.insert(symI); if(fter != pt2base.end()) {//seen this terminal before set<unsigned int>& bsymset = fter->second; //the base syms that have been seen to parse this terminal if(bsymset.find(symI) == bsymset.end()) { //never found this pterm rule before preterms[term].insert(goodmanIndex); index = goodmanIndex; baseSym.push_back(symI); ++goodmanIndex; canL.push_back(false); canR.push_back(false); bsymset.insert(symI); } else { //this is a small set...maybe an unideal implementation tho set<unsigned int>& symset = preterms[term]; for(set<unsigned int>::iterator iter = symset.begin();iter != symset.end();++iter) { if(baseSym[*iter] == symI) index = *iter; } } } else { preterms[term].insert(goodmanIndex); index = goodmanIndex; baseSym.push_back(symI); canL.push_back(false); canR.push_back(false); ++goodmanIndex; set<unsigned int> bsymset; bsymset.insert(symI); pt2base[term] = bsymset; } } // printf("returning %u - %u\n",index,goodmanIndex); return index; } } return 1; }