#include "InputProcessor.h" #include #include #include InputProcessor::InputProcessor() { _fileIn = std::ifstream(); _allWords = std::vector(); } bool InputProcessor::openStream() { std::string file; std::cout << "What is the name of the file you would like to read? "; std::cin >> file; if (std::cin.fail()) { std::cerr << "Invalid file input" << std::endl; return false; } _fileIn.open(file); if (_fileIn.fail()) { std::cerr << "Unable to open file, does it exist?" << std::endl; return false; } return true; } void InputProcessor::closeStream() { _fileIn.close(); } void InputProcessor::read() { // Loop over every character of the file, adding it to the buffer and // flushing that buffer to _allWords if a separator is found std::string characterBuffer = ""; char currentChar; while (_fileIn.get(currentChar)) { switch (currentChar) { case ' ': case '\n': case '\r': if (!characterBuffer.empty()) { _allWords.push_back(characterBuffer); characterBuffer.clear(); } break; default: // Normalize to uppercase if (currentChar >= 'a' && currentChar <= 'z') { currentChar -= 32; } characterBuffer += currentChar; break; } } // Flush the rest of the buffer if the file doesn't end with a space or // newline if (!characterBuffer.empty()) { _allWords.push_back(characterBuffer); } } std::vector InputProcessor::getAllWords() const { return _allWords; }