-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.cc
More file actions
60 lines (52 loc) · 1.42 KB
/
encoder.cc
File metadata and controls
60 lines (52 loc) · 1.42 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
#include "huffman.hh"
#include "bitio.hh"
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
using namespace std;
void encode(const char* file)
{
Huffman huff_enc;
ifstream input_file(file); // access input file and ensure readability
if (!input_file.is_open())
{
cerr << "Can't open input file" << file << "\n";
}
// create output file
string name(file);
ofstream output_file(name + ".comp", ios::binary);
// initialize other necessary objects
BitIO bitio(&output_file, nullptr);
Huffman::bits_t vec_of_bits;
char symbol;
// main encoding loop
while (input_file.get(symbol)) // aka: for each symbol in the input file
{
// encode the symbol and output the cooresponding bits to the oputput file
vec_of_bits = huff_enc.encode(symbol);
for (auto v : vec_of_bits)
{
bitio.output_bit(v);
}
}
// when there are no characters left in the input file
// encode the end of file symbol so decoder knows when to be done
vec_of_bits = huff_enc.encode(Huffman::HEOF);
for (auto v : vec_of_bits)
{
bitio.output_bit(v);
}
}
int main(int argc, char** argv)
{
if (argc < 2)
{
cerr << "Need files to encode, pass them as arguments";
}
for (int i = 1; i < argc; i++)
{
encode(argv[i]); // encode each given file in turn
}
return 0;
}