-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileSystemCommands.cpp
87 lines (73 loc) · 3.06 KB
/
FileSystemCommands.cpp
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
#include "FileSystemCommands.hpp"
#include <fstream>
#include <iostream>
void ICommandWithPathOption::execute(const std::string& args) {
if (!args.empty()) {
ICommand::parametrizeCommand(args);
this->path = std::filesystem::path(ICommand::args.front());
} else {
this->path = std::filesystem::current_path();
}
}
void ICommandWithTwoPathOptions::execute(const std::string& args) {
if (!args.empty()) {
ICommand::parametrizeCommand(args);
this->pathFrom = std::filesystem::path(ICommand::args.front());
this->pathTo = std::filesystem::path(*(++ICommand::args.begin()));
} else {
this->pathFrom = std::filesystem::current_path();
this->pathTo = std::filesystem::current_path();
}
}
void ListCommand::execute(const std::string& args) {
ICommandWithPathOption::execute(args);
for (auto temp : std::filesystem::directory_iterator(ICommandWithPathOption::path)) {
std::cout << "\t> " << (--temp.path().end())->string() << "\n";
}
}
void PathCommand::execute(const std::string& args) {
static_cast<void>(args);
std::cout << "\t> " << std::filesystem::current_path().string() << "\n";
}
void RemoveCommand::execute(const std::string& args) {
ICommandWithPathOption::execute(args);
std::filesystem::remove(ICommandWithPathOption::path);
}
void RemoveDirectoryCommand::execute(const std::string& args) {
ICommandWithPathOption::execute(args);
std::filesystem::remove_all(ICommandWithPathOption::path);
}
void MakeDirectoryCommand::execute(const std::string& args) {
ICommandWithPathOption::execute(args);
std::filesystem::create_directory(ICommandWithPathOption::path);
}
void TouchFileCommand::execute(const std::string& args) {
ICommandWithPathOption::execute(args);
std::ofstream(ICommandWithPathOption::path.string());
}
void EnterDirectoryCommand::execute(const std::string& args) {
ICommandWithPathOption::execute(args);
std::filesystem::current_path(ICommandWithPathOption::path);
}
void PrintFileCommand::execute(const std::string& args) {
ICommandWithPathOption::execute(args);
std::ifstream fin(ICommandWithPathOption::path, std::ios_base::ate | std::ios_base::binary);
size_t size = fin.tellg();
char* content = new char[size];
fin.seekg(0);
fin.read(content, size);
std::cout << content << "\n";
delete[] content;
}
void CopyCommand::execute(const std::string& args) {
ICommandWithTwoPathOptions::execute(args);
std::filesystem::copy_file(ICommandWithTwoPathOptions::pathFrom, ICommandWithTwoPathOptions::pathTo, std::filesystem::copy_options::overwrite_existing);
}
void CopyDirectoryCommand::execute(const std::string& args) {
ICommandWithTwoPathOptions::execute(args);
std::filesystem::copy(ICommandWithTwoPathOptions::pathFrom, ICommandWithTwoPathOptions::pathTo, std::filesystem::copy_options::recursive);
}
void MoveCommand::execute(const std::string& args) {
ICommandWithTwoPathOptions::execute(args);
std::filesystem::rename(ICommandWithTwoPathOptions::pathFrom, ICommandWithTwoPathOptions::pathTo);
}