-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.cpp
60 lines (50 loc) · 1.21 KB
/
files.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
#include <sys/types.h>
#include <dirent.h>
#include <fstream>
#include "files.h"
std::vector<std::string> files_in(std::string dir, std::string suffix)
{
std::vector<std::string> ret;
DIR *dp;
dirent *dirp;
if ( (dp = opendir(dir.c_str())) == NULL )
return ret;
while ( (dirp = readdir(dp)) != NULL ) {
std::string filename(dirp->d_name);
if (suffix == "" || filename.find(suffix) != std::string::npos) {
ret.push_back( std::string(dirp->d_name) );
}
}
closedir(dp);
return ret;
}
std::vector<std::string> directories_in(std::string dir)
{
std::vector<std::string> ret;
DIR *dp;
dirent *dirp;
if ( (dp = opendir(dir.c_str())) == NULL ) {
return ret;
}
while ( (dirp = readdir(dp)) != NULL ) {
if (dirp->d_type == DT_DIR) {
std::string dname = dirp->d_name;
if (dname[0] != '.') {
ret.push_back( std::string(dirp->d_name) );
}
}
}
return ret;
}
std::string slurp_file(const std::string &filename)
{
std::string ret;
std::ifstream fin;
fin.open(filename.c_str());
if (!fin.is_open()) {
return ret;
}
ret.assign( (std::istreambuf_iterator<char>(fin) ),
(std::istreambuf_iterator<char>() ) );
return ret;
}