-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecursive_dir_example.cc
52 lines (47 loc) · 1.13 KB
/
recursive_dir_example.cc
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
#include <sys/types.h>
#include <dirent.h>
#include<string>
#include<iostream>
#include"generators.h"
using namespace std;
#define MAX_PATH 4096
struct Ls : GeneratorHeart<dirent*> {
DIR* dir;
string dirname; // Just so that we can report it in the debugging info
void run(string _dirname) {
dirname = _dirname;
dir = opendir(dirname.c_str());
while(dirent* file = readdir(dir)) {
yield(file);
}
}
// We need a desctructor here because DIRs are a c-style concept
~Ls(){
closedir(dir);
cout << "closing " << dirname << endl; // So you can see it works
}
};
struct LsDashR : GeneratorHeart<string> {
void run (string dirname) {
for (dirent* file : Gen<Ls>(dirname)) {
if (file->d_name[0] == '.') {
continue;
}
string fullpath = dirname + "/" + file->d_name;
if (file->d_type == DT_DIR) {
run(fullpath);
} else {
yield(fullpath);
}
}
}
// We don't need a destructor because everything is RAII
};
main() {
for (string fn : Gen<LsDashR>("/etc/X11")) {
cout << fn << endl;
if (fn == "/etc/X11/xinit/xinput.d/all_ALL") {
break;
}
}
}