checks-parser/parser/parser.cpp

58 lines
1.9 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")) + "/" + 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;
}
2024-08-20 02:38:40 +03:00
void Parser::set_module(std::string 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;
}