-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelection.cpp
117 lines (91 loc) · 3.01 KB
/
Selection.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
#include <algorithm>
#include <memory>
#include <sstream>
#include "Selection.h"
using namespace std;
Selection::Selection(string _typeFile, vector<shared_ptr<Algorithm>> added_algos) : isActive(false), typeFile(_typeFile), compressionRatio(0), addedAlgos(std::move(added_algos)) {
fout.open("selection.log", std::ios::app);
}
void Selection::GetActive() {
Add(shared_ptr<Huffman>(new Huffman));
Add(shared_ptr<LZW>(new LZW));
for(auto &it : addedAlgos){
Add(it);
}
ChooseAlgorithm();
isActive = true;
}
double Selection::Compress(string input_filepath, string output_filepath){
if(!isActive){
GetActive();
}
Input input(input_filepath);
Output output(output_filepath);
string Recovery = "Сжимаю файл " + input_filepath + " ...\n" ;
fout << Recovery;
algo->Compress(input, output); //getAlgo
compressionRatio = algo->compressionRatio;
string Recovering_success = "Сжал исходный файл в: " + output_filepath + "\n";
fout << Recovering_success;
if(IsOrigLessCompr((double)output.GetFileSize(), (double)input.GetFileSize())){
fout << "Сжатый файл оказался больше исходного.\n";
//fout << "Удаляю сжатый файл.\n";
// output.RemoveFile();
compressionRatio = 1;
}
std::ostringstream oss;
oss << compressionRatio;
string comprRat = "Коэффициент сжатия " + oss.str();
fout << comprRat;
return compressionRatio;
}
void Selection::Decompress(string input_filepath, string output_filepath){
if(!isActive){
GetActive();
}
string Recovery = "Восстанавливаю сжатый файл " + input_filepath + " ...\n";
fout << Recovery;
Input input(input_filepath);
Output output(output_filepath);
algo->Decompress(input, output);
string Recovering_success = "Восстанавил сжатый файл: " + output_filepath + "\n";
fout << Recovering_success;
}
void Selection::Add(shared_ptr<Algorithm> chosen_alg) {
arrayAlgos.push_back(chosen_alg);
}
void Selection::SetDefaultAlg() {
static shared_ptr<Huffman> huffman_algo(new Huffman);
algo = huffman_algo;
fout << "Выбрал алгоритм: ";
fout << algo->GetName();
fout << "\n";
}
void Selection::ChooseAlgorithm() {
fout << "Выбираю алгоритм...\n";
for (auto &it : arrayAlgos){
if(it->ShouldChoose(typeFile)){
algo = it;
fout << "Выбрал алгоритм: ";
fout << algo->GetName();
fout << "\n";
return;
}
}
SetDefaultAlg();
}
string Selection::GetNameAlgorithm(){
if(!isActive){
GetActive();
}
return algo->GetName();
}
string Selection::GetTypeFile() {
return typeFile;
}
bool Selection::IsOrigLessCompr(double size_compressed_f, double size_origin_f) {
return (size_origin_f < size_compressed_f);
}
Selection::~Selection() {
fout.close();
}