2024-08-19 02:35:16 +03:00
|
|
|
#include "parser.h"
|
|
|
|
#include "../settings.h"
|
|
|
|
#include <filesystem>
|
2024-08-20 02:38:40 +03:00
|
|
|
#include <iostream>
|
|
|
|
#include "../goods/goods.h"
|
2024-08-19 02:35:16 +03:00
|
|
|
|
|
|
|
Parser::Parser() {}
|
|
|
|
|
|
|
|
std::vector<std::string> Parser::search_modules() {
|
2024-08-30 05:03:32 +03:00
|
|
|
std::string path = std::string(std::getenv("HOME")) + "/" + STORES_MODULES_DIR;
|
2024-08-19 02:35:16 +03:00
|
|
|
std::filesystem::directory_entry modules_dir(path);
|
|
|
|
|
|
|
|
if (!modules_dir.exists()) {
|
|
|
|
std::filesystem::create_directories(path);
|
2024-08-20 02:38:40 +03:00
|
|
|
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;
|
2024-08-19 02:35:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> modules_files;
|
|
|
|
|
|
|
|
for (auto file : std::filesystem::directory_iterator(path)) {
|
|
|
|
modules_files.push_back(file.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
return modules_files;
|
|
|
|
}
|
|
|
|
|
2024-08-30 05:03:32 +03:00
|
|
|
void Parser::set_module(std::string path) { module = StoreModule(path); }
|
2024-08-20 02:38:40 +03:00
|
|
|
|
|
|
|
std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
|
|
|
|
std::vector<Goods> result;
|
|
|
|
|
2024-08-24 00:45:47 +03:00
|
|
|
module.trim_check(check_plaintext);
|
|
|
|
|
2024-08-20 02:38:40 +03:00
|
|
|
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()) {
|
2024-08-24 00:45:47 +03:00
|
|
|
//Error. Amount of names, prices or quantities are not equal. That means, that some regex(es) has mismatched.
|
2024-08-20 02:38:40 +03:00
|
|
|
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;
|
2024-08-19 02:35:16 +03:00
|
|
|
}
|