complete parsing

This commit is contained in:
leca 2024-08-20 02:38:40 +03:00
parent 984102fd64
commit dfa98f758b
8 changed files with 146 additions and 44 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 14.0.0, 2024-08-18T13:48:17. --> <!-- Written by QtCreator 14.0.0, 2024-08-20T00:01:53. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
@ -58,6 +58,35 @@
<value type="bool" key="EditorConfiguration.tintMarginArea">true</value> <value type="bool" key="EditorConfiguration.tintMarginArea">true</value>
</valuemap> </valuemap>
</data> </data>
<data>
<variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap">
<valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks">
<value type="bool" key="AutoTest.Framework.Boost">true</value>
<value type="bool" key="AutoTest.Framework.CTest">false</value>
<value type="bool" key="AutoTest.Framework.Catch">true</value>
<value type="bool" key="AutoTest.Framework.GTest">true</value>
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
</valuemap>
<value type="bool" key="AutoTest.ApplyFilter">false</value>
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
<valuelist type="QVariantList" key="AutoTest.PathFilters"/>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value>
<value type="int" key="ClangTools.ParallelJobs">4</value>
<value type="bool" key="ClangTools.PreferConfigFile">true</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data> <data>
<variable>ProjectExplorer.Project.Target.0</variable> <variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap"> <valuemap type="QVariantMap">
@ -92,6 +121,7 @@
<value type="bool" key="CMakeProjectManager.MakeStep.ClearSystemEnvironment">false</value> <value type="bool" key="CMakeProjectManager.MakeStep.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="CMakeProjectManager.MakeStep.UserEnvironmentChanges"/> <valuelist type="QVariantList" key="CMakeProjectManager.MakeStep.UserEnvironmentChanges"/>
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">CMakeProjectManager.MakeStep</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">CMakeProjectManager.MakeStep</value>
</valuemap> </valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value> <value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
@ -108,6 +138,7 @@
<value type="bool" key="CMakeProjectManager.MakeStep.ClearSystemEnvironment">false</value> <value type="bool" key="CMakeProjectManager.MakeStep.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="CMakeProjectManager.MakeStep.UserEnvironmentChanges"/> <valuelist type="QVariantList" key="CMakeProjectManager.MakeStep.UserEnvironmentChanges"/>
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">CMakeProjectManager.MakeStep</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">CMakeProjectManager.MakeStep</value>
</valuemap> </valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value> <value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
@ -364,12 +395,14 @@
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="bool" key="PE.EnvironmentAspect.PrintOnRun">false</value> <value type="bool" key="PE.EnvironmentAspect.PrintOnRun">false</value>
<value type="QString" key="PerfRecordArgsId">-e cpu-cycles --call-graph dwarf,4096 -F 250</value> <value type="QString" key="PerfRecordArgsId">-e cpu-cycles --call-graph dwarf,4096 -F 250</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">checks-parser</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">CMakeProjectManager.CMakeRunConfiguration.checks-parser</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value> <value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">checks-parser</value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value> <value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/home/leca/projects/qt/checks-parser/build/Desktop-Debug</value>
</valuemap> </valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value> <value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap> </valuemap>

View File

