-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
383 lines (295 loc) · 10.1 KB
/
filesystem.cpp
File metadata and controls
383 lines (295 loc) · 10.1 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "filesystem.h"
#include <fstream>
#include <algorithm>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#include <windows.h>
#define PATH_MAX 260
#define getcwd _getcwd
#define mkdir(path, mode) _mkdir(path)
#define rmdir _rmdir
#define access _access
#define F_OK 0
#ifndef S_ISREG
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
#ifndef S_ISDIR
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
#else
#include <dirent.h>
#include <unistd.h>
#include <climits>
#endif
// ====================== Win32 Dirent Implementation ======================
#ifdef _WIN32
struct dirent {
char d_name[MAX_PATH];
};
struct DIR {
HANDLE hFind;
WIN32_FIND_DATAW findData;
struct dirent entry;
bool first;
bool done;
};
static DIR *opendir(const char *path) {
std::string searchPath = path;
if (searchPath.empty()) searchPath = ".";
if (searchPath.back() == '/' || searchPath.back() == '\\')
searchPath += "*";
else
searchPath += "/*";
int size_needed = MultiByteToWideChar(CP_UTF8, 0, searchPath.c_str(), -1, nullptr, 0);
std::wstring wSearchPath(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, searchPath.c_str(), -1, &wSearchPath[0], size_needed);
DIR *dir = new DIR;
dir->hFind = FindFirstFileW(wSearchPath.c_str(), &dir->findData);
if (dir->hFind == INVALID_HANDLE_VALUE) {
delete dir;
return nullptr;
}
dir->first = true;
dir->done = false;
return dir;
}
static struct dirent *readdir(DIR *dir) {
if (dir->done) return nullptr;
if (!dir->first) {
if (!FindNextFileW(dir->hFind, &dir->findData)) {
dir->done = true;
return nullptr;
}
} else {
dir->first = false;
}
WideCharToMultiByte(CP_UTF8, 0, dir->findData.cFileName, -1, dir->entry.d_name, MAX_PATH, nullptr, nullptr);
return &dir->entry;
}
static int closedir(DIR *dir) {
if (dir) {
FindClose(dir->hFind);
delete dir;
}
return 0;
}
#endif
// ====================== File I/O ======================
std::pair<size_t, std::vector<char>> Filesystem::readFile(const std::string &path) {
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file)
throw std::runtime_error("Cannot open file: " + path);
auto size = static_cast<size_t>(file.tellg());
if (size == 0)
return {0, {}};
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (!file.read(buffer.data(), static_cast<std::streamsize>(size)))
throw std::runtime_error("Cannot read file: " + path);
return {size, std::move(buffer)};
}
bool Filesystem::writeFile(const std::string &path, const char *data, size_t length) {
if (!data) return false;
std::ofstream file(path, std::ios::binary | std::ios::trunc);
if (!file) return false;
file.write(data, static_cast<std::streamsize>(length));
return file.good();
}
bool Filesystem::writeFile(const std::string &path, const std::vector<char> &data) {
return writeFile(path, data.data(), data.size());
}
bool Filesystem::writeFile(const std::string &path, const std::string &data) {
return writeFile(path, data.data(), data.size());
}
bool Filesystem::appendFile(const std::string &path, const char *data, size_t length) {
if (!data) return false;
std::ofstream file(path, std::ios::binary | std::ios::app);
if (!file) return false;
file.write(data, static_cast<std::streamsize>(length));
return file.good();
}
// ====================== Path Operations ======================
std::string Filesystem::getCurrentDir() {
char buf[PATH_MAX];
if (getcwd(buf, PATH_MAX) == nullptr)
return "";
std::string path(buf);
fixPathSeparators(path);
if (!path.empty() && path.back() != '/')
path += '/';
return path;
}
std::string Filesystem::getParentDir(const std::string &path) {
if (path.empty()) return "";
std::string normalized = path;
// Rimuovi trailing slash
while (normalized.size() > 1 && (normalized.back() == '/' || normalized.back() == '\\'))
normalized.pop_back();
size_t pos = normalized.find_last_of("/\\");
if (pos == std::string::npos) return "";
if (pos == 0) return "/";
return normalized.substr(0, pos);
}
std::string Filesystem::getFilename(const std::string &path) {
if (path.empty()) return "";
size_t pos = path.find_last_of("/\\");
if (pos == std::string::npos) return path;
return path.substr(pos + 1);
}
std::string Filesystem::getBasename(const std::string &path) {
std::string filename = getFilename(path);
size_t pos = filename.find_last_of('.');
if (pos == std::string::npos || pos == 0)
return filename;
return filename.substr(0, pos);
}
std::string Filesystem::getExtension(const std::string &path) {
std::string filename = getFilename(path);
size_t pos = filename.find_last_of('.');
if (pos == std::string::npos || pos == filename.length() - 1)
return "";
return filename.substr(pos + 1);
}
std::string Filesystem::removeExtension(const std::string &path) {
size_t pos = path.find_last_of('.');
size_t slashPos = path.find_last_of("/\\");
// Assicurati che il punto sia nel nome file, non nel percorso
if (pos == std::string::npos || (slashPos != std::string::npos && pos < slashPos))
return path;
return path.substr(0, pos);
}
std::string Filesystem::removeFilename(const std::string &path) {
size_t pos = path.find_last_of("/\\");
if (pos == std::string::npos) return "";
return path.substr(0, pos);
}
std::string Filesystem::joinPath(const std::string &base, const std::string &name) {
if (base.empty()) return name;
if (name.empty()) return base;
std::string result = base;
// Rimuovi trailing slash da base
while (!result.empty() && (result.back() == '/' || result.back() == '\\'))
result.pop_back();
// Rimuovi leading slash da name
std::string cleanName = name;
while (!cleanName.empty() && (cleanName.front() == '/' || cleanName.front() == '\\'))
cleanName = cleanName.substr(1);
return result + "/" + cleanName;
}
std::string Filesystem::normalizePath(const std::string &path) {
if (path.empty()) return "";
std::string result = path;
fixPathSeparators(result);
// Rimuovi doppi slash
std::string cleaned;
bool lastWasSlash = false;
for (char c : result) {
if (c == '/') {
if (!lastWasSlash) {
cleaned += c;
lastWasSlash = true;
}
} else {
cleaned += c;
lastWasSlash = false;
}
}
// Rimuovi trailing slash (eccetto per root)
while (cleaned.size() > 1 && cleaned.back() == '/')
cleaned.pop_back();
return cleaned;
}
unsigned int Filesystem::getPathDepth(const std::string &path) {
if (path.empty()) return 0;
unsigned int depth = 0;
for (size_t i = 1; i < path.length(); i++) {
if (path[i] == '/' || path[i] == '\\')
depth++;
}
return depth;
}
// ====================== File/Directory Checks ======================
bool Filesystem::exists(const std::string &path) {
return access(path.c_str(), F_OK) != -1;
}
bool Filesystem::isFile(const std::string &path) {
struct stat s{};
if (stat(path.c_str(), &s) != 0) return false;
return S_ISREG(s.st_mode);
}
bool Filesystem::isDirectory(const std::string &path) {
struct stat s{};
if (stat(path.c_str(), &s) != 0) return false;
return S_ISDIR(s.st_mode);
}
size_t Filesystem::getFileSize(const std::string &path) {
struct stat s{};
if (stat(path.c_str(), &s) != 0) return 0;
return static_cast<size_t>(s.st_size);
}
// ====================== Directory Operations ======================
bool Filesystem::createDirectory(const std::string &path, bool recursive) {
if (path.empty()) return false;
if (isDirectory(path)) return true;
if (recursive) {
std::string parent = getParentDir(path);
if (!parent.empty() && !isDirectory(parent)) {
if (!createDirectory(parent, true))
return false;
}
}
return mkdir(path.c_str(), 0777) == 0;
}
bool Filesystem::removeFile(const std::string &path) {
if (!isFile(path)) return false;
return std::remove(path.c_str()) == 0;
}
bool Filesystem::removeDirectory(const std::string &path, bool recursive) {
if (!isDirectory(path)) return false;
if (recursive) {
auto entries = listDirectory(path);
for (const auto &entry : entries) {
std::string fullPath = joinPath(path, entry.name);
if (entry.isDirectory) {
if (!removeDirectory(fullPath, true))
return false;
} else {
if (!removeFile(fullPath))
return false;
}
}
}
return rmdir(path.c_str()) == 0;
}
std::vector<Filesystem::DirEntry> Filesystem::listDirectory(const std::string &path) {
std::vector<DirEntry> entries;
DIR *dir = opendir(path.c_str());
if (!dir) return entries;
struct dirent *entry;
while ((entry = readdir(dir)) != nullptr) {
std::string name = entry->d_name;
if (name == "." || name == "..")
continue;
std::string fullPath = joinPath(path, name);
DirEntry dirEntry;
dirEntry.name = name;
dirEntry.isDirectory = isDirectory(fullPath);
dirEntry.size = dirEntry.isDirectory ? 0 : getFileSize(fullPath);
entries.push_back(std::move(dirEntry));
}
closedir(dir);
return entries;
}
// ====================== Utilities ======================
void Filesystem::fixPathSeparators(std::string &path) {
std::replace(path.begin(), path.end(), '\\', '/');
}
std::string Filesystem::toAbsolutePath(const std::string &path) {
if (path.empty()) return getCurrentDir();
// Già assoluto?
if (path[0] == '/' || (path.length() > 1 && path[1] == ':'))
return normalizePath(path);
return normalizePath(joinPath(getCurrentDir(), path));
}