/////////////////////////////////////////////////////////////// // // // catalog.cpp - demonstrate directory navidation and // // ver 1.3 matching wild cards // // // // Language: Visual C++, ver 5.0 // // Platform: Micron Dual Pentium 200, Windows NT 4.0 // // Application: CSE687 Project #1, S98 // // Author: Jim Fawcett, CST 2-187, (315) 443-3948 // // jfawcett@twcny.rr.com // // // /////////////////////////////////////////////////////////////// /* Module Operations: ================== This module is the catalog program executive. It uses the services of modules nav, fileinfo, and wildcards to search a directory subtree for a specific file pattern, store the results, using STL containers, and display a set of all the directories that have matches and their corresponding file matches. This program provides the same functionality as the DOS dir command, except that it does not show directories that have no matching files. */ /////////////////////////////////////////////////////////////////// // build process // /////////////////////////////////////////////////////////////////// // Required files: // // catalog.cpp, wildcards.h wildcards.cpp, // // nav.h, nav.cpp, fileInfo.h, fileInfo.cpp // // // // compiler command: // // cl /GX /GZ catalog.cpp wildcards.cpp nav.cpp fileInfo.cpp // /////////////////////////////////////////////////////////////////// /* Maintenance History: ==================== ver 1.3 : 09 Jan 2002 - fixed bug in wildcards module ver 1.2 : 17 Jun 2001 - cosmetic modifications to this page ver 1.1 : 21 Feb 2001 - cosmetic modifications to this page ver 1.0 : 30 Jan 2000 - first release */ // #pragma warning(disable : 4786) #include #include #include #include #include "nav.h" #include "wildcards.h" using namespace std; //----< convert string to lowercase >-------------------------- string lower(const string &s) { string temp; int i; for(i=0; i---- class smaller { public: bool operator()(const fileInfo &fi1, const fileInfo &fi2) { return (strcmp(lower(fi1.name()).c_str(),lower(fi2.name()).c_str()) == -1); } }; //----< define types for saving file and directory info >------ typedef set fileSet; typedef map dirMap; // //----< define navigator processing >-------------------------- // // - Store a map holding a name and set of fileInfo objects // for each directory encountered. // - The fileInfo set holds fileInfo objects for all files // matching a pattern in that directory. class userProc : public defProc { public: //----< create processing object with default mask >------- userProc() : mask("*.*") { } //----< store name of current directory >------------------ virtual void dirsProc(const string &dir) { currentDir = dir; } //----< save fileInfo objects and directory names >-------- virtual void fileProc(const fileInfo &fi) { wildcards wc(mask); if(!wc.matches(lower(fi.name()))) return; dirMap::iterator it = store.find(currentDir); if(it == store.end()) { fileSet fs; // create new fileInfo set fs.insert(fi); // insert fileInfo object store[currentDir] = fs; // insert currentDir and fs in map } else { ((*it).second).insert(fi); // insert fi in fileInfo set } } //----< set file name pattern >---------------------------- void filePattern(const string &pat) { mask = lower(pat); } //----< grant access to internal data store >-------------- dirMap& getStore() { return store; } private: dirMap store; string currentDir; string mask; }; // //----< display all the files that match a pattern >----------- // // - Starting from a specified path, find all files // matching a pattern on the subtree rooted at that path. // - The path is specified by argv[1] and pattern by argv[2] // void main(int argc, char *argv[]) { cout << "\n Demonstrating Navigation and Wildcards " << "\n ========================================\n"; if(argc == 1) { cout << "\n please enter path of start directory\n" << "\n you may then enter an optional pattern\n"; return; } //----< create processing object and start looking >--------- userProc udp; if(argc > 2) udp.filePattern(argv[2]); navig nav(udp); nav.start(argv[1]); //----< searching is done so show what is in store >--------- dirMap dl = udp.getStore(); dirMap::iterator it; for(it = dl.begin(); it != dl.end(); it++) { // loop thru map keys cout << "\n " << lower((*it).first); // display directory fileSet fs = (*it).second; fileSet::iterator fit; for(fit = fs.begin(); fit != fs.end(); fit++) { // loop thru map values cout << "\n " << lower((*fit).name()); // display file } cout << '\n'; } cout << "\n\n"; }