-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_utilities.cpp
119 lines (109 loc) · 4.09 KB
/
file_utilities.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "file_utilities.h"
namespace fileUtilities {
template<class iterclass>
void populateList(iterclass& iter, std::vector<std::string>& paths,
std::vector<std::string>& filenames) {
//Extensions list
const std::unordered_set<std::string> extensions = {".bmp", ".gif", ".jpg", ".jpeg",
".png", ".pbm", ".pgm", ".ppm",
".xbm", ".xpm",
".BMP", ".GIF", ".JPG", ".JPEG",
".PNG", ".PBM", ".PGM", ".PPM",
".XBM", ".XPM"};
for (auto& p: iter) {
const fs::path& file(p.path());
std::string filename = file.filename().u8string();
std::string extension = file.extension().u8string();
std::string filepath = p.path().u8string();
if (!fs::is_directory(filepath) && extensions.find(extension) != extensions.end()){
paths.push_back(filepath);
filenames.push_back(filename);
}
}
}
void fileWalker (const char * root_dir, std::vector<std::string>& paths,
std::vector<std::string>& filenames, bool recursive, std::string& error) {
try {
if (recursive) {
fs::recursive_directory_iterator iter(root_dir);
populateList(iter, paths, filenames);
} else {
fs::directory_iterator iter(root_dir);
populateList(iter, paths, filenames);
}
} catch (fs::filesystem_error & e) {
error = e.what();
}
std::sort(paths.begin(), paths.end());
std::sort(filenames.begin(), filenames.end());
}
bool checkIfExistOrDir(const char * path) {
try {
fs::path pathObj(path);
// Check if the path exists and it's a directory
if (fs::exists(pathObj) && fs::is_directory(pathObj))
return true;
} catch (fs::filesystem_error & e) {
std::cerr << e.what() << std::endl;
}
return false;
}
template<class iterclass>
bool _checkIfImages(iterclass& iter) {
const std::unordered_set<std::string> extensions = {".bmp", ".gif", ".jpg", ".jpeg",
".png", ".pbm", ".pgm", ".ppm",
".xbm", ".xpm",
".BMP", ".GIF", ".JPG", ".JPEG",
".PNG", ".PBM", ".PGM", ".PPM",
".XBM", ".XPM"};
for (auto& p: iter) {
const fs::path& file(p.path());
std::string extension = file.extension().u8string();
std::string filepath = p.path().u8string();
if (!fs::is_directory(filepath) && extensions.find(extension) != extensions.end()){
return true;
}
}
return false;
}
bool checkIfImagesExist (const char * path, bool recursive, std::string& error) {
try {
if (recursive) {
fs::recursive_directory_iterator iter(path);
return _checkIfImages(iter);
} else {
fs::directory_iterator iter(path);
return _checkIfImages(iter);
}
} catch (fs::filesystem_error & e) {
error = e.what();
}
return false;
}
int checkIfWritable(const char * path) {
try {
fs::path pathObj(path);
// Check if the path exists and it's a directory
if (fs::exists(pathObj)) {
if (fs::is_directory(pathObj)) {
//Try if writable
pathObj /= "test";
fs::create_directory(pathObj);
fs::remove(pathObj);
return 0;
} else {
//Not a directory
return 1;
}
} else {
fs::create_directory(path); //Test if we can create and remove the directory
fs::remove(path);
return 0;
}
}
catch (fs::filesystem_error & e) {
std::cerr << e.what() << std::endl;
}
return 2; //No write permissions
}
} //namespace