/////////////////////////////////////////////////////////////////////// // MT2Q4b.cpp - Asynchronous text finder // // // // Jim Fawcett, CSE687 - Object Oriented Design, Midterm Spring 2018 // /////////////////////////////////////////////////////////////////////// #include "../StringUtilities/StringUtilities.h" #include #include #include #include #include using FPtr = bool(*)(const std::string&, const std::string&); //----< synchronous text finder >-------------------------------------- bool findText(const std::string& text, const std::string& filePath) { std::ifstream in(filePath); if (!in.good()) { std::cout << "\n can't open \"" << filePath << "\""; return false; } std::string fileText; std::ostringstream out; out << in.rdbuf(); fileText = out.str(); //while (in.good()) //{ // char ch; // in >> ch; // fileText += ch; //} size_t pos = fileText.find(text); return pos < fileText.size(); } int main() { Utilities::Title("MT2Q4b - text finder"); std::string text = "main"; std::string fPath1 = "../Test/Copy_MT2Q4.h"; std::string fPath2 = "../Test/Copy_MT2Q4.cpp"; std::future f1 = std::async(std::launch::async, findText, text, fPath1); std::future f2 = std::async(std::launch::async, findText, text, fPath2); if (f1.get()) std::cout << "\n found \"" << text << "\" in file \"" << fPath1; else std::cout << "\n didn't find \"" << text << "\" in file \"" << fPath1; if (f2.get()) std::cout << "\n found \"" << text << "\" in file \"" << fPath2; else std::cout << "\n didn't find \"" << text << "\" in file \"" << fPath2; std::cout << "\n\n"; return 0; }