readServicesFromCSV working

This commit is contained in:
eleonore12345 2024-08-01 18:00:04 +02:00
parent c31fc58874
commit e2a37fae16
3 changed files with 25 additions and 20 deletions

View File

@ -1,7 +1,7 @@
ECHO = @echo ECHO = @echo
GCC = g++ GCC = g++
RM = @rm -f RM = @rm -f
CCFLAGS = -c -g -ansi -pedantic -Wall #optimization? CCFLAGS = -c -g -std=c++11 -pedantic -Wall #optimization?
OBJETS = $(SRC:.cpp=.o) OBJETS = $(SRC:.cpp=.o)
SRC = $(wildcard *.cpp) SRC = $(wildcard *.cpp)
EXE = deployer EXE = deployer

View File

@ -1,9 +1,12 @@
// deployer Services implementation // deployer Services implementation
// Copyright (C) 2024 Jean-Cloud // Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3 // GNU General Public License v3
#include <unistd.h>
#include <sstream>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <fstream>
#include <string>
#include "Services.h" #include "Services.h"
Services::Services(const char *ServicesCSV) Services::Services(const char *ServicesCSV)
@ -13,33 +16,35 @@ Services::Services(const char *ServicesCSV)
Services::~Services(){} 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; vector <serviceData> result;
FILE *streamServices=fopen(CSV,"r"); ifstream streamServices(CSV);
if (streamServices==NULL){ if (!streamServices){
cout << "Invalid services.csv file." << endl; cout << "Invalid services.csv file." << endl;
}else{ }else{
char line [1000]; string line;
int userID; int userID;
string tmpUserID;
string username; string username;
string serveur; string serveur;
list <string> serveurs; list <string> serveurs;
while(fgets(line,sizeof(line),streamServices)!=NULL){ while(getline(streamServices,line)){
userID=atoi(strtok(line,";")); if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
username=strtok(NULL,";"); continue;
//while loop
serveur=strtok(NULL,";");
cout << serveur << " ";
while(!serveur.empty()){
serveur=strtok(NULL,";");
//serveurs.push_back(serveur);
cout << serveur << " ";
} }
//serviceData entry = {userID,username,serveurs}; stringstream streamLine(line);
//cout << entry.userID <<entry.username << entry.serveurs.front() << endl; getline(streamLine,tmpUserID,';'); //extracting the userID
//result.push_back(entry); 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; return result;
} }

BIN
deployer

Binary file not shown.