Blame view

src/csv_reader_V2.c 5.89 KB
fc996bc8   mdupre1   version 2(afficha...
1
2
3
4
5
6
  #include <stdio.h>
  #include <string.h>
  #include <stdbool.h>
  #include <stdlib.h>
  
  typedef struct Personne Personne;
813e29bb   mdupre1   Lecture (non fini)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  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;};
      
fc996bc8   mdupre1   version 2(afficha...
41
42
43
  typedef Personne* Liste;
  
  
813e29bb   mdupre1   Lecture (non fini)
44
  void affiche_header_top50()
fc996bc8   mdupre1   version 2(afficha...
45
46
47
48
  {
      printf("Index nom prenom score");
  }
  
813e29bb   mdupre1   Lecture (non fini)
49
  void affiche_header_recherche()
fc996bc8   mdupre1   version 2(afficha...
50
51
52
53
  {
      printf("athId nom prenom gender age taille poids score18.1 score18.2 score 18.2a score18.3 score18.4 score18.5");
  }
  
813e29bb   mdupre1   Lecture (non fini)
54
  void affiche_top50(Personne P, int i)
fc996bc8   mdupre1   version 2(afficha...
55
  {
813e29bb   mdupre1   Lecture (non fini)
56
      printf("%d %s %s %d", i , P.lastName, P.firstName, P.overallScrore);
fc996bc8   mdupre1   version 2(afficha...
57
58
  }
  
813e29bb   mdupre1   Lecture (non fini)
59
  void affiche_recherche(Personne P)
fc996bc8   mdupre1   version 2(afficha...
60
  {
690aa1f7   mdupre1   affichage fini
61
      printf("%d %s %s %c %d %s %s %d %d %d %d %d %d", P.athId, P.lastName, P.firstName, P.gender, P.age, P.height, P.weight, P.score18_1, P.score18_2, P.score18_2a, P.score18_3, P.score18_4, P.score18_5);
fc996bc8   mdupre1   version 2(afficha...
62
63
64
  }
  
  // Conditionals
813e29bb   mdupre1   Lecture (non fini)
65
  const bool          IS_DEBUG            = false;
fc996bc8   mdupre1   version 2(afficha...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  
  // 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;
  
  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
         // printf("%s ", *token);
      }
      
      if (IS_DEBUG) display_header();
  }
  
813e29bb   mdupre1   Lecture (non fini)
122
123
124
125
126
127
128
129
130
  
  void ajout_personne_tab(Personne P, char* token)
  {
      
      
      
  }
  
  
fc996bc8   mdupre1   version 2(afficha...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  void read_csv_file(const char * filename)
  {
      FILE*   fp = fopen(filename, "r");
      char    buffer[BUFFER_SIZE];
  
      // 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);
813e29bb   mdupre1   Lecture (non fini)
146
147
148
      
      Personne P[10];
      unsigned int    i = 0;
fc996bc8   mdupre1   version 2(afficha...
149
150
151
152
  
      // Remaining rows are the entries
      while ( NULL != fgets(buffer, BUFFER_SIZE, fp) )
      {
fc996bc8   mdupre1   version 2(afficha...
153
          // strtok init.
813e29bb   mdupre1   Lecture (non fini)
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
          P[i].athId = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].regId = atoi(strtok(NULL, CSV_DELIMITERS));
          P[i].divId = atoi(strtok(NULL, CSV_DELIMITERS));
          P[i].lastName = strtok(NULL, CSV_DELIMITERS);
          P[i].firstName = strtok(buffer, CSV_DELIMITERS);
          P[i].gender = *strtok(buffer, CSV_DELIMITERS);
          P[i].age = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].weight = strtok(buffer, CSV_DELIMITERS);
          P[i].height  = strtok(buffer, CSV_DELIMITERS);
          P[i].affiliateId = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].affiliateName = strtok(buffer, CSV_DELIMITERS);
          P[i].overallScrore = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].overallRank = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].score18_1 = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].scoreDisplay18_1 = strtok(buffer, CSV_DELIMITERS);
          P[i].rank18_1 = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].score18_2 = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].score18_2a = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].scoreDisplay18_2a = strtok(buffer, CSV_DELIMITERS);
          P[i].rank18_2a = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].score18_3 = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].scoreDisplay18_3 = strtok(buffer, CSV_DELIMITERS);
          P[i].rank18_3 = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].score18_4 = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].scoreDisplay18_4 = strtok(buffer, CSV_DELIMITERS);
          P[i].rank18_4 = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].score18_5 = atoi(strtok(buffer, CSV_DELIMITERS));
          P[i].scoreDisplay18_5 = strtok(buffer, CSV_DELIMITERS);
          P[i].rank18_5 = atoi(strtok(buffer, CSV_DELIMITERS));
          
          i++;
fc996bc8   mdupre1   version 2(afficha...
185
186
      }
  
813e29bb   mdupre1   Lecture (non fini)
187
188
      printf("%d ", P[2].regId);
      
fc996bc8   mdupre1   version 2(afficha...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
      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[])
  {
      
      printf("test \n");
      if (2 != argc)
      {
          usage(argv[0]);
          return 0;
      }
  
      read_csv_file(argv[1]);
      
      for(unsigned int i = 0; i < CSV_NB_FIELDS; i++)
      {
          free(CSV_HEADER_FIELDS[i]);
      }
      free(CSV_HEADER_FIELDS);
  
      return 0;
  }