-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cpp
More file actions
131 lines (118 loc) · 5.33 KB
/
Commands.cpp
File metadata and controls
131 lines (118 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
#include "Commands.h"
// SORT BY SIZE FUNCTIONS
bool Commands::compareSizeAscending(const Item* lhs, const Item* rhs) {
return lhs->getSize() < rhs->getSize();
}
bool Commands::compareSizeDescending(const Item* lhs, const Item* rhs) {
return lhs->getSize() > rhs->getSize();
}
void Commands::sortSize(std::vector<Item*>& contents, const std::vector<std::string>& input) {
if (input.size() == 1) {
sort(contents.begin(), contents.end(), compareSizeAscending);
std::cout << "Sorted by size in ascending order" << std::endl;
}
else if (input[1] == "/r") {
sort(contents.begin(), contents.end(), compareSizeDescending);
std::cout << "Sorted by size in descending order" << std::endl;
}
}
// SORT BY NAME FUNCTIONS
bool Commands::compareNameAscending(const Item* lhs, const Item* rhs) {
return lhs->getName() < rhs->getName();
}
bool Commands::compareNameDescending(const Item* lhs, const Item* rhs) {
return lhs->getName() > rhs->getName();
}
void Commands::sortName(std::vector<Item*>& contents, const std::vector<std::string>& input) {
if (input.size() == 1) {
sort(contents.begin(), contents.end(), compareNameAscending);
std::cout << "Sorted alphabetically in ascending order" << std::endl;
}
else if (input[1] == "/r") {
sort(contents.begin(), contents.end(), compareNameDescending);
std::cout << "Sorted alphabetically in descending order" << std::endl;
}
}
// MKFILE FUNCTION
void Commands::createFile(std::vector<Item*>& contents, const std::vector<std::string>& input) {
if (input.size() == 1 || input.size() > 2) {
std::cout << "Invalid filename entered. Filenames cannot include spaces" << std::endl;
return;
}
auto check = find_if(contents.begin(), contents.end(), [&](Item* const x) {
// same principle as the folder checker
File* f = dynamic_cast<File*>(x);
return f != nullptr && x->getName() == input[1];
});
if (check == contents.end()) {
time_t t = time(0);
tm now;
localtime_s(&now, &t);
srand(time(NULL));
int random = rand() % 1000;
contents.push_back(new File(input[1], now, random));
std::cout << input[1] << " created succesfully" << std::endl;
}
else {
std::cout << "File already exists" << std::endl;
}
}
void Commands::createFolder(std::vector<Item*>& contents, const std::vector<std::string>& input, Folder* parent) {
if (input.size() == 1 || input.size() > 2) {
std::cout << "Invalid Folder name entered. Names cannot include spaces" << std::endl;
return;
}
auto check = find_if(contents.begin(), contents.end(), [&](Item* const x) {
// perform a cast to ensure that it is of type Folder
Folder* f = dynamic_cast<Folder*>(x);
// if the cast was successful (x is of Folder* type)
// AND if the name matches the input value, the folder must already exist
// if no object is found that matches both checks, the iterator returns the last iterator (beyond end of vector range)
return f != nullptr && x->getName() == input[1];
});
// if check is last iterator, meaning nothing was found
if (check == contents.end()) {
time_t t = time(0);
tm now;
localtime_s(&now, &t);
contents.push_back(new Folder(input[1], now, 0, parent));
}
else {
std::cout << "Directory already exists" << std::endl;
}
}
void Commands::showDirectory(Folder* current) {
// lambda function based off of tutorial 6's list manipulation
int files = 0; // counters for number of files, folders
int folders = 0;
__int64 contents = 0; // initialising variable for total files' size
const auto& currentDirectory = current->getContents();
std::for_each(currentDirectory.cbegin(), currentDirectory.cend(), [&](Item* x) { // & used to access variables outside the scope of the lambda
if (x != nullptr) {
x->printDetails(); // print all details of every object, regardless of type
//dynamic_cast<File*>(x) ? files++ : folders++; // performs a cast with ternary counter if(file)++ else folder++
x->isFile() ? files++ : folders++;
contents += x->getSize(); // summation size of all files in current directory
}
});
std::cout << "\t" << files << " File(s)\t" << contents << " bytes" << std::endl;
std::cout << "\t" << folders << " Dir(s)" << std::endl;
}
void Commands::deleteItem(Folder* current, const std::vector<std::string>& input) {
if (input.size() > 1) {
std::vector<Item*>& contents = current->editContents();
// check to see if the item exists in the vector
auto check = find_if(contents.begin(), contents.end(), [&](Item* const x) { return x->getName() == input[1]; });
// if check is not the iterator to the end of the vector
if (check != contents.end()) {
contents.erase(check);
std::cout << input[1] << " was sucesfully deleted" << std::endl;
}
else {
std::cout << input[1] << " could not be found." << std::endl;
}
}
else {
std::cout << "Please enter file or directory name to delete" << std::endl;
}
}