#include "parser.h" #include "../settings.h" #include #include #include "../goods/goods.h" Parser::Parser() {} std::vector Parser::search_modules() { std::string path = std::string(std::getenv("HOME")) + "/" + STORES_MODULES_DIR; std::filesystem::directory_entry modules_dir(path); 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::vector modules_files; for (auto file : std::filesystem::directory_iterator(path)) { modules_files.push_back(file.path()); } return modules_files; } void Parser::set_module(std::string path) { module = StoreModule(path); } std::vector Parser::parse(std::wstring check_plaintext) { std::vector result; module.trim_check(check_plaintext); 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()) { //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(); 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; }