-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cpp
126 lines (104 loc) · 4.37 KB
/
functions.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
#include "functions.h"
#include "lz77.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
bool validateParams(const CompressionParams ¶ms) {
return !(params.inputFileName.empty() || params.outputFileName.empty() || params.mode.empty() || params.inputBufferSize <= 0 || params.historyBufferSize <= 0);
}
std::vector<char> readFileContent(const std::string &fileName) {
std::ifstream file(fileName, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Error opening file: " << fileName << std::endl;
return {};
}
file.seekg(0, std::ios::end);
std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> content(fileSize);
file.read(content.data(), fileSize);
file.close();
return content;
}
void writeCompressedData(const std::vector<Token> &compressedData, const std::string &outputFileName) {
std::ofstream outputFile(outputFileName);
if (!outputFile.is_open()) {
std::cerr << "Error writing file: " << outputFileName << std::endl;
return;
}
for (const auto &token : compressedData) {
if(static_cast<int>(token.code_word) != 0)
{
outputFile << "<" << token.offset << "," << token.length_of_match << "," << static_cast<int>(token.code_word) << ">";
}
}
outputFile.close();
}
void performCompression(const std::vector<char> &fileContent, const CompressionParams ¶ms) {
std::string data(fileContent.begin(), fileContent.end());
std::vector<Token> compressedData = compression_lz77(data, params.inputBufferSize, params.historyBufferSize);
writeCompressedData(compressedData, params.outputFileName);
auto startingSize = static_cast<double>(fileContent.size());
double compressedSize = static_cast<double>(compressedData.size()) * (sizeof(Token) / sizeof(char));
double compressionRatio = (startingSize / compressedSize) * 100.0;
std::cout << "Theoretical compression ratio: " << compressionRatio << "%" << std::endl;
std::cout << "Theoretical compression degree: " << (compressedSize / startingSize) << std::endl;
}
void performDecompression(const std::vector<char> &fileContent, const CompressionParams ¶ms) {
std::ofstream outputFile(params.outputFileName, std::ios::binary);
if (!outputFile.is_open()) {
std::cerr << "Error writing input file: " << params.outputFileName << std::endl;
return;
}
// Convert the char vector to a string
std::string input(fileContent.begin(), fileContent.end());
// Vector to store compressed data
std::vector<Token> compressedData;
// Variable to store the current position in the string
size_t pos = 0;
// Loop parsing the string and creating tokens
while (pos < input.size()) {
// Find the occurrence of the '>' character in the string
size_t nextPos = input.find('>', pos);
// If not found, break the loop
if (nextPos == std::string::npos) {
break;
}
// Get the fragment containing the token
std::string tokenStr = input.substr(pos, nextPos - pos + 1);
// Create a new structure and initialize values
Token t{};
std::istringstream tokenStream(tokenStr);
char dummy;
int temp = 0;
tokenStream >> dummy >> t.offset >> dummy >> t.length_of_match >> dummy >> temp;
t.code_word = static_cast<char>(temp);
compressedData.push_back(t);
// Update the position
pos = nextPos + 1;
}
std::string decompressedOutput = decompression_lz77(compressedData);
outputFile.write(decompressedOutput.c_str(), decompressedOutput.size());
outputFile.close();
}
int solve(const CompressionParams ¶ms) {
if (!validateParams(params)) {
std::cout << "Invalid command line parameters. Please provide all required options." << std::endl;
printInstructions();
return 1;
}
std::vector<char> fileContent = readFileContent(params.inputFileName);
if (params.mode == "c") {
if (!fileContent.empty()) {
performCompression(fileContent, params);
}
} else if (params.mode == "d") {
performDecompression(fileContent, params);
} else {
std::cerr << "Invalid mode. Please use 'c' for compression or 'd' for decompression." << std::endl;
printInstructions();
return 1;
}
return 0;
}