#ifndef SEARCHFILE_H #define SEARCHFILE_H /////////////////////////////////////////////////////////////////////////// // // // searchFile.h - search a file for criteria determined // // ver 1.0 by template argument // // // // Language: Visual C++, ver 6.0 // // Platform: Dell XPS B1000r, Windows 2000 // // Application: CSE687 Demonstration for Proj#3, Sp2001 // // Author: Jim Fawcett, CST 2-187, (315) 443-3948 // // jfawcett@twcny.rr.com // // // /////////////////////////////////////////////////////////////////////////// /* Class Operations: ----------------------------- The searchFile class is responsible for searching a file for lines that meet some criteria, deterimined by the match(string&) function of the criteria class. searchFile.search() examines each line in its input stream to see if it matches the criteria provided by the criteria class. If so, it calls the found function on every derived class pointer passed to it by those wanting to observe the events. Interested clients can register to be notified by calling one of the functions: searchFile::registerFound(eventsProc*) and searchFile::registerDone(eventsProc*). The eventsProc class is a base class providing an events protocol. Whenever a found or done event occurs in searchFile::search() the found(...) or done() functions are called. Usually, the application responding to events will need to derive from this class to define what should happen when an event occurs. eventsProc class Public Interface: ------------------------------ eventsProc myEvents; // create an eventsProc object myEvents.found(foundInString); // send a found notification myEvents.done(); // send a done notification criteria class Public Interface: -------------------------------- criteria crit; // create a criteria object bool b = crit.match(line); // does line match the criteria? cout << crit.line(); // display the matching line searchFile class Operations: ---------------------------- ifstream in("file.txt"); // open a stream searchFile(&in) srh; // create a searchFile object // with search criteria argument void srh.registerFound(&myEvents);// ask to be notified of found event void srh.registerDone(&myEvents); // ask to be notified of done event void srh.setStream(&in); // set a new stream void srh.search(); // start the search process */ // /////////////////////////////////////////////////////////////////////////// // maintenance page // /////////////////////////////////////////////////////////////////////////// // Build Process // // // // Files Required: // // searchFile.h, searchFile.cpp // // // // Building with Visual C++ , ver 6.0, from command line: // // cl -GX -DTEST_SEARCHFILE searchFile.cpp // // // /////////////////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 1.0 : 03 Apr 01 - first release Planned Modifications: ====================== - These classes would be more useful if they used tokenizing and semi-expression analysis, rather than reading files line by line. */ // #include #include #include /////////////////////////////////////////////////////////////////////////// // base eventsProc class // // defines default processing for events: // // - found(string &s) writes the string to cout // // - done() writes completion notice to cout // /////////////////////////////////////////////////////////////////////////// class eventsProc { public: virtual void found(const std::string &s); virtual void done(); }; /////////////////////////////////////////////////////////////////////////// // base matchCriteria class // // defines criteria for a file line to match // // - match(const string &s) determines if line matches criteria // // - line( ) returns matching line // /////////////////////////////////////////////////////////////////////////// class matchCriteria { public: virtual ~matchCriteria() { } virtual bool match(const std::string &fileLine); virtual std::string line( ); protected: std::string saveLine; }; //----< default matches all lines >---------------------------------------- inline bool matchCriteria::match(const std::string &fileLine) { saveLine = fileLine; return true; } //----< returns last saved line >------------------------------------------ inline std::string matchCriteria::line() { return saveLine; } // /////////////////////////////////////////////////////////////////////////// // notifier class // // - searches a file and invokes event processing // // whenever it has a matching event or done event. // // - Matching events are defined by the helper class // // used as a template argument. // /////////////////////////////////////////////////////////////////////////// template class searchFile { public: searchFile(std::istream *pStream = NULL); void search(); void registerFound(eventsProc *pEventsProc); void registerDone(eventsProc *pEventsProc); void setStream(std::istream *pStream); private: someCriteria criteria; std::vector foundNotify; std::vector doneNotify; std::istream *inStream; void doFound(const std::string &s); void doDone(); }; //----< construct searchFile object with stream >-------------------------- template searchFile::searchFile(std::istream *pStream) : inStream(pStream) { } // //----< request notification of a match event >---------------------------- template void searchFile::registerFound(eventsProc *pEventsProc) { foundNotify.push_back(pEventsProc); } //----< request notification of search completion >------------------------ template void searchFile::registerDone(eventsProc *pEventsProc) { doneNotify.push_back(pEventsProc); } //----< private helper function calls everyone on vector >----------------- template void searchFile::doFound(const std::string &foundString) { vector::const_iterator it; for(it = foundNotify.begin(); it != foundNotify.end(); it++) (*it)->found(foundString); } //----< private helper function calls everyone on vector >----------------- template void searchFile::doDone() { vector::const_iterator it; for(it = doneNotify.begin(); it != doneNotify.end(); it++) (*it)->done(); } //----< provides searchFile object with new stream >----------------------- template void searchFile::setStream(std::istream *pStream) { inStream = pStream; } //----< searches file for match defined by matchCriteria::match(...) >----- template void searchFile::search() { std::string line; if(inStream == NULL) throw exception("no input stream provided"); while(inStream->good()) { getline(*inStream,line); if(criteria.match(line)) doFound(criteria.line()); } doDone(); } #endif