starting work on export module, added check trimming
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
#include "module.h"
|
||||
#include "../settings.h"
|
||||
#include <codecvt>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <locale>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
|
||||
std::string to_utf8(std::wstring wide_string) {
|
||||
static std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
|
||||
@@ -28,11 +28,17 @@ Module::Module(std::string path) {
|
||||
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 << "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
|
||||
}
|
||||
|
||||
@@ -40,30 +46,61 @@ 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);
|
||||
|
||||
for (std::wsregex_iterator it {str.begin(), str.end(), r}, end {}; it != end; it ++) {
|
||||
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> Module::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 ++) {
|
||||
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> Module::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 ++) {
|
||||
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 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;
|
||||
}
|
||||
|
||||
std::wstring Module::get_name() { return this->name; }
|
||||
|
||||
@@ -10,6 +10,8 @@ class Module {
|
||||
std::wstring goods_name_regex;
|
||||
std::wstring goods_price_regex;
|
||||
std::wstring goods_quantity_regex;
|
||||
std::wstring check_start_regex;
|
||||
std::wstring check_end_regex;
|
||||
public:
|
||||
Module(std::string);
|
||||
Module();
|
||||
@@ -17,6 +19,8 @@ public:
|
||||
std::vector<std::string> parse_name(std::wstring);
|
||||
std::vector<std::string> parse_price(std::wstring);
|
||||
std::vector<std::string> parse_quantity(std::wstring);
|
||||
std::wstring trim_check(std::wstring&);
|
||||
|
||||
|
||||
std::wstring get_name();
|
||||
};
|
||||
|
||||
@@ -27,11 +27,13 @@ std::vector<std::string> Parser::search_modules() {
|
||||
return modules_files;
|
||||
}
|
||||
|
||||
void Parser::set_module(std::string path) { module = *(new Module(path)); }
|
||||
void Parser::set_module(std::string path) { module = Module(path); }
|
||||
|
||||
std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
|
||||
std::vector<Goods> result;
|
||||
|
||||
module.trim_check(check_plaintext);
|
||||
|
||||
std::vector<std::string> goods_names = module.parse_name(check_plaintext);
|
||||
std::vector<std::string> goods_prices = module.parse_price(check_plaintext);
|
||||
std::vector<std::string> goods_quantities =
|
||||
@@ -42,7 +44,7 @@ std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
|
||||
if (goods_names.size() != goods_prices.size() ||
|
||||
goods_names.size() != goods_quantities.size() ||
|
||||
goods_prices.size() != goods_quantities.size()) {
|
||||
std::cerr << "An error has occured. Check was matched incorrectly. Vector sizes are different" << std::endl;
|
||||
//Error. Amount of names, prices or quantities are not equal. That means, that some regex(es) has mismatched.
|
||||
return {};
|
||||
}
|
||||
short goods_amount = goods_names.size();
|
||||
|
||||
Reference in New Issue
Block a user