-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartie2.cpp
More file actions
437 lines (355 loc) · 10.4 KB
/
partie2.cpp
File metadata and controls
437 lines (355 loc) · 10.4 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "partie2.h"
#include <iostream>
#include <bitset>
//#define _LOG_DEBUG_
#ifdef _LOG_DEBUG_
#include "tests.h"
#endif
#define TAILLE_BLOC_SMALL 1024
#define TAILLE_BLOC_BIG 71680
#define TAILLE_BLOC TAILLE_BLOC_SMALL
std::pair<char*,int> utilitaire2::readfile(const std::string& file_path, unsigned decalage_bloc) const
{
std::ifstream f(file_path, std::ios_base::in | std::ios_base::binary);
std::pair<char*,int> pr;
pr.first = NULL;
pr.second = 0;
//on lit un bloc a partir d'un decalage par rapport au debut du fichier
if (f.is_open())
{
f.seekg(0, f.end);
unsigned end = f.tellg();
if(decalage_bloc<end)
{
f.seekg(decalage_bloc, f.beg);
char* buffer = new char[TAILLE_BLOC];
f.read(buffer,TAILLE_BLOC);
pr.first = buffer;
if(f)
pr.second = TAILLE_BLOC;
else
pr.second = f.gcount();
}
f.close();
}
return pr;
}
//on accumule les freq de caracteres dans input
void utilitaire2::frequence_bloc(const char input[], int size, std::list< std::pair< char, int > >& items) const
{
std::list< std::pair< char, int> >::iterator it;
int i;
for(i=0; i < size; i+=1)
{
for(it = items.begin(); it != items.end() && it->first != input[i] ; ++it)
;
if(it==items.end())
{
std::pair<char, int> p;
p.first = input[i];
p.second = 1;
items.push_back(p);
}
else
{
it->second += 1;
}
}
}
std::pair<char,int>* utilitaire2::frequence(std::list< std::pair< char, int > >& items) const
{
std::pair<char,int> * freq = new std::pair<char,int>[items.size()+1];
std::list< std::pair< char, int> >::iterator it;
int i = 0;
for (it = items.begin(); it != items.end(); ++it){
freq[i].first = it->first;
freq[i].second = it->second;
i += 1;
}
//le dernier element mis a nul pour marquer la fin du tableau
freq[i].first = freq[i].second = 0;
return freq;
}
#ifdef TAILLE_BLOC
//#undef TAILLE_BLOC
//#define TAILLE_BLOC TAILLE_BLOC_BIG
void utilitaire2::compresser(const std::string& src, const std::string& dest)
{
std::cout << "commencement compression (taille de bloc " << TAILLE_BLOC << ")" << std::endl;
arbre2 a(src);
std::cout << "taille de l'arbre:" << a.ecrire(dest) << std::endl;
unsigned decalage = 0;
char buffer[TAILLE_BLOC];
unsigned ecrits = 0;
bool continuer = true;
unsigned bit_offset = 0;
unsigned last_bit_offset = 0;
char c = '\0';
//on lit par bloc et on compresse chaque bloc qu'on rajoute en fin de fichier destination
#ifdef _LOG_DEBUG_
reset_file("codes.txt");
reset_file("bits.txt");
#endif
do {
std::ifstream ifs(src);
std::string data;
if(ifs.is_open())
{
ifs.seekg(decalage,ifs.beg);
ifs.read(buffer,TAILLE_BLOC);
if (ifs)
{
data.append(buffer,TAILLE_BLOC);
decalage += TAILLE_BLOC;
std::cout << TAILLE_BLOC << " octets lus / " ;
}
else
{
data.append(buffer,ifs.gcount());
std::cout << ifs.gcount() << " octets lus / ";
continuer = false;
}
ifs.close();
}
else{
std::cerr << "Impossible d'ouvrir fichier source " << src << ". Compression interrompue" << std::endl;
return;
}
int * code = convertir_vers_code(data,a.table_codage);
char* bits = convertir_en_bit2(code+1,code[0]);
#ifdef _LOG_DEBUG_
log_code("codes.txt",code);
std::string sep = "@";
#endif
std::ofstream ofs(dest,std::ios_base::in | std::ios_base::out | std::ios_base::binary | std::ios_base::ate);
if (ofs.is_open())
{
//si il y a un nombre de bits non multiple de 8 il faut ecraser la fin de fichier
//en rajoutant la partie droite du premier octect code suivant
const unsigned size_octets = code[0] / 8;
const unsigned size_tableau = utilitaire2::nb_octets(code[0]);
const unsigned modulo = code[0] % 8;
//on écrase la fin du fichier avec le dernier caractère mixe avec le debut du code suivant
//on ecrit les code en tenant compte de l'offset de bit
static const unsigned char mask_bytes [] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
std::cout << size_octets << " octets compresses; bit offset=" << bit_offset << " nb bits code=" << code[0] << " nb octets=" << size_octets << " last octet="
<< std::bitset<8>(c);
std::bitset<8> b(c | ( (bits[0] >> bit_offset) & mask_bytes[bit_offset] ));
std::cout << " first octet " << b << " stream offset=" << ofs.tellp() << std::endl;
unsigned j;
for ( j = 0; j < size_octets; j += 1 )
{
c = c | ( (bits[j] >> bit_offset) & mask_bytes[bit_offset] );
ofs.write(&c,1);
#ifdef _LOG_DEBUG_
log_bits("bits.txt",c);
#endif
c = (bits[j] << (8-bit_offset)); //0 si bit_offset = 0
}
ecrits += size_octets;
if ( j < size_tableau )
{
c = c | ( (bits[j] >> bit_offset) & mask_bytes[bit_offset] );
if( modulo + bit_offset >= 8 )
{
ofs.write(&c,1);
#ifdef _LOG_DEBUG_
sep.append("@");
log_bits("bits.txt",c);
#endif
unsigned offset = (bit_offset + modulo) % 8;
c = (bits[j] << (8-bit_offset)); //0 si bit_offset = 0
ecrits += 1;
}
}
bit_offset = (bit_offset + modulo) % 8; // on réévalue le nouveau offset
ofs.close();
}
else
{
std::cerr << "cannot open destination file " << dest << ". Compression areetée." << std::endl;
continuer = false;
}
#ifdef _LOG_DEBUG_
log_file("codes.txt",sep);
log_file("bits.txt",sep);
#endif
delete [] code;
delete [] bits;
}while(continuer);
if (bit_offset)
{
std::ofstream ofs(dest,std::ios_base::in | std::ios_base::out | std::ios_base::binary | std::ios_base::ate);
if (ofs.is_open())
{
ofs.write(&c,1);
ofs.close();
}
else
{
std::cerr << "Impossible d'ouvrir fichier destination pour ecrire byte final" << std::endl;
}
}
std::cout << ecrits << " octets écrits" << std::endl;
}
#undef TAILLE_BLOC
#define TAILLE_BLOC TAILLE_BLOC_SMALL
//#pragma message ("TAILLE_BLOC redefined to " ##TAILLE_BLOC)
#endif
#ifdef TAILLE_BLOC
#undef TAILLE_BLOC
#define TAILLE_BLOC TAILLE_BLOC_SMALL
void utilitaire2::decompresser(const std::string& src, const std::string& dest)
{
std::cout << "commencement decompression (taille de bloc " << TAILLE_BLOC << ")" << std::endl;
arbre2 a("");
//on lit l'arbre en en-tete du fichier source
unsigned lu = a.lire(src);
if(lu)
{
unsigned size, end ;
//calcul de la taille de fichier source
std::ifstream ifs(src, std::ios_base::in | std::ios_base::binary);
if(ifs.is_open())
{
ifs.seekg(0, ifs.end);
end = ifs.tellg();
ifs.close();
}
else
{
std::cerr << "Impossible ouvrir le fichier source " << src << std::endl;
return;
}
//on retire la taille de l'arbre
size = end - lu;
char buffer[TAILLE_BLOC];
bool continuer = true;
unsigned decalage_bloc = lu;
unsigned decalage_bit = 0;
unsigned taille_bloc_lu = 0;
lu = 0;
{
std::ofstream ofs(dest,std::ios_base::out | std::ios_base::binary);
if (ofs.is_open())
{
ofs.close();
}
}
do{
std::ifstream ifs(src, std::ios_base::in | std::ios_base::binary);
if(ifs.is_open())
{
ifs.seekg(decalage_bloc,ifs.beg);
ifs.read(buffer,TAILLE_BLOC);
if(ifs)
taille_bloc_lu = TAILLE_BLOC;
else // on est en fin de fichier, on fait la derniere passe et on arrete
{
taille_bloc_lu = ifs.gcount();
continuer = false;
}
ifs.close();
std::cout << "[" << decalage_bloc << "," << decalage_bloc+taille_bloc_lu << "] taille bloc: " << taille_bloc_lu << " ";
decalage_bloc += taille_bloc_lu;
}
std::ofstream ofs(dest,std::ios_base::out | std::ios_base::binary | std::ios_base::app);
if(ofs.is_open())
{
char c;
unsigned offset = decalage_bit;
//std::cout << "reading chars : ";
while( offset < taille_bloc_lu*8 )
{
c='\0';
//on decode chq caractere
int fait = a.getchar(buffer,taille_bloc_lu,offset,c);
if(c!='\0')
{
//std << cout << "'" << c << "' ";
ofs.put(c);
offset = fait;
decalage_bit = 0;
}
else
{
if (size*8 - decalage_bloc*8 - offset >= 8) //
{
// le décodage a echoué il faut relire un bloc a partir du dernier caractère lu correctement
// on doit revenir en arrière en arrondissant à l'octet inferieur à offset (qui est en bits)
decalage_bloc += ((offset/8) - taille_bloc_lu);
decalage_bit = (offset%8);
std::cout << "error reading, restarting from " << decalage_bloc << " with bit offset " << decalage_bit ;
break;
}
else //il ne reste que quelques bits de bourrage en fin de fichier: on arrete
{
continuer = false;
}
}
}
std::cout << std::endl;
//ofs << '@' << decalage_bit << '@';
ofs.close();
}
else
{
std::cerr << "Imposible d'ouvrir le fichier de destination " << dest << std::endl;
continuer = false;
}
}while(continuer);
std::cout << decalage_bloc - lu << " octets lus" << std::endl;
}
else
std::cerr << "l'arbre d'en tete n'a pas ete lu" << std::endl;
}
#undef TAILLE_BLOC
#define TAILLE_BLOC TAILLE_BLOC_SMALL
#endif
// on court circuite le constructeur de la class mere avec un nom de fichier non valide
arbre2::arbre2(const std::string& file_path) : arbre("")
{
utilitaire2 u;
int decalage_bloc = 0;
std::list< std::pair< char, int > > items;
//lecture du fichier par bloc
std::pair<char*,int> chars = u.readfile(file_path ,decalage_bloc);
while (chars.second==TAILLE_BLOC) {
//frequence des elements calculee par iteration
u.frequence_bloc(chars.first, chars.second, items);
decalage_bloc += TAILLE_BLOC;
delete [] chars.first;
chars = u.readfile(file_path, decalage_bloc);
}
u.frequence_bloc(chars.first, chars.second, items);
delete [] chars.first;
//on construit la paire pour la conruction de l'arbre
std::pair<char,int>* freq = u.frequence(items);
racine = u.construire_arbre(freq);
int i = 0;
//double total_occurences = 0;
while(freq[i].first!='\0')
{
//total_occurences += (double)freq[i].second;
char_list.append(1,freq[i++].first);
}
creer_table_codage(char_list);
/*double esp = 0.0;
for(i=0; i<256;++i)
{
if ( table_codage[i] )
{
int j;
for( j = 0; freq[j].first != '\0' && freq[j].first != (char)i ; j+=1)
;
if(freq[j].first != '\0'){
esp += (double)(freq[j].second * table_codage[i][0]);
}
else
std::cerr << "Calcul esperance: le caractere " << (char)i << " n'est pas trouvé" << std::endl ;
}
}
esp = esp / total_occurences;
std::cout << "L'espérence de la taille en bit de l'encodage dun caractere est de " << esp << std::endl;*/
delete freq;
}