-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
126 lines (107 loc) · 4.16 KB
/
main.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
123
124
125
126
//////////////////////////////////////////////////////////////////////////////
// Main file
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "include/ByteStream.h"
#include "include/ByteStreamEncoder.h"
#include "include/SimpleCompression.h"
int main()
{
bool generate_key = true;
// Filenames used for testing
const std::string in_testfile = "../assets/molspin_source.txt";
const std::string out_encoded_testfile = "../assets/molspin_source.encoded";
const std::string out_decoded_testfile = "../assets/molspin_source.decoded";
const std::string keyfile = "../assets/encoding_map.key";
// Setup byte stream for a file
ByteStream inputStream;
if (inputStream.load(in_testfile))
std::cout << "Loaded file \"" << in_testfile << "\"." << std::endl;
else
std::cout << "Failed to load file \"" << in_testfile << "\"!" << std::endl;
// Update statistics
inputStream.bytes_changed();
// We create a separate stream for the output file
ByteStream outputStream;
// And a key stream
ByteStream keyStream;
// Setup compression algorithm
std::unique_ptr<ByteStreamEncoder> encoder = std::make_unique<SimpleCompression>(inputStream, outputStream, keyStream);
// Generate a key
if (generate_key)
{
if (inputStream.size() > 0 && encoder->GenerateKey())
{
if (keyStream.save(keyfile))
std::cout << "Generated encoding map and saved it to \"" << keyfile << "\"." << std::endl;
else
std::cout << "Failed to save generated encoding map to file \"" << keyfile << "\"!" << std::endl;
}
else
{
std::cout << "Failed to generate key!" << std::endl;
}
}
else
{
// Or load a key instead
if (keyStream.load(keyfile))
std::cout << "Loaded key \"" << keyfile << "\"." << std::endl;
else
std::cout << "Failed to load key \"" << keyfile << "\"!" << std::endl;
}
if (inputStream.size() > 0)
{
// Input file statistics
std::cout << "\n--- Input file statistics:\n";
std::cout << " - File size: " << inputStream.size() << " bytes\n";
std::cout << " - File entropy (bytes): " << inputStream.byte_entropy() << " bits\n";
std::cout << " - File entropy (bits): " << inputStream.bit_entropy() << " bits\n" << std::endl;
// Perform the byte stream manipulations
if (encoder->Encode())
{
std::cout << "Successfully encoded file!\n" << std::endl;
double compression_ratio = static_cast<double>(outputStream.size()) / static_cast<double>(inputStream.size());
// Output file statistics
std::cout << "--- Encoded output file statistics:\n";
std::cout << " - Algorithm: " << encoder->Name() << "\n";
std::cout << " - File size: " << outputStream.size() << " bytes\n";
std::cout << " - Compression ratio: " << compression_ratio << "\n";
std::cout << " - File size reduction: " << (100.0 - compression_ratio * 100.0) << "%\n";
std::cout << " - File entropy (bytes): " << outputStream.byte_entropy() << " bits\n";
std::cout << " - File entropy (bits): " << outputStream.bit_entropy() << " bits\n" << std::endl;
// Write the output stream to a file
outputStream.save(out_encoded_testfile);
}
else
{
std::cout << "Failed to encode file!" << std::endl;
}
// Also decode the file again
std::cout << " -------- Decoding file --------" << std::endl;
inputStream = outputStream;
if (encoder->Decode())
{
std::cout << "Successfully decoded file!\n" << std::endl;
// Output file statistics
std::cout << "--- Decoded output file statistics:\n";
std::cout << " - Algorithm: " << encoder->Name() << "\n";
std::cout << " - File size: " << outputStream.size() << " bytes\n";
std::cout << " - File entropy (bytes): " << outputStream.byte_entropy() << " bits\n";
std::cout << " - File entropy (bits): " << outputStream.bit_entropy() << " bits\n" << std::endl;
// Write the output stream to a file
outputStream.save(out_decoded_testfile);
}
else
{
std::cout << "Failed to decode file!" << std::endl;
}
}
else
{
std::cout << "No bytes read from file.";
}
// Keep console open to display results
std::cin.get();
return 0;
}