Skip to content

Commit 5910d3d

Browse files
committed
refactoring to use oop
1 parent 4d2c5dd commit 5910d3d

File tree

11 files changed

+124
-0
lines changed

11 files changed

+124
-0
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"editor.tabSize": 4,
3+
"editor.detectIndentation": false
4+
}

src/command.cpp

Whitespace-only changes.

src/command.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include <string>
3+
4+
class Command {
5+
public:
6+
virtual ~Command() = default;
7+
virtual void execute(const std::string& args) = 0;
8+
virtual std::string type() = 0;
9+
};

src/command_factory.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "command_factory.h"
2+
3+
CommandFactory::CommandFactory() {
4+
commands["exit"] = std::make_unique<ExitCommand>();
5+
commands["echo"] = std::make_unique<EchoCommand>();
6+
}
7+
8+
Command* CommandFactory::get(const std::string& name) {
9+
auto it = commands.find(name);
10+
return it != commands.end() ? it->second.get() : nullptr;
11+
}

src/command_factory.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
#include <map>
3+
#include <memory>
4+
#include <string>
5+
#include "command.h"
6+
#include "exit_command.h"
7+
#include "echo_command.h"
8+
9+
class CommandFactory {
10+
std::map<std::string, std::unique_ptr<Command>> commands;
11+
public:
12+
CommandFactory();
13+
Command* get(const std::string& name);
14+
};

src/echo_command.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "echo_command.h"
2+
#include <iostream>
3+
4+
void EchoCommand::execute(const std::string& args) {
5+
std::cout << args << std::endl;
6+
}
7+
8+
std::string EchoCommand::type() {
9+
return "builtin";
10+
}

src/echo_command.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
#include <string>
3+
#include "command.h"
4+
5+
class EchoCommand : public Command {
6+
public:
7+
void execute(const std::string& args) override;
8+
9+
std::string type() override;
10+
};

src/exit_command.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "exit_command.h"
2+
#include <iostream>
3+
4+
void ExitCommand::execute(const std::string& args) {
5+
int exit_code = std::stoi(args);
6+
exit(exit_code);
7+
}
8+
9+
std::string ExitCommand::type() {
10+
return "builtin";
11+
}

src/exit_command.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
#include <string>
3+
#include "command.h"
4+
5+
class ExitCommand : public Command {
6+
public:
7+
void execute(const std::string& args) override;
8+
9+
std::string type() override;
10+
};

src/shell.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "shell.h"
2+
#include <iostream>
3+
#include <sstream>
4+
5+
void Shell::run() {
6+
// Flush after every std::cout / std:cerr
7+
std::cout << std::unitbuf;
8+
std::cerr << std::unitbuf;
9+
10+
std::string input;
11+
while (true) {
12+
std::cout << "$ ";
13+
if(!std::getline(std::cin, input) || input.empty()) {
14+
break;
15+
}
16+
std::istringstream iss(input);
17+
std::string cmd, args;
18+
iss >> cmd;
19+
std::getline(iss, args);
20+
21+
22+
// trim leading whitespace
23+
size_t start = args.find_first_not_of(" ");
24+
if (start != std::string::npos) {
25+
args = args.substr(start);
26+
} else {
27+
args.clear();
28+
}
29+
30+
if(auto* command = commandFactory.get(cmd)) {
31+
command->execute(args);
32+
} else {
33+
std::cout << cmd << ": command not found" << std::endl;
34+
}
35+
}
36+
};

0 commit comments

Comments
 (0)