This commit is contained in:
IlyaShurupov 2024-03-27 17:24:48 +03:00
parent 31fa6e96bb
commit 34d04c02b0
3 changed files with 28 additions and 10 deletions

View file

@ -5,6 +5,7 @@
class Interpreter {
struct Command {
const char* description = nullptr;
ui32 numArguments = 0;
bool(*callback)(FileSystem&, const std::vector<std::string>&) = nullptr;
};
@ -13,6 +14,7 @@ public:
Interpreter();
void interpret(const std::string& command);
void printHelp();
private:
static void reportError(const std::string& string);

View file

@ -235,7 +235,7 @@ void Directory::getMaxDepthUtil(ui32 depth, ui32& maxDepth) const {
}
ui32 Directory::getMaxDepth() const {
ui32 maxDepth = 0;
ui32 maxDepth = 1;
getMaxDepthUtil(1, maxDepth);
return maxDepth;
}
@ -280,7 +280,7 @@ bool FileSystem::changeCurrent(const Path& path) {
}
auto node = path.isAbsolute() ? root->findNode(path.getChain(), 0) : currentDirectory->findNode(path.getChain(), 0);
if (node->type != Node::DIRECTORY) {
if (!node || node->type != Node::DIRECTORY) {
gError = "No such directory";
return false;
}

View file

@ -10,22 +10,37 @@ void getWords(const std::string& in, std::vector<std::string>& out) {
}
Interpreter::Interpreter() {
mCommands["cd"] = { 1, [](FileSystem& filesystem, const std::vector<std::string>& args){
return filesystem.changeCurrent(args[1]);
}};
mCommands["cd"] = {
"change working directory",
1,
[](FileSystem& filesystem, const std::vector<std::string>& args){
return filesystem.changeCurrent(args[1]);
}
};
mCommands["md"] = { 1, [](FileSystem& filesystem, const std::vector<std::string>& args){
return filesystem.makeDirectory(args[1]);
}};
mCommands["md"] = {
"create directory",
1,
[](FileSystem& filesystem, const std::vector<std::string>& args){
return filesystem.makeDirectory(args[1]);
}
};
}
void Interpreter::reportError(const std::string& description) {
std::cout << "ERROR : " << description << std::endl;
}
void Interpreter::interpret(const std::string& command) {
std::cout << "\"" << command << "\"\n";
void Interpreter::printHelp() {
std::cout << "Commands: \n";
for (auto& command : mCommands) {
std::cout << command.first << " - ";
std::cout << command.second.description;
std::cout << "\n";
}
}
void Interpreter::interpret(const std::string& command) {
std::vector<std::string> words;
getWords(command, words);
@ -41,6 +56,7 @@ void Interpreter::interpret(const std::string& command) {
if (iter == mCommands.end()) {
reportError("Command not found");
printHelp();
return;
}