-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchiver.cpp
More file actions
170 lines (124 loc) · 5.45 KB
/
archiver.cpp
File metadata and controls
170 lines (124 loc) · 5.45 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
#include "archiver.h"
const size_t Archiver::NUMBER_OF_BITS_IN_BYTE = 8;
const size_t Archiver::SYMBOLS_COUNT = (1 << 9);
const size_t Archiver::ALPHABET_SIZE = 9;
const size_t Archiver::FILENAME_END = 256;
const size_t Archiver::ONE_MORE_FILE = 257;
const size_t Archiver::ARCHIVE_END = 258;
void Archiver::Decompress(const char* file_name) const {
Huffman huffman(file_name);
}
void Archiver::Compress(int argc, char* argv[]) const {
if (argc <= 3) {
throw std::runtime_error("error - too few arguments");
}
Writer writer(argv[2]);
std::deque<bool>& bits_to_push_in_archive = writer.bits_to_push_;
for (size_t index = 3; index < static_cast<size_t>(argc); ++index) {
char* next_file_name = argv[index];
Reader reader_to_count_frequencies(next_file_name);
std::string next_file_name_str = static_cast<std::string>(next_file_name);
std::vector<int> file_name = TransformStringToNumbers(next_file_name_str);
CompressNextFile(next_file_name, writer, bits_to_push_in_archive,
reader_to_count_frequencies, next_file_name_str, file_name, index, argc);
}
writer.PushTillEnd();
}
void Archiver::CompressNextFile(char* next_file_name, Writer& writer,
std::deque<bool>& bits_to_push_in_archive,
Reader& reader_to_count_frequencies,
const std::string& next_file_name_str,
const std::vector<int>& file_name, size_t index, int argc) const {
std::vector<size_t> frequencies_of_symbols =
GetFrequenciesOfSymbols(reader_to_count_frequencies);
for (auto symbol : file_name) {
frequencies_of_symbols[symbol]++;
}
size_t number_of_symbols = GetNumberOfSymbols(frequencies_of_symbols);
for (size_t counter = 0; counter < ALPHABET_SIZE; ++counter) {
if ((number_of_symbols >> counter) & 1) {
bits_to_push_in_archive.push_back(true);
} else {
bits_to_push_in_archive.push_back(false);
}
writer.PushTillCan();
}
Huffman huffman(frequencies_of_symbols);
for (size_t next_index = 0; next_index < number_of_symbols; next_index++) {
PushNumberToDequeBool(writer, bits_to_push_in_archive,
huffman.order_of_symbols[next_index]);
}
for (size_t current_index = 1; current_index < huffman.number_of_codes_with_size.size();
current_index++) {
PushNumberToDequeBool(writer, bits_to_push_in_archive,
huffman.number_of_codes_with_size[current_index]);
}
for (auto value : file_name) {
AppearVectorBoolToDequeBool(writer, bits_to_push_in_archive, huffman.code_of_symbol[value]);
}
AppearVectorBoolToDequeBool(writer, bits_to_push_in_archive,
huffman.code_of_symbol[FILENAME_END]);
Reader reader(next_file_name);
while (!reader.bits_of_file_.empty()) {
reader.ReadNextBits();
int value = reader.GetValueOfNextBits(NUMBER_OF_BITS_IN_BYTE);
AppearVectorBoolToDequeBool(writer, bits_to_push_in_archive, huffman.code_of_symbol[value]);
reader.DeleteUselessBitsAtTheBeginning(NUMBER_OF_BITS_IN_BYTE);
}
AppearVectorBoolToDequeBool(
writer, bits_to_push_in_archive,
huffman.code_of_symbol[(index + 1 == static_cast<size_t>(argc) ? ARCHIVE_END
: ONE_MORE_FILE)]);
writer.PushTillCan();
}
std::vector<size_t> Archiver::GetFrequenciesOfSymbols(Reader& reader_to_count_frequencies) const {
std::vector<size_t> frequencies_of_symbols(SYMBOLS_COUNT);
frequencies_of_symbols[FILENAME_END] = 1;
frequencies_of_symbols[ONE_MORE_FILE] = 1;
frequencies_of_symbols[ARCHIVE_END] = 1;
while (!reader_to_count_frequencies.bits_of_file_.empty()) {
reader_to_count_frequencies.ReadNextBits();
int symbol = reader_to_count_frequencies.GetValueOfNextBits(NUMBER_OF_BITS_IN_BYTE);
frequencies_of_symbols[symbol]++;
reader_to_count_frequencies.DeleteUselessBitsAtTheBeginning(NUMBER_OF_BITS_IN_BYTE);
}
return frequencies_of_symbols;
}
size_t Archiver::GetNumberOfSymbols(const std::vector<size_t>& frequencies_of_symbols) const {
size_t number_of_symbols = 0;
for (auto frequency : frequencies_of_symbols) {
if (frequency) {
number_of_symbols++;
}
}
return number_of_symbols;
}
void Archiver::PushNumberToDequeBool(Writer& writer, std::deque<bool>& vec, int number) const {
for (size_t index = 0; index < ALPHABET_SIZE; ++index) {
vec.push_back((number >> index) & 1);
}
writer.PushTillCan();
}
int Archiver::GetIntFromChar(char character) const {
int value = 0;
for (size_t index = 0; index < NUMBER_OF_BITS_IN_BYTE; ++index) {
if ((character >> index) & 1) {
value |= (1 << index);
}
}
return value;
}
std::vector<int> Archiver::TransformStringToNumbers(const std::string& str) const {
std::vector<int> numbers;
for (auto character : str) {
numbers.push_back(static_cast<unsigned char>(character));
}
return numbers;
}
void Archiver::AppearVectorBoolToDequeBool(Writer& writer, std::deque<bool>& bits,
const std::vector<bool>& to_push) const {
for (auto bit : to_push) {
bits.push_back(bit);
}
writer.PushTillCan();
}