-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (112 loc) · 3.16 KB
/
main.cpp
File metadata and controls
127 lines (112 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "HashTable.hpp"
#include "LinkedList.hpp"
#include "Node.hpp"
/******************************
M ROGOVE
2019-08-04
Please see specification document for further detail
main.cpp
checks and reads in given file, allows user to interact.
*******************************/
/***********************
helper functions for main()
***********************/
/*
//ingestFile
given a filename and a declared HashTable, goes to town ingesting
lines and adding to our HashTable. HashTable handles all resizing.
*/
bool ingestFile(HashTable* table, std::string filename){
std::ifstream inputfile;
std::string inputstring; //declare str
// std::cout << filename << std::endl;
inputfile.open(filename);
if(inputfile.fail()){//if file opening failed
std::cout << "Error" << std::endl;
return false;
}
//let's get one line:
else if(inputfile.is_open()){
while(std::getline(inputfile,inputstring)){
std::istringstream is(inputstring);
std::string word;
int freq;
is >> word >> freq;
//TODO: comment this out
// std::cout << "word is: "<<word<<" freq is "<<freq<<std::endl;
table->add(table->convert(word,freq));
// std::cout << table->toString()<<std::endl;
}
inputfile.close();
}
return true;
}
//getcmd
void getCmd(HashTable* table, std::string word){
Node* node = table->getData(word);
if(node==nullptr){std::cout<<"Not found"<<std::endl;}
else{std::cout<<node->toString()<<std::endl;}
}
//putcmd
void putCmd(HashTable* table, std::string newWord, int newFreq){
//check input
if(newFreq<0){
std::cout<<"Invalid"<<std::endl;
}
else{
table->add(table->convert(newWord,newFreq));
std::cout<<"Added"<<std::endl;
}
}
//removecmd
void removeCmd(HashTable* table, std::string removalWord){
if(table->removeWord(removalWord)){std::cout<<"Deleted"<<std::endl;}
else{std::cout<<"Not found"<<std::endl;}
}
/**********************
Main itself
**********************/
int main(int argc, char* argv[]){
if(argc != 2){
std::cout << "Error" << std::endl;}
else{//variable prep
std::string filename = argv[1]; //filename from input
HashTable* table = new HashTable();
if(ingestFile(table,filename)==false){return 0;}
//now, start loop for input.
std::string userCmd = "";
//TODO: extract these as functions?
while(userCmd!=":q"){
std::cin >> userCmd;
if(userCmd == ":g"){
std::string searchWord = "";
std::cin >> searchWord;
getCmd(table, searchWord);
}
else if(userCmd == ":p"){
std::string newWord;
int newFreq;
std::cin >> newWord;
std::cin >> newFreq;
putCmd(table, newWord, newFreq);
}
else if(userCmd == ":r"){
std::string removalWord;
std::cin >> removalWord;
removeCmd(table, removalWord);
}
//need a handler for whitelist. not a great approach :/
else if(userCmd != ":g" && userCmd != ":q"
&& userCmd != ":p" && userCmd != ":r"){
// std::cout<<"I am acting on \'"<<userCmd<<'\''<<std::endl;
std::cout<<"Unknown command"<<std::endl;
}
}
delete table;
}
return 0;
}