checks-parser/parser/parser.cpp

60 lines
1.9 KiB
C++

#include "parser.h"
#include "../settings.h"
#include <filesystem>
#include <iostream>
#include "../goods/goods.h"
Parser::Parser() {}
std::vector<std::string> Parser::search_modules() {
std::string path = std::string(std::getenv("HOME")) + "/" + 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<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 = Module(path); }
std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
std::vector<Goods> result;
module.trim_check(check_plaintext);
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.
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;
}