net weights implementation WIP
This commit is contained in:
@@ -1,11 +1,25 @@
|
||||
#include "module.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
#include "../utils/utils.h"
|
||||
#include <utils/utils.h>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
StoreModule::StoreModule() {}
|
||||
|
||||
std::vector<std::string> StoreModule::parse(std::wstring str, std::wstring regexp, boost::regex_constants::flag_type_ flag) {
|
||||
std::vector<std::string> result;
|
||||
boost::wregex r(regexp, flag);
|
||||
|
||||
for (boost::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
||||
it++) {
|
||||
result.push_back(to_utf8(it->str()));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
StoreModule::StoreModule(std::string path) {
|
||||
std::ifstream settings_file(path);
|
||||
nlohmann::json settings = nlohmann::json::parse(settings_file);
|
||||
@@ -13,54 +27,35 @@ StoreModule::StoreModule(std::string path) {
|
||||
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_net_weight_regex = from_utf8(settings["goods_net_weight_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;
|
||||
boost::wregex r(this->goods_name_regex, boost::regex_constants::extended);
|
||||
|
||||
for (boost::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
||||
it++) {
|
||||
result.push_back(to_utf8(it->str()));
|
||||
}
|
||||
|
||||
return result;
|
||||
return parse(str, this->goods_name_regex, boost::regex_constants::extended);
|
||||
}
|
||||
|
||||
std::vector<std::string> StoreModule::parse_price(std::wstring str) {
|
||||
return parse(str, this->goods_price_regex, boost::regex_constants::collate);
|
||||
}
|
||||
|
||||
std::vector<std::string> StoreModule::parse_net_weight(std::vector<std::string> &names) {
|
||||
std::vector<std::string> result;
|
||||
boost::wregex r(this->goods_price_regex, boost::regex::collate);
|
||||
|
||||
for (boost::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
||||
it++) {
|
||||
result.push_back(to_utf8(it->str()));
|
||||
for (std::string &name : names) {
|
||||
std::vector<std::string> parsed = parse(from_utf8(name), this->goods_net_weight_regex, boost::regex_constants::collate);
|
||||
if (parsed.size() == 0) {
|
||||
result.push_back("?");
|
||||
} else {
|
||||
result.push_back(parsed[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> StoreModule::parse_quantity(std::wstring str) {
|
||||
std::vector<std::string> result;
|
||||
boost::wregex r(this->goods_quantity_regex, boost::regex::collate);
|
||||
|
||||
for (boost::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
|
||||
it++) {
|
||||
result.push_back(to_utf8(it->str()));
|
||||
}
|
||||
return result;
|
||||
return parse(str, this->goods_quantity_regex, boost::regex_constants::collate);
|
||||
}
|
||||
|
||||
std::wstring StoreModule::trim_check(std::wstring& check) {
|
||||
|
||||
@@ -3,22 +3,28 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
class StoreModule {
|
||||
std::string path;
|
||||
std::wstring name;
|
||||
std::wstring goods_name_regex;
|
||||
std::wstring goods_price_regex;
|
||||
std::wstring goods_net_weight_regex;
|
||||
std::wstring goods_quantity_regex;
|
||||
std::wstring check_start_regex;
|
||||
std::wstring check_end_regex;
|
||||
|
||||
std::vector<std::string> parse(std::wstring, std::wstring, boost::regex_constants::flag_type_);
|
||||
public:
|
||||
StoreModule(std::string);
|
||||
StoreModule();
|
||||
|
||||
std::vector<std::string> parse_name(std::wstring);
|
||||
std::vector<std::string> parse_price(std::wstring);
|
||||
std::vector<std::string> parse_net_weight(std::vector<std::string> &names);
|
||||
std::vector<std::string> parse_quantity(std::wstring);
|
||||
|
||||
std::wstring trim_check(std::wstring&);
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "parser.h"
|
||||
#include "../goods/goods.h"
|
||||
#include "../net/net.h"
|
||||
#include "../settings/settings.h"
|
||||
#include "../utils/utils.h"
|
||||
|
||||
#include <goods/goods.h>
|
||||
#include <net/net.h>
|
||||
#include <settings/settings.h>
|
||||
#include <utils/utils.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
@@ -15,27 +16,6 @@
|
||||
using namespace std::filesystem;
|
||||
#endif
|
||||
|
||||
static void dumpVectorsToStdErr(std::vector<std::string> &goods_names, std::vector<std::string> &goods_prices, std::vector<std::string>& goods_quantities) {
|
||||
std::cerr << goods_names.size() << " " << goods_prices.size() << " " << goods_quantities.size() << std::endl;
|
||||
std::cerr << "Found goods names: ";
|
||||
for (auto &goods_name : goods_names) {
|
||||
std::cerr << goods_name << " ";
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "Found goods prices: ";
|
||||
for (auto &goods_price : goods_prices) {
|
||||
std::cerr << goods_price << " ";
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "Found goods names: ";
|
||||
for (auto &goods_quantity : goods_quantities) {
|
||||
std::cerr << goods_quantity << " ";
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
Parser::Parser() {}
|
||||
|
||||
std::vector<std::string> Parser::search_modules() {
|
||||
@@ -84,22 +64,21 @@ std::vector<Goods> Parser::parse(std::wstring 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 =
|
||||
module.parse_quantity(check_plaintext);
|
||||
std::vector<std::string> goods_net_weights = module.parse_net_weight(goods_names);
|
||||
std::vector<std::string> goods_quantities = module.parse_quantity(check_plaintext);
|
||||
|
||||
if (goods_names.size() != goods_prices.size() ||
|
||||
goods_names.size() != goods_quantities.size() ||
|
||||
goods_prices.size() != goods_quantities.size()) {
|
||||
|
||||
// dumpVectorsToStdErr(goods_names, goods_prices, goods_quantities);
|
||||
if (areAllSizesEqual(goods_names, goods_prices, goods_net_weights, goods_quantities)) {
|
||||
|
||||
//Error. Amount of names, prices or quantities are not equal. That means, that some regex(es) has mismatched.
|
||||
// dumpVectorsToStderr(goods_names, goods_prices, goods_net_weights, goods_quantities);
|
||||
|
||||
//Error. Amount of names, prices, net weights or quantities are not equal. That means, that some regex(es) has mismatched.
|
||||
return {};
|
||||
}
|
||||
short goods_amount = goods_names.size();
|
||||
|
||||
for (short i = 0; i < goods_amount; i++) {
|
||||
Goods goods(goods_names[i], std::stof(goods_prices[i]), std::stof(goods_quantities[i]));
|
||||
Goods goods(goods_names[i], std::stod(goods_prices[i]), goods_net_weights[i], std::stod(goods_quantities[i]));
|
||||
|
||||
result.push_back(goods);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user