///////////////////////////////////////////////////////////////////// // fileStor.cpp - Store and retrieve files and their associated // // ver 1.0 paths. Store each unique file and path only // // once. // // Language: Visual C++ ver 6.0 // // Platform: Micron dual pentium pro 200, Windows NT 4.0 // // Application: CRI_OOD, S'99, Project #2 example. // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, fawcett@ecs.syr.edu // ///////////////////////////////////////////////////////////////////// #pragma warning(disable : 4786) #include #include "fileStor.h" using namespace std; //----< add a file and its associated path >------------------------- fileStor& fileStor::add(const string &file, const string &path) { // Insert path into pathSet if not already there. // In any case, return an iterator to it. pathSet::iterator itr = _paths.insert(path).first; if(_files.find(file) == _files.end()) { // This file name not encountered before so // make a new list of pathSet iterators and // push the current path's iterator onto it. itrList paths; paths.push_back(itr); // Store the file and list of path iterators // into the fileMap. _files[file] = paths; } else { // This file name is already stored so just // just append the path iterator to its // iterator list. _files[file].push_back(itr); } return *this; } // //----< display files and their associated paths >------------------- void fileStor::display() { // Iterate through the file map showing each file // and its associated list of paths fileMap::iterator it; for(it = _files.begin(); it != _files.end(); it++) { // show the fileMap's file key: cout << "\n " << (*it).first << "\n"; // for that key show each of the paths on its // associated list of path iterators itrList paths = (*it).second; itrList::iterator lit; // Walk through the iterator list showing each // path pointed to by the current iterator. // Remember that lit is an iterator on a list of // iterators - very like a pointer to a pointer. for(lit = paths.begin(); lit != paths.end(); lit++) cout << " " << *(*lit) << '\n'; } cout << endl; } //----< display the complete set of files >-------------------------- void fileStor::showFiles() { cout << "files:\n"; fileMap::iterator it; for(it = _files.begin(); it != _files.end(); it++) cout << "\n " << (*it).first; cout << "\n\n"; } //----< display the complete set of paths >-------------------------- void fileStor::showPaths() { cout << "paths:\n"; pathSet::iterator it; for(it = _paths.begin(); it != _paths.end(); it++) cout << "\n " << *it; cout << "\n\n"; } // //----< test stub >-------------------------------------------------- #ifdef TEST_FILESTOR void main() { fileStor fs; fs.add("foo","path1").add("foo","path2").add("foo","path3"); fs.add("bar","path1").add("bar","path3").add("bar","path4").add("bar","path5"); cout << "\nThere are " << fs.files() << " files\n"; fs.showFiles(); cout << "\nThere are " << fs.paths() << " paths\n"; fs.showPaths(); fs.display(); } #endif