2024-08-19 02:35:16 +03:00
|
|
|
#include "module.h"
|
2024-08-20 02:38:40 +03:00
|
|
|
#include "../settings.h"
|
2024-08-24 00:45:47 +03:00
|
|
|
#include <codecvt>
|
2024-08-19 02:35:16 +03:00
|
|
|
#include <fstream>
|
2024-08-20 02:38:40 +03:00
|
|
|
#include <iostream>
|
2024-08-24 00:45:47 +03:00
|
|
|
#include <locale>
|
2024-08-19 02:35:16 +03:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <regex>
|
2024-08-20 02:38:40 +03:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
std::string to_utf8(std::wstring wide_string) {
|
|
|
|
static std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
|
|
|
|
return utf8_conv.to_bytes(wide_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring from_utf8(std::string string) {
|
|
|
|
static std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
|
|
|
|
return utf8_conv.from_bytes(string);
|
|
|
|
}
|
2024-08-19 02:35:16 +03:00
|
|
|
|
|
|
|
Module::Module() {}
|
|
|
|
|
|
|
|
Module::Module(std::string path) {
|
|
|
|
std::ifstream settings_file(path);
|
|
|
|
nlohmann::json settings = nlohmann::json::parse(settings_file);
|
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
this->name = from_utf8(settings["name"]);
|
|
|
|
this->goods_name_regex = from_utf8(settings["goods_name_regex"]);
|
|
|
|
this->goods_price_regex = from_utf8(settings["goods_price_regex"]);
|
|
|
|
this->goods_quantity_regex = from_utf8(settings["goods_quantity_regex"]);
|
2024-08-24 00:45:47 +03:00
|
|
|
this->check_start_regex = from_utf8(settings["check_start_regex"]);
|
|
|
|
this->check_end_regex = from_utf8(settings["check_end_regex"]);
|
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
std::wcout << "Name: " << this->name << std::endl;
|
|
|
|
std::wcout << "Goods name regex: " << this->goods_name_regex << std::endl;
|
|
|
|
std::wcout << "Goods price regex: " << this->goods_price_regex << std::endl;
|
2024-08-24 00:45:47 +03:00
|
|
|
std::wcout << "Goods quantity regex: " << this->goods_quantity_regex
|
|
|
|
<< std::endl;
|
|
|
|
std::wcout << "Check start regex: " << this->check_start_regex << std::endl;
|
|
|
|
std::wcout << "Check end regex: " << this->check_end_regex << std::endl;
|
2024-08-20 02:38:40 +03:00
|
|
|
#endif
|
2024-08-19 02:35:16 +03:00
|
|
|
}
|
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
std::vector<std::string> Module::parse_name(std::wstring str) {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::wregex r(this->goods_name_regex, std::regex::collate);
|
2024-08-19 02:35:16 +03:00
|
|
|
|
2024-08-24 00:45:47 +03:00
|
|
|
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
|
|
|
it++) {
|
2024-08-20 02:38:40 +03:00
|
|
|
result.push_back(to_utf8(it->str()));
|
|
|
|
}
|
2024-08-19 02:35:16 +03:00
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
return result;
|
2024-08-19 02:35:16 +03:00
|
|
|
}
|
2024-08-24 00:45:47 +03:00
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
std::vector<std::string> Module::parse_price(std::wstring str) {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::wregex r(this->goods_price_regex, std::regex::collate);
|
2024-08-19 02:35:16 +03:00
|
|
|
|
2024-08-24 00:45:47 +03:00
|
|
|
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
|
|
|
it++) {
|
2024-08-20 02:38:40 +03:00
|
|
|
result.push_back(to_utf8(it->str()));
|
|
|
|
}
|
2024-08-19 02:35:16 +03:00
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
return result;
|
2024-08-19 02:35:16 +03:00
|
|
|
}
|
2024-08-24 00:45:47 +03:00
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
std::vector<std::string> Module::parse_quantity(std::wstring str) {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::wregex r(this->goods_quantity_regex, std::regex::collate);
|
2024-08-19 02:35:16 +03:00
|
|
|
|
2024-08-24 00:45:47 +03:00
|
|
|
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
|
|
|
it++) {
|
2024-08-20 02:38:40 +03:00
|
|
|
result.push_back(to_utf8(it->str()));
|
|
|
|
}
|
|
|
|
return result;
|
2024-08-19 02:35:16 +03:00
|
|
|
}
|
|
|
|
|
2024-08-24 00:45:47 +03:00
|
|
|
std::wstring Module::trim_check(std::wstring& check) {
|
|
|
|
unsigned int start_pos;
|
|
|
|
unsigned int end_pos;
|
|
|
|
|
|
|
|
std::wregex start_regex(this->check_start_regex, std::regex::collate);
|
|
|
|
std::wregex end_regex(this->check_end_regex, std::regex::collate);
|
|
|
|
|
|
|
|
for (std::wsregex_iterator it{check.begin(), check.end(), start_regex}, end{};
|
|
|
|
it != end; it++) {
|
|
|
|
start_pos = it->position() + it->str().size();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
check = check.substr(start_pos, check.size());
|
|
|
|
|
|
|
|
for (std::wsregex_iterator it{check.begin(), check.end(), end_regex}, end{};
|
|
|
|
it != end; it++) {
|
|
|
|
end_pos = it->position() - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
check = check.substr(0, end_pos);
|
|
|
|
|
|
|
|
return check;
|
|
|
|
}
|
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
std::wstring Module::get_name() { return this->name; }
|