-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir_common.cpp
More file actions
47 lines (41 loc) · 1.09 KB
/
dir_common.cpp
File metadata and controls
47 lines (41 loc) · 1.09 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
#include <dirent.h>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <vector>
namespace dir_common {
// Function to check if the path is a directory
bool is_directory(const std::string &path) {
struct stat statbuf;
if (stat(path.c_str(), &statbuf) != 0) {
return false;
}
return S_ISDIR(statbuf.st_mode);
}
void traverse_directory(const std::string &dir_path,
std::vector<std::string> &file_list) {
if (dir_path.empty()) {
return;
}
DIR *dir = opendir(dir_path.c_str());
if (dir == nullptr) {
std::cerr << "The path specified is not a valid directory: " << dir_path
<< std::endl;
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != nullptr) {
std::string entry_name = entry->d_name;
if (entry_name == "." || entry_name == "..") {
continue;
}
std::string full_path = dir_path + "/" + entry_name;
if (is_directory(full_path)) {
traverse_directory(full_path, file_list);
} else {
file_list.push_back(full_path);
}
}
closedir(dir);
}
} // namespace dir_common