Daily update, added .cpp files for two commands: initialize.cpp and run.cpp

This commit is contained in:
Андреев Григорий 2024-08-16 00:06:35 +03:00
parent ef3af2ec45
commit ef4a6dec24
8 changed files with 110 additions and 1 deletions

2
.gitignore vendored
View File

@ -11,3 +11,5 @@ building/*.svg
.idea/
compile_commands.json
local.sh
iu9-ca-web-chat.db

View File

@ -145,6 +145,8 @@ struct CAWebChat {
"main.cpp",
"initialize.cpp",
"run.cpp",
"str_fields_check.cpp",
"find_db.cpp",
};
for (std::string& u: T.units)
u = "web_chat/" + u;

14
src/web_chat/find_db.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "find_db.h"
int find_db_sqlite_file_path(const json::JSON& config, std::string& res_path) {
const json::JSON& type = config["database"]["type"].g();
if (!type.isString() && type.asString() == "sqlite3")
return -1;
const json::JSON& path = config["database"]["file"].g();
if (!path.isString())
return -1;
if (path.asString().empty() || path.asString()[0] == ':')
return -1;
res_path = path.asString();
return 0;
}

8
src/web_chat/find_db.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef IU9_CA_WEB_CHAT_SRC_WEB_CHAT_FIND_DB_H
#define IU9_CA_WEB_CHAT_SRC_WEB_CHAT_FIND_DB_H
#include <jsonincpp/jsonobj.h>
int find_db_sqlite_file_path(const json::JSON& config, std::string& res_path);
#endif

View File

@ -1,5 +1,40 @@
#include "actions.h"
#include <engine_engine_number_9/baza_throw.h>
#include "str_fields_check.h"
#include <sqlite3.h>
#include <engine_engine_number_9/os_utils.h>
#include <find_db.h>
void initialize_website(const json::JSON& config, const std::string& root_pw) {
void sqlite_single_statement(sqlite3* db_hand_ptr, const std::string& req_statement) {
sqlite3_stmt* stmt_obj_ptr = NULL;
int ret = sqlite3_prepare16_v2(db_hand_ptr, req_statement.c_str(), -1, &stmt_obj_ptr, NULL);
een9_ASSERT(ret == 0, "Can't compile request expression");
struct Guard1{sqlite3_stmt*& r; ~Guard1(){if (sqlite3_finalize(r) != 0) {abort();}}} guard1{stmt_obj_ptr};
while (true) {
ret = sqlite3_step(stmt_obj_ptr);
if (ret == SQLITE_DONE)
break;
if (ret != SQLITE_ROW) {
printf("sqlite_row error!!!\n");
break;
}
}
printf("Request steps are done\n");
}
void initialize_website(const json::JSON& config, const std::string& root_pw) {
printf("Initialization...\n");
een9_ASSERT(check_password(root_pw), "Bad root password");
std::string db_path;
int ret;
ret = find_db_sqlite_file_path(config, db_path);
een9_ASSERT(ret == 0, "Invalid settings[\"database\"] field");
een9_ASSERT(!een9::isRegularFile(db_path), "Database file exists prior to initialization. "
"Can't preceed withut harming existing data");
sqlite3* db_hand_ptr = NULL;
ret = sqlite3_open(db_path.c_str(), &db_hand_ptr);
een9_ASSERT(ret == 0, "Can't open database");
struct Guard1{sqlite3*& dhp; ~Guard1(){if (sqlite3_close(dhp) != 0) {abort();}}} guard1{db_hand_ptr};
sqlite_single_statement(db_hand_ptr, "CREATE TABLE tb(a INT, b INT);");
}

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <jsonincpp/string_representation.h>
#include "actions.h"
#include <stdexcept>
void usage(char** argv) {
printf("Usage: %s <file with settings>\n", argv[0]);

View File

@ -0,0 +1,37 @@
#include "str_fields_check.h"
#include <jsonincpp/utf8.h>
bool isALPHA(char ch) {
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z');
}
bool isNUM(char ch) {
return '0' <= ch && ch <= '9';
}
bool isUNCHAR(char ch) {
return isALPHA(ch) || isNUM(ch) || ch == '-' || ch == '_';
}
bool isSPACE(char ch) {
return ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n';
}
bool check_password(const std::string &pwd) {
return isUtf8String(pwd) && pwd.size() >= 8;
}
bool check_name(const std::string &name) {
return isUtf8String(name);
}
bool check_nickname(const std::string &nickname) {
if (nickname.empty())
return false;
for (char ch: nickname) {
if (!isUNCHAR(ch))
return false;
}
return true;
}

View File

@ -0,0 +1,10 @@
#ifndef IU9_CA_WEB_CHAT_SRC_WEB_CHAT_STR_FIELDS_CHECK_H
#define IU9_CA_WEB_CHAT_SRC_WEB_CHAT_STR_FIELDS_CHECK_H
#include <string>
bool check_password(const std::string& pwd);
bool check_name(const std::string& name);
bool check_nickname(const std::string& nickname);
#endif