| 12
 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
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 
 |  
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
 
typedef struct
{
   int posLecture;
   int tailleBuffer;
   char *buffer;
   FILE *fichier;
}
WordF;
 
int endOfFile (WordF * w)
{
   int x = 0;
 
   return x;
}
 
int estPonctuation (int c)
{
   return ispunct ((unsigned char) c);
}
 
int lire_mot_ponctuation (WordF * w, char **mot, char **ponctuation)
{
   int tmp;
   int i;
 
   /* fin du fichier && traitement du buffer termine */
   if (endOfFile (w) && w->posLecture >= w->tailleBuffer)
      return 0;
 
   /* 1ere appel || traitement de "l'ancien buffer" termine */
   if ((w->posLecture == -1 || w->posLecture >= w->tailleBuffer))
   {
      memset (&(w->buffer[0]), EOF, w->tailleBuffer); /* initialisation a EOF */
      tmp = fread (&(w->buffer[0]), sizeof (char), w->tailleBuffer, w->fichier); /* lecture fread */
 
      if (tmp == -1)
         return 0;
 
      /* nouvelle position de lecture pour le traitement */
      w->posLecture = 0;
   }
 
   /* memorisation de la derniere position de lecture connue */
   tmp = w->posLecture;
 
   /* marqueur EOF => feof() + fin traitement du buffer */
   if (w->buffer[w->posLecture] == EOF)
      return 0;
 
   /* recherche de la ponctuation */
   while (!estPonctuation (w->buffer[w->posLecture])
          && w->posLecture < w->tailleBuffer)
      w->posLecture++;
 
   /* si on a pas trouve de ponctuation, on recherche la fin du mot */
   if (w->posLecture >= w->tailleBuffer)
   {
 
      /* si le mot remplit entierement le buffer et non termine => on le coupe */
      if (tmp == 0)
      {
 
         *mot = (char *) malloc (sizeof (char) * (w->tailleBuffer + 1));
         if (*mot == NULL)
            return 0;
         strcpy (*mot, &(w->buffer[tmp]));
         *ponctuation = (char *) malloc (sizeof (char));
         *ponctuation[0] = '*';
 
         return 1;
      }
 
      /* on copie le mot en cours de lecture au debut du buffer */
      for (i = 0; i < w->tailleBuffer - tmp; i++)
         w->buffer[i] = w->buffer[tmp + i];
 
      /* on marque le reste du buffer de EOF */
      memset (&(w->buffer[i]), EOF, w->tailleBuffer - i);
 
      /* nouvelle lecture pour completer le mot */
      tmp =
         fread (&(w->buffer[i]), sizeof (char), w->tailleBuffer - i - 1,
                w->fichier);
 
      if (tmp == -1)
         return 0;
 
      /* memorise le debut de lecture du mot = debut du buffer */
      w->posLecture = 0;
      tmp = 0;                  /* tmp = w->posLecture */
 
      /* on place un '\O' en fin de buffer ce qui va arreter la boucle
         while de recherche de ponctuation si le mot est de taille >= 50 */
      w->buffer[w->tailleBuffer - 1] = '\0';
 
      while (!estPonctuation (w->buffer[w->posLecture])
             && w->posLecture < w->tailleBuffer)
         w->posLecture++;
 
   }
 
   *ponctuation = (char *) malloc (sizeof (char));
   strncpy (*ponctuation, &(w->buffer[w->posLecture]), 1);
   /* on stocke la ponctuation */
    /**ponctuation = w->buffer[w->posLecture];*/
 
   /* on place un \0 a la place de la ponctuation puis on stocke le mot */
   w->buffer[w->posLecture] = '\0';
 
   /* allocation dynamique du mot */
   *mot = (char *) malloc (sizeof (char) * (strlen (&(w->buffer[tmp])) + 1));
   if (*mot == NULL)
      return 0;
 
   /* on stocke le mot */
   strcpy (*mot, &(w->buffer[tmp]));
 
   w->posLecture++;
 
   return 1;
}
 
int main (void)
{
   WordF w = { 0 };
   char *mot;
   char *ponct;
 
   int ret = lire_mot_ponctuation (&w, &mot, &ponct);
 
   printf ("ret = %d, mot = '%s' ponst = '%s'\n", ret, mot, ponct);
   return 0;
} | 
Partager