// SubStrHandler.h // This is the class declaration for the substring handler class. // Because this is a templated class, all of the source for the methods is // also located in this file. #ifndef SUBSTRHANDLER #define SUBSTRHANDLER #include "SSContainer.h" #include "SubStr.h" #include #include #include // This class is templated so that it can be used to work with any form of // SubStr. This puts certain restrictions on the SubStr classes that can be // used with this handler. See each method for the restrictions it imposses. template class SubStrHandler { public: // This method generates all of the substrings and writes them // out to file. For a SubStr class to be used with this it must // have static methods for maxToGen and generate. The maxToGen // method returns how many substrings of that type there are. The // generate method writes out the ith substring to the file. Note // that this implies some form of direct generation. static void generate() { FILE *fout=fopen("substrings.bin","wb"); for(int i=0; iadd(ss); ret++; ss=new SSClass(fin); } fclose(fin); return ret; } // This method reads a file of English words and for all the ones that // are the same length as the substrings it searches for them in the // provided container. If it is found, it marks it as English and // writes that one record back out to file. It required that the // SubStr class have a static method stringLength. static void markEnglish(SSContainer *cont) { FILE *fout=fopen("substrings.bin","rb+"); ifstream ifs("../english.txt"); char buf[250]; ifs.getline(buf,250); while(!ifs.eof()) { if(strlen(buf)==SSClass::stringLength()) { for(int i=0; i='A' && buf[i]<='Z') buf[i]=buf[i]-'A'+'a'; SSClass *ss=dynamic_cast(cont->search(buf)); if(ss!=0) { ss->setEnglish(); ss->writeRandom(fout); } else { cout << "Error: didn't find substring " << buf << "\n"; } } ifs.getline(buf,250); } fclose(fout); } private: // Nothing private. This is a utility class. }; #endif