-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordsearch.cpp
executable file
·147 lines (136 loc) · 3.21 KB
/
wordsearch.cpp
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
// Ryan Wiener, Porter Haet, 2/16/17
#include <vector>
#include <dirent.h>
#include <cctype>
#include "wordsearch.h"
using namespace std;
// Given a directory, return all the files in that directory
// if the directory does not exist -- report error.
int getdir (string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error opening " << dir << endl;
return 1;
}
while ((dirp = readdir(dp)) != NULL) {
if (string(dirp->d_name)[0] != '.')
{
files.push_back(string(dirp->d_name));
}
}
closedir(dp);
return 0;
}
int main(int argc, char **argv)
{
if (argc <= 1)
{
cerr << "No directory inputted.\nUsage: ./wordsearch directory ...\n";
exit(1);
}
string dir;
vector<string> filenames;
dir = string(argv[1]);
if (getdir(dir,filenames)!=0) {
cout << "Error opening " << dir << "; Exiting ..." << endl;
return(-2);
}
ifstream fin;
list<word> words; // list of words
string next;
string slash = "/";
for (int i = 0; i < filenames.size(); i++)
{
string absoluteFile =(string(argv[1])+slash+filenames[i]);
fin.open(absoluteFile.c_str());
if (fin.fail())
{
cerr << "Couldn't open file " << filenames[i] << endl;
exit(1);
}
while (fin >> next)
{
for (int j = 0; j < next.length(); j++)
{
// gets rid of punctuation and makes lowercase
if(ispunct(next[j]) || next[j] == '"')
{
next.erase(j, 1);
j--;
}
else
{
next[j] = tolower(next[j]);
}
}
cout << next << endl;
word newWord(next);
word* currentWord = words.getDataPointer(newWord); // gets pointer to word in list or returns NULL if not found
if (currentWord) // checks if word was found
{
list<file>* filesWithWord = currentWord->getFiles();
file newFile(filenames[i], 1);
file* fileWithWord = filesWithWord->getDataPointer(newFile); // gets pointer to word in list or returns NULL if not found
if (fileWithWord) // checks if file was found
{
fileWithWord->setCount(fileWithWord->getCount() + 1); // increments count by 1 in file
}
else
{
currentWord->getFiles()->pushfront(newFile); // push new file object
}
}
else
{
// create new word object and append to list of words
file newFile(filenames[i], 1);
newWord.getFiles()->pushfront(newFile);
words.pushfront(newWord);
}
}
fin.close();
}
string input;
cout << "Enter word: ";
cin >> input;
while (input != "exit")
{
for (int i = 0; i < input.length(); i++)
{
// gets rid of punctuation and makes lowercase
if(ispunct(input[i]) || next[i] == '"')
{
input.erase(i, 1);
i--;
}
else
{
input[i] = tolower(input[i]);
}
}
word search(input);
word* currentWord = words.getDataPointer(search);
if (currentWord) // checks to see if word was found
{
printResults(*currentWord);
}
else
{
cout << "No results found.\n";
}
cout << "Enter word: ";
cin >> input;
}
return 0;
}
void printResults(word current)
{
cout << "Filename Count" << endl;
list<file> files = *(current.getFiles());
for (int i = files.length() - 1; i >= 0; i--) {
file f = files.get(i);
cout << f.getFilename() << " " << f.getCount() << endl; // print out filename and count
}
}