@ -1,10 +1,10 @@
#include "goods.h" #include "goods.h"
#include <string> #include <string>
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->name = name;
this->quantity = quantity;
this->price_per_unit = price_per_unit; this->price_per_unit = price_per_unit;
this->quantity = quantity;
} }
double Goods::calculate_total_price() { double Goods::calculate_total_price() {

View File

@ -2,7 +2,7 @@
#include "./ui_mainwindow.h" #include "./ui_mainwindow.h"
#include "goods/goods.h" #include "goods/goods.h"
#include "check/check.h" #include "check/check.h"
#include "settings.h"
#include <iostream> #include <iostream>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
@ -17,9 +17,11 @@ void MainWindow::setupStoresList() {
// Make file format that is a regeeex which parse check, load these files // Make file format that is a regeeex which parse check, load these files
// here // here
parser = *(new Parser()); parser = *(new Parser());
#ifdef DEBUG
for (auto module : parser.search_modules()) { 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) { void MainWindow::on_checkType_currentIndexChanged(int index) {
@ -34,5 +36,15 @@ void MainWindow::on_checkType_currentIndexChanged(int index) {
} }
void MainWindow::on_parseButton_clicked() { 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<Goods> c = parser.parse(check_plaintext);
Check check;
for (auto g : c) {
check.add_goods(g);
}
} }

View File

@ -1,7 +1,22 @@
#include "module.h" #include "module.h"
#include "../settings.h"
#include <fstream> #include <fstream>
#include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <regex> #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;
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);
}
Module::Module() {} Module::Module() {}
@ -9,35 +24,46 @@ Module::Module(std::string path) {
std::ifstream settings_file(path); std::ifstream settings_file(path);
nlohmann::json settings = nlohmann::json::parse(settings_file); nlohmann::json settings = nlohmann::json::parse(settings_file);
this->name = settings["name"]; this->name = from_utf8(settings["name"]);
this->goods_name_regex = settings["goods_name_regex"]; this->goods_name_regex = from_utf8(settings["goods_name_regex"]);
this->goods_price_regex = settings["goods_price_regex"]; this->goods_price_regex = from_utf8(settings["goods_price_regex"]);
this->goods_quantity_regex = settings["goods_quantity_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::vector<std::string> Module::parse_name(std::wstring str) {
std::regex r(this->goods_name_regex); std::vector<std::string> result;
std::wregex r(this->goods_name_regex, std::regex::collate);
std::smatch m; for (std::wsregex_iterator it {str.begin(), str.end(), r}, end {}; it != end; it ++) {
regex_search(str, m, r); result.push_back(to_utf8(it->str()));
return m;
}
std::smatch Module::parse_price(std::string str) {
std::regex r(this->goods_price_regex);
std::smatch m;
regex_search(str, m, r);
return m;
}
std::smatch Module::parse_quantity(std::string str) {
std::regex r(this->goods_quantity_regex);
std::smatch m;
regex_search(str, m, r);
return m;
} }
std::string Module::get_name() { return this->name; } 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 ++) {
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 ++) {
result.push_back(to_utf8(it->str()));
}
return result;
}
std::wstring Module::get_name() { return this->name; }

View File

@ -3,23 +3,22 @@
#include <string> #include <string>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <regex>
class Module { class Module {
std::string path; std::string path;
std::string name; std::wstring name;
std::string goods_name_regex; std::wstring goods_name_regex;
std::string goods_price_regex; std::wstring goods_price_regex;
std::string goods_quantity_regex; std::wstring goods_quantity_regex;
public: public:
Module(std::string); Module(std::string);
Module(); Module();
std::smatch parse_name(std::string); std::vector<std::string> parse_name(std::wstring);
std::smatch parse_price(std::string); std::vector<std::string> parse_price(std::wstring);
std::smatch parse_quantity(std::string); std::vector<std::string> parse_quantity(std::wstring);
std::string get_name(); std::wstring get_name();
}; };
#endif // MODULE_H #endif // MODULE_H

View File

@ -1,7 +1,8 @@
#include "parser.h" #include "parser.h"
#include <iostream>
#include "../settings.h" #include "../settings.h"
#include <filesystem> #include <filesystem>
#include <iostream>
#include "../goods/goods.h"
Parser::Parser() {} Parser::Parser() {}
@ -11,8 +12,10 @@ std::vector<std::string> Parser::search_modules() {
if (!modules_dir.exists()) { if (!modules_dir.exists()) {
std::filesystem::create_directories(path); std::filesystem::create_directories(path);
std::cout << "No modules directory found. Created one at " << path << std::endl; std::cout << "No modules directory found. Created one at " << path
std::cout << "Please, download modules to that directory from my git." << std::endl; << std::endl;
std::cout << "Please, download modules to that directory from my git."
<< std::endl;
} }
std::vector<std::string> modules_files; std::vector<std::string> modules_files;
@ -24,6 +27,31 @@ std::vector<std::string> Parser::search_modules() {
return modules_files; return modules_files;
} }
void Parser::set_module(std::string path) { void Parser::set_module(std::string path) { module = *(new Module(path)); }
module = *(new Module(path));
std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
std::vector<Goods> result;
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);
#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;
} }

View File

@ -5,6 +5,7 @@
#include "module.h" #include "module.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include "../goods/goods.h"
class Parser { class Parser {
@ -15,6 +16,8 @@ public:
std::vector<std::string> search_modules(); std::vector<std::string> search_modules();
void set_module(std::string); void set_module(std::string);
std::vector<Goods> parse (std::wstring);
}; };
#endif // PARSER_H #endif // PARSER_H

View File

@ -2,5 +2,6 @@
#define SETTINGS_H #define SETTINGS_H
#define MODULES_DIR ".local/share/checks_parser/modules" #define MODULES_DIR ".local/share/checks_parser/modules"
#define DEBUG
#endif // SETTINGS_H #endif // SETTINGS_H