-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cpp
More file actions
1088 lines (1009 loc) · 32.5 KB
/
Commands.cpp
File metadata and controls
1088 lines (1009 loc) · 32.5 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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <string>
#include <process.h>
#include <direct.h>
#include <string.h>
#include "Commands.h"
#include "dirent.h"
/*
#include <libproc.h>
#import <sys/proc_info.h>
*/
#ifdef _WIN32
#define PLATFORM_NAME "windows"
#include <process.h>
#include <direct.h>
#elif defined __unix__
#define PLATFORM_NAME "linux"
#elif defined __APPLE__
#define PLATFORM_NAME "osx"
#endif
#define ERROR 313
using namespace std;
Commands::Commands()
{
CurrentPath = "C:\\";
}
//
Commands::~Commands()
{
CurrentPath = "";
}
//
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
/*
void Commands::listProcesses(string& currentPath, string sInput) {
if (PLATFORM_NAME == "osx")
{
stringstream getin(sInput);
string command;
string optional;
string fileName;
getin >> command >> optional >> fileName;
ofstream myfile; //file work using ofstream
int numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);
pid_t pids[numberOfProcesses];
bzero(pids, sizeof(pids));
proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(pids));
if (optional == ">")
{
myfile.open(currentPath + "/" + fileName); //open file
for (int i = 0; i < numberOfProcesses; ++i)
{
if (pids[i] == 0)
{
continue;
}
char pathBuffer[PROC_PIDPATHINFO_MAXSIZE];
bzero(pathBuffer, PROC_PIDPATHINFO_MAXSIZE);
proc_pidpath(pids[i], pathBuffer, sizeof(pathBuffer));
if (strlen(pathBuffer) > 0)
{
if (myfile.is_open())
{
myfile << getProcessName(pathBuffer) << endl; //put the dir out put in there
}
}
}
myfile.close();
}
else
{
for (int i = 0; i < numberOfProcesses; ++i)
{
if (pids[i] == 0)
{
continue;
}
char pathBuffer[PROC_PIDPATHINFO_MAXSIZE];
bzero(pathBuffer, PROC_PIDPATHINFO_MAXSIZE);
proc_pidpath(pids[i], pathBuffer, sizeof(pathBuffer));
if (strlen(pathBuffer) > 0)
{
cout << getProcessName(pathBuffer) << endl;
}
}
}
}
else {
cout << "this function is not compatible in" << PLATFORM_NAME << "operating system." << endl;
}
}
*/
string Commands::getProcessName(const string& processAddress) {
auto pos = processAddress.find_last_of("/");
string processName = processAddress.substr(pos + 1, processAddress.length());
return processName;
}
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
void Commands::clearScreen() {
cout << "\x1B[2J\x1B[H"; //clears the screen
}
//
void Commands::date() {
time_t theTime = time(0); //pure time that has passed from a certain date (1970).
////
tm* DATE = localtime(&theTime); //tm is a struct that can give us the date using localtime funtion
////
/*
struct tm
{
int tm_sec; // seconds after the minute - [0, 60] including leap second
int tm_min; // minutes after the hour - [0, 59]
int tm_hour; // hours since midnight - [0, 23]
int tm_mday; // day of the month - [1, 31] //this ***
int tm_mon; // months since January - [0, 11] //this ***
int tm_year; // years since 1900 //this ***
int tm_wday; // days since Sunday - [0, 6]
int tm_yday; // days since January 1 - [0, 365]
int tm_isdst; // daylight savings time flag
};
*/
////
string day;
switch (DATE->tm_wday)
{
case 0:
day = "Sun"; break;
case 1:
day = "Mon"; break;
case 2:
day = "Tue"; break;
case 3:
day = "Wed"; break;
case 4:
day = "Thu"; break;
case 5:
day = "Fri"; break;
case 6:
day = "Sat"; break;
default:
day = "someday";
break;
}
cout << "The current date is: " << day;
printf(" %02d\\%02d\\%04d \n", DATE->tm_mon + 1, DATE->tm_mday, DATE->tm_year + 1900);
}
//
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
void Commands::cd(string& currentPath, string sInput) {
stringstream getin(sInput);
string command;
string optional;
getin >> command >> optional; //gets the optional to see what to do
//optiona can only be /d or /?
if (optional == "/d") {
string des = "";
int i = 0;
int countSpace = 0;
bool fill = false;
int sizeDes = 0;
while (sInput[i] != '\0') {
if (sInput[i] == '/' && sInput[i + 1] == 'd') { //when you saw /d look for a character
i += 2;
while (sInput[i] != '\0') {
if (sInput[i] != ' ' && !fill) { //when you saw character start filling des
fill = true;
}
if (fill) {
////
if (sInput[i] == '\\' && sizeDes > 0) {
if (des[sizeDes - 1] == '\\') {
des += "";
}
else {
des += sInput[i]; //this part of code prevents having d:\\\\\\\\\\\\\games as current path
sizeDes++;
}
}
else {
des += sInput[i];
sizeDes++;
}
////
}
i++;
}
break;
}
i++;
}
//exception going to different drive only like cd /d d: will change it to d:\ .
if (sizeDes > 1) {
if (des[2] != '\\') {
des += '\\';
}
}
int valid = validation(des);
if (valid == 2) {
currentPath = des;
saveCurrentDir(currentPath); //for chdir
cout << endl;
}
else {
if (des == "c:\\" || des == "C:\\" || des == "C:\\users" || des == "c:\\users" || des == "C:\\windows" || des == "c:\\windows" || des == "C:\\program files" || des == "c:\\program files") { //exceptions
currentPath = des;
saveCurrentDir(currentPath); //for chdir
cout << endl;
}
else cout << "The system cannot find the path specified." << endl << endl;
if (valid == 3) {
cout << "The directory name is invalid." << endl << endl;
}
}
}
else
if (optional == "/?") { //explanation
printf("\nTo change the directory : CD [/D] [drive:][path]\nTo go to the parent folder : CD [..]\nto go to sub folder : CD [name of the folder in this directory]\n");
printf("\nUse the /D switch to change current drive in addition to changing current\ndirectory for a drive.\n\n");
}
else {
////
string des = "";
int i = 2; //we start after cd and first character we saw we will start filling des
int countSpace = 0;
bool fill = false;
int sizeDes = 0;
while (sInput[i] != '\0') {
if (sInput[i] != ' ' && !fill) {
fill = true;
}
if (fill) {
if (sInput[i] == '\\' && sizeDes > 0) {
if (des[sizeDes - 1] == '\\') {
des += "";
}
else {
des += sInput[i];
sizeDes++;
}
}
else {
des += sInput[i];
sizeDes++;
}
}
i++;
}
////
////
int j = 0;
while (des[j] != '\0') {
if (des[j] == ' ') {
des[j] = -1; //to protect spaces in the file name or folder name
}
if (des[j] == '\\' || des[j] == '/') {
des[j] = ' ';
}
j++;
}
////
string hold = currentPath;
stringstream getit(des);
string word;
while (getit >> word) {
if (word == "..") {
goBackwards(hold);
}
else if (word == ".") {
goCurrent(hold);
}
else
{
goForward(hold, word);
}
}
////
int k = 0;
while (hold[k] != '\0') {
if (hold[k] == -1) { //to bring back the spaces from protection
hold[k] = ' ';
}
k++;
}
////
int valid = validation(hold);
if (valid == 2) {
currentPath = hold;
saveCurrentDir(currentPath); //for chdir
cout << endl;
}
else {
if (valid == 1) {
if (hold == "c:\\" || hold == "C:\\" || hold == "C:\\users" || hold == "c:\\users" || hold == "C:\\windows" || hold == "c:\\windows" || hold == "C:\\program files" || hold == "c:\\program files") { //exceptions //cause there is no premission to make file here
currentPath = hold;
saveCurrentDir(currentPath); //for chdir
cout << endl;
}
else cout << "The system cannot find the path specified." << endl << endl;
}
if (valid == 3) {
cout << "The directory name is invalid." << endl << endl;
}
}
}
}
//
void Commands::goBackwards(string& currentPath) {
int length = currentPath.length();
int i = length - 1; //start from the end and go back.
while (1) {
if (currentPath[i] == ':') { //we have reached the drive name so we should add a '\\'
currentPath += '\\';
break;
}
if (currentPath[i] == '\\') { //prevents deleting '\\' after drive name
if (currentPath[i - 1] != ':')
currentPath.erase(currentPath.end() - 1); //deletes a character from end of the string
break;
}
currentPath.erase(currentPath.end() - 1);
i--;
}
}
//
void Commands::goCurrent(string currentPath) {
currentPath = currentPath;
//kinda nothing happens
}
//
void Commands::goForward(string& currentPath, string word) {
if (currentPath[currentPath.length() - 1] != '\\') //if it didn't have '\\' already
currentPath += '\\' + word;
else
currentPath += word; //it has '\\' already
}
//
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
int Commands::absoluteAddress(string& address, string currentPath) {
string hold = currentPath; //keeps current path
if (address == "") { //checks if it is not empty
return ERROR;
}
if (address[1] == ':') { //checks if the address is absolute
int valid = validation(address);
if (valid == 3) { //it is a file
return 3; //for file
}
else {
if (valid == 2) { //it is a folder
return 2; //for folder
}
return ERROR; //does not exist
}
}
else {
////
int i = 0; //leads the way through address
while (address[i] != '\0') {
if (address[i] == ' ') {
address[i] = -1; //keeps the spaces in file names
}
if (address[i] == '\\' || address[i] == '/') { //to work with stringstream uses space to seprate
address[i] = ' ';
}
i++;
}
////
////
stringstream getit(address);
string word; //each word can be a word, ".." or "." we should add the word(goForward) go one folder upper for ".."(goBackwards) or goCurrent for "."
while (getit >> word) {
if (word == "..") {
goBackwards(hold);
}
else if (word == ".") {
goCurrent(hold);
}
else
{
goForward(hold, word);
}
}
////
////
int j = 0;
while (hold[j] != '\0') {
if (hold[j] == -1) { //gets us back the spaces in the file names
hold[j] = ' ';
}
j++;
}
////
////
address = hold;
int valid = validation(address); //check if the address exists
if (valid == 3) { //it is a file
return 3; //for file
}
else {
if (valid == 2) { //it is a folder
return 2; //for folder
}
return ERROR; //does not exist
}
////
}
}
//
int Commands::validation(string address) {
string addressCheck = address + "\\somekindaweirdnameNOhumanbeingwouldeverchoosetonamesomefilewithweirdformatandstuff.ohmygod!";
const char* address01 = &address[0];
const char* address02 = &addressCheck[0];
FILE* file1;
FILE* file2;
//for file existence checking
file1 = fopen(address01, "r");
//IF IT IS A FILE I SHOULD BE ABLE TO READ IT
if (file1 == NULL) { //for folder existence checking
file2 = fopen(address02, "w");
//IF IT IS A FOLDER I SHOULD BE ABLE TO CREATE FILE IN IT
if (file2 == NULL) {
//not a file nor a folder ::1
return 1;
}
else {
//not a file but a folder ::2
fclose(file2);
del(address02);
return 2;
}
}
else { //not a folder but a file ::3
fclose(file1);
return 3;
}
}
//
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
void Commands::dir1(string currentPath) {
int fileNUM = 0;
int folderNUM = 0;
string str;
string identity;
struct dirent* d1;
// struct dirent {
/* Always zero */
// long d_ino;
/* File position within stream */
// long d_off;
/* Structure size */
// unsigned short d_reclen;
/* Length of name without \0 */
// size_t d_namlen;
/* File type */
// int d_type;
/* File name */ //we use this
// char d_name[PATH_MAX + 1];
//};
//typedef struct dirent dirent;
const char* address = ¤tPath[0];
DIR* d2 = opendir(address);
if (d2 == NULL)
{
printf("Could not open current directory\n");
return;
}
while (1) {
d1 = readdir(d2);
if (d1 == NULL) break; //we reached the end
str = currentPath + '\\' + (d1->d_name);
if ((validation(str) == 2) || str == currentPath + '\\' + "..") { //folder ::2
cout << "<DIR> " << d1->d_name << endl;
folderNUM++;
}
else {
cout << d1->d_name << endl;
fileNUM++;
}
}
cout << " " << fileNUM << " file(s)" << endl;
cout << " " << folderNUM << " Dir(s)" << endl;
closedir(d2);
}
//
void Commands::dir2(string currentPath) {
int fileNUM = 0;
int folderNUM = 0;
//
string str;
struct dirent* d1;
const char* a = ¤tPath[0];
DIR* d2 = opendir(a);
if (d2 == NULL) {
cout << "Could not open current directory" << endl;
}
char list[100][1000];
int count = 0;
while (1) {
d1 = readdir(d2);
if (d1 == NULL) break; //we reached the end
strcpy(list[count], d1->d_name); //put the name in list
count++; //count names
}
//
sortByName(list, count);
//
for (int i = 0; i < count; i++) { //print sorted array
str = currentPath + '\\' + list[i];
if ((validation(str) == 2) || str == currentPath + '\\' + "..") { //folder ::2
cout << "<DIR> " << list[i] << endl;
folderNUM++;
}
else {
cout << list[i] << endl;
fileNUM++;
}
}
cout << " " << fileNUM << " file(s)" << endl;
cout << " " << folderNUM << " Dir(s)" << endl;
}
//
void Commands::dir3(string currentPath) {
//
string address;
struct dirent* d1;
const char* a = ¤tPath[0];
DIR* d2 = opendir(a);
if (d2 == NULL) {
cout << "Could not open current directory" << endl;
}
char list[100][1000];
int count = 0;
while (1) {
d1 = readdir(d2);
if (d1 == NULL) break;
int i = 0;
address = currentPath + '\\' + (d1->d_name);
if (validation(address) == 3) {
strcpy(list[count], d1->d_name); //get only file names in list
count++;
}
}
//
if (count == 0) {
cout << "Could Not Find any file" << endl << endl;
}
else {
sortByType(list, count); //sort file names by their extention
for (int i = 0; i < count; i++) { //print the sorted
cout << list[i] << endl;
}
}
}
//
void Commands::dir4(string currentPath) {
//same as dir1 but for every thing inside too
struct dirent* d1;
const char* address = ¤tPath[0];
DIR* d2 = opendir(address);
if (d2 == NULL)
{
return;
}
while (1) {
d1 = readdir(d2);
if (d1 == NULL) break;
string fname = d1->d_name;
if (fname != "." && fname != "..") {
cout << currentPath << ": " << d1->d_name << endl;
dir4(currentPath + "\\" + d1->d_name); //this makes it recrusivly dir every directory
}
}
closedir(d2);
}
//
void Commands::dir5(string currentPath, string sInput) {
stringstream getin(sInput);
string command;
string optional;
string fileName;
getin >> command >> optional >> fileName;
int i = 0;
while (fileName[i] != '\0') {
if (fileName[i] == -2) { //get back the spaces in fileName we protected before
fileName[i] = ' ';
}
i++;
}
struct dirent* d1;
const char* address = ¤tPath[0];
DIR* d2 = opendir(address);
if (d2 == NULL)
{
cout << "Could not open current directory" << endl;
return;
}
ofstream myfile; //file work usig ofstream
string absolutePath = fileName;
absoluteAddress(absolutePath, currentPath);
myfile.open(absolutePath); //open file
if (myfile.is_open()) {
while (1) {
d1 = readdir(d2);
if (d1 == NULL) break;
myfile << "<DIR> " << d1->d_name << endl; //put the dir out put in there
}
myfile.close();
}
else cout << "Unable to open file" << endl; // file not found
closedir(d2);
}
//
void Commands::sortByType(char list[][1000], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
string a = list[j];
string b = list[j + 1];
a = getType(list[j]); //gets the type
b = getType(list[j + 1]);
tolow(a);
tolow(b);
char* a1 = &a[0]; //copmpare type
char* b1 = &b[0]; //copmpare type
if (strcmp(a1, b1) > 0) {
swap(list, j, j + 1); //swap happens based on type differences
}
}
}
}
//
string Commands::getType(string str) {
string type = "";
int k = str.length() - 1;
int here = 0;
while (k != 0) {
if (str[k] == '.') {
here = k + 1;
break;
}
k--;
}
if (here == 0) {
type = "";
return type;
}
else {
while (str[here] != 0) {
type += str[here];
here++;
}
return type;
}
}
//
void Commands::sortByName(char list[][1000], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
string a = list[j];
string b = list[j + 1];
tolow(a);
tolow(b);
char* a1 = &a[0]; //compare names
char* b1 = &b[0]; //compare names
if (strcmp(a1, b1) > 0) {
swap(list, j, j + 1); //a normal swap function
}
}
}
}
//
void Commands::tolow(string& a) { //to compare 2 strings we should first make both lower case in order to do it currectly
int i = 0;
while (a[i] != '\0') {
a[i] = tolower(a[i]);
i++;
}
}
//
void Commands::swap(char list[][1000], int i, int j) {
char hold[1000];
int c1 = 0;
while (1) {
hold[c1] = list[i][c1];
if (list[i][c1] == '\0') {
break;
}
c1++;
}
int c2 = 0;
while (1) {
list[i][c2] = list[j][c2];
if (list[j][c2] == '\0') {
break;
}
c2++;
}
int c3 = 0;
while (1) {
list[j][c3] = hold[c3];
if (hold[c3] == '\0') {
break;
}
c3++;
}
}
//
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
void Commands::chdir(string& currentPath, string sInput) {
stringstream getin(sInput);
string command;
string driveName;
getin >> command >> driveName;
if (driveName != "") {
if (driveName[1] != ':') {
cout << "The system cannot find the path specified." << endl << endl;
}
else if (driveName.length() == 2) {
loadCurrentDir(currentPath, driveName); //loadCurrent dir by the name of drive name
}
}
else {
loadCurrentDir(currentPath, driveName); // loadCurrent dir by the current path
}
}
//
struct driveStruct {
char driveName = 0;
string driveDir; //do you know any PC with more than 28 drives? I don't :]
} currentPathOnDrive[28];
//
void Commands::saveCurrentDir(string& currentPath) { //saves the last path we have been in ,in each drive
char driveName = tolower(currentPath[0]);
for (auto& i : currentPathOnDrive) { //check if the drive already has a value
if (i.driveName == 0) {
i.driveName = driveName;
i.driveDir = currentPath;
break;
}
else if (i.driveName == driveName) { // make a room for this drive for the first time
i.driveDir = currentPath;
break;
}
}
}
//
void Commands::loadCurrentDir(string& currentPath, string driveName) {
if (driveName == "") {
driveName = tolower(currentPath[0]);
}
bool isFound = false;
for (auto& i : currentPathOnDrive) { //search to see if we have been in that drive or not
if (i.driveName == driveName[0]) { //if found
cout << i.driveDir << endl << endl;
isFound = true;
break;
}
}
if (!isFound) //if not found we have not been there
cout << driveName[0] << ":\\" << endl << endl;
}
//
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
void Commands::copy(string address01, string address02) {
//// code in the specified section would make it possible to copy a file with different name for example copy a.txt and see b.txt in the folder we want
int leng = address02.length() - 1; //but it is ignored cause we check and only copy in another folder
bool isFIL = false;
while (address02[leng] != '\\') {
if (address02[leng] == '.') {
isFIL = true;
}
leng--;
}
////
if (!isFIL) { // if isFIL was true we had a different name to copy the file to in destination
int len = address01.length() - 1;
bool isFILE = false;
while (address01[len] != '\\')
{
if (address01[len] == '.') {
isFILE = true;
}
len--;
}
//get the file name and add it to address02 so we make a file there with that name and start copying
int i = len + 1;
string lastword = "";
if (isFILE) {
while (address01[i] != '\0') {
lastword += address01[i];
i++;
}
}
address02 += "\\" + lastword;
}
//copy is done here
char* address1 = &address01[0];
char* address2 = &address02[0];
FILE* source, * destination;
char c;
source = fopen(address1, "rb"); //we need to read from address01
if (source == NULL) {
cout << "There is no such file in directory" << endl << endl;
}
else {
destination = fopen(address2, "wb"); //we need to write and create address02
while (!feof(source)) {
fscanf(source, "%c", &c);
fprintf(destination, "%c", c);
}
fclose(source);
fclose(destination);
cout << " 1 file copied." << endl << endl;
}
}
//
void Commands::cut(string address01, string address02) {
//// code in the specified section would make it possible to cut a file with different name for exaple copy a.txt and see b.txt in the folder we want
int leng = address02.length() - 1; //but it is ignored cause we check and only copy in another folder
bool isFIL = false;
while (address02[leng] != '\\') {
if (address02[leng] == '.') {
isFIL = true;
}
leng--;
}
////
if (!isFIL) { // if isFIL was true we had a different name to copy the file to in destination
int len = address01.length() - 1;
bool isFILE = false;
while (address01[len] != '\\')
{
if (address01[len] == '.') {
isFILE = true;
}
len--;
}
//get the file name and add it to address02 so we make a file there with that name and start cutting
int i = len + 1;
string lastword = "";
if (isFILE) {
while (address01[i] != '\0') {
lastword += address01[i];
i++;
}
}
address02 += "\\" + lastword;
}
//cut is copy but you delete what you copied in source address
char* address1 = &address01[0];
char* address2 = &address02[0];
FILE* source, * destination;
char c;
source = fopen(address1, "rb");
if (source == NULL) {
printf("There is no such file in directory");
}
destination = fopen(address2, "wb");
while (!feof(source)) {
fscanf(source, "%c", &c);
fprintf(destination, "%c", c);
}
fclose(source);
fclose(destination);
del(address01);
}
//
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
void Commands::del(string address) { //deletes a file
const char* add = &address[0];
remove(add);
}
//
void Commands::del1(string address) {
const char* add = &address[0];
if (remove(add) == 0) {
cout << endl;
}
else cout << "there is a problem!" << endl;
}
//
void Commands::del2(string address) {
char answer;
cout << endl << "ARE YOU SURE?(Y/N) : ";
cin >> answer;
getchar(); //the damaging \n would be terminated
if (answer == 'y' || answer == 'Y') {
const char* add = &address[0];
if (remove(add) == 0) {
cout << endl;
}
else cout << "there is a problem!" << endl;
}
}
//
void Commands::deldir(string address) {
const char* add = &address[0];
struct dirent* entry; /*/*/
DIR* dir; /*/*/
dir = opendir(add); //open directory of the address to check directories in it
while (entry = readdir(dir))
{
DIR* subDir;
FILE* file;
char a[100] = { 0 };
if (*(entry->d_name) != '.')
{
sprintf(a, "%s\\%s", add, entry->d_name); // a = add \\ entry->name this can be adress of a directoy or file
if (subDir = opendir(a)) //checks for being a directory
{
closedir(subDir);
deldir(a); //recrusivly deletes directory
}
else
{
if (file = fopen(a, "r")) //checks for being a file
{
fclose(file);
del(a);
}
}
}
}
del(add);
_rmdir(add); //after deleting everything in it we delete it
}
//
//\//\//\//\\//\\//\\//\\//\\//\\//\\//\\//
void Commands::renam(string address1, string address2) {