diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user index dfe6e8f..76fe84d 100644 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -58,6 +58,35 @@ true + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + false + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 4 + true + + + + true + + + ProjectExplorer.Project.Target.0 @@ -92,6 +121,7 @@ false true + Build CMakeProjectManager.MakeStep 1 @@ -108,6 +138,7 @@ false true + Build CMakeProjectManager.MakeStep 1 @@ -364,12 +395,14 @@ false -e cpu-cycles --call-graph dwarf,4096 -F 250 - - ProjectExplorer.CustomExecutableRunConfiguration - + checks-parser + CMakeProjectManager.CMakeRunConfiguration.checks-parser + checks-parser false true + true true + /home/leca/projects/qt/checks-parser/build/Desktop-Debug 1 diff --git a/goods/goods.cpp b/goods/goods.cpp index fcaf428..b41578f 100644 --- a/goods/goods.cpp +++ b/goods/goods.cpp @@ -1,10 +1,10 @@ #include "goods.h" #include -Goods::Goods(std::string name, double quantity, double price_per_unit) { +Goods::Goods(std::string name, double price_per_unit, double quantity) { this->name = name; - this->quantity = quantity; this->price_per_unit = price_per_unit; + this->quantity = quantity; } double Goods::calculate_total_price() { diff --git a/mainwindow.cpp b/mainwindow.cpp index 33cb4d8..d0fb3cb 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -2,7 +2,7 @@ #include "./ui_mainwindow.h" #include "goods/goods.h" #include "check/check.h" - +#include "settings.h" #include MainWindow::MainWindow(QWidget *parent) @@ -17,9 +17,11 @@ void MainWindow::setupStoresList() { // Make file format that is a regeeex which parse check, load these files // here parser = *(new Parser()); +#ifdef DEBUG for (auto module : parser.search_modules()) { - std::cout << module << std::endl; + std::cout << "Module: " << module << std::endl; } +#endif } void MainWindow::on_checkType_currentIndexChanged(int index) { @@ -34,5 +36,15 @@ void MainWindow::on_checkType_currentIndexChanged(int index) { } void MainWindow::on_parseButton_clicked() { - std::cout << ui->checkContent->toPlainText().toStdString() << std::endl; + QString s(ui->checkContent->toPlainText()); + std::wstring check_plaintext = s.toStdWString(); + parser.set_module(parser.search_modules()[0]); + + std::vector c = parser.parse(check_plaintext); + + Check check; + + for (auto g : c) { + check.add_goods(g); + } } diff --git a/parser/module.cpp b/parser/module.cpp index 43112f4..93860dc 100644 --- a/parser/module.cpp +++ b/parser/module.cpp @@ -1,7 +1,22 @@ #include "module.h" +#include "../settings.h" #include +#include #include #include +#include +#include +#include + +std::string to_utf8(std::wstring wide_string) { + static std::wstring_convert> utf8_conv; + return utf8_conv.to_bytes(wide_string); +} + +std::wstring from_utf8(std::string string) { + static std::wstring_convert> utf8_conv; + return utf8_conv.from_bytes(string); +} Module::Module() {} @@ -9,35 +24,46 @@ Module::Module(std::string path) { std::ifstream settings_file(path); nlohmann::json settings = nlohmann::json::parse(settings_file); - this->name = settings["name"]; - this->goods_name_regex = settings["goods_name_regex"]; - this->goods_price_regex = settings["goods_price_regex"]; - this->goods_quantity_regex = settings["goods_quantity_regex"]; + 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"]); +#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; +#endif } -std::smatch Module::parse_name(std::string str) { - std::regex r(this->goods_name_regex); +std::vector Module::parse_name(std::wstring str) { + std::vector result; + std::wregex r(this->goods_name_regex, std::regex::collate); - std::smatch m; - regex_search(str, m, r); + for (std::wsregex_iterator it {str.begin(), str.end(), r}, end {}; it != end; it ++) { + result.push_back(to_utf8(it->str())); + } - return m; + return result; } -std::smatch Module::parse_price(std::string str) { - std::regex r(this->goods_price_regex); +std::vector Module::parse_price(std::wstring str) { + std::vector result; + std::wregex r(this->goods_price_regex, std::regex::collate); - std::smatch m; - regex_search(str, m, r); + for (std::wsregex_iterator it {str.begin(), str.end(), r}, end {}; it != end; it ++) { + result.push_back(to_utf8(it->str())); + } - return m; + return result; } -std::smatch Module::parse_quantity(std::string str) { - std::regex r(this->goods_quantity_regex); +std::vector Module::parse_quantity(std::wstring str) { + std::vector result; + std::wregex r(this->goods_quantity_regex, std::regex::collate); - std::smatch m; - regex_search(str, m, r); - - return m; + for (std::wsregex_iterator it {str.begin(), str.end(), r}, end {}; it != end; it ++) { + result.push_back(to_utf8(it->str())); + } + return result; } -std::string Module::get_name() { return this->name; } +std::wstring Module::get_name() { return this->name; } diff --git a/parser/module.h b/parser/module.h index 7d75454..c4506c6 100644 --- a/parser/module.h +++ b/parser/module.h @@ -3,23 +3,22 @@ #include #include -#include class Module { std::string path; - std::string name; - std::string goods_name_regex; - std::string goods_price_regex; - std::string goods_quantity_regex; + std::wstring name; + std::wstring goods_name_regex; + std::wstring goods_price_regex; + std::wstring goods_quantity_regex; public: Module(std::string); Module(); - std::smatch parse_name(std::string); - std::smatch parse_price(std::string); - std::smatch parse_quantity(std::string); + std::vector parse_name(std::wstring); + std::vector parse_price(std::wstring); + std::vector parse_quantity(std::wstring); - std::string get_name(); + std::wstring get_name(); }; #endif // MODULE_H diff --git a/parser/parser.cpp b/parser/parser.cpp index e6a7f02..9b856d4 100644 --- a/parser/parser.cpp +++ b/parser/parser.cpp @@ -1,7 +1,8 @@ #include "parser.h" -#include #include "../settings.h" #include +#include +#include "../goods/goods.h" Parser::Parser() {} @@ -11,8 +12,10 @@ std::vector Parser::search_modules() { if (!modules_dir.exists()) { std::filesystem::create_directories(path); - std::cout << "No modules directory found. Created one at " << path << std::endl; - std::cout << "Please, download modules to that directory from my git." << std::endl; + std::cout << "No modules directory found. Created one at " << path + << std::endl; + std::cout << "Please, download modules to that directory from my git." + << std::endl; } std::vector modules_files; @@ -24,6 +27,31 @@ std::vector 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 = *(new Module(path)); } + +std::vector Parser::parse(std::wstring check_plaintext) { + std::vector result; + + std::vector goods_names = module.parse_name(check_plaintext); + std::vector goods_prices = module.parse_price(check_plaintext); + std::vector goods_quantities = + module.parse_quantity(check_plaintext); +#ifdef DEBUG + std::cout << goods_names.size() << " " << goods_prices.size() << " " << goods_quantities.size() << std::endl; +#endif + 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; + 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])); + + result.push_back(goods); + } + + return result; } diff --git a/parser/parser.h b/parser/parser.h index dda4ceb..d97fd25 100644 --- a/parser/parser.h +++ b/parser/parser.h @@ -5,6 +5,7 @@ #include "module.h" #include #include +#include "../goods/goods.h" class Parser { @@ -15,6 +16,8 @@ public: std::vector search_modules(); void set_module(std::string); + + std::vector parse (std::wstring); }; #endif // PARSER_H diff --git a/settings.h b/settings.h index 47c7d0e..7ad05b3 100644 --- a/settings.h +++ b/settings.h @@ -2,5 +2,6 @@ #define SETTINGS_H #define MODULES_DIR ".local/share/checks_parser/modules" +#define DEBUG #endif // SETTINGS_H