checks-parser/parser/parser.cpp

60 lines
2.0 KiB
C++
Raw Normal View History

#include "parser.h"
#include "../settings.h"
#include <filesystem>
2024-08-20 02:38:40 +03:00
#include <iostream>
#include "../goods/goods.h"
Parser::Parser() {}
std::vector<std::string> 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);
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;
}
std::vector<std::string> 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); }
2024-08-20 02:38:40 +03:00
std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
std::vector<Goods> result;
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()) {
//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;
}