diff --git a/src/commands/command.cpp b/src/commands/command.cpp deleted file mode 100644 index d9c8b77..0000000 --- a/src/commands/command.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "command.hpp" - -#include -#include -#include -#include -#include - -#include "utils.hpp" - -namespace command { - -std::unique_ptr MakeCommand(const std::string& command_string) { - auto splitted_command = utils::SplitString(command_string, " "); - - if (splitted_command.size() < 1) { - throw BadCommand("Command need have name"); - } - - if (splitted_command[0] == "put" || splitted_command[0] == "p") { - std::vector args; - args.insert(args.begin(), splitted_command.begin() + 1, - splitted_command.end()); - return std::make_unique(args); - } - - if (splitted_command[0] == "get" || splitted_command[0] == "g") { - std::vector args; - args.insert(args.begin(), splitted_command.begin() + 1, - splitted_command.end()); - return std::make_unique(args); - } - - if (splitted_command[0] == "exit" || splitted_command[0] == "e") { - return std::make_unique(); - } - - throw UnknownCommand("Unknown command for string" + command_string); -} - -} // namespace command diff --git a/src/commands/command.hpp b/src/commands/command.hpp deleted file mode 100644 index 4fd1112..0000000 --- a/src/commands/command.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -namespace command { - -class CommandError : public std::runtime_error { - public: - using std::runtime_error::runtime_error; -}; - -class UnknownCommand : public CommandError { - public: - using CommandError::CommandError; -}; - -class BadCommand : public CommandError { - public: - using CommandError::CommandError; -}; - -class BaseCommand { - public: - BaseCommand() = default; - virtual ~BaseCommand(){}; - virtual std::string Execute(std::map& storage) = 0; -}; - -std::unique_ptr MakeCommand(const std::string& command_string); - -} // namespace command diff --git a/src/commands/exit_command.cpp b/src/commands/exit_command.cpp deleted file mode 100644 index 6176ca3..0000000 --- a/src/commands/exit_command.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "exit_command.hpp" - -#include - -namespace command { - -ExitCommand::ExitCommand() {} - -std::string ExitCommand::Execute([ - [maybe_unused]] std::map &storage) { - exit(0); -} -} // namespace command diff --git a/src/commands/exit_command.hpp b/src/commands/exit_command.hpp deleted file mode 100644 index 67c0b7b..0000000 --- a/src/commands/exit_command.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "command.hpp" - -namespace command { - -class ExitCommand : public BaseCommand { - public: - ExitCommand(); - - std::string Execute(std::map& storage) override; -}; - -} // namespace command diff --git a/src/commands/get_command.cpp b/src/commands/get_command.cpp deleted file mode 100644 index a34e882..0000000 --- a/src/commands/get_command.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "get_command.hpp" - -#include - -namespace command { - -GetCommand::GetCommand(const std::vector &args) { - if (args.size() < 1) { - throw command::BadCommand( - "Bad arguments of command get, need at least 1 argument"); - } - - key = args[0]; -} - -std::string GetCommand::Execute(std::map &storage) { - std::stringstream result; - - result << "Get from storage[" << key << "]: " << storage[key]; - return result.str(); -} -} // namespace command diff --git a/src/commands/get_command.hpp b/src/commands/get_command.hpp deleted file mode 100644 index 148b6e7..0000000 --- a/src/commands/get_command.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "command.hpp" - -namespace command { - -class GetCommand : public BaseCommand { - public: - GetCommand(const std::vector& args); - - std::string Execute(std::map& storage) final; - - private: - std::string key; -}; -} // namespace command diff --git a/src/commands/put_command.cpp b/src/commands/put_command.cpp deleted file mode 100644 index 50df00f..0000000 --- a/src/commands/put_command.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "put_command.hpp" - -#include -#include - -namespace command { - -PutCommand::PutCommand(const std::vector& args) { - if (args.size() < 2) { - throw command::BadCommand("Bad arguments of command put"); - } - - key = args[0]; - value = args[1]; -} - -std::string PutCommand::Execute(std::map& storage) { - storage[key] = value; - - std::stringstream result; - - result << "Put in storage (key='" << key << "'" - << "value='" << value << ")" << std::endl; - return result.str(); -} - -} // namespace command diff --git a/src/commands/put_command.hpp b/src/commands/put_command.hpp deleted file mode 100644 index f910e51..0000000 --- a/src/commands/put_command.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "command.hpp" - -namespace command { - -class PutCommand : public BaseCommand { - public: - PutCommand(const std::vector& args); - - std::string Execute(std::map& storage) override; - - private: - std::string key; - std::string value; -}; -} // namespace command diff --git a/src/main.cpp b/src/main.cpp index 003f297..b6dfd92 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,24 +1,59 @@ -#include -#include #include -#include +#include +#include +#include +#include -#include "utils.hpp" +using Storage = std::map; -int main([[maybe_unused]] int argc, [[maybe_unused]] char const* argv[]) { +std::vector SplitString(const std::string& line) { + std::stringstream stream(line); + std::vector result; + while (stream) { + std::string part; + stream >> part; + result.push_back(part); + } + + return result; +} + +struct Command { + std::string command; + std::vector args; + Command(const std::string& command_string) { + auto splited_command = SplitString(command_string); + + command = splited_command.at(0); + + args.insert(args.begin(), splited_command.begin() + 1, + splited_command.end()); + } +}; + +int main() { std::map storage; while (true) { - try { - std::string buffer; - std::getline(std::cin, buffer); - - auto executed_command = command::MakeCommand(buffer); - auto result = executed_command->Execute(storage); - std::cout << result << std::endl; - } catch (const std::exception& e) { - std::cout << "Error: " << e.what() << std::endl; + std::string buffer; + std::getline(std::cin, buffer); + + const auto& command = Command(buffer); + + if (command.command == "Exit") { + std::cout << "Exit!" << std::endl; + exit(0); + } else if (command.command == "Put") { + storage[command.args.at(0)] = command.args.at(1); + std::cout << "Put [" << command.args[0] << "] = " << command.args[1] + << "\n"; + } else if (command.command == "Get") { + std::cout << command.args.at(0) << ": " << storage[command.args.at(0)]; + } else if (command.command == "help") { + std::cout + << "Available commands:\n Exit - [Exit] exit from program\nPut - " + "[Put (key) (value)]. Put in storage in key value\nGet - [Get " + "(value)]. Get value by (key)"; } } - return 0; } diff --git a/src/utils.cpp b/src/utils.cpp deleted file mode 100644 index 3374c9c..0000000 --- a/src/utils.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace utils { -std::vector SplitString(const std::string_view& data, - const std::string_view& separator) { - if (data.empty()) return {}; - - size_t first_separator = data.find(separator); - if (first_separator == data.npos) return {std::string{data}}; - - std::string first_part = std::string{data.substr(0, first_separator)}; - auto last_part = - SplitString(data.substr(first_separator + separator.size()), separator); - std::vector result{first_part}; - result.insert(result.end(), last_part.begin(), last_part.end()); - - return result; -} - -std::string ContatinateStrings(const std::vector& strings, - const std::string& separator) { - std::string result; - for (const auto& string : strings) { - result += separator + string; - } - return result; -} - -} // namespace utils diff --git a/src/utils.hpp b/src/utils.hpp deleted file mode 100644 index fbe0603..0000000 --- a/src/utils.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include -#include - -namespace utils { -std::vector SplitString(const std::string_view& data, - const std::string_view& separator); - -std::string ContatinateStrings(const std::vector& strings, - const std::string& separator); -} // namespace utils