-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
193 lines (168 loc) · 3.58 KB
/
main.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
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
186
187
188
189
190
191
192
193
#include <dirent.h>
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <boost/tokenizer.hpp>
using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::ofstream;
using std::ifstream;
using std::string;
void readFile(string *fileN, int size, string argu);
void readFileT(string file, string argu);
void makeIndex(string *fileN, int size);
void Indexed(string *words, int size, string file, int line);
int main(int argc, char** argv)
{
bool inde = false;
string tmp = "";
int count = 0;
string *files = new string[count];
DIR *pdir = NULL;
pdir = opendir (".");
struct dirent *pent = NULL;
//Using current directory, this will find .txt files
while (pent = readdir (pdir))
{
if (pent == NULL)
{
return -1;
}else{
tmp = pent->d_name;
if(tmp == "index.log"){
inde = true;
}
if(tmp.size() > 4 && tmp.substr(tmp.size()-4,tmp.size()) == ".txt"){
string *tmp = new string[count+1];
for(int i = 0; i < count; i++){
tmp[i] = files[i];
}
delete [] files;
files = tmp;
files[count] = pent->d_name;
count++;
}
}
}
closedir (pdir);
string cmd = argv[1];
if(cmd == "--make-index"){
makeIndex(files,count);
}else{
if(inde == true){
for(int i = 1; i < argc; i++ ){
cout << "Keyword[" << i << "]" << endl;
readFileT("index.log",argv[i]);
}
}else{
for(int i = 1; i < argc; i++ ){
cout << "Keyword[" << i << "]" << endl;
readFile(files,count,argv[i]);
}
}
}
delete [] files;
return 0;
}
void makeIndex(string *fileN, int size){
string line = "";
int lineC = 0;
int count = 0;
string *wordS = new string[count];
for(int i = 0; i < size; i++){
ifstream myfile(fileN[i]);
while(getline(myfile,line)){
lineC++;
string *wordS = new string[count];
boost::tokenizer<> tokens(line);
for(string word : tokens){
string* tmp = new string[count + 1];
for(int x = 0; x < count; x++){
tmp[x] = wordS[x];
}
delete [] wordS;
wordS = tmp;
wordS[count] = word;
count++;
}
Indexed(wordS,count, fileN[i], lineC);
delete [] wordS;
count = 0;
}
myfile.close();
}
}
void Indexed(string *word,int size, string file, int line){
sort(word, word +size);
int count = 1;
ofstream outFile("index.log", std::ios::app);
for(int i = 0; i < size; i++){
if(word[i] == word[i+1]){
count++;
}else{
outFile << word[i] << ",Found in file "
<< file << " in line "
<< line << " (" << count << " time)\n";
count = 1;
}
}
outFile.close();
}
void readFile(string *fileN, int size, string argu){
string line = "";
int wordC = 0;
int lineC = 0;
int buf = 0;
for(int i = 0; i < size; i++){
ifstream myfile(fileN[i]);
wordC = 0;
lineC = 0;
while(getline(myfile,line)){
wordC = 0;
lineC++;
boost::tokenizer<> tokens(line);
for(string word: tokens){
if(word == argu){
wordC++;
}else{
buf++;
}
}
if(wordC > 0){
cout << "\tfound in file "<< fileN[i]
<< " in line " << lineC
<< " (" << wordC <<" time)" << endl;
}
}
myfile.close();
}
if(buf == size){
cout<< "\tNot found\n";
}
}
void readFileT(string file, string argu){
int count = 0;
string line = "";
ifstream myfile(file);
while(getline(myfile, line)){
boost::tokenizer<>tokens(line);
for(string word : tokens){
if(word == argu){
cout << "\t";
for(string wordT : tokens){
if(count > 1){
cout << wordT << " ";
}else{
count++;
}
}
cout << endl;
}
count = 0;
break;
}
}
}