-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch_answer.cpp
156 lines (124 loc) · 3.66 KB
/
search_answer.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
#include "search_answer.h"
#include "helping_funs.h"
search_answer::search_answer(unsigned int dir_index, unsigned int text_file_index, char * text_file_path, char * text_file_name)
{
lines_size = 0;
lines = NULL;
lines_indexes = NULL;
this->dir_index = dir_index;
this->text_file_index = text_file_index;
this->text_file_name = text_file_name;
this->text_file_path = text_file_path;
}
search_answer::~search_answer()
{
free(lines);
free(lines_indexes);
}
void search_answer::print()
{
cout<<"Text file name: '"<<text_file_name<<"'"<<endl;
cout<<"Text file's path: '"<<text_file_path<<"'"<<endl;
cout<<dir_index<<"_"<<text_file_index<<endl;
cout<<"-->Printing text file's lines<--"<<endl;
for(unsigned int i = 0; i < lines_size; i++)
{
cout<<"line index:'"<<lines_indexes[i]<<" - '"<<lines[i]<<"'"<<endl;
}
cout<<"------------------------------------------------"<<endl;
}
bool search_answer::line_exists(unsigned int line_index)
{
for(unsigned int i = 0; i < lines_size; i++)
{
if(lines_indexes[i] == line_index)
{
return 1;
}
}
return 0;
}
unsigned int search_answer::get_n_lines()
{
return lines_size;
}
unsigned int search_answer::get_line_id(unsigned int line_index)
{
return lines_indexes[line_index];
}
char * search_answer::get_line(unsigned int line_index)
{
return lines[line_index];
}
char * search_answer::get_full_file_name_path()
{
return make_full_file_path(text_file_path, text_file_name);
}
bool search_answer::cmp_doc_id(unsigned int dir_index, unsigned text_file_index)
{
if((dir_index == this->dir_index) && (text_file_index == this->text_file_index))
{
return 1;
}
else
{
return 0;
}
}
void search_answer::add_line(unsigned int line_index, char * line)
{
lines_size++;
lines_indexes = (unsigned int*) realloc(lines_indexes, lines_size * sizeof(unsigned int));
lines = (char **) realloc(lines, lines_size * sizeof(char *));
lines_indexes[lines_size - 1] = line_index;
lines[lines_size - 1] = line;
}
void search_answer::update(unsigned int line_index, char * line)
{
if(line_exists(line_index) == 0)
{
add_line(line_index, line);
}
}
search_answer ** update_search_answers(search_answer ** sa_array, unsigned int & sa_array_size,
unsigned int dir_index, unsigned int text_file_index, unsigned int line_index, char * line,
char * text_file_path, char * text_file_name)
{
for(unsigned int i = 0; i < sa_array_size; i++)
{
if(sa_array[i]->cmp_doc_id(dir_index, text_file_index) == 1)
{
sa_array[i]->update(line_index, line);
return sa_array;
}
}
sa_array_size++;
sa_array = (search_answer **) realloc(sa_array, sa_array_size * sizeof(search_answer *));
sa_array[sa_array_size - 1] = new search_answer(dir_index, text_file_index, text_file_path, text_file_name);
sa_array[sa_array_size - 1]->update(line_index, line);
return sa_array;
}
void print_search_answers(search_answer ** sa_array, unsigned int sa_array_size)
{
for(unsigned int i = 0; i < sa_array_size; i++)
{
sa_array[i]->print();
}
}
void delete_search_answers(search_answer ** sa_array, unsigned int sa_array_size)
{
for(unsigned int i = 0; i < sa_array_size; i++)
{
delete(sa_array[i]);
}
free(sa_array);
}
unsigned int get_total_n_lines(search_answer ** sa_array, unsigned int sa_array_size)
{
unsigned int total_n_lines = 0;
for(unsigned int i = 0; i < sa_array_size; i++)
{
total_n_lines += sa_array[i]->get_n_lines();
}
return total_n_lines;
}