///////////////////////////////////////////////////////////////////////////////////////// // FileMgr.cpp - Find all files on specified path or tree matching specified patterns // // // // Application: Project #1, CSE687 - Object Oriented Design, Spring 2014 // // Platform: Dell XPS 2720, Win 8.1, Visual Studio 2013 // // Author: Jim Fawcett, CST 4-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////////////////////////// #include "FileMgr.h" #include "FileSystem.h" //#include "../DataStore/DataStore.h" #define STATIC_LIB #include "../Utilities/Utilities.h" #include #include #include #include #include #include FileMgr::FileMgr(const Path& path, DataStore& ds) : path_(path), store_(ds) { patterns_.push_back("*.*"); srand(time(NULL)); } bool FileMgr::changePath(const Path& path) { if (FileSystem::Directory::exists(path)) { path_ = path; return true; } return false; } void FileMgr::addPattern(const std::string& patt) { if (patterns_.size() == 1 && patterns_[0] == "*.*") patterns_.pop_back(); patterns_.push_back(patt); } std::string FileMgr::reformatDate(const Date& date) { size_t spcPos = date.find_first_of(" "); std::string timeStr = date.substr(spcPos + 1); std::string dateStr = date.substr(0, spcPos); size_t sepPos = dateStr.find_last_of("/"); std::string year = dateStr.substr(sepPos + 1); std::string remd = dateStr.substr(0, sepPos); sepPos = remd.find_last_of("/"); std::string day = remd.substr(sepPos + 1); std::string month = remd.substr(0, sepPos); std::string refDate = year + "/" + month + "/" + day; refDate += " " + timeStr; return refDate; } std::string FileMgr::randomString() { int rval = rand(); std::ostringstream out; out << rval; return (" @" + out.str()); } std::string FileMgr::keyFromDate(const Date& date) { return date + " @" + randomString(); } std::string FileMgr::dateFromKey(const std::string& key) { size_t sentinalPos = key.find(" @"); return key.substr(0, sentinalPos); } void FileMgr::search(bool recurse) { std::string fullPath = FileSystem::Path::getFullFileSpec(path_); if (recurse) find(fullPath); else { for (auto patt : patterns_) { std::vector files = FileSystem::Directory::getFiles(fullPath, patt); for (auto file : files) { std::string fileSpec = fullPath + "\\" + file; FileSystem::FileInfo fi(fileSpec); Date date = fi.date(); store_[keyFromDate(reformatDate(date))] = fileSpec; } } } } void FileMgr::find(const Path& path) { //std::cout << "\n-- finding on path " << path; for (auto patt : patterns_) { std::vector files = FileSystem::Directory::getFiles(path, patt); for (auto f : files) { //std::cout << "\n -- saving file: " << f << ", on path: " << path; std::string fileSpec = path + "\\" + f; FileSystem::FileInfo fi(fileSpec); std::string date = fi.date(); store_[keyFromDate(reformatDate(date))] = fileSpec; } } std::vector subdirs = FileSystem::Directory::getDirectories(path); for (auto d : subdirs) { if (d != "." && d != "..") { //std::cout << "\n -- found dir: " << d; find(path + "\\" + d); } } } void FileMgr::display() { for (auto item : store_) { std::cout << "\n " << std::setw(14) << dateFromKey(item.first) << " " << item.second; } std::cout << "\n\n"; } #ifdef TEST_FILEMGR #include #include #include #include "../Utilities/Utilities.h" using Date = std::string; using Path = std::string; using File = std::string; using DataStore = std::map>; int main() { title("Testing FileMgr", '='); std::cout << "\n"; DataStore ds; FileMgr fm("..", ds); title("Non-recursive search"); fm.search(); for (auto fs : ds) { std::string file = fs.first; std::cout << "\n " << file; std::cout << " - " << fs.second; } title("Recursive search"); fm.search(true); for (auto fs : ds) { std::string date = fs.first; std::cout << "\n " << date; std::cout << " - " << fs.second; } std::cout << "\n\n"; return 0; } #endif