-
Notifications
You must be signed in to change notification settings - Fork 74
/
Directory.cpp
48 lines (41 loc) · 1.11 KB
/
Directory.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
/*
* Directory.cpp
*
*/
#include <string>
#include <list>
#include <dirent.h>
#include <cstring>
#include "Directory.h"
Directory::Directory(const char* path, const char* extension) :
_path(path), _extension(extension) {
}
std::list<std::string> Directory::list() {
std::list<std::string> files;
DIR *dir;
struct dirent *ent;
if ((dir = opendir(_path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if ((ent->d_type == DT_REG || ent->d_type == DT_LNK)
&& hasExtension(ent->d_name, _extension.c_str())) {
files.push_back(std::string(ent->d_name));
}
}
closedir(dir);
}
return files;
}
std::string Directory::fullpath(const std::string filename) {
std::string path(_path);
path += "/";
path += filename;
return path;
}
bool Directory::hasExtension(const char* name, const char* ext) {
if (NULL == name || NULL == ext) {
return false;
}
size_t blen = strlen(name);
size_t slen = strlen(ext);
return (blen >= slen) && (0 == strcmp(name + blen - slen, ext));
}