-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXyz_Database.cpp
More file actions
186 lines (165 loc) · 3.43 KB
/
Xyz_Database.cpp
File metadata and controls
186 lines (165 loc) · 3.43 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
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
#include"Xyz_Database.h"
#include"BST_of_Words.h"
#include<iostream>
#include<sstream>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
using namespace std;
/* A C++ program to implement itoa() */
#include <iostream>
using namespace std;
/* A utility function to reverse a string */
void reverse(char str[], int length)
{
int start = 0;
int end = length -1;
while (start < end)
{
swap(*(str+start), *(str+end));
start++;
end--;
}
}
// Implementation of itoa()
char* itoa(int num, char* str, int base)
{
int i = 0;
bool isNegative = false;
/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if (num < 0 && base == 10)
{
isNegative = true;
num = -num;
}
// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
// If number is negative, append '-'
if (isNegative)
str[i++] = '-';
str[i] = '\0'; // Append string terminator
// Reverse the string
reverse(str, i);
return str;
}
Xyz_Database::Xyz_Database()
{
ifstream fileList;
string nameOfFile;
fileList.open("file_list.txt");
if((!fileList.fail())&&(!fileList.eof()))
{
while(!fileList.eof())
{
fileList>>nameOfFile;
tree.addFile(nameOfFile);
}
fileList.close();
}
}
string Xyz_Database::transducer(std::string &command)
{
temp.clear();
int i;
string result="";
string name;
char buffer[4];
int lineNumberInt;
if(command.substr(0,8)=="ADD_FILE")
{
name=command.substr(9,9999);
if(fileListString!="")
fileListString+="\n";
fileListString+=name;
(*this).tree.addFile(name);
}
if(command.substr(0,9)=="FIND_WORD")
{
name=command.substr(10,9999);//everything after the instruction (the word to find for example)
i=0;
while(i<name.length())
{
name[i]=tolower(name[i]);
i++;
}
string test;
temp=tree.myFind(name);
j=temp.begin();
while(j!=temp.end())
{
result+=(*(*(*j)).lineWhereFound).lineText;
result+="\n";
result+=(*(*(*j)).lineWhereFound).filename;
result+=" [";
lineNumberInt=(*(*(*j)).lineWhereFound).lineNumber;
itoa(lineNumberInt,buffer,10);
result+=buffer;
result+="]\n\n";
j++;
}
}
if(command=="PRINT")
{
result=tree.print();
}
if(command=="CLEAR")
{
tree.clear(tree.pRoot);
}
if(command.substr(0,5)=="QUERY")
{
name=command.substr(6,999);
//First, put parentheses around every word.
for(i=0;i<name.length();i++)
{
char letter = (name.c_str())[i];
if(isalnum(letter))
{
name.insert(i,"(");
i++;
letter = (name.c_str())[i];
while(isalnum(letter))
{
i++;
letter = (name.c_str())[i];
}
name.insert(i,")");
}
}
lineTemp=tree.query(name);
k=lineTemp.begin();
while(k!=lineTemp.end())
{
result+=(*k)->lineText;
result+="\n";
result+=(*k)->filename;
result+=" [";
lineNumberInt=(*k)->lineNumber;
itoa(lineNumberInt,buffer,10);
result+=buffer;
result+="]\n\n";
k++;
}
}
return result;
}
Xyz_Database::~Xyz_Database()
{
ofstream fileList;
fileList.open("file_list.txt");
fileList<<fileListString;
fileList.close();
}