-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_file.cpp
More file actions
109 lines (86 loc) · 2.29 KB
/
Copy pathsearch_file.cpp
File metadata and controls
109 lines (86 loc) · 2.29 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fstream>
#include <cmath>
#include <numeric>
#include <queue>
#include "search_file.h"
std::string
search::replaceOtherStr(std::string &replacedStr, std::string from, std::string to)
{
const unsigned int pos = replacedStr.find( from );
const int len = from.length();
if( pos == std::string::npos || from.empty() )
{
return replacedStr;
}
return replacedStr.replace( pos, len, to );
}
std::vector< std::string >
search::command_result( std::string command )
{
std::vector< std::string > result;
char word[256];
std::string instance;
FILE* fp = popen( command.c_str(), "r");
if( fp )
{
while( fgets( word, 256, fp ) != NULL )
{
instance = word;
replaceOtherStr( instance, "\n", "" );
result.push_back( instance );
}
}
return result;
}
std::vector< std::string >
search::search_file()
{
std::vector< std::string > result;
std::vector< std::string > instance;
std::queue< std::string > search;
std::string ls = "ls -F ";
std::string file_check = "| grep -v /";
std::string dir_check = " | grep /";
std::string home_path;
std::string current_path;
instance = command_result( "echo $HOME" );
if( instance.empty() )
{
return result;
}
home_path = instance.front() + "/Desktop/";
current_path = home_path;
bool finish = false;
while( !finish )
{
instance = command_result( ls + current_path + file_check );
for( int i = 0; i < instance.size(); i++ )
{
result.push_back( current_path + instance[i] );
}
instance = command_result( ls + current_path + dir_check );
//std::cout<< "----> " << current_path << "\n";
for( int i = 0; i < instance.size(); i++ )
{
if( i == 0 )
{
//std::cout<< "---> " << instance[i] <<"\n";
}
search.push( current_path + instance[i] );
}
if( search.empty() )
{
finish = true;
}
else
{
current_path = search.front();
search.pop();
}
}
return result;
}