sh8s_deployer/Services.cpp
2024-08-01 18:00:04 +02:00

50 lines
1.6 KiB
C++

// deployer Services implementation
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include "Services.h"
Services::Services(const char *ServicesCSV)
{
services=readServicesFromCSV(ServicesCSV);
}
Services::~Services(){}
vector <serviceData> Services::readServicesFromCSV (const char *CSV) const
{
//this method extracts the list of uid|username|servers from the services.csv file
//and returns them in a vector <serviceData>, with serviceData a structure defined in the header
vector <serviceData> result;
ifstream streamServices(CSV);
if (!streamServices){
cout << "Invalid services.csv file." << endl;
}else{
string line;
int userID;
string tmpUserID;
string username;
string serveur;
list <string> serveurs;
while(getline(streamServices,line)){
if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
continue;
}
stringstream streamLine(line);
getline(streamLine,tmpUserID,';'); //extracting the userID
userID=stoi(tmpUserID);
getline(streamLine,username,';'); //extracting the username
while(getline(streamLine,serveur,';')){ //extracting the server(s)
serveurs.push_back(serveur);
}
serviceData entry = {userID,username,serveurs};
result.push_back(entry);
}
}
return result;
}