readServicesFromCSV working
This commit is contained in:
parent
c31fc58874
commit
e2a37fae16
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
||||
ECHO = @echo
|
||||
GCC = g++
|
||||
RM = @rm -f
|
||||
CCFLAGS = -c -g -ansi -pedantic -Wall #optimization?
|
||||
CCFLAGS = -c -g -std=c++11 -pedantic -Wall #optimization?
|
||||
OBJETS = $(SRC:.cpp=.o)
|
||||
SRC = $(wildcard *.cpp)
|
||||
EXE = deployer
|
||||
|
43
Services.cpp
43
Services.cpp
@ -1,9 +1,12 @@
|
||||
// deployer Services implementation
|
||||
// Copyright (C) 2024 Jean-Cloud
|
||||
// GNU General Public License v3
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "Services.h"
|
||||
|
||||
Services::Services(const char *ServicesCSV)
|
||||
@ -13,33 +16,35 @@ Services::Services(const char *ServicesCSV)
|
||||
Services::~Services(){}
|
||||
|
||||
|
||||
vector <serviceData> Services::readServicesFromCSV (const char *CSV) const {
|
||||
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;
|
||||
FILE *streamServices=fopen(CSV,"r");
|
||||
if (streamServices==NULL){
|
||||
ifstream streamServices(CSV);
|
||||
if (!streamServices){
|
||||
cout << "Invalid services.csv file." << endl;
|
||||
}else{
|
||||
char line [1000];
|
||||
string line;
|
||||
int userID;
|
||||
string tmpUserID;
|
||||
string username;
|
||||
string serveur;
|
||||
list <string> serveurs;
|
||||
while(fgets(line,sizeof(line),streamServices)!=NULL){
|
||||
userID=atoi(strtok(line,";"));
|
||||
username=strtok(NULL,";");
|
||||
//while loop
|
||||
serveur=strtok(NULL,";");
|
||||
cout << serveur << " ";
|
||||
while(!serveur.empty()){
|
||||
serveur=strtok(NULL,";");
|
||||
//serveurs.push_back(serveur);
|
||||
cout << serveur << " ";
|
||||
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};
|
||||
//cout << entry.userID <<entry.username << entry.serveurs.front() << endl;
|
||||
//result.push_back(entry);
|
||||
serviceData entry = {userID,username,serveurs};
|
||||
result.push_back(entry);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
Loading…
Reference in New Issue
Block a user