upd: ready to lex the command
This commit is contained in:
@ -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
|
@ -5,13 +5,14 @@
|
||||
#include <vector>
|
||||
|
||||
#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<std::string> Execute(const std::string &cmd,
|
||||
std::stack<std::string> &login_stack);
|
||||
};
|
||||
|
10
backend/include/lexer.h
Normal file
10
backend/include/lexer.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef PROTECTOR_LEXER_H
|
||||
#define PROTECTOR_LEXER_H
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
bool CommandShowLexer(const std::string &command, std::string &ISBN,
|
||||
std::string &name, std::string &author,
|
||||
std::string &keyword);
|
||||
#endif // PROTECTOR_LEXER_H
|
@ -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<std::string> login_stack;
|
||||
|
@ -2,18 +2,78 @@
|
||||
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#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<std::string> BookStoreEngineClass::Execute(
|
||||
const std::string &cmd, std::stack<std::string> &login_stack) {
|
||||
if (cmd == "quit" || cmd == "exit") {
|
||||
BookStore_ZYM::shut_down = true;
|
||||
static std::unordered_map<std::string, OperationType> 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<std::string>();
|
||||
}
|
||||
if (operation_map.find(head) == operation_map.end()) {
|
||||
for (int i = 0; i < cmd.length(); i++)
|
||||
if (cmd[i] != ' ') return std::vector<std::string>({"Invalid"});
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
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<std::string>({cmd});
|
||||
}
|
45
backend/src/lexer.cpp
Normal file
45
backend/src/lexer.cpp
Normal file
@ -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;
|
||||
}
|
Reference in New Issue
Block a user