summaryrefslogtreecommitdiff
path: root/sem_3/SYSTEME/TP3/HUFFMAN/distrib.c
blob: 3ae45b1f69588e2ae82d392992a4169249f1453f (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
185
186
187
188
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct proba proba;
struct proba {
  char * carac;
  int * nb_carac;
  float * proba;
  int taille;
  int somme;
};
typedef struct branche branche;
struct branche {
  char noeud;
  branche * filsg;
  branche * filsd;
};
typedef struct doublet doublet;
struct doublet{
  int a;
  int b;
};
typedef struct table table;
struct table {
  unsigned int taille;
  unsigned int * taillecode;
  char * carac;
  char ** code;
  unsigned int indice[256];
};
typedef struct buffer buffer;
struct buffer {
  unsigned int taille;
  char contenu;
};

void loading(int, FILE *,float *);
void forceprintbuffer(buffer *,FILE *);
void supprimerTable(table *);
void ecrirecomp(FILE *,FILE *,table *,buffer *,int );
void videbuffer(buffer *);
void ajouterbuffer(buffer*, char,FILE *);
void printcaraccode(table *,char,FILE *, buffer*);
void printchemin (char *, int, FILE *, buffer *);
void parcoursarbre ( branche *, char *, int, FILE *, table *, buffer *);
table * ecrireArbre(FILE *, branche*, int,buffer *);
void afficheTab(proba *);
void supprimerArbre(branche *);
branche* creerArbre(char , branche * , branche * );
int estdanstab (char *,int,char);
void creertab (proba *, FILE *);
branche* creerHuff(proba*);
doublet deuxpluspetits(proba *);
void aff(branche *, int );
void printinfofinales(table *, FILE *, FILE *);


int main (int argc, char ** argv) {
  if (argc != 3) {
    printf("Erreur, la syntaxe est la suivante : ./huffhuff [source] [destination]");
    return -1;
  }
  FILE * fichier = fopen(argv[1], "r");
  if (fichier == NULL) {
    printf("Erreur, l'ouverture du fichier source à rencontré une erreur, verifiez que vous avez les droits en lecture sur le fichier.");
  }
  FILE * fichierdest = fopen(argv[2],"wb");
  if (fichierdest == NULL) {
    printf("Erreur, l'ouverture du fichier de destination à rencontré une erreur.");
    return -2;
  }
  fseek(fichier,0,SEEK_END);
  int size = ftell(fichier);
  rewind(fichier);
  proba tab;
  printf("Le fichier %s fait %d octets.\n",argv[1],size);
  creertab (&tab, fichier);
  afficheTab(&tab);
  branche * Arbre = creerHuff(&tab);
  free(tab.carac);
  free(tab.nb_carac);
  free(tab.proba);
  buffer * buf = malloc(sizeof(buffer));
  videbuffer(buf);
  table * tablecodage = ecrireArbre(fichierdest, Arbre, tab.taille,buf);
  ecrirecomp(fichier,fichierdest,tablecodage,buf,size);
  printinfofinales(tablecodage,fichier,fichierdest);
  supprimerArbre(Arbre);
  supprimerTable(tablecodage);
  free(buf);
  fclose(fichierdest);
  fclose( fichier);
  return 0;
}


void printinfofinales(table * tablecode, FILE * source, FILE * destination){
  int somme=0;
  for (int i = 0; i < tablecode->taille;i++){
    somme+=tablecode->taillecode[i];
  }
  float moy = (float)somme/(float)tablecode->taille;
  printf("Fini ! \n");
  printf("Longueur moyenne du codage : %2.2f bits;", moy);
  fseek(source,0,SEEK_END);
  fseek(destination,0,SEEK_END);
  int taillesource = ftell(source);
  int taillecible = ftell(destination);
  printf (" Taille d'origine : %d; Taille compressée : %d; Soit un gain de %3.2f %%\n", taillesource,taillecible,100-(float)taillecible*100/(float)taillesource);
}
void supprimerTable(table * tablecod){
  for (unsigned int i = 0; i < tablecod->taille; i ++){
    free(tablecod->code[i]);
  }
  free(tablecod->taillecode);
  free(tablecod->carac);
  free(tablecod);
}
void loading(int taille, FILE * fichier, float * anciennevaleur){
  int current = ftell(fichier)-1;
  float avancement = current*100/taille;
  if (current == 0){
    *anciennevaleur=0;
    printf("[");
    fflush(stdout);
  }
  if (*anciennevaleur+10 <= avancement){
    printf("##");
    fflush(stdout);
    *anciennevaleur=avancement;
  }
  if (current == taille-2){
    printf("##]");
    fflush(stdout);
  }

}
void ecrirecomp(FILE * fichier,FILE * fichierdest,table * tablecodage, buffer * buf, int size){
  fseek(fichier,0,SEEK_SET);
  int current=fgetc(fichier);
  float * anciennevaleur=malloc(sizeof(float));
  *anciennevaleur=0;
  while(current!=EOF){
    printcaraccode(tablecodage,(char)current,fichierdest,buf);
    loading(size,fichier,anciennevaleur);
    current =fgetc(fichier);
  }
  char remplissage = (char) 8-buf->taille;
  forceprintbuffer(buf,fichierdest);
  fwrite(&remplissage,sizeof(char),1,fichierdest);
  free(buf);
}
void videbuffer(buffer * buf){
  buf->taille=0;
  buf->contenu = 0;
}
void ajouterbuffer(buffer* buf, char carac,FILE * destination){
  char tmp='1';
  if ( buf->taille < 8){
    if (  carac == tmp)buf->contenu+=pow(2,7-buf->taille);
    buf->taille++;
  }
  if (buf->taille == 8) {
    fwrite(&buf->contenu,sizeof(char),1,destination);
    videbuffer(buf);
    fflush(destination);
  }
}
void forceprintbuffer(buffer * buf,FILE * destination){
  if ( buf->taille>0){
    fwrite(&buf->contenu,sizeof(char),1,destination);
    videbuffer(buf);
  }
}
void printcaraccode(table* tablecodage,char carac,FILE * fichierdest, buffer * buf){
  unsigned int i =0;
  i=tablecodage->indice[(unsigned char)carac];
  for (unsigned int j=0; j < tablecodage->taillecode[i];j++){
    ajouterbuffer(buf,tablecodage->code[i][j],fichierdest);
  }
}
void printchemin (char * chemin, int taille, FILE *destination,buffer * buf){
  char taillechar = (char)taille;
  fwrite(&taillechar, sizeof(char),1,destination);
  for (int i =0; i < taille; i ++){
    ajouterbuffer(buf,chemin[i],destination);
  };
  forceprintbuffer(buf, destination);
}
branche* creerHuff(proba*prob){
  branche * arbrehuf[prob->taille];
  for ( int i =0; i <= prob->taille; i ++){
    arbrehuf[i]= creerArbre(prob->carac[i], NULL, NULL);
  }
  doublet indices = deuxpluspetits(prob);
  while (indices.b!=-1) {
    arbrehuf[indices.a] = creerArbre (0, arbrehuf[indices.a],arbrehuf[indices.b]);
    prob->proba[indices.a]+=prob->proba[indices.b];
    prob->proba[indices.b]=0;
    indices = deuxpluspetits(prob);
  }
  return arbrehuf[indices.a];
}
void parcoursarbre ( branche * Arbre, char * chemin, int taille, FILE * destination, table * tabcode, buffer * buf){
  if ( Arbre->filsd == NULL && Arbre->filsg==NULL ){
    printchemin(chemin, taille, destination, buf);
    fwrite(&Arbre->noeud,sizeof(char),1,destination);
    char * code = malloc(taille*sizeof(char));
    tabcode->taille+=1;
    tabcode->code= realloc(tabcode->code,tabcode->taille*sizeof(char*));
    tabcode->carac=realloc(tabcode->carac,tabcode->taille*sizeof(char));
    tabcode->taillecode=realloc(tabcode->taillecode,tabcode->taille*sizeof(unsigned int));
    tabcode->carac[tabcode->taille-1]=Arbre->noeud;
    tabcode->taillecode[tabcode->taille-1]=taille;
    tabcode->indice[(unsigned char)Arbre->noeud]=tabcode->taille-1;
    for (int i =0; i < taille; i ++){
      code[i]=chemin[i];
    }
    tabcode->code[tabcode->taille-1]=code;
    if (Arbre->noeud==(char)10) {
      printf("Carac = LF");
    }
    else if (Arbre->noeud==(char)13) {
      printf("Carac = CR");
    }
    else printf("Carac = %c", Arbre->noeud);
    printf(", taille chemin = %d, chemin = ", taille);
    for (int i =0; i < taille; i ++){
      printf("%c",chemin[i]);
    }
    printf("\n");
  }
  else{
    int newtaille= taille + 1;
    chemin = realloc(chemin, newtaille*sizeof(char));
    if (Arbre->filsg!=NULL){
      chemin[newtaille-1]='0';
      parcoursarbre ( Arbre->filsg, chemin, newtaille, destination, tabcode,buf );
    }
    if (Arbre->filsd!=NULL){
      chemin[newtaille-1]='1';
      parcoursarbre ( Arbre->filsd, chemin, newtaille, destination, tabcode,buf);
    }
  }
}
table * ecrireArbre(FILE * destination, branche* arbre, int taille,buffer * buf){
  char taille2 = (char)taille;
  table * tabcode = malloc(sizeof(table));
  tabcode->taille = 0;
  tabcode->taillecode= malloc(0);
  tabcode->code=malloc(0);
  tabcode->carac=malloc(0);
  fwrite(&taille2,sizeof(char),1,destination);
  char * chemin = malloc(0);
  parcoursarbre(arbre, chemin, 0, destination, tabcode,buf);
  free(chemin);
  fflush(destination);
  return tabcode;
}
branche* creerArbre(char carac, branche * ssag, branche * ssad){
  branche * tmp = malloc(sizeof(branche));
  tmp->noeud= carac;
  tmp->filsd= ssad;
  tmp->filsg=ssag;
  return tmp;
}
void supprimerArbre(branche * arbre){
  branche * gauche = arbre->filsg;
  branche * droite = arbre->filsd;
  if ( gauche != NULL){
    supprimerArbre(gauche);
  }
  if (droite!=NULL){
    supprimerArbre(droite);
  }
  if (droite==NULL && gauche == NULL) {
    free(arbre);
  }
}
void creertab (proba * tab, FILE * fichier){
  tab->carac = malloc(0);
  tab->nb_carac = malloc (0);
  tab->taille = 0;
  tab->somme=0;
  int caracint = 0;
  char caracchar = 0;
  int indice = -1;
  caracint= fgetc(fichier);
  while (caracint != EOF ) {
    caracchar = (char)caracint;
    tab->somme ++;
    indice = estdanstab(tab->carac, tab->taille, caracchar);
    if ( indice != -1) {
      tab->nb_carac[indice]++;
    }
    else {
      tab->taille ++;
      tab->carac=realloc(tab->carac,tab->taille*(sizeof(char)));
      tab->nb_carac=realloc(tab->nb_carac,tab->taille*(sizeof(int)));
      tab->carac[tab->taille-1]= caracchar;
      tab->nb_carac[tab->taille-1]= 1;
    }
    caracint = fgetc(fichier);
  }
  tab->proba=malloc(tab->taille*(sizeof(float)));
  for (int i =0; i<tab->taille;i++){
    tab->proba[i]= (float)tab->nb_carac[i]/(float)tab->somme;
  }
}
int estdanstab(char * tab, int n, char x) {
  int i=0;
  int ret=0;
  while (i<n && !ret) {
    if (tab[i]==x) ret = 1;
    i ++;
  }
  return ret ? i-1 : -1;
}
void afficheTab(proba * prob){
  for (int i =0; i < prob->taille; i ++) {
    if (prob->carac[i]==(char)10) {
      printf("LF | %1.3f \n", prob->proba[i]);
    }
    if (prob->carac[i]==(char)13) {
      printf("CR | %1.3f \n", prob->proba[i]);
    }
    else {
      printf("%c | %1.3f \n", prob->carac[i], prob->proba[i]);
    }

  }
  printf("Le fichier comporte %d caracteres differents\n", prob->taille);
}
doublet deuxpluspetits(proba * prob) { //renvoie les indices des deux plus petits float du tableau
  doublet ret;
  ret.a= -1;
  ret.b=-1;
  float aval,bval;
  aval = 2;
  bval = 2;
  for (int i =0; i< prob->taille; i ++){
    if ( prob->proba[i]!=0){
      if ( prob->proba[i]< aval){
        ret.b= ret.a;
        bval=aval;
        ret.a=i;
        aval=prob->proba[i];
      }
      else if (prob->proba[i]<bval){
        ret.b=i;
        bval=prob->proba[i];
      }
    }
  }
  return ret;
}