Commit b2c30e825d9cb02a0d172c36f65e62e81cdf8b56

Authored by Corto Callerisa
1 parent ca16311e

Push code des vacances : chargement d'un dictionnaire avec la structure update a…

…vec un tableau a la place de listes
Showing 1 changed file with 118 additions and 0 deletions   Show diff stats
dictionnaire.c 0 → 100644
... ... @@ -0,0 +1,118 @@
  1 +
  2 +#include <stdbool.h>
  3 +#include <stdio.h>
  4 +#include <stdlib.h>
  5 +#include <ctype.h>
  6 +#include <string.h>
  7 +
  8 +
  9 +Noeud *racine;
  10 +
  11 +long fileSize;
  12 +
  13 +
  14 +unsigned int nbr_mots;
  15 +
  16 +
  17 +Noeud *reserve_noeuds;
  18 +
  19 +
  20 +bool appartient(const char *mot)
  21 +{
  22 +
  23 + Noeud *trav = NULL;
  24 +
  25 +
  26 + trav = racine;
  27 +
  28 +
  29 + int i = 0;
  30 +
  31 +
  32 + while (mot[i] != '\0') {
  33 +
  34 + char c = tolower(mot[i]);
  35 +
  36 + if (isalpha(c)) {
  37 +
  38 + if (trav->enfants[c - 'a'] == NULL) {
  39 + return false;
  40 + }
  41 + trav = trav->enfants[c - 'a'];
  42 + }
  43 + i++;
  44 + }
  45 + return trav->mot_fini;
  46 +}
  47 +
  48 +bool charger_dict(const char *dictionary)
  49 +{
  50 +
  51 + FILE *dict = fopen(dictionary, "rb");
  52 +
  53 + if (dict == false) {
  54 + printf("Could not open this dictionary (dictionary.c file)");
  55 + return false;
  56 + }
  57 +
  58 + fseek(dict, 0, SEEK_END);
  59 + fileSize = ftell(dict);
  60 +
  61 + fseek(dict, 0, SEEK_SET);
  62 +
  63 + reserve_noeuds = calloc((fileSize), sizeof(Noeud));
  64 +
  65 + Noeud *nextFreeNode = reserve_noeuds;
  66 +
  67 + char *buffer = malloc(fileSize + 1);
  68 + fread(buffer, 1, fileSize, dict);
  69 +
  70 + buffer[fileSize] = '\0';
  71 +
  72 + racine = nextFreeNode + 1;
  73 +
  74 + Noeud *trav = NULL;
  75 +
  76 + char *words = buffer;
  77 +
  78 + nbr_mots = 0;
  79 +
  80 + while (*words) {
  81 +
  82 + trav = racine;
  83 +
  84 + for (; *words != '\n' && *words; words++) {
  85 +
  86 + else {
  87 + if (trav->enfants[*words - 'a'] == NULL) {
  88 + trav->enfants[*words - 'a'] = nextFreeNode++;
  89 + }
  90 +
  91 + trav = trav->enfants[*words - 'a'];
  92 + }
  93 + }
  94 +
  95 + trav->mot_fini = true;
  96 + nbr_mots++;
  97 +
  98 + if (*words == '\n') {
  99 + words++;
  100 + }
  101 + }
  102 +
  103 + fclose(dict);
  104 + free(buffer);
  105 +
  106 + return true;
  107 +}
  108 +
  109 +unsigned int taille()
  110 +{
  111 + return nbr_mots;
  112 +}
  113 +
  114 +bool decharger()
  115 +{
  116 + free(reserve_noeuds);
  117 + return true;
  118 +}
... ...