diff --git a/src/recup_fichierV4.c b/src/recup_fichierV4.c new file mode 100644 index 0000000..8548dc6 --- /dev/null +++ b/src/recup_fichierV4.c @@ -0,0 +1,313 @@ +#include +#include +#include +#include +#include + +// Conditionals +const bool IS_DEBUG = true; + +// Constants +const unsigned int BUFFER_SIZE = 2048; +const unsigned int FIELD_SIZE = 20; +const char CSV_DELIMITERS[] = ","; + +// Globals +char** CSV_HEADER_FIELDS; +unsigned int CSV_NB_FIELDS; + + + +//By Bianca and Mathis + +//Ce programme permet : +//Avec un tableau de char[] donné peut créer une personne et l'ajouter a une liste. + + +//Le struct de personne: +typedef struct Personne Personne; +struct Personne { + int athId; + int regId; + int divId; + char* lastName; + char* firstName; + char gender; + int age; + char* weight; + char* height; + int affiliateId; + char* affiliateName; + int overallScrore; + int overallRank; + int score18_1; + char* scoreDisplay18_1; + int rank18_1; + int score18_2; + char* scoreDisplay18_2; + int rank18_2; + int score18_2a; + char* scoreDisplay18_2a; + int rank18_2a; + int score18_3; + char* scoreDisplay18_3; + int rank18_3; + int score18_4; + char* scoreDisplay18_4; + int rank18_4; + int score18_5; + char* scoreDisplay18_5; + int rank18_5; + Personne* suivant;}; + +typedef Personne* Liste; + + + +/************************************************************************/ +//Gestion de listes +void ajout_tete(Liste *l, Personne P) +{ + Personne *p; + p= malloc(sizeof(Personne)); + + p->athId = P.athId; + p->regId = P.regId; + p->divId = P.divId; + p->lastName = P.lastName; + p->firstName = P.firstName; + p->gender = P.gender; + p->age = P.age; + p->weight = P.weight; + p->height = P.height; + p->affiliateId = P.affiliateId; + p->affiliateName = P.affiliateName; + p->overallScrore = P.overallScrore; + p->overallRank = P.overallRank; + p->score18_1 = P.score18_1; + p->scoreDisplay18_1 = P.scoreDisplay18_1; + p->rank18_1 = P.rank18_1; + p->score18_2 = P.score18_2; + p->scoreDisplay18_2 = P.scoreDisplay18_2; + p->rank18_2 = P.rank18_2; + p->score18_2a = P.score18_2a; + p->scoreDisplay18_2a = P.scoreDisplay18_2a; + p->rank18_2a = P.rank18_2a; + p->score18_3 = P.score18_3; + p->scoreDisplay18_3 = P.scoreDisplay18_3; + p->rank18_3 = P.rank18_3; + p->score18_4 = P.score18_4; + p->scoreDisplay18_4 = P.scoreDisplay18_4; + p->rank18_4 = P.rank18_4; + p->score18_5 = P.score18_5; + p->scoreDisplay18_5 = P.scoreDisplay18_5; + p->rank18_5 = P.rank18_5; + + p->suivant = *l; + *l=p; +} + +void suppr_tete(Liste *l) +{ + Personne* a_suppr = *l; + *l = (*l)->suivant; + free(a_suppr); +} + +void affiche_personne(Personne P) +{ + printf("%s %s %d %c \n", P.lastName, P.firstName, P.overallScrore, P.gender); +} + +void affiche_liste(Liste l) +{ + Liste tmp = l; + + if(tmp==NULL) + { + printf("Liste de personne vide\n"); + return; + } + + while(tmp != NULL) + { + affiche_personne(*tmp); + tmp = tmp->suivant; + } + printf("\n"); +} + +/************************************************************************/ + + + +void remplissage_personne(char* tab_char[], Personne *P) +{ + P->athId = atoi(tab_char[0]); + P->regId = atoi(tab_char[1]); + P->divId = atoi(tab_char[2]); + P->lastName = tab_char[3]; + P->firstName = tab_char[4]; + P->gender = *tab_char[5]; + P->age = atoi(tab_char[6]); + P->weight = tab_char[7]; + P->height = tab_char[8]; + P->affiliateId = atoi(tab_char[9]); + P->affiliateName = tab_char[10]; + P->overallScrore = atoi(tab_char[11]); + P->overallRank = atoi(tab_char[12]); + P->score18_1 = atoi(tab_char[13]); + P->scoreDisplay18_1 = tab_char[14]; + P->rank18_1 = atoi(tab_char[15]); + P->score18_2 = atoi(tab_char[16]); + P->scoreDisplay18_2 = tab_char[17]; + P->rank18_2 = atoi(tab_char[18]); + P->score18_2a = atoi(tab_char[19]); + P->scoreDisplay18_2a = tab_char[20]; + P->rank18_2a = atoi(tab_char[21]); + P->score18_3 = atoi(tab_char[22]); + P->scoreDisplay18_3 = tab_char[23]; + P->rank18_3 = atoi(tab_char[24]); + P->score18_4 = atoi(tab_char[25]); + P->scoreDisplay18_4 = tab_char[26]; + P->rank18_4 = atoi(tab_char[27]); + P->score18_5 = atoi(tab_char[28]); + P->scoreDisplay18_5 = tab_char[29]; + P->rank18_5 = atoi(tab_char[30]); + +} + + + + +void display_header() +{ + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) + { + printf("%d - %s\n", i, CSV_HEADER_FIELDS[i]); + } +} + +void read_csv_header(char * header_line) +{ + //int line_length = strlen(header_line); + int nb_fields = 0; + char* string_ptr = header_line; + + // Count the occurrences of delimiters + while (NULL != string_ptr) + { + nb_fields++; + string_ptr = strpbrk(string_ptr, CSV_DELIMITERS); + if (NULL != string_ptr) + { + string_ptr++; + } + } + + // Globals allocation + CSV_NB_FIELDS = nb_fields; + CSV_HEADER_FIELDS = malloc( nb_fields * sizeof(char*) ); + + char* token = strtok(header_line, CSV_DELIMITERS); // strtok init. + + // Re-read the line to get the header of the columns + for (unsigned int i = 0; i < nb_fields; i++) + { + CSV_HEADER_FIELDS[i] = malloc( FIELD_SIZE * sizeof(char) ); // alloc + memset(CSV_HEADER_FIELDS[i], 0, FIELD_SIZE); // 0 init. + strcpy(CSV_HEADER_FIELDS[i], token); // copy field in the structure + token = strtok(NULL, CSV_DELIMITERS); // loop to get a new field label + } + + if (IS_DEBUG) display_header(); +} + +void read_csv_file(const char * filename, Liste *liste_personne) +{ + FILE* fp = fopen(filename, "r"); + char buffer[BUFFER_SIZE]; + + char *string; //pointeur de string va être utiliser dans strsep + + + // Check if the file is really opened + if (NULL == fp) + { + fprintf(stderr, "Unable to open file: %s\n", filename); + return; + } + + // 1st row is a header with field descriptions + fgets(buffer, BUFFER_SIZE, fp); + read_csv_header(buffer); + + string = strdup(buffer); //string pointe vers la chaine de caractère buffer + + // Remaining rows are the entries + while ( NULL != fgets(buffer, BUFFER_SIZE, fp) ) + { + char* token; + unsigned int i = 0; + char* tab_char[50]; + Personne P; + + + string = strdup(buffer); //string pointe vers la chaine de caractère buffer + + + // strsep init. + token = strsep(&string, CSV_DELIMITERS); + //strsep permet de séparer une chaine de caractère en prenant en compte les ,, + + while (NULL != token) + { + //if (IS_DEBUG) printf("Field %d is %s\n", i++, token); + + tab_char[i]=token; + + // printf(" %s",tab_char[i]); + i++; + + token = strsep(&string, CSV_DELIMITERS); + } + //printf("\n"); + + remplissage_personne(tab_char, &P); + ajout_tete(liste_personne, P); + + } + + fclose(fp); +} + +void usage(const char * prog_name) +{ + printf("Usage is %s your_csv_file\n\n", prog_name); +} + +int main(int argc, char * argv[]) +{ + Liste liste_personne = NULL; + + if (2 != argc) + { + usage(argv[0]); + return 0; + } + + read_csv_file(argv[1], &liste_personne); + + affiche_liste(liste_personne); + + printf("Done\n"); + + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) + { + free(CSV_HEADER_FIELDS[i]); + } + free(CSV_HEADER_FIELDS); + + + return 0; +} \ No newline at end of file -- libgit2 0.21.2