91 lines
2.8 KiB
C++
91 lines
2.8 KiB
C++
|
|
#include <expected>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <cstring>
|
|
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include <cliconfig.hpp>
|
|
#include <stringaux.hpp>
|
|
#include <networkaux.hpp>
|
|
|
|
int ClientConfig::Servport(void) {
|
|
return servport;
|
|
}
|
|
|
|
std::string ClientConfig::Servaddr(void) {
|
|
return servaddr;
|
|
}
|
|
|
|
std::vector<std::string> ClientConfig::Localnets(void) {
|
|
return localnets;
|
|
}
|
|
|
|
std::expected<void, std::string> ClientConfig::Read(std::string filename) {
|
|
std::ifstream file;
|
|
file.open(filename);
|
|
if (file.fail()) {
|
|
auto state = file.rdstate();
|
|
if (state & std::ios_base::badbit) {
|
|
return std::unexpected("Read/writing error on i/o operation");
|
|
} else if (state & std::ios_base::failbit) {
|
|
return std::unexpected("Logical error on i/o operation");
|
|
}
|
|
}
|
|
|
|
std::string line;
|
|
while (std::getline(file, line)) {
|
|
auto tokens = split(line, "#;");
|
|
auto keyval = trim(tokens[0]);
|
|
if (keyval.size() == 0) continue;
|
|
tokens = split(keyval, "=");
|
|
if (tokens.size() < 2) continue;
|
|
auto key = trim(tokens[0]);
|
|
auto val = trim(tokens[1]);
|
|
kvmap[key] = val;
|
|
if (key == "localnet") {
|
|
localnets.push_back(val);
|
|
} else if (key == "servaddr") {
|
|
servaddr = val;
|
|
} else if (key == "servport") {
|
|
auto convRes = strtoint(val);
|
|
if (!convRes) {
|
|
auto msg = std::format("servport: {}", convRes.error());
|
|
return std::unexpected(msg);
|
|
}
|
|
servport = convRes.value();
|
|
}
|
|
}
|
|
file.close();
|
|
return {};
|
|
}
|
|
|
|
std::expected<void, std::string> ClientConfig::Validate(void) {
|
|
if (servport == 0) {
|
|
return std::unexpected("Zero server port");
|
|
}
|
|
if (servaddr.size() == 0) {
|
|
return std::unexpected("Zero server address");
|
|
}
|
|
|
|
for (const auto& val : localnets) {
|
|
auto netprefixRes = netprefix(val);
|
|
if (!netprefixRes) {
|
|
return std::unexpected("Incorrect local network " + val + ":" + netprefixRes.error());
|
|
}
|
|
auto networkRes = network(val);
|
|
if (!networkRes) {
|
|
return std::unexpected("Incorrect local network " + val + ":" + networkRes.error());
|
|
}
|
|
}
|
|
return {};
|
|
}
|