-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir.cpp
110 lines (82 loc) · 2.34 KB
/
dir.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
#include "dir.h"
#include "helping_funs.h"
dir::dir(char * dir_path, my_trie * my_trie_ptr, unsigned int dir_index)
{
this->path = (char *)malloc((strlen(dir_path) + 1) * sizeof(char));
strcpy(this->path, dir_path);
DIR * fd = opendir(this->path);
struct dirent * in_dir;
n_files = 0;
text_files = NULL;
while ((in_dir = readdir(fd)))
{//find every text file contained in the dir
if (!strcmp (in_dir->d_name, "."))
continue;
if (!strcmp (in_dir->d_name, ".."))
continue;
text_files = (text_file **) realloc(text_files, (n_files + 1) * sizeof(text_file *));
text_files[n_files] = new text_file(this->path, in_dir->d_name, my_trie_ptr, dir_index, n_files);
n_files++;
}
closedir(fd);
}
dir::~dir()
{
free(path);
for(unsigned int i = 0; i < n_files; i++)
{
delete(text_files[i]);
}
free(text_files);
}
void dir::print(bool full_print_flag)
{
cout<<"===>Printing all directories' info<==="<<endl;
cout<<"Directory path: '"<<path<<"'"<<endl;
cout<<"Directory contains: "<<n_files<<" text files."<<endl;
if(full_print_flag)
{
cout<<"--->Printing directory's text files<---"<<endl;
for(unsigned int i = 0; i < n_files; i++)
{
text_files[i]->print(0);
}
}
cout<<"======================================="<<endl<<endl;
}
char * dir::get_text_file_line(unsigned int text_file_index, unsigned int line_index)
{
return text_files[text_file_index]->get_line(line_index);
}
char * dir::get_text_file_name(unsigned int text_file_index)
{
return text_files[text_file_index]->get_name();
}
unsigned int dir::get_text_files_n_words()
{
unsigned int total_words = 0;
for(unsigned int i = 0; i < n_files; i++)
{
total_words += text_files[i]->get_n_words();
}
return total_words;
}
unsigned int dir::get_text_files_n_lines()
{
unsigned int total_lines = 0;
for(unsigned int i = 0; i < n_files; i++)
{
total_lines += text_files[i]->get_n_lines();
}
return total_lines;
}
unsigned int dir::get_text_files_n_chars()
{
unsigned int total_chars = 0;
for(unsigned int i = 0; i < n_files; i++)
{
total_chars += text_files[i]->get_n_chars();
}
return total_chars;
}
char * dir::get_path() { return path; }