Commit bd36ea26cabf307581175b3fba0a5ecdf797553f
1 parent
997055b1
recuperation fichier avec ajout liste qui fonctionne
Showing
1 changed file
with
313 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,313 @@ | @@ -0,0 +1,313 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <string.h> | ||
3 | +#include <stdbool.h> | ||
4 | +#include <stdlib.h> | ||
5 | +#include <ctype.h> | ||
6 | + | ||
7 | +// Conditionals | ||
8 | +const bool IS_DEBUG = true; | ||
9 | + | ||
10 | +// Constants | ||
11 | +const unsigned int BUFFER_SIZE = 2048; | ||
12 | +const unsigned int FIELD_SIZE = 20; | ||
13 | +const char CSV_DELIMITERS[] = ","; | ||
14 | + | ||
15 | +// Globals | ||
16 | +char** CSV_HEADER_FIELDS; | ||
17 | +unsigned int CSV_NB_FIELDS; | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | +//By Bianca and Mathis | ||
22 | + | ||
23 | +//Ce programme permet : | ||
24 | +//Avec un tableau de char[] donné peut créer une personne et l'ajouter a une liste. | ||
25 | + | ||
26 | + | ||
27 | +//Le struct de personne: | ||
28 | +typedef struct Personne Personne; | ||
29 | +struct Personne { | ||
30 | + int athId; | ||
31 | + int regId; | ||
32 | + int divId; | ||
33 | + char* lastName; | ||
34 | + char* firstName; | ||
35 | + char gender; | ||
36 | + int age; | ||
37 | + char* weight; | ||
38 | + char* height; | ||
39 | + int affiliateId; | ||
40 | + char* affiliateName; | ||
41 | + int overallScrore; | ||
42 | + int overallRank; | ||
43 | + int score18_1; | ||
44 | + char* scoreDisplay18_1; | ||
45 | + int rank18_1; | ||
46 | + int score18_2; | ||
47 | + char* scoreDisplay18_2; | ||
48 | + int rank18_2; | ||
49 | + int score18_2a; | ||
50 | + char* scoreDisplay18_2a; | ||
51 | + int rank18_2a; | ||
52 | + int score18_3; | ||
53 | + char* scoreDisplay18_3; | ||
54 | + int rank18_3; | ||
55 | + int score18_4; | ||
56 | + char* scoreDisplay18_4; | ||
57 | + int rank18_4; | ||
58 | + int score18_5; | ||
59 | + char* scoreDisplay18_5; | ||
60 | + int rank18_5; | ||
61 | + Personne* suivant;}; | ||
62 | + | ||
63 | +typedef Personne* Liste; | ||
64 | + | ||
65 | + | ||
66 | + | ||
67 | +/************************************************************************/ | ||
68 | +//Gestion de listes | ||
69 | +void ajout_tete(Liste *l, Personne P) | ||
70 | +{ | ||
71 | + Personne *p; | ||
72 | + p= malloc(sizeof(Personne)); | ||
73 | + | ||
74 | + p->athId = P.athId; | ||
75 | + p->regId = P.regId; | ||
76 | + p->divId = P.divId; | ||
77 | + p->lastName = P.lastName; | ||
78 | + p->firstName = P.firstName; | ||
79 | + p->gender = P.gender; | ||
80 | + p->age = P.age; | ||
81 | + p->weight = P.weight; | ||
82 | + p->height = P.height; | ||
83 | + p->affiliateId = P.affiliateId; | ||
84 | + p->affiliateName = P.affiliateName; | ||
85 | + p->overallScrore = P.overallScrore; | ||
86 | + p->overallRank = P.overallRank; | ||
87 | + p->score18_1 = P.score18_1; | ||
88 | + p->scoreDisplay18_1 = P.scoreDisplay18_1; | ||
89 | + p->rank18_1 = P.rank18_1; | ||
90 | + p->score18_2 = P.score18_2; | ||
91 | + p->scoreDisplay18_2 = P.scoreDisplay18_2; | ||
92 | + p->rank18_2 = P.rank18_2; | ||
93 | + p->score18_2a = P.score18_2a; | ||
94 | + p->scoreDisplay18_2a = P.scoreDisplay18_2a; | ||
95 | + p->rank18_2a = P.rank18_2a; | ||
96 | + p->score18_3 = P.score18_3; | ||
97 | + p->scoreDisplay18_3 = P.scoreDisplay18_3; | ||
98 | + p->rank18_3 = P.rank18_3; | ||
99 | + p->score18_4 = P.score18_4; | ||
100 | + p->scoreDisplay18_4 = P.scoreDisplay18_4; | ||
101 | + p->rank18_4 = P.rank18_4; | ||
102 | + p->score18_5 = P.score18_5; | ||
103 | + p->scoreDisplay18_5 = P.scoreDisplay18_5; | ||
104 | + p->rank18_5 = P.rank18_5; | ||
105 | + | ||
106 | + p->suivant = *l; | ||
107 | + *l=p; | ||
108 | +} | ||
109 | + | ||
110 | +void suppr_tete(Liste *l) | ||
111 | +{ | ||
112 | + Personne* a_suppr = *l; | ||
113 | + *l = (*l)->suivant; | ||
114 | + free(a_suppr); | ||
115 | +} | ||
116 | + | ||
117 | +void affiche_personne(Personne P) | ||
118 | +{ | ||
119 | + printf("%s %s %d %c \n", P.lastName, P.firstName, P.overallScrore, P.gender); | ||
120 | +} | ||
121 | + | ||
122 | +void affiche_liste(Liste l) | ||
123 | +{ | ||
124 | + Liste tmp = l; | ||
125 | + | ||
126 | + if(tmp==NULL) | ||
127 | + { | ||
128 | + printf("Liste de personne vide\n"); | ||
129 | + return; | ||
130 | + } | ||
131 | + | ||
132 | + while(tmp != NULL) | ||
133 | + { | ||
134 | + affiche_personne(*tmp); | ||
135 | + tmp = tmp->suivant; | ||
136 | + } | ||
137 | + printf("\n"); | ||
138 | +} | ||
139 | + | ||
140 | +/************************************************************************/ | ||
141 | + | ||
142 | + | ||
143 | + | ||
144 | +void remplissage_personne(char* tab_char[], Personne *P) | ||
145 | +{ | ||
146 | + P->athId = atoi(tab_char[0]); | ||
147 | + P->regId = atoi(tab_char[1]); | ||
148 | + P->divId = atoi(tab_char[2]); | ||
149 | + P->lastName = tab_char[3]; | ||
150 | + P->firstName = tab_char[4]; | ||
151 | + P->gender = *tab_char[5]; | ||
152 | + P->age = atoi(tab_char[6]); | ||
153 | + P->weight = tab_char[7]; | ||
154 | + P->height = tab_char[8]; | ||
155 | + P->affiliateId = atoi(tab_char[9]); | ||
156 | + P->affiliateName = tab_char[10]; | ||
157 | + P->overallScrore = atoi(tab_char[11]); | ||
158 | + P->overallRank = atoi(tab_char[12]); | ||
159 | + P->score18_1 = atoi(tab_char[13]); | ||
160 | + P->scoreDisplay18_1 = tab_char[14]; | ||
161 | + P->rank18_1 = atoi(tab_char[15]); | ||
162 | + P->score18_2 = atoi(tab_char[16]); | ||
163 | + P->scoreDisplay18_2 = tab_char[17]; | ||
164 | + P->rank18_2 = atoi(tab_char[18]); | ||
165 | + P->score18_2a = atoi(tab_char[19]); | ||
166 | + P->scoreDisplay18_2a = tab_char[20]; | ||
167 | + P->rank18_2a = atoi(tab_char[21]); | ||
168 | + P->score18_3 = atoi(tab_char[22]); | ||
169 | + P->scoreDisplay18_3 = tab_char[23]; | ||
170 | + P->rank18_3 = atoi(tab_char[24]); | ||
171 | + P->score18_4 = atoi(tab_char[25]); | ||
172 | + P->scoreDisplay18_4 = tab_char[26]; | ||
173 | + P->rank18_4 = atoi(tab_char[27]); | ||
174 | + P->score18_5 = atoi(tab_char[28]); | ||
175 | + P->scoreDisplay18_5 = tab_char[29]; | ||
176 | + P->rank18_5 = atoi(tab_char[30]); | ||
177 | + | ||
178 | +} | ||
179 | + | ||
180 | + | ||
181 | + | ||
182 | + | ||
183 | +void display_header() | ||
184 | +{ | ||
185 | + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) | ||
186 | + { | ||
187 | + printf("%d - %s\n", i, CSV_HEADER_FIELDS[i]); | ||
188 | + } | ||
189 | +} | ||
190 | + | ||
191 | +void read_csv_header(char * header_line) | ||
192 | +{ | ||
193 | + //int line_length = strlen(header_line); | ||
194 | + int nb_fields = 0; | ||
195 | + char* string_ptr = header_line; | ||
196 | + | ||
197 | + // Count the occurrences of delimiters | ||
198 | + while (NULL != string_ptr) | ||
199 | + { | ||
200 | + nb_fields++; | ||
201 | + string_ptr = strpbrk(string_ptr, CSV_DELIMITERS); | ||
202 | + if (NULL != string_ptr) | ||
203 | + { | ||
204 | + string_ptr++; | ||
205 | + } | ||
206 | + } | ||
207 | + | ||
208 | + // Globals allocation | ||
209 | + CSV_NB_FIELDS = nb_fields; | ||
210 | + CSV_HEADER_FIELDS = malloc( nb_fields * sizeof(char*) ); | ||
211 | + | ||
212 | + char* token = strtok(header_line, CSV_DELIMITERS); // strtok init. | ||
213 | + | ||
214 | + // Re-read the line to get the header of the columns | ||
215 | + for (unsigned int i = 0; i < nb_fields; i++) | ||
216 | + { | ||
217 | + CSV_HEADER_FIELDS[i] = malloc( FIELD_SIZE * sizeof(char) ); // alloc | ||
218 | + memset(CSV_HEADER_FIELDS[i], 0, FIELD_SIZE); // 0 init. | ||
219 | + strcpy(CSV_HEADER_FIELDS[i], token); // copy field in the structure | ||
220 | + token = strtok(NULL, CSV_DELIMITERS); // loop to get a new field label | ||
221 | + } | ||
222 | + | ||
223 | + if (IS_DEBUG) display_header(); | ||
224 | +} | ||
225 | + | ||
226 | +void read_csv_file(const char * filename, Liste *liste_personne) | ||
227 | +{ | ||
228 | + FILE* fp = fopen(filename, "r"); | ||
229 | + char buffer[BUFFER_SIZE]; | ||
230 | + | ||
231 | + char *string; //pointeur de string va être utiliser dans strsep | ||
232 | + | ||
233 | + | ||
234 | + // Check if the file is really opened | ||
235 | + if (NULL == fp) | ||
236 | + { | ||
237 | + fprintf(stderr, "Unable to open file: %s\n", filename); | ||
238 | + return; | ||
239 | + } | ||
240 | + | ||
241 | + // 1st row is a header with field descriptions | ||
242 | + fgets(buffer, BUFFER_SIZE, fp); | ||
243 | + read_csv_header(buffer); | ||
244 | + | ||
245 | + string = strdup(buffer); //string pointe vers la chaine de caractère buffer | ||
246 | + | ||
247 | + // Remaining rows are the entries | ||
248 | + while ( NULL != fgets(buffer, BUFFER_SIZE, fp) ) | ||
249 | + { | ||
250 | + char* token; | ||
251 | + unsigned int i = 0; | ||
252 | + char* tab_char[50]; | ||
253 | + Personne P; | ||
254 | + | ||
255 | + | ||
256 | + string = strdup(buffer); //string pointe vers la chaine de caractère buffer | ||
257 | + | ||
258 | + | ||
259 | + // strsep init. | ||
260 | + token = strsep(&string, CSV_DELIMITERS); | ||
261 | + //strsep permet de séparer une chaine de caractère en prenant en compte les ,, | ||
262 | + | ||
263 | + while (NULL != token) | ||
264 | + { | ||
265 | + //if (IS_DEBUG) printf("Field %d is %s\n", i++, token); | ||
266 | + | ||
267 | + tab_char[i]=token; | ||
268 | + | ||
269 | + // printf(" %s",tab_char[i]); | ||
270 | + i++; | ||
271 | + | ||
272 | + token = strsep(&string, CSV_DELIMITERS); | ||
273 | + } | ||
274 | + //printf("\n"); | ||
275 | + | ||
276 | + remplissage_personne(tab_char, &P); | ||
277 | + ajout_tete(liste_personne, P); | ||
278 | + | ||
279 | + } | ||
280 | + | ||
281 | + fclose(fp); | ||
282 | +} | ||
283 | + | ||
284 | +void usage(const char * prog_name) | ||
285 | +{ | ||
286 | + printf("Usage is %s your_csv_file\n\n", prog_name); | ||
287 | +} | ||
288 | + | ||
289 | +int main(int argc, char * argv[]) | ||
290 | +{ | ||
291 | + Liste liste_personne = NULL; | ||
292 | + | ||
293 | + if (2 != argc) | ||
294 | + { | ||
295 | + usage(argv[0]); | ||
296 | + return 0; | ||
297 | + } | ||
298 | + | ||
299 | + read_csv_file(argv[1], &liste_personne); | ||
300 | + | ||
301 | + affiche_liste(liste_personne); | ||
302 | + | ||
303 | + printf("Done\n"); | ||
304 | + | ||
305 | + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) | ||
306 | + { | ||
307 | + free(CSV_HEADER_FIELDS[i]); | ||
308 | + } | ||
309 | + free(CSV_HEADER_FIELDS); | ||
310 | + | ||
311 | + | ||
312 | + return 0; | ||
313 | +} | ||
0 | \ No newline at end of file | 314 | \ No newline at end of file |