Blame view

dictionnaire.c 1.8 KB
b2c30e82   Corto Callerisa   Push code des vac...
1
2
3
4
5
6
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
  
  #include <stdbool.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <ctype.h>
  #include <string.h>
  
  
  Noeud *racine;
  
  long fileSize;
  
  
  unsigned int nbr_mots;
  
  
  Noeud *reserve_noeuds;
  
  
  bool appartient(const char *mot)
  {
      
      Noeud *trav = NULL;
  
      
      trav = racine;
  
      
      int i = 0;
  
      
      while (mot[i] != '\0') {
  
          char c = tolower(mot[i]);
  
          if (isalpha(c)) {
  
              if (trav->enfants[c - 'a'] == NULL) {
                  return false;
              }
              trav = trav->enfants[c - 'a'];
          }
          i++;
      }
      return trav->mot_fini;
  }
  
  bool charger_dict(const char *dictionary)
  {
  
      FILE *dict = fopen(dictionary, "rb");
  
      if (dict == false) {
          printf("Could not open this dictionary (dictionary.c file)");
          return false;
      }
  
      fseek(dict, 0, SEEK_END);
      fileSize = ftell(dict);
  
      fseek(dict, 0, SEEK_SET);
  
      reserve_noeuds = calloc((fileSize), sizeof(Noeud));
  
      Noeud *nextFreeNode = reserve_noeuds;
  
      char *buffer = malloc(fileSize + 1);
      fread(buffer, 1, fileSize, dict);
  
      buffer[fileSize] = '\0';
  
      racine = nextFreeNode + 1;
  
      Noeud *trav = NULL;
  
      char *words = buffer;
  
      nbr_mots = 0;
  
      while (*words) {
         
          trav = racine;
  
          for (; *words != '\n' && *words; words++) {
           
              else {
                  if (trav->enfants[*words - 'a'] == NULL) {
                      trav->enfants[*words - 'a'] = nextFreeNode++;
                  }
  
                  trav = trav->enfants[*words - 'a'];
              }
          }
  
          trav->mot_fini = true;
          nbr_mots++;
  
          if (*words == '\n') {
              words++;
          }
      }
  
      fclose(dict);
      free(buffer);
  
      return true;
  }
  
  unsigned int taille()
  {
      return nbr_mots;
  }
  
  bool decharger()
  {
      free(reserve_noeuds);
      return true;
  }