-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.cpp
More file actions
207 lines (179 loc) · 5.18 KB
/
FileUtil.cpp
File metadata and controls
207 lines (179 loc) · 5.18 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "FileUtil.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <filesystem>
#include <stdexcept>
#include <random>
namespace fs = std::filesystem;
namespace FileUtil {
std::vector<std::string> getFilesInDirectory(const std::string& directory_name){
std::vector<std::string> filenames;
fs::path directory(directory_name);
fs::directory_iterator end_iterator;
if(!fs::exists(directory)){
throw std::runtime_error(directory_name + " does not exist");
}
if(!fs::is_directory(directory)){
throw std::runtime_error(directory_name + " is not a directory");
}
for(fs::directory_iterator it(directory); it!=end_iterator; it++){
if(fs::is_regular_file(it->path())){
filenames.push_back(it->path().filename().string());
}
}
std::sort(filenames.begin(), filenames.end());
return filenames;
}
std::vector<std::string> getDirectoriesInDirectory(const std::string& directory_name){
std::vector<std::string> filenames;
fs::path directory(directory_name);
fs::directory_iterator end_iterator;
if(!fs::exists(directory)){
throw std::runtime_error(directory_name + " does not exist");
}
if(!fs::is_directory(directory)){
throw std::runtime_error(directory_name + " is not a directory");
}
for(fs::directory_iterator it(directory); it!=end_iterator; it++){
if(fs::is_directory(it->path())){
filenames.push_back(it->path().filename().string());
}
}
return filenames;
}
std::string extractFileExtension(const std::string& filename){
for(int i=filename.size()-1; i>=0; i--){
if(filename[i] == '.'){
return filename.substr(i+1);
}
}
return "";
}
std::string getFileContents(const std::string& filename){
if(!fileExists(filename)){
throw std::runtime_error("No such file "+filename);
}
std::ifstream fp(filename, std::ios::binary|std::ios::in);
std::string contents;
std::stringstream buffer;
if(fp.fail()){
throw std::runtime_error("Could not open file "+filename);
}
buffer << fp.rdbuf();
contents = buffer.str();
fp.close();
return contents;
}
void setFileContents(const std::string& path, const std::string& contents){
const fs::path fp = path;
if(fs::exists(fp)){
if(!fs::is_regular_file(fp)){
throw std::runtime_error("Entity "+path+" exists, but is not a regular file. Refusing to overwrite.");
}
}
std::ofstream fp2(path, std::ios::binary|std::ios::out);
if(fp2.bad()){
throw std::runtime_error("Could not create file at "+path);
}
fp2 << contents;
fp2.close();
}
std::string createTemporaryFile(const std::string& contents)
{
std::random_device seed;
std::default_random_engine rand_engine(seed());
std::uniform_int_distribution<std::uint64_t>distribute(1,2UL<<63UL);
std::string path_name = "/tmp/futil.temp."+std::to_string(distribute(rand_engine));
setFileContents(path_name, contents);
return path_name;
}
void ensureDirectoryExists(const std::string& path){
const fs::path directory = path;
if(fs::exists(directory)){
if(fs::is_directory(directory)){
// all good
return;
}
throw std::runtime_error("Entity "+path+" exists, but is not a directory. Refusing to overwrite.");
}else{
if(!fs::create_directories(directory)){
throw std::runtime_error("Could not create directory at "+path);
}
}
}
void ensureFileExists(const std::string& path){
const fs::path fp = path;
if(fs::exists(fp)){
if(!fs::is_regular_file(fp)){
throw std::runtime_error("Entity "+path+" exists, but is not a regular file. Refusing to overwrite.");
}
}else{
std::ofstream fp2(path);
if(fp2.bad()){
throw std::runtime_error("Could not create file at "+path);
}
fp2.close();
}
}
bool fileExists(const std::string& path){
const fs::path fp = path;
if(fs::exists(fp)){
if(!fs::is_regular_file(fp)){
throw std::runtime_error("Entity "+path+" exists, but is not a regular file");
}
return true;
}
return false;
}
void ensureFileDoesNotExist(const std::string& path){
const fs::path fp = path;
if(fs::exists(fp)){
// Destroy it >:(
if(!fs::is_regular_file(fp)){
throw std::runtime_error("Can't destroy file "+path+" because it is a non-file entity");
}
fs::remove(fp);
}
// Work is done for you
}
void destroyFile(const std::string& path){
if(!fs::exists(fs::path(path))){
throw std::runtime_error("Can't destroy file "+path+" because it doesn't exist");
}
ensureFileDoesNotExist(path);
}
void ensureDirectoryDoesNotExist(const std::string& path){
fs::path dir = path;
if(fs::exists(dir)){
// Destroy it >:(
if(!fs::is_directory(dir)){
throw std::runtime_error("Can't destroy directory "+path+" because it is a non-directory entity");
}
fs::remove_all(dir);
}
// Work is done for you
}
void destroyDirectory(const std::string& path){
if(!fs::exists(fs::path(path))){
throw std::runtime_error("Can't destroy directory "+path+" because it doesn't exist");
}
ensureDirectoryDoesNotExist(path);
}
std::string getParentPath(std::string path){
if(path == ""){
throw std::runtime_error("Can't get parent path of empty string");
}
while(path.back() == '/'){
if(path != "/"){
path.pop_back();
}
}
const fs::path fp = path;
if(!fs::exists(fp)){
throw std::runtime_error("Can't get parent directory of path that doesn't exist"+path);
}
return fp.parent_path().string();
}
}