diff --git a/backend/include/bs-utility.h b/backend/include/bs-utility.h index 6598136..32c7ee0 100644 --- a/backend/include/bs-utility.h +++ b/backend/include/bs-utility.h @@ -140,4 +140,21 @@ class OperationLogItemClass { char command[256]; int fid; }; + +enum OperationType { + __Ksu, + __Klogout, + __Kregister, + __Kpasswd, + __Kuseradd, + __Kdelete, + __Kshow, + __Kbuy, + __Kselect, + __Kmodify, + __Kimport, + __Kshowfinance, + __Klog, + __Kreport, +}; #endif // PROTECTOR_UTILITY_H \ No newline at end of file diff --git a/backend/include/engine.h b/backend/include/engine.h index 3790c92..d9f4f2e 100644 --- a/backend/include/engine.h +++ b/backend/include/engine.h @@ -5,13 +5,14 @@ #include #include "database.h" +#include "lexer.h" class BookStoreEngineClass { std::string config_dir; UserDataBase user_data_base; - + bool is_server; public: BookStoreEngineClass() = delete; - BookStoreEngineClass(std::string __config_dir); + BookStoreEngineClass(std::string __config_dir, bool __is_server); std::vector Execute(const std::string &cmd, std::stack &login_stack); }; diff --git a/backend/include/lexer.h b/backend/include/lexer.h new file mode 100644 index 0000000..81ec949 --- /dev/null +++ b/backend/include/lexer.h @@ -0,0 +1,10 @@ +#ifndef PROTECTOR_LEXER_H +#define PROTECTOR_LEXER_H +#include +#include +#include +#include +bool CommandShowLexer(const std::string &command, std::string &ISBN, + std::string &name, std::string &author, + std::string &keyword); +#endif // PROTECTOR_LEXER_H \ No newline at end of file diff --git a/backend/src/builtin-cli.cpp b/backend/src/builtin-cli.cpp index 5ed1d3b..a681f7c 100644 --- a/backend/src/builtin-cli.cpp +++ b/backend/src/builtin-cli.cpp @@ -9,7 +9,7 @@ #include "bs-utility.h" #include "engine.h" void BookStoreMain(bool is_server, std::string config_dir) { - BookStoreEngineClass engine(config_dir); + BookStoreEngineClass engine(config_dir, is_server); std::ios::sync_with_stdio(false); if (!is_server) { std::stack login_stack; diff --git a/backend/src/engine.cpp b/backend/src/engine.cpp index 849db28..8e14942 100644 --- a/backend/src/engine.cpp +++ b/backend/src/engine.cpp @@ -2,18 +2,78 @@ #include #include +#include #include "bs-utility.h" -BookStoreEngineClass::BookStoreEngineClass(std::string __config_dir) { +BookStoreEngineClass::BookStoreEngineClass(std::string __config_dir, + bool __is_server) { config_dir = __config_dir; user_data_base.Open(config_dir + "user"); + is_server = __is_server; } std::vector BookStoreEngineClass::Execute( const std::string &cmd, std::stack &login_stack) { - if (cmd == "quit" || cmd == "exit") { - BookStore_ZYM::shut_down = true; + static std::unordered_map operation_map = { + {"su", OperationType::__Ksu}, + {"logout", OperationType::__Klogout}, + {"useradd", OperationType::__Kuseradd}, + {"register", OperationType::__Kregister}, + {"delete", OperationType::__Kdelete}, + {"passwd", OperationType::__Kpasswd}, + {"select", OperationType::__Kselect}, + {"modify", OperationType::__Kmodify}, + {"import", OperationType::__Kimport}, + {"show", OperationType::__Kshow}, + {"buy", OperationType::__Kbuy}, + {"report", OperationType::__Kreport}, + {"log", OperationType::__Klog}}; + std::stringstream ss(cmd); + std::string head = ""; + ss >> head; + if (head == "quit" || head == "exit") { + if (!is_server) BookStore_ZYM::shut_down = true; return std::vector(); } + if (operation_map.find(head) == operation_map.end()) { + for (int i = 0; i < cmd.length(); i++) + if (cmd[i] != ' ') return std::vector({"Invalid"}); + return std::vector(); + } + switch (operation_map[head]) { + case OperationType::__Ksu: { + } + case OperationType::__Klogout: { + } + case OperationType::__Kuseradd: { + } + case OperationType::__Kregister: { + } + case OperationType::__Kdelete: { + } + case OperationType::__Kpasswd: { + } + case OperationType::__Kselect: { + } + case OperationType::__Kmodify: { + } + case OperationType::__Kimport: { + } + case OperationType::__Kshow: { + ss >> head; + if (head == "finance") goto dst_showfinance; + } + case OperationType::__Kshowfinance: { + dst_showfinance:; + } + case OperationType::__Kbuy: { + } + case OperationType::__Kreport: { + throw FatalError("report Not implemented", 2); + } + case OperationType::__Klog: { + throw FatalError("log Not implemented", 3); + } + } return std::vector({cmd}); } \ No newline at end of file diff --git a/backend/src/lexer.cpp b/backend/src/lexer.cpp new file mode 100644 index 0000000..dee0b79 --- /dev/null +++ b/backend/src/lexer.cpp @@ -0,0 +1,45 @@ +#include "lexer.h" +bool CommandShowLexer(const std::string &command, std::string &ISBN, + std::string &name, std::string &author, + std::string &keyword) { + static std::basic_regex main_pattern( + R"(^ *show(?: +-ISBN=(?:\S{1,20})| +-name=\"(?:[^\s"]{1,60})\"| +-author=\"(?:[^\s"]{1,60})\"| +-keyword=\"(?:[^\s"]{1,60})\")* *$)", + std::regex_constants::optimize); + + std::smatch results; + bool has_ISBN = false; + bool has_name = false; + bool has_author = false; + bool has_keyword = false; + ISBN = ""; + name = ""; + author = ""; + keyword = ""; + if (std::regex_match(command, results, main_pattern)) { + std::stringstream ss(command); + std::string token; + ss >> token; + while (ss >> token) { + if (token[1] == 'I') { + if (has_ISBN) return false; + has_ISBN = true; + ISBN = token.substr(6); + } else if (token[1] == 'n') { + if (has_name) return false; + has_name = true; + name = token.substr(6 + 1, token.size() - 7 - 1); + } else if (token[1] == 'a') { + if (has_author) return false; + has_author = true; + author = token.substr(8 + 1, token.size() - 9 - 1); + } else if (token[1] == 'k') { + if (has_keyword) return false; + has_keyword = true; + keyword = token.substr(9 + 1, token.size() - 10 - 1); + } else + return false; + } + return true; + } else + return false; +} \ No newline at end of file