233 lines
4.8 KiB
C++
233 lines
4.8 KiB
C++
#ifndef NUMBER_PARSER_H_
|
|
#define NUMBER_PARSER_H_
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <set>
|
|
#include <iostream>
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
|
|
#include "nlohmann/json.hpp"
|
|
|
|
|
|
namespace parser
|
|
{
|
|
|
|
class some_object
|
|
{
|
|
public:
|
|
some_object() = default;
|
|
some_object(std::string value) :
|
|
value(value)
|
|
{
|
|
}
|
|
|
|
~some_object() = default;
|
|
|
|
std::string value;
|
|
private:
|
|
};
|
|
|
|
|
|
template<typename Obj>
|
|
class number_parser
|
|
{
|
|
public:
|
|
number_parser(const std::string &filepath) :
|
|
filepath(filepath)
|
|
{ }
|
|
|
|
template<typename Con = std::vector<Obj>>
|
|
Con file_dump()
|
|
{
|
|
Con container;
|
|
|
|
std::fstream file(this->filepath.c_str(), std::ios::in);
|
|
|
|
if (!file.is_open())
|
|
{
|
|
std::cout << "File is not openned...\n";
|
|
std::cout << "Openning file: " << this->filepath << "\n";
|
|
file.open(filepath.c_str());
|
|
}
|
|
|
|
std::string line;
|
|
int i = 0;
|
|
|
|
while (std::getline(file, line))
|
|
{
|
|
if (line.size() > 1)
|
|
{
|
|
container.push_back(line);
|
|
}
|
|
}
|
|
|
|
file.close();
|
|
|
|
return container;
|
|
}
|
|
|
|
template<typename Con = std::vector<Obj>>
|
|
Con update_values(const Con &values)
|
|
{
|
|
Con container;
|
|
std::vector<char> remove_chars;
|
|
remove_chars.reserve(4);
|
|
remove_chars.push_back(')');
|
|
remove_chars.push_back('(');
|
|
remove_chars.push_back(32);
|
|
remove_chars.push_back('-');
|
|
|
|
auto prefix = "+1";
|
|
std::string parsed;
|
|
|
|
for (const auto &item : values)
|
|
{
|
|
parsed.assign(item.value);
|
|
|
|
if (parsed.size() < 2)
|
|
{
|
|
std::cout << parsed << " is an invalid number. Continuing with the next number\n";
|
|
continue;
|
|
}
|
|
|
|
auto pars = remove_some_data(parsed, remove_chars);
|
|
parsed.clear();
|
|
parsed.assign(pars);
|
|
|
|
add_prefix(parsed);
|
|
|
|
container.push_back(parsed);
|
|
parsed.clear();
|
|
}
|
|
|
|
return container;
|
|
}
|
|
|
|
template<typename Con = std::vector<Obj>>
|
|
void remove_duplicates(Con &container)
|
|
{
|
|
std::set<std::string> tmpContainer;
|
|
|
|
auto act = [&](Obj& ob)
|
|
{
|
|
tmpContainer.emplace(ob.value);
|
|
};
|
|
|
|
std::for_each(container.begin(), container.end(), act);
|
|
container.clear();
|
|
|
|
auto actReplace = [&](std::string value)
|
|
{
|
|
Obj o(value);
|
|
container.emplace_back(o);
|
|
};
|
|
|
|
std::for_each(tmpContainer.begin(), tmpContainer.end(), actReplace);
|
|
}
|
|
|
|
template<typename Con = std::vector<Obj>>
|
|
void save_file(const Con container)
|
|
{
|
|
std::set<std::string> my_set;
|
|
|
|
const std::string filepath("my_file.json");;
|
|
|
|
for (const auto &s : container)
|
|
{
|
|
my_set.insert(s.value);
|
|
}
|
|
|
|
|
|
nlohmann::json j(my_set);
|
|
|
|
std::cout << "Saving data to file: " << filepath << "\n";
|
|
|
|
std::fstream output(filepath.c_str(), std::ios::out);
|
|
output << j.dump(4);
|
|
|
|
output.close();
|
|
|
|
std::cout << "The file has been saved\n";
|
|
}
|
|
|
|
template<typename Con = std::vector<Obj>>
|
|
void print_values(const Con container)
|
|
{
|
|
if (!(container.size() > 0))
|
|
{
|
|
std::cout << "Empty container\n";
|
|
return;
|
|
}
|
|
|
|
std::cout << "Printing values\n";
|
|
|
|
for (const auto &item : container)
|
|
{
|
|
std::cout << "Value: " << item.value << "\n";
|
|
}
|
|
}
|
|
private:
|
|
std::string remove_some_data(const std::string &unparsed, const std::vector<char> &remove_chars)
|
|
{
|
|
std::string parsed;
|
|
|
|
std::cout << "Unparsed: " << unparsed << "\n";
|
|
|
|
for (auto i = 0; i < unparsed.size(); ++i)
|
|
{
|
|
char c = unparsed.at(i);
|
|
|
|
if (std::isdigit(c))
|
|
{
|
|
parsed.append(std::to_string(c-48));
|
|
}
|
|
}
|
|
|
|
std::cout << "Parsed: " << parsed << "\n";
|
|
|
|
return parsed;
|
|
}
|
|
|
|
void add_prefix(std::string &parsed)
|
|
{
|
|
auto last_char = parsed.at(parsed.size() - 1);
|
|
auto first_char = parsed.at(0);
|
|
auto second_char = parsed.at(1);
|
|
|
|
|
|
if (first_char == '1')
|
|
{
|
|
parsed.insert(0, "+");
|
|
}
|
|
else if (first_char != '+')
|
|
{
|
|
if (second_char != '1')
|
|
{
|
|
parsed.insert(0, "+1");
|
|
}
|
|
else
|
|
{
|
|
parsed.insert(0, "+1");
|
|
}
|
|
}
|
|
}
|
|
|
|
int add_prefix_if_not_found(std::string &parsed, const std::string &prefixes)
|
|
{
|
|
auto prefix_size = prefixes.size();
|
|
auto parsed_beginning = parsed.substr(0, prefix_size);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
std::string filepath;
|
|
};
|
|
|
|
}
|
|
|
|
|
|
#endif
|