checks-parser/parser/module.cpp

94 lines
3.0 KiB
C++
Raw Permalink Normal View History

2024-11-22 23:26:42 +03:00
#include "module.h"
#include <fstream>
#include <nlohmann/json.hpp>
#include <regex>
#include <string>
#include "../utils/utils.h"
StoreModule::StoreModule() {}
StoreModule::StoreModule(std::string path) {
std::ifstream settings_file(path);
nlohmann::json settings = nlohmann::json::parse(settings_file);
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"]);
this->check_start_regex = from_utf8(settings["check_start_regex"]);
this->check_end_regex = from_utf8(settings["check_end_regex"]);
#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;
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;
#endif
}
std::vector<std::string> StoreModule::parse_name(std::wstring str) {
std::vector<std::string> result;
std::wregex r(this->goods_name_regex, std::regex::collate);
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
it++) {
result.push_back(to_utf8(it->str()));
}
return result;
}
std::vector<std::string> StoreModule::parse_price(std::wstring str) {
std::vector<std::string> result;
std::wregex r(this->goods_price_regex, std::regex::collate);
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
it++) {
result.push_back(to_utf8(it->str()));
}
return result;
}
std::vector<std::string> StoreModule::parse_quantity(std::wstring str) {
std::vector<std::string> result;
std::wregex r(this->goods_quantity_regex, std::regex::collate);
for (std::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
it++) {
result.push_back(to_utf8(it->str()));
}
return result;
}
std::wstring StoreModule::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;
}
std::wstring StoreModule::get_name() { return this->name; }