-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordsearch.cpp
91 lines (81 loc) · 2.03 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
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "word.h"
#include "list.h"
#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(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL)
{
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
int main(int argc, char* argv[])
{
string dir;
vector<string> files = vector<string>();
Word* head = NULL;
if (argc < 2)
{
cout << "No Directory specified; Exiting ..." << endl;
return(-1);
}
dir = string(argv[1]);
if (getdir(dir,files)!=0)
{
cout << "Error opening " << dir << "; Exiting ..." << endl;
return(-2);
}
for (unsigned int i = 0; i < files.size(); i++) {
if(files[i][0]=='.') continue; //skip hidden files
cout << "reading " << files[i] << "...";
ifstream fin((string(argv[1])+ "/" +files[i]).c_str()); //open using absolute path
// ...read the file..
string word;
while(fin >> word)
insert_file(insert_word(head, word), files[i]);
fin.close();
cout << "done" << endl;
}
string keyword;
cout << "\nWelcome to The Dictionary\n";
while(true)
{
cout << "\nPlease enter a word: ";
cin >> keyword;
if (keyword == "exit") //skip process if keyword == exit
return 0;
Word* iterW = head;
while (iterW != NULL && iterW->getWord() != keyword) //search word
iterW = iterW->getNext();
if (iterW)
{
cout << "Keyword \"" + keyword + "\" found in: ";
File* iterF = iterW->getFilePtr();
while (iterF != NULL) //print files
{
cout << "[\"" << iterF->getFilename() << "\", " << iterF->getCount() << "] ";
iterF = iterF->getNext();
}
cout << endl;
}
}
return 0;
}