structure, layout, parser and its modules
This commit is contained in:
43
parser/module.cpp
Normal file
43
parser/module.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "module.h"
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <regex>
|
||||
|
||||
Module::Module() {}
|
||||
|
||||
Module::Module(std::string path) {
|
||||
std::ifstream settings_file(path);
|
||||
nlohmann::json settings = nlohmann::json::parse(settings_file);
|
||||
|
||||
this->name = settings["name"];
|
||||
this->goods_name_regex = settings["goods_name_regex"];
|
||||
this->goods_price_regex = settings["goods_price_regex"];
|
||||
this->goods_quantity_regex = settings["goods_quantity_regex"];
|
||||
}
|
||||
|
||||
std::smatch Module::parse_name(std::string str) {
|
||||
std::regex r(this->goods_name_regex);
|
||||
|
||||
std::smatch m;
|
||||
regex_search(str, m, r);
|
||||
|
||||
return m;
|
||||
}
|
||||
std::smatch Module::parse_price(std::string str) {
|
||||
std::regex r(this->goods_price_regex);
|
||||
|
||||
std::smatch m;
|
||||
regex_search(str, m, r);
|
||||
|
||||
return m;
|
||||
}
|
||||
std::smatch Module::parse_quantity(std::string str) {
|
||||
std::regex r(this->goods_quantity_regex);
|
||||
|
||||
std::smatch m;
|
||||
regex_search(str, m, r);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
std::string Module::get_name() { return this->name; }
|
||||
25
parser/module.h
Normal file
25
parser/module.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef MODULE_H
|
||||
#define MODULE_H
|
||||
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <regex>
|
||||
|
||||
class Module {
|
||||
std::string path;
|
||||
std::string name;
|
||||
std::string goods_name_regex;
|
||||
std::string goods_price_regex;
|
||||
std::string goods_quantity_regex;
|
||||
public:
|
||||
Module(std::string);
|
||||
Module();
|
||||
|
||||
std::smatch parse_name(std::string);
|
||||
std::smatch parse_price(std::string);
|
||||
std::smatch parse_quantity(std::string);
|
||||
|
||||
std::string get_name();
|
||||
};
|
||||
|
||||
#endif // MODULE_H
|
||||
29
parser/parser.cpp
Normal file
29
parser/parser.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "parser.h"
|
||||
#include <iostream>
|
||||
#include "../settings.h"
|
||||
#include <filesystem>
|
||||
|
||||
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 = *(new Module(path));
|
||||
}
|
||||
20
parser/parser.h
Normal file
20
parser/parser.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
#include <iostream>
|
||||
#include "module.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Parser {
|
||||
|
||||
Module module;
|
||||
public:
|
||||
Parser();
|
||||
|
||||
std::vector<std::string> search_modules();
|
||||
|
||||
void set_module(std::string);
|
||||
};
|
||||
|
||||
#endif // PARSER_H
|
||||
Reference in New Issue
Block a user