generated from AfonsoCMSousa/CPP-Template
57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
#include "file.hpp"
|
|
|
|
vector<string> read_file(const char *filePath) {
|
|
ifstream file(filePath);
|
|
if (!file.is_open()) {
|
|
return vector<string>();
|
|
}
|
|
|
|
vector<string> lines;
|
|
string line;
|
|
|
|
while (getline(file, line)) {
|
|
lines.push_back(line);
|
|
}
|
|
|
|
file.close();
|
|
return lines;
|
|
}
|
|
|
|
app_info parce_args(int argc, char *argv[]) {
|
|
// example
|
|
// ./player_tracker 130 12000 13000
|
|
// -id -serverin -serverout
|
|
app_info __processed_info;
|
|
|
|
if (argc <= 1) {
|
|
throw invalid_argument("No argc provided");
|
|
}
|
|
|
|
__processed_info.app_id = (u_int16_t)atoi(argv[1]);
|
|
__processed_info.app_port_in = (u_int16_t)atoi(argv[2]);
|
|
__processed_info.app_port_out = (u_int16_t)atoi(argv[3]);
|
|
|
|
// Parce .env
|
|
vector<string> __read_lines = read_file("./.env");
|
|
map<string, string> __env_args;
|
|
|
|
for (size_t i = 0; i < __read_lines.size() - 1; i++) {
|
|
string token = __read_lines[i].substr(0, __read_lines[i].find(" = "));
|
|
|
|
__read_lines[i].erase(0, __read_lines[i].find(" = ") + 3);
|
|
|
|
__env_args[token] = __read_lines[i];
|
|
}
|
|
|
|
// DEBUG
|
|
// for (const auto& [key, value] : __env_args) {
|
|
// std::cout << '[' << key << "] = " << value << "; ";
|
|
// }
|
|
|
|
__processed_info.app_api_socket_path = __env_args["API_SOCKET_PATH"].c_str();
|
|
__processed_info.max_players = (u_int16_t)atoi(__env_args["MAX_PLAYERS"].c_str());
|
|
__processed_info.app_server_out_ip = __env_args["SERVER_OUT_IP"].c_str();
|
|
|
|
return __processed_info;
|
|
}
|