-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSystemPlusPlus.hpp
530 lines (410 loc) · 18.2 KB
/
FileSystemPlusPlus.hpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*# MIT License
#
# Copyright (c) 2020-2021 Ferhat Geçdoğan All Rights Reserved.
# Distributed under the terms of the MIT License.
#*/
#ifndef FILE_SYSTEM_PLUS_PLUS_HPP
#define FILE_SYSTEM_PLUS_PLUS_HPP
#include <cstring>
#include <iostream>
#include <pwd.h>
#include <dirent.h>
#include <fstream>
#include <sys/stat.h>
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#define FS_PLUS_PLUS_VERSION "0.2-beta-1"
namespace fsplusplus {
static std::string EraseAllSubString(std::string & mainString, const std::string & erase) {
size_t pos = std::string::npos;
while((pos = mainString.find(erase)) != std::string::npos) mainString.erase(pos, erase.length());
return mainString;
}
// Get Between String
static void GetBtwString(std::string oStr, std::string sStr1, std::string sStr2, std::string &rStr) {
int start = oStr.find(sStr1);
if(start >= 0) {
std::string tstr = oStr.substr(start + sStr1.length());
int stop = tstr.find(sStr2);
if (stop >1) rStr = oStr.substr(start + sStr1.length(), stop);
else rStr ="error";
} else
rStr = "error";
}
static std::string GetCurrentWorkingDir(void) {
char buff[FILENAME_MAX];
GetCurrentDir( buff, FILENAME_MAX );
std::string current_working_dir(buff);
return current_working_dir;
}
static bool IsExistFile(std::string path) {
struct stat buffer;
return (stat(path.c_str(), &buffer) == 0);
}
/*
-1 : error
0 : directory
+1 : file
-2 : anything else?
*/
static int8_t TypeOf(std::string path) {
struct stat info;
if(stat(path.c_str(), &info) != 0) return -1;
else if(info.st_mode & S_IFDIR) return 0;
else return 1;
return -2;
}
static void List() {
DIR *directory;
struct dirent *entryname;
struct stat filestat;
directory = opendir(fsplusplus::GetCurrentWorkingDir().c_str());
if(directory == NULL) {
std::cout << "Error: Directory not found.\n";
return;
}
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type == DT_DIR) {// DT_DIR -> directory
if(strstr(entryname->d_name, ".")) {} else if(strstr(entryname->d_name, "..")){}
else
std::cout << "[Dir]: " << entryname->d_name << "\n";
}
else if(strstr(entryname->d_name, "Elitefile")) std::cout << "[Elitebuild]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".scrift_log")) std::cout << "[FeLog*]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".scrift_ascii")) std::cout << "[Ascii Art]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".scrift_settings")) std::cout << "[Settings*]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".scrift_history")) std::cout << "[History*]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".scr")) std::cout << "[Scrift]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".cpp") ||
strstr(entryname->d_name, ".hpp") ||
strstr(entryname->d_name, ".cxx") ||
strstr(entryname->d_name, ".hxx") ||
strstr(entryname->d_name, ".cc") ||
strstr(entryname->d_name, ".hh")) std::cout << "[C++]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".c") ||
strstr(entryname->d_name, ".h")) std::cout << "[C]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, "CMakeLists.txt")) std::cout << "[CMake]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".sh")) std::cout << "[Shell]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".py")) std::cout << "[Python]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".fls") ||
strstr(entryname->d_name, ".flsh")) std::cout << "[FlaScript]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".md")) std::cout << "[Markdown]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".frbr")) std::cout << "[FreeBrain]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".png")) std::cout << "[Png]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".jpg") ||
strstr(entryname->d_name, ".jpeg")) std::cout << "[Jpg]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".gif")) std::cout << "[Gif]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".html") ||
strstr(entryname->d_name, ".htm")) std::cout << "[Html]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".rs") ||
strstr(entryname->d_name, ".rslib")) std::cout << "[Rust]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".lua")) std::cout << "[Lua]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".inclink")) std::cout << "[includeLink]: " << entryname->d_name << "\n";
else std::cout << "[File]: " << entryname->d_name << "\n";
}
closedir(directory);
}
static void ListFile() {
DIR *directory;
struct dirent *entryname;
struct stat filestat;
directory = opendir(fsplusplus::GetCurrentWorkingDir().c_str());
if(directory == NULL) {
std::cout << "Error: Directory not found.\n";
return;
}
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type != DT_DIR) {
if(strstr(entryname->d_name, "Elitefile")) std::cout << "[Elitebuild]: " << entryname->d_name << "\n";
else if(strstr(entryname->d_name, ".scrift_log")) { std::cout << "[FeLog*]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".scrift_ascii")) { std::cout << "[Ascii Art]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".scrift_settings")) { std::cout << "[Settings*]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".scrift_history")) { std::cout << "[History*]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".scr")) { std::cout << "[Scrift]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".cpp") ||
strstr(entryname->d_name, ".hpp") ||
strstr(entryname->d_name, ".cxx") ||
strstr(entryname->d_name, ".hxx") ||
strstr(entryname->d_name, ".cc") ||
strstr(entryname->d_name, ".hh")) { std::cout << "[C++]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".c") ||
strstr(entryname->d_name, ".h")) {std::cout << "[C]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, "CMakeLists.txt")) { std::cout << "[CMake]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".sh")) { std::cout << "[Shell]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".py")) { std::cout << "[Python]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".fls") ||
strstr(entryname->d_name, ".flsh")) { std::cout << "[FlaScript]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".md")) { std::cout << "[Markdown]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".frbr")) { std::cout << "[FreeBrain]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".png")) { std::cout << "[Png]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".jpg") ||
strstr(entryname->d_name, ".jpeg")) { std::cout << "[Jpg]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".gif")) { std::cout << "[Gif]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".html") ||
strstr(entryname->d_name, ".htm")) { std::cout << "[Html]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".rs") ||
strstr(entryname->d_name, ".rslib")) { std::cout << "[Rust]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".lua")) { std::cout << "[Lua]: " << entryname->d_name << "\n"; }
else if(strstr(entryname->d_name, ".inclink")) { std::cout << "[includeLink]: " << entryname->d_name << "\n"; }
else std::cout << "[File]: " << entryname->d_name << "\n";
}
}
closedir(directory);
}
static void ListDirectory() {
DIR *directory;
struct dirent *entryname;
struct stat filestat;
directory = opendir(fsplusplus::GetCurrentWorkingDir().c_str());
if(directory == NULL) {
std::cout << "Error: Directory not found.\n";
return;
}
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type == DT_DIR) {// DT_DIR -> directory
if(strstr(entryname->d_name, ".")) {} else if(strstr(entryname->d_name, "..")) {}
else
std::cout << "[Dir]: " << entryname->d_name << "\n";
}
}
closedir(directory);
}
static void ListFileDefault() {
DIR *directory;
struct dirent *entryname;
struct stat filestat;
directory = opendir(fsplusplus::GetCurrentWorkingDir().c_str());
if(directory == NULL) {
std::cout << "Error: Directory not found.\n";
return;
}
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type != DT_DIR) std::cout << entryname->d_name << "\n";
}
closedir(directory);
}
static void ListDirectoryDefault() {
DIR *directory;
struct dirent *entryname;
struct stat filestat;
directory = opendir(fsplusplus::GetCurrentWorkingDir().c_str());
if(directory == NULL) {
std::cout << "Error: Directory not found.\n";
return;
}
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type == DT_DIR) {// DT_DIR -> directory
if(strstr(entryname->d_name, ".")) {} else if(strstr(entryname->d_name, "..")) {}
else
std::cout << entryname->d_name << "\n";
}
}
closedir(directory);
}
static void ListPath(std::string dir) {
DIR *directory;
struct dirent *entryname;
struct stat filestat;
directory = opendir((fsplusplus::GetCurrentWorkingDir() + "/" + dir).c_str());
if(directory == NULL) {
std::cout << "Error: Directory not found.\n";
return;
}
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type == DT_DIR) {// DT_DIR -> directory
if(strstr(entryname->d_name, ".")) {} else if(strstr(entryname->d_name, "..")) {}
else
std::cout << "[Dir]: " << entryname->d_name << "\n";
}
}
closedir(directory);
}
static std::string ListPathWithReturn(std::string path) {
DIR *directory;
std::string add;
struct dirent *entryname;
struct stat filestat;
directory = opendir(path.c_str());
if(directory == NULL) return "null";
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type == DT_DIR) {
if(strstr(entryname->d_name, ".")) {} else if(strstr(entryname->d_name, "..")) {}
else
add.append(entryname->d_name);
} else
add.append(entryname->d_name);
}
closedir(directory);
return add;
}
static std::string ListDirectoryWithReturn(std::string path) {
DIR *directory;
std::string add;
struct dirent *entryname;
struct stat filestat;
directory = opendir(path.c_str());
if(directory == NULL) return "null";
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type == DT_DIR) {
if(strstr(entryname->d_name, ".")) {} else if(strstr(entryname->d_name, "..")) {}
else
add.append(entryname->d_name);
}
}
closedir(directory);
return add;
}
static std::string ListFileWithReturn(std::string path) {
DIR *directory;
std::string add;
struct dirent *entryname;
struct stat filestat;
directory = opendir(path.c_str());
if(directory == NULL) return "null";
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type != DT_DIR) add.append(entryname->d_name);
}
closedir(directory);
return add;
}
static std::string CDFunction(std::string path) {
return fsplusplus::GetCurrentWorkingDir() + path;
}
static void ReadFile(std::string file) {
std::string line;
std::ifstream readfile((fsplusplus::GetCurrentWorkingDir() + "/" + file).c_str());
if(readfile.is_open()) {
while (std::getline(readfile, line)) std::cout << line << "\n";
readfile.close();
} else
std::cout << "Unable to open file\n";
}
static std::string ReadFileWithReturn(std::string file) {
std::string line, data;
std::ifstream readfile((fsplusplus::GetCurrentWorkingDir() + "/" + file).c_str());
if(readfile.is_open()) {
while (std::getline(readfile, line)) data.append(line + "\n");
readfile.close();
} else
std::cout << "Unable to open file\n";
return data;
}
static std::string FindStringWithReturn(std::string file, std::string str) {
std::string line;
std::ifstream readfile(file.c_str());
if(readfile.is_open()) {
while (std::getline(readfile, line))
if(strstr(line.c_str(), str.c_str())) return line + "\n";
readfile.close();
}
return "null";
}
static void FindPath(std::string name) {
DIR *directory;
struct dirent *entryname;
struct stat filestat;
directory = opendir(fsplusplus::GetCurrentWorkingDir().c_str());
if(directory == NULL) {
std::cout << "Error: Directory not found.\n";
return;
}
while ((entryname = readdir(directory))) {
stat(entryname->d_name, &filestat);
if(entryname->d_type == DT_DIR) {
if(strstr(entryname->d_name, ".")) {} else if(strstr(entryname->d_name, "..")) {}
else if(strstr(entryname->d_name, name.c_str())) std::cout << "[Dir]:" << entryname->d_name << "\n";
} else if(strstr(entryname->d_name, name.c_str())) std::cout << "[File]:" << entryname->d_name << "\n";
}
closedir(directory);
}
static void ReadFilePath(std::string path) {
std::string line;
std::ifstream readfile(path.c_str());
if(readfile.is_open()) {
while (std::getline(readfile, line)) std::cout << line + "\n";
readfile.close();
} else
std::cout << "Unable to open file\n";
}
static void ReadCPU() {
std::string line;
#ifdef __FreeBSD__
std::ifstream readfile("/var/run/dmesg.boot");
#else
std::ifstream readfile("/proc/cpuinfo");
#endif
if(readfile.is_open()) {
while (std::getline(readfile, line)) {
#ifdef __FreeBSD__
if(line.find("CPU: ") == 0) {
line = EraseAllSubString(line, "CPU: ");
std::cout << line + "\n";
return;
}
#else
if(line.find("model name : ") == 0) {
line = fsplusplus::EraseAllSubString(line, "model name : ");
std::cout << line + "\n";
return;
}
#endif
}
readfile.close();
} else
std::cout << "Unable to open file\n";
}
static void ReadOSName(std::string path) {
#ifdef __FreeBSD__
std::cout << "FreeBSD\n";
#else
std::string line;
std::ifstream readfile(path.c_str());
if(readfile.is_open()) {
while (std::getline(readfile, line)) {
if(line.find("PRETTY_NAME=\"") == 0) {
fsplusplus::GetBtwString(line, "\"", "\"", line);
std::cout << line + "\n";
}
}
readfile.close();
} else
std::cout << "Unable to open file\n";
#endif
}
static void CreateFile(std::string name, std::string input) {
std::string path;
if(name.front() != '/') {
path.append(fsplusplus::GetCurrentWorkingDir());
path.append("/");
}
path.append(name);
std::ofstream file(path, std::ios::app);
file << input;
file.close();
}
static void CreateFileWithoutAppend(std::string name) {
std::string path;
if(name.front() != '/') {
path.append(fsplusplus::GetCurrentWorkingDir());
path.append("/");
}
path.append(name);
std::ofstream file(path);
file.close();
}
}
#endif // FILE_SYSTEM_PLUS_PLUS_HPP