-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompressor.cpp
More file actions
55 lines (46 loc) · 1.5 KB
/
Copy pathcompressor.cpp
File metadata and controls
55 lines (46 loc) · 1.5 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
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include "lexer.cpp"
#include "dictionary.cpp"
#define COMPRESSOR
using namespace std;
class Compressor
{
public:
void compress_file(std::string source_path, std::string dest_path)
{
auto compression_dictionary = Dictionary::get_words('\n');
ifstream source_file(source_path);
ofstream compressed_file(dest_path);
string word;
vector<CompressionType> compression_types;
vector<char> punctuations_map;
while (source_file >> word)
{
tuple<string, CompressionType, char> normalized_word = Lexer::normalize_string(word);
// Create a compression map to ease decompression
CompressionType compression_type = get<1>(normalized_word);
compression_types.push_back(compression_type);
punctuations_map.push_back(get<2>(normalized_word));
Mapper mapper(compression_types, punctuations_map);
// Remove capitalization, and punctuation if present
word = get<0>(normalized_word);
// If the word is in the map, write the key to the file
if (compression_dictionary.find(word) != compression_dictionary.end())
{
compressed_file << compression_dictionary[word] << " ";
}
// Otherwise, write the word to the file
else
{
compressed_file << word << " ";
}
}
Mapper mapper(compression_types, punctuations_map);
compressed_file << '\n'
<< mapper.generate_map()
<< '\n';
}
};