forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory_path.cpp
More file actions
55 lines (46 loc) · 1.27 KB
/
directory_path.cpp
File metadata and controls
55 lines (46 loc) · 1.27 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
48
49
50
51
52
53
54
55
#include "stdafx.h"
#include "directory_path.h"
#include "file_path.h"
#include <sys/types.h>
#include <sys/stat.h>
#include "dirent.h"
DirectoryPath::DirectoryPath(const std::string& p) : path(p) {}
FilePath DirectoryPath::file(const std::string& f) const {
return FilePath(*this, f);
}
DirectoryPath DirectoryPath::subdirectory(const std::string& s) const {
return DirectoryPath(path + "/" + s);
}
static bool isDirectory(const string& path) {
struct stat path_stat;
if (stat(path.c_str(), &path_stat))
return false;
else
return S_ISDIR(path_stat.st_mode);
}
bool DirectoryPath::exists() const {
return isDirectory(get());
}
static bool isRegularFile(const string& path) {
struct stat path_stat;
if (stat(path.c_str(), &path_stat))
return false;
else
return S_ISREG(path_stat.st_mode);
}
vector<FilePath> DirectoryPath::getFiles() const {
vector<FilePath> ret;
if (DIR* dir = opendir(path.c_str())) {
while (dirent* ent = readdir(dir))
if (isRegularFile(path + "/" + ent->d_name))
ret.push_back(FilePath(*this, ent->d_name));
closedir(dir);
}
return ret;
}
const char* DirectoryPath::get() const {
return path.c_str();
}
std::ostream& operator <<(std::ostream& d, const DirectoryPath& path) {
return d << path.get();
}