-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob.cpp
executable file
·200 lines (175 loc) · 8.03 KB
/
glob.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "glob.hpp"
#include <dirent.h>
#include <sys/stat.h> //stat(), S_ISDIR
#include <string.h> //strcmp()
#include <stdio.h> //std::sort
#include <vector> //std::sort
#include <algorithm> //std::sort
#include <functional> //std::sort
#include <stdexcept>
#include <stdio.h>
#include "path.hpp"
#include "../print/pdbg.hpp"
#include "../string/strmatch.hpp"
#include "../definitions/typeDef.h"
//-----------------------------------------------------------------------------------------------------------------------------------------------
std::vector<std::string> _glob_worker(const std::string& search_path_in, const char* wild_card, const bool opt_d, const bool opt_f, const bool opt_p, const bool opt_r){
DIR *pDir;
struct dirent *pEnt;
struct stat Stat;
std::vector<std::string> v_path;
std::vector<std::string> v_path_stack; // Useing STACK in order NOT to exceed the limit of opendir() number.
v_path_stack.push_back(search_path_in+'/');
for(;v_path_stack.size()!=0;){
std::string search_path = v_path_stack.back(); v_path_stack.pop_back();
pDir = opendir(search_path.c_str());
if(pDir==NULL){ continue; } // No files or directories.
for(pEnt=readdir(pDir); pEnt!=0; pEnt=readdir(pDir)){
// get file status
std::string path = search_path + pEnt->d_name;
if( stat( path.c_str(), &Stat ) ){
sstd::pdbg_err("glob_worker(): Failed to get stat %s\n", path.c_str());
break;
}
bool TF_match;
if(opt_p){ TF_match = sstd::pathmatch(path, wild_card);
} else { TF_match = sstd::strmatch (path, wild_card); }
if (S_ISDIR(Stat.st_mode)){
// when a directory
if((strcmp( pEnt->d_name, "." )!=0) && (strcmp( pEnt->d_name, ".." )!=0)){
// subdirectory
// printf( "subdirectory: %s\n", pEnt->d_name );
if(opt_d && TF_match){
v_path.push_back( path );
}
if(opt_r){
v_path_stack.push_back( path + '/' );
}
}else{
// . current directory
// .. parent directory
// printf( "current or parent directory: %s\n", pEnt->d_name );
// --- nothing to do ---
}
}else{
// when a file
if(opt_f && TF_match){
v_path.push_back( path );
}
}
}
closedir(pDir);
if(!opt_r){ break; }
}
return v_path;
}
std::vector<std::string> _glob_base(const char* path_wc, const bool opt_d, const bool opt_f, const bool opt_p, const bool opt_r){
std::string search_path = sstd::path2basePath_woWC(path_wc);
std::vector<std::string> v_path = _glob_worker(search_path.c_str(), path_wc, opt_d, opt_f, opt_p, opt_r);
std::sort(v_path.begin(), v_path.end());
return v_path;
}
std::vector<std::string> sstd::glob(const char* path, const char* opt){
bool opt_d=false;
bool opt_f=false;
bool opt_p=false;
bool opt_r=false;
for(uint i=0; opt[i]!='\0'; ++i){
switch(opt[i]){
case 'd': { opt_d=true; break; }
case 'f': { opt_f=true; break; }
case 'p': { opt_p=true; break; }
case 'r': { opt_r=true; break; }
default: { sstd::pdbg_err("glob(): Unexpected option.\n"); break; }
}
}
return _glob_base(path, opt_d, opt_f, opt_p, opt_r);
}
std::vector<std::string> sstd::glob(const std::string& path, const char* opt){ return sstd::glob(path.c_str(), opt); }
std::vector<std::string> sstd::glob(const char* path){ return sstd::glob(path, "f"); }
std::vector<std::string> sstd::glob(const std::string& path){ return sstd::glob(path.c_str(), "f"); }
//-----------------------------------------------------------------------------------------------------------------------------------------------
std::vector<struct sstd::pathAndType> _glob_worker_pt(const std::string& search_path_in, const char* wild_card, const bool opt_d, const bool opt_f, const bool opt_p, const bool opt_r){
DIR *pDir;
struct dirent *pEnt;
struct stat Stat;
std::vector<struct sstd::pathAndType> v_path;
std::vector<std::string> v_path_stack; // Useing STACK in order NOT to exceed the limit of opendir() number.
v_path_stack.push_back(search_path_in+'/');
for(;v_path_stack.size()!=0;){
std::string search_path = v_path_stack.back(); v_path_stack.pop_back();
pDir = opendir(search_path.c_str());
if(pDir==NULL){ continue; } // No files or directories.
for(pEnt=readdir(pDir); pEnt!=0; pEnt=readdir(pDir)){
// get file status
std::string path = search_path + pEnt->d_name;
if( stat( path.c_str(), &Stat ) ){
sstd::pdbg_err("glob_worker(): Failed to get stat %s\n", path.c_str());
break;
}
bool TF_match;
if(opt_p){ TF_match = sstd::pathmatch(path, wild_card);
} else { TF_match = sstd::strmatch (path, wild_card); }
if (S_ISDIR(Stat.st_mode)){
// when a directory
if((strcmp( pEnt->d_name, "." )!=0) && (strcmp( pEnt->d_name, ".." )!=0)){
// subdirectory
// printf( "subdirectory: %s\n", pEnt->d_name );
if(opt_d && TF_match){
struct sstd::pathAndType pt;
pt.type = 'd';
pt.st = Stat;
pt.path = path;
v_path.push_back( pt );
}
if(opt_r){
v_path_stack.push_back( path + '/' );
}
}else{
// . current directory
// .. parent directory
// printf( "current or parent directory: %s\n", pEnt->d_name );
// --- nothing to do ---
}
}else{
// when a file
if(opt_f && TF_match){
struct sstd::pathAndType pt;
pt.type = 'f';
pt.st = Stat;
pt.path = path;
v_path.push_back( pt );
}
}
}
closedir(pDir);
if(!opt_r){ break; }
}
return v_path;
}
std::vector<struct sstd::pathAndType> _glob_base_pt(const char* path_wc, const bool opt_d, const bool opt_f, const bool opt_p, const bool opt_r){
std::string search_path = sstd::path2basePath_woWC(path_wc);
std::vector<struct sstd::pathAndType> v_path = _glob_worker_pt(search_path.c_str(), path_wc, opt_d, opt_f, opt_p, opt_r);
std::sort(v_path.begin(), v_path.end());
return v_path;
}
std::vector<struct sstd::pathAndType> sstd::glob_pt(const char* path, const char* opt){
bool opt_d=false;
bool opt_f=false;
bool opt_p=false;
bool opt_r=false;
for(uint i=0; opt[i]!='\0'; ++i){
switch(opt[i]){
case 'd': { opt_d=true; break; }
case 'f': { opt_f=true; break; }
case 'p': { opt_p=true; break; }
case 'r': { opt_r=true; break; }
default: { sstd::pdbg_err("glob(): Unexpected option.\n"); break; }
}
}
return _glob_base_pt(path, opt_d, opt_f, opt_p, opt_r);
}
std::vector<struct sstd::pathAndType> sstd::glob_pt(const std::string& path, const char* opt){ return sstd::glob_pt(path.c_str(), opt); }
std::vector<struct sstd::pathAndType> sstd::glob_pt(const char* path){ return sstd::glob_pt(path, "f"); }
std::vector<struct sstd::pathAndType> sstd::glob_pt(const std::string& path){ return sstd::glob_pt(path.c_str(), "f"); }
//-----------------------------------------------------------------------------------------------------------------------------------------------