-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuncompress.cpp
122 lines (107 loc) · 2.69 KB
/
uncompress.cpp
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
/**************************************************************************
*
* Kening Zhang, Gang Yang
* CSE 100, fall 17
* 11/9/2017
* cs100fje cs100fiy
*
* Assignment 3
* Filename: uncompress.cpp
* Description: this file will read the contents of the file named by
* its first command line argument. Then use the contents of that file to
* reconstruct the original, uncompressed version
*************************************************************************/
#include <queue>
#include <vector>
#include <iostream>
#include <fstream>
#include "HCNode.h"
#include "HCTree.h"
#include "BitInputStream.h"
#include "BitOutputStream.h"
#define arg_val 3
#define freq_size 256
#define N_BYTE 3
using namespace std;
typedef unsigned char byte;
/** Use the Huffman algorithm to build a Huffman coding trie.
* PRECONDITION: freqs is a vector of ints, such that freqs[i] is
* the frequency of occurrence of byte i in the message.
* POSTCONDITION: root points to the root of the trie,
* and leaves[i] points to the leaf node containing byte i.
*/
int main(int argc, char** argv){
// check the number of argument
if (argc != arg_val)
{
cout<< "Please enter infile outfile"<< endl;
return false;
}
// open the file
ifstream in;
in.open(argv[1]);
// check whether we can open the file
if ( in.is_open()== false )
{
cout<<"Can Not open input file, please retry."<<endl;
return false;
}
/*
// Read header from the file for fake header
std::vector<int> freqs(256,0);
for (int i=0; i<256; i++)
{
int nextInt;
in >> nextInt;
if (in.eof())
break;
cout<< nextInt<<endl;
freqs[i]=nextInt;
// get the newline character
in.get();
}
*/
// Read header from the file
std::vector<int> freqs(freq_size,0);
// use the for loop to read header
for (int i=0; i<freq_size; i++)
{
int nextInt;
in.read((char*)& nextInt,N_BYTE*sizeof(char));
if (in.eof())
break;
freqs[i]=nextInt;
}
// build the Huffman tree
HCTree tree;
tree.build(freqs);
// open the output file for writing
ofstream out;
out.open(argv[2]);
if ( out.is_open()== false )
{
cout<<"Can Not open output file, please retry."<< endl;
return false;
}
// FAKE READ
/* while (1)
{
int retvalue= tree.decode(in);
if (retvalue== 256)
break;
out.put((byte)retvalue);
}
*/
// decode the codes from the bottom of the input file
BitInputStream is(in);
for (int i =0; i< tree.getSum(); i++)
{
int retvalue= tree.decode(is);
// Read bits until reaching the size of bits encoded
if (retvalue== freq_size)
break;
out.put((byte)retvalue);
}
in.close();
out.close();
}