///////////////////////////////////////////////////////////////// // // // searchFile.cpp - 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 // // // ///////////////////////////////////////////////////////////////// #include "searchFile.h" using namespace std; //----< default processing for found event >------------------------------- inline void eventsProc::found(const std::string &s) { cout << "\n found: " << s.c_str(); } //----< default processing for done event >-------------------------------- void eventsProc::done() { cout << "\n finished processing\n"; } //----< test stub >------------------------------------------------ #ifdef TEST_SEARCHFILE #include using namespace std; ///////////////////////////////////////////////////////////////// // derived criteria class to detect preprocessor statements // ///////////////////////////////////////////////////////////////// class isPreproc : public matchCriteria { public: bool match(string &line); }; bool isPreproc::match(string &line) { if(line.find("#") == 0) { saveLine = line; return true; } return false; } // ///////////////////////////////////////////////////////////////// // derived criteria class to find changes in C++ scope // ///////////////////////////////////////////////////////////////// class findScope : public matchCriteria { public: bool match(std::string &line); }; bool findScope::match(std::string &line) { if(line.find("{") < line.size()) { saveLine = line; return true; } else if(line.find("}") < line.size()) { saveLine = line; return true; } return false; } ///////////////////////////////////////////////////////////////// // derived events processing class // ///////////////////////////////////////////////////////////////// class scopeEvents : public eventsProc { public: void found(const std::string &s); }; void scopeEvents::found(const string &line) { cout << "\n analyzed line: " << line << "\n "; if(line.find("{") < line.size()) cout << " increasing scope"; if(line.find("}") < line.size()) cout << " decreasing scope"; } // ///////////////////////////////////////////////////////////////// // test searchFile class // ///////////////////////////////////////////////////////////////// void main(int argc, char *argv[]) { cout << "\n Testing searchFile Processing " << "\n ===============================\n"; if(argc < 2) { cout << "\n please cite a file to analyze on the command line\n\n"; return; } cout << "\n searching for preprocessor statements " << "\n ---------------------------------------"; ifstream in(argv[1]); if(in.good()) { // The eventsProc object determines what happens when an event occurs. // This base class provides default operations. eventsProc baseEvents; // The isPreproc class determines the citeria for matching a line. // In this case we look for '#' at the beginning of a line. searchFile test(&in); test.registerFound(&baseEvents); test.registerDone(&baseEvents); test.search(); } in.close(); in.clear(); cout << "\n"; // cout << "\n searching for change of scope statements " << "\n ------------------------------------------"; in.open(argv[1]); if(in.good()) { // The derivedEvents class redefines the default processing operations. // In this case it just uses different display formats. scopeEvents derivedEvents; // The findScope class determines a new criteria for matching a line. // In this case we must find either "{" or "}" in the line. searchFile testScope(&in); testScope.registerFound(&derivedEvents); testScope.registerDone(&derivedEvents); testScope.search(); } in.close(); in.clear(); cout << "\n"; cout << "\n test error handling " << "\n ---------------------\n"; try { searchFile errorTest; // default construction with no stream errorTest.search(); } catch(exception &e) { cout << "\n " << e.what(); } try { searchFile errorTest; // default construction with no stream in.open(argv[1]); errorTest.setStream(&in); eventsProc baseEvents; errorTest.registerDone(&baseEvents); errorTest.search(); } catch(exception &e) { cout << "\n " << e.what() << "\n"; } cout << "\n"; } #endif