/* * dict.cpp * * Created on: Oct 18, 2010 * Author: mike * for use in Yale course CPSC 427a, Fall 2010 */ #include "dict.hpp" #define BUFLEN 128 // Read dictionary from file, discarding words that // are too long or contain non-allowed characters void Dictionary::readDict(const char *fname) { char cword[BUFLEN]; ifstream in(fname); if (!in) { fatal("Can't open %s\n", fname); } for (;;) { in >> ws; in.getline(cword, BUFLEN); if (!in.good()) break; if (Word::isAllowed(cword)) add(cword); ; } } // Add a word to the dictionary, and return a pointer // to the WordEntry in which it is placed WordEntry* Dictionary::add(const Word& word) { int len = word.getLen(); return wl[len].add(word); } // Print the dictionary, in order of increasing length of words ostream& Dictionary::print(ostream& out) const { for (int k = 1; k <= Word::triangle_max; k++) { const WordList& wlk = wl[k]; WordList::iterator wp; for (wp=wlk.begin(); wp!=wlk.end(); wp++) { out << wp->getCword() << endl; } } return out; }