-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathFileUtil.cpp
More file actions
215 lines (188 loc) · 5.33 KB
/
Copy pathFileUtil.cpp
File metadata and controls
215 lines (188 loc) · 5.33 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
208
209
210
211
212
213
214
215
#include "FileUtil.h"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include "FetlangException.h"
#include "thirdparty/whereami.h"
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 FetlangException(directory_name + " does not exist");
}
if (!fs::is_directory(directory)) {
throw FetlangException(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 FetlangException(directory_name + " does not exist");
}
if (!fs::is_directory(directory)) {
throw FetlangException(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 FetlangException("No such file " + filename);
}
std::ifstream fp(filename);
std::string contents;
std::stringstream buffer;
if (fp.fail()) {
throw FetlangException("Could not open file " + filename);
}
buffer << fp.rdbuf();
contents = buffer.str();
fp.close();
std::string new_contents = "";
for (const char& i : contents) {
if (i != '\r') new_contents += i;
}
return new_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 FetlangException("Entity " + path +
" exists, but is not a regular file. Refusing to overwrite.");
}
}
std::ofstream fp2(path);
if (fp2.bad()) {
throw FetlangException("Could not create file at " + path);
}
fp2 << contents;
fp2.close();
}
void ensureDirectoryExists(const std::string& path) {
const fs::path directory = path;
if (fs::exists(directory)) {
if (fs::is_directory(directory)) {
// all good
return;
}
throw FetlangException("Entity " + path +
" exists, but is not a directory. Refusing to overwrite.");
} else {
std::error_code ec;
if (!fs::create_directories(directory, ec)) {
throw FetlangException("Could not create directory at " + path +
". Error code:" + ec.message());
}
}
}
void ensureFileExists(const std::string& path) {
const fs::path fp = path;
if (fs::exists(fp)) {
if (!fs::is_regular_file(fp)) {
throw FetlangException("Entity " + path +
" exists, but is not a regular file. Refusing to overwrite.");
}
} else {
std::ofstream fp2(path);
if (fp2.bad()) {
throw FetlangException("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 FetlangException("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 FetlangException("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 FetlangException("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 FetlangException("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 FetlangException("Can't destroy directory " + path + " because it doesn't exist");
}
ensureDirectoryDoesNotExist(path);
}
std::string getParentPath(std::string path) {
if (path == "") {
throw FetlangException("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 FetlangException("Can't get parent directory of path that doesn't exist" + path);
}
return fp.parent_path().string();
}
std::string getExecutableParentPath() {
constexpr int buffer_size = 256;
char buffer[buffer_size];
int size;
wai_getExecutablePath(buffer, buffer_size, &size);
return std::string(buffer, size);
}
} // namespace FileUtil