Commit fc996bc8ee2a8414235eef515cdad8794dac93d0
1 parent
634be673
version 2(affichage et structure de personnes)
Showing
1 changed file
with
153 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,153 @@ |
1 | +#include <stdio.h> | |
2 | +#include <string.h> | |
3 | +#include <stdbool.h> | |
4 | +#include <stdlib.h> | |
5 | + | |
6 | +typedef struct Personne Personne; | |
7 | +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}; | |
8 | +typedef Personne* Liste; | |
9 | + | |
10 | + | |
11 | +affiche_header_top50() | |
12 | +{ | |
13 | + printf("Index nom prenom score"); | |
14 | +} | |
15 | + | |
16 | +affiche_header_recherche() | |
17 | +{ | |
18 | + printf("athId nom prenom gender age taille poids score18.1 score18.2 score 18.2a score18.3 score18.4 score18.5"); | |
19 | +} | |
20 | + | |
21 | +affiche_top50(Personne P, int i) | |
22 | +{ | |
23 | + printf("%d %s %s %d", i , P.lastName, P.firstName, P.score); | |
24 | +} | |
25 | + | |
26 | +affiche_recherche(Personne P) | |
27 | +{ | |
28 | + printf("%d %s %s %c %d %s %s %", P.athId, P.lastName, P.firstName, P.gender, P.age, P.height, P.weight, P.); | |
29 | +} | |
30 | + | |
31 | +// Conditionals | |
32 | +const bool IS_DEBUG = true; | |
33 | + | |
34 | +// Constants | |
35 | +const unsigned int BUFFER_SIZE = 2048; | |
36 | +const unsigned int FIELD_SIZE = 20; | |
37 | +const char CSV_DELIMITERS[] = ","; | |
38 | + | |
39 | +// Globals | |
40 | +char** CSV_HEADER_FIELDS; | |
41 | +unsigned int CSV_NB_FIELDS; | |
42 | + | |
43 | +void display_header() | |
44 | +{ | |
45 | + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) | |
46 | + { | |
47 | + printf("%d - %s\n", i, CSV_HEADER_FIELDS[i]); | |
48 | + } | |
49 | +} | |
50 | + | |
51 | +void read_csv_header(char * header_line) | |
52 | +{ | |
53 | + int line_length = strlen(header_line); | |
54 | + int nb_fields = 0; | |
55 | + char* string_ptr = header_line; | |
56 | + | |
57 | + // Count the occurrences of delimiters | |
58 | + while (NULL != string_ptr) | |
59 | + { | |
60 | + nb_fields++; | |
61 | + string_ptr = strpbrk(string_ptr, CSV_DELIMITERS); | |
62 | + if (NULL != string_ptr) | |
63 | + { | |
64 | + string_ptr++; | |
65 | + } | |
66 | + } | |
67 | + | |
68 | + // Globals allocation | |
69 | + CSV_NB_FIELDS = nb_fields; | |
70 | + CSV_HEADER_FIELDS = malloc( nb_fields * sizeof(char*) ); | |
71 | + | |
72 | + char* token = strtok(header_line, CSV_DELIMITERS); // strtok init. | |
73 | + | |
74 | + | |
75 | + | |
76 | + // Re-read the line to get the header of the columns | |
77 | + for (unsigned int i = 0; i < nb_fields; i++) | |
78 | + { | |
79 | + CSV_HEADER_FIELDS[i] = malloc( FIELD_SIZE * sizeof(char) ); // alloc | |
80 | + memset(CSV_HEADER_FIELDS[i], 0, FIELD_SIZE); // 0 init. | |
81 | + strcpy(CSV_HEADER_FIELDS[i], token); // copy field in the structure | |
82 | + token = strtok(NULL, CSV_DELIMITERS); // loop to get a new field label | |
83 | + // printf("%s ", *token); | |
84 | + } | |
85 | + | |
86 | + if (IS_DEBUG) display_header(); | |
87 | +} | |
88 | + | |
89 | +void read_csv_file(const char * filename) | |
90 | +{ | |
91 | + FILE* fp = fopen(filename, "r"); | |
92 | + char buffer[BUFFER_SIZE]; | |
93 | + | |
94 | + // Check if the file is really opened | |
95 | + if (NULL == fp) | |
96 | + { | |
97 | + fprintf(stderr, "Unable to open file: %s\n", filename); | |
98 | + return; | |
99 | + } | |
100 | + | |
101 | + // 1st row is a header with field descriptions | |
102 | + fgets(buffer, BUFFER_SIZE, fp); | |
103 | + read_csv_header(buffer); | |
104 | + | |
105 | + // Remaining rows are the entries | |
106 | + while ( NULL != fgets(buffer, BUFFER_SIZE, fp) ) | |
107 | + { | |
108 | + char* token; | |
109 | + unsigned int i = 0; | |
110 | + | |
111 | + // strtok init. | |
112 | + token = strtok(buffer, CSV_DELIMITERS); | |
113 | + | |
114 | + while (NULL != token) | |
115 | + { | |
116 | + if (IS_DEBUG) printf("Field %d is %s\n", i++, token); | |
117 | + | |
118 | + // ... | |
119 | + // you can strcpy the `token` string in your data structures | |
120 | + // ... | |
121 | + | |
122 | + token = strtok(NULL, CSV_DELIMITERS); | |
123 | + } | |
124 | + } | |
125 | + | |
126 | + fclose(fp); | |
127 | +} | |
128 | + | |
129 | +void usage(const char * prog_name) | |
130 | +{ | |
131 | + printf("Usage is %s your_csv_file\n\n", prog_name); | |
132 | +} | |
133 | + | |
134 | +int main(int argc, char * argv[]) | |
135 | +{ | |
136 | + | |
137 | + printf("test \n"); | |
138 | + if (2 != argc) | |
139 | + { | |
140 | + usage(argv[0]); | |
141 | + return 0; | |
142 | + } | |
143 | + | |
144 | + read_csv_file(argv[1]); | |
145 | + | |
146 | + for(unsigned int i = 0; i < CSV_NB_FIELDS; i++) | |
147 | + { | |
148 | + free(CSV_HEADER_FIELDS[i]); | |
149 | + } | |
150 | + free(CSV_HEADER_FIELDS); | |
151 | + | |
152 | + return 0; | |
153 | +} | ... | ... |