-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-phrases
185 lines (166 loc) · 5.26 KB
/
search-phrases
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Title: search_phrases.cpp
* By: Daniel Westrich, Fall 2016
* Purpose: To iterate through the FSTree
*/
#include <iostream>
#include <vector>
#include <stack>
#include <fstream>
#include <string>
#include <set>
#include "runProgram.h"
using namespace std;
/* Constructor */
Gerp::Gerp(string root, string sensitivity)
{
numFiles = 0;
FSTree mainTree(root);
build(mainTree.getRoot(), mainTree, sensitivity);
}
/* Destructor */
Gerp::~Gerp()
{
for (size_t i = 0; i < fileVec.size(); i++) {
delete fileVec[i];
}
}
/* Name: build()
* Arguments: A DirNode pointer, An FSTree address
* Returns: None
*
* Note:
* This function iterates through the FSTree, calling helper functions
* to open each file and hashing its contents.
*/
void Gerp::build(DirNode *node, FSTree &tree, string sensitivity)
{
if (node->isEmpty()) {
return;
}
if (node->hasFiles()) {
for (int i = 0; i < node->numFiles(); i++) {
string path = getPath(node, tree);
path += node->getFile(i);
HashTable *newHashTable = new HashTable(path);
fileVec.push_back(newHashTable);
read_from_file(path, newHashTable, sensitivity);
numFiles++;
}
}
if (node->hasSubDir()) {
for (int i = 0; i < node->numSubDirs(); i++) {
build(node->getSubDir(i), tree, sensitivity);
}
}
}
/* Name: getPath()
* Arguments: A DirNode pointer, An FSTree address
* Returns: A string variable
*
* Note:
* This function returns the path of a given file. It does this by
* iterating through each parent directory of the file, and pushing
* the name of each parent onto the vector. However, at this point
* the path is in reverse order, and so it is printed in opposite order.
*/
string Gerp::getPath(DirNode *node, FSTree &tree)
{
string filePath;
// push directory names onto stack, then print in reverse order
stack<string> pathStack;
filePath += tree.getRoot()->getName(); // Push root name
while (node != tree.getRoot()) { // push directories onto stack
pathStack.push(node->getName());
pathStack.push("/");
node = node->getParent();
}
int stackSize = pathStack.size();
for (int i = 0; i < stackSize; i++) { // Print items on stack
filePath += pathStack.top();
pathStack.pop();
}
filePath += "/";
return filePath;
}
/* Name: search()
* Arguments: A string variable
* Returns: None
*
* Note:
* This function is accessed by main.cpp and is used to search for lines
* where a given key exists.
*/
void Gerp::search(string key)
{
bool found = false;
for (int i = 0; i < numFiles; i++) {
if (fileVec[i]->search(key)) {
fileVec[i]->printResults(key);
found = true;
}
}
if (not found) {
cout << key << " Not Found. Try with @insensitive"
<< " or @i." << endl;
}
}
bool Gerp::checkDuplicate(string word, vector<string> wordVec)
{
bool duplicate = false;
for (size_t i = 0; i < wordVec.size(); i++) {
if (wordVec[i] == word) {
duplicate = true;
break;
}
}
return duplicate;
}
/* Name: read_from_file()
* Arguments: A string variable, A HashTable pointer
* Returns: None
*
* Note:
* This function reads in and parses each file individually. It
* accesses an instance of the HashTable class itself, rather than
* returning anything.
*/
void Gerp::read_from_file(string filename, HashTable *HashT, string sensitivity)
{
ifstream file;
file.open(filename);
if (not file.is_open()) {
cerr << "Cannot open file" << endl;
exit(1);
} else {
int lineNum = 1;
string line;
string word;
set<string> lineSet;
set<string>::iterator iter;
vector<string> lineVec;
while (getline(file, line)) {
for (size_t i = 0; i < line.size(); i++) {
if (not isspace(line[i])) {
if(sensitivity == "insensitive"){
line[i] = tolower(line[i]);
}
word += line[i];
}
if ((isspace(line[i]) or i == line.size()-1)
and (i > 0 and not isspace(line[i-1]))) {
lineVec.push_back(word);
lineSet.insert(word);
word.erase();
}
}
for (iter = lineSet.begin(); iter != lineSet.end();
iter++) {
HashT->addString(*iter, lineNum, lineVec);
}
lineVec.clear();
lineSet.clear();
lineNum++;
}
file.close();
}
}