/************************************************************** * * * Entry: a file with something in it, preferably at some point in it * combinations of alpha characters separated by spaces on one or * both sides, and comprised of intelligible words. * * * Exit: wordSet is filled with unique and sorted strings * alpha only, lowercase, excluding ones greater than max length * * * Purpose: to parse a file into WORDS, for dictionary data set * * * ***************************************************************/ void ReadFromFile ( fstream &instream , set<string> &wordSet , const int MaxWordLength ) { string nextWord; while ( !instream.eof() ) { while ( !swansonString::IsALetter( (instream.peek()) ) ) { instream.ignore( 1 ); } string nextWord; getline( instream , nextWord , ' ' ); //check last character for alpha // for !.?" while ( !swansonString::IsALetter( nextWord.at( nextWord.size() - 1 ) ) ) { nextWord.erase( nextWord.size() - 1 , 1 ); // delete last character } if ( !nextWord.empty() && swansonString::AllLetters( nextWord ) ) { nextWord = swansonString::LowerCase( nextWord ); wordSet.insert( nextWord ); } } }
void ReadFromFile ( fstream &instream , set<string> &wordSet , const int MaxWordLength ) { string nextWord; int timein = time(NULL); while ( !instream.eof() && time(NULL)-timein<BAIL) { //x second bailout while ( !swansonString::IsALetter( (instream.peek()) ) ) { instream.ignore( 1 ); } string nextWord; getline( instream , nextWord , ' ' ); //check last character for alpha // for !.?" while ( !swansonString::IsALetter( nextWord.at( nextWord.size() - 1 ) ) ) { nextWord.erase( nextWord.size() - 1 , 1 ); // delete last character } if ( (!nextWord.empty() && swansonString::AllLetters( nextWord )) && (MaxWordLength == UNRESTRICTED || nextWord.size() < MaxWordLength) ) { nextWord = swansonString::LowerCase( nextWord ); wordSet.insert( nextWord ); } } //cout << "time out is " << time(NULL) << " file took " << time(NULL)-timein; }
// return nabber of filled objects int fillFromFile(fstream & fin, employee ** pArr, int nSize) { int id; int i = 0; while (i < nSize && fin.good() && fin.peek() != EOF) { fin >> id; switch(id) { case abstr_emp::EMPLOYEE: pArr[i] = new employee; break; case abstr_emp::MANAGER: pArr[i] = new manager; break; case abstr_emp::FINK: pArr[i] = new fink; break; case abstr_emp::HIGHFINK: pArr[i] = new highfink; break; default: throw "Error while reading file"; } fin.get(); // '\n' pArr[i]->GetAll(fin); i++; } fin.clear(); return i; }
/** * Read in the file by recursive descent. * Modify as necessary and add whatever functions you need to get things done. */ void parse(fstream & file, Container * container) { char c = '\0'; Loop* loop = nullptr; int multiples = 0; while (file >> c) { switch (c) { case '+': case '-': case '<': case '>': case ',': case '.': multiples = 1; while (file.peek() == c){ multiples++; file >> c; } container->children.push_back(new CommandNode(c, multiples)); break; case '[': loop = new Loop(); parse(file, loop); if (loop->children.size() == 1) { CommandNode*leaf = (CommandNode*)loop->children[0]; if (leaf->command == '+' || leaf->command == '-') { container->children.push_back(new CommandNode('0', 1)); break; } else { container->children.push_back(loop); break; } } container->children.push_back(loop); break; case ']': return; } } }
void buildTable(HashTable<Movie>& table, fstream& movies, fstream& output) { int MPAC; string title; while (movies.peek() != -1) { movies >> MPAC; getline(movies, title); Movie movie(MPAC, title); table.insert(movie); output << movie << " is being added" << endl; output << "\tThe hashed location is " << hash(movie) << endl; if (table.getChainSize(hash(movie)) > 1) { output << "\tThere was a collision loading " << movie << endl; output << "\tIt collided with " << table.getChainHead(hash(movie)) << endl; } else output << "\tThere was no collision loading " << movie << endl; output << LINE << endl; } }
/** * Read in the file by recursive descent. * Modify as necessary and add whatever functions you need to get things done. */ void parse(fstream & file, Container * container) { Loop * program; // Our loop object char c; int count; // How to insert a node into the container while (file >> c) { count = 1; // reset count //command case if(c == '+' || c == '-' || c == '<' || c == '>' || c == ',' || c == '.') { while(((char)file.peek()) == c) { // Squash down repeats, aka +++ -> Node with + and count = 3 file >> c; // move file pointer count++; // increase } container->children.push_back(new CommandNode(c,count)); //add our node to the tree } else if(c == '[') { // Loop case program = new Loop(); // Create new loop object parse(file, program); // Parse the inside of the loop if (program->children.size() == 1) { // If we have only one object inside the loop, check for special cases. CommandNode* child = dynamic_cast<CommandNode*>(program->children.front()); // if (child->command == INCREMENT || child->command == DECREMENT) { // If loop is either [+] or [-] container->children.push_back(new CommandNode('z',1)); // Add special ZERO node. delete program; // Avoid lingering loop objects } else { container->children.push_back(program); // Normal non-special loop with size one. } } else { container->children.push_back(program); //Add a normal non-special loop to the tree. } } else if (c == ']') { //end of loop object return; } }
bool isEmpty(fstream & f){ if(f.peek() == std::ifstream::traits_type::eof()) return true; return false; }