-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearchcommand.cpp
More file actions
137 lines (124 loc) · 3.27 KB
/
searchcommand.cpp
File metadata and controls
137 lines (124 loc) · 3.27 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
//**********************************************************************
// Header file Included
//**********************************************************************
#include "myheader.h"
vector<string> searchResult;
//**********************************************************************
// It match two file name are same or not
//**********************************************************************
int isNameMatch(string tobesearch, string name)
{
if (tobesearch == name)
return 1;
else
return 0;
}
//**********************************************************************
// This function used to print the search Data
//**********************************************************************
void printsearchData()
{
totalFiles = dirList.size();
unsigned int len = getFilePrintingcount();
sort(dirList.begin(), dirList.end());
wintrack = 0;
printf("\033[H\033[J");
printf("%c[%d;%dH", 27, 1, 1);
for (unsigned int i = 0, itr = 1; i < totalFiles && itr <= len; i++, itr++)
{
char *searchfullpath = new char[dirList[i].length() + 1];
strcpy(searchfullpath, dirList[i].c_str());
display(searchfullpath, root);
}
printf("%c[%d;%dH", 27, 1, 1);
}
//**********************************************************************
// It searches given filename into its recursive subfolder
// if filename found push its absolute path into searchResult vector
//**********************************************************************
void searchanything(char *path, string filename)
{
DIR *d;
struct dirent *dir;
d = opendir(path);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if ((string(dir->d_name) == "..") || (string(dir->d_name) == "."))
{
}
else
{
string name = string(dir->d_name);
string finalpath = string(path) + "/" + name;
char *newpath = new char[finalpath.length() + 1];
strcpy(newpath, finalpath.c_str());
struct stat sb;
if (stat(newpath, &sb) == -1)
{
perror("lstat");
}
else
{
if ((S_ISDIR(sb.st_mode)))
{
if (isNameMatch(filename, name))
{
searchResult.push_back(string(newpath));
}
searchanything(newpath, filename);
}
else
{
if (isNameMatch(filename, name))
{
searchResult.push_back(string(newpath));
}
}
}
}
}
}
else
{
// showError("No such Directory path Exist while searching !!!");
}
}
//**********************************************************************
// This function process search argument given by user & display results
//**********************************************************************
int searchcommand(vector<string> list)
{
searchResult.clear();
unsigned int len = list.size();
if (len != 2)
{
showError("Less number of Argument in search command !!!");
return 0;
}
else
{
string filename = list[1];
char *path = new char[strlen(curPath) + 1];
strcpy(path, curPath);
// cout<<"\npath : "<<path<<endl;
// cout<<"\nfilename : "<<filename<<endl;
searchanything(path, filename);
if (searchResult.size() < 1)
{
showError("No search result found for file/Dir ::::: " + filename);
return 0;
}
else
{
back_stack.push(string(curPath));
clearStack(forw_stack);
searchflag = 1;
dirList.clear();
dirList = searchResult;
printsearchData();
}
}
return 1;
}