bettola/common/src/command_processor.cpp

46 lines
1.3 KiB
C++

#include "command_processor.h"
#include "vfs.h"
CommandProcessor::CommandProcessor(vfs_node* starting_dir) {
_current_dir = starting_dir;
}
vfs_node* CommandProcessor::get_current_dir(void) {
return _current_dir;
}
std::string CommandProcessor::process_command(const std::string& command) {
if(command.rfind("cd ", 0) == 0) {
std::string target_dir_name = command.substr(3);
if(target_dir_name == "..") {
if(_current_dir->parent) {
_current_dir = _current_dir->parent;
}
} else if(_current_dir->children.count(target_dir_name)) {
vfs_node* target_node = _current_dir->children[target_dir_name];
if(target_node->type == DIR_NODE) {
_current_dir = target_node;
} else {
return "cd: not a directory\n";
}
} else {
return "cd: no such file or directory\n";
}
return get_full_path(_current_dir);
} else if(command == "ls") {
std::string response = "";
if(_current_dir && _current_dir->type == DIR_NODE) {
for(auto const& [name, node] : _current_dir->children) {
response += name;
if(node->type == DIR_NODE) {
response += "/";
}
response += " ";
}
}
return response;
}
return "Unknown command: " + command + "\n";
}