A3/InputProcessor.cpp

56 lines
1.2 KiB
C++
Raw Normal View History

2024-10-07 02:08:54 -06:00
#include "InputProcessor.h"
#include <fstream>
#include <iostream>
#include <vector>
InputProcessor::InputProcessor() {
_fileIn = std::ifstream();
_allWords = std::vector<std::string>();
}
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::cout << "Invalid file input";
return false;
}
_fileIn.open(file);
if (_fileIn.fail()) {
std::cout << "Unable to open file, does it exist?" << std::endl;
return false;
}
return true;
}
void InputProcessor::closeStream() { _fileIn.close(); }
void InputProcessor::read() {
std::string characterBuffer = "";
char currentChar;
while (_fileIn.get(currentChar)) {
switch (currentChar) {
case ' ':
case '\n':
_allWords.push_back(characterBuffer);
characterBuffer.clear();
break;
default:
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<std::string> InputProcessor::getAllWords() { return _allWords; }