-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_file.cpp
More file actions
274 lines (238 loc) · 9.88 KB
/
Copy pathread_file.cpp
File metadata and controls
274 lines (238 loc) · 9.88 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include<bits/stdc++.h>
#include<fstream>// it provides tools for i/p and o/p operations
#include<atomic>
#include"csv_txt.h"
#include"pdf_txt.h"
#include"frequency_map.h"
#include"frequency_chunk_map.h"
#include"hauffmann_compression.h"
#include"splitting_file.h"
#include"merging_maps.h"
#include"element_creation.h"
#include"encryption.h"
#include"decryption.h"
#include<termios.h>
#include<unistd.h>
using namespace std;
int main(int argc,char* argv[]){
/*================================================================================================================
ofstream is for creating and writing in files
ifstream is for reading files
fstream is combination of ofstream and ifstream
*/
// ofstream new_file("filename.txt");//new_file is variable or object of the ofstream class of type ofstream
// new_file<<"I am going to create a txt file compressor using hauffmann algo";
// new_file.close();
//since the file is created its time for reading the file for this i will store it in string
// string text;
// ifstream file("filename.txt");
// while (getline(file,text))
// {
// cout<<text;
// }
/*NOTE:-getline is basically for reading the input if getline(cin,string)then it will read input
from user and put it onto a string and here it reads from the .txt file and put it onto string text*/
// file.close();
//================================================================================================================
//opening a csv file and reading it
string csv_filepath="/Users/vedantgoyal/Desktop/CODING/PYTHON/customers.csv";
ifstream csv(csv_filepath);
if (!csv.is_open())
{
cout<<"Unable to open the file"<<csv_filepath<<endl;
return 1;
}
char delim=',';
//conversion of .csv to .txt file:
ofstream converted_csv("csv_txt.txt");
string line;
while(getline(csv,line)){
vector<string> temp=split(line,delim);
string final=combine(temp);
converted_csv<<final<<endl;
}
csv.close();
converted_csv.close();
// now i will convert pdf to txt
ofstream converted_pdf("pdf_txt.txt");
string pdf_filepath="/Users/vedantgoyal/Documents/ECED.pdf";
conversion(pdf_filepath,converted_pdf);
converted_pdf.close();
//====================================================================================================================
// csv.clear() it clears the EOF flag
// csv.seekg(0,ios::beg) it again make the pointer to point at beginning
//====================================================================================================================
// now i will pass these .txt files to hauffmann and make them .huff
cout<<"Compression starts"<<endl;
auto start_single = chrono::high_resolution_clock::now();
ofstream compressed_csv("compressed_csv.huff", ios::binary);
string csv_txt_filepath="/Users/vedantgoyal/Desktop/CODING/FILE_COMP/csv_txt.txt";
ifstream csv_txt(csv_txt_filepath);
if (!csv_txt.is_open())
{
cout << "Unable to open the file" << csv_filepath << endl;
return 1;
}
string csv_content((istreambuf_iterator<char>(csv_txt)),istreambuf_iterator<char>());
vector<node*> ele=frequency(csv_content);
node* top=huffmann_tree(ele);
vector<pair<char,string> > ans;
encoded(ans,top);
map<char, string> huffman_map;
for (auto& p : ans) {
huffman_map[p.first] = p.second;
}
//Conversion of 0 and 1 in bits
unsigned char buffer = 0;
int bit_count = 0;
size_t total = csv_content.size();
size_t processed = 0;
const size_t report_interval = max(total / 100, (size_t)1);
for (char ch : csv_content) {
string code = huffman_map[ch];
processed++;
if (processed % report_interval == 0 || processed == total) {
auto now = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<chrono::seconds>(now - start_single).count();
float percent = (float)processed / total;
int eta = (int)((elapsed / percent) - elapsed);
cout << "\rCompressing: " << int(percent * 100) << "% | ETA: " << eta << "s" << flush;
}
for (char bit : code) {
buffer <<= 1;
if (bit == '1'){
buffer |= 1;
}
bit_count++;
if (bit_count == 8){
compressed_csv.put(buffer); // write full byte
buffer = 0;
bit_count = 0;
}
}
}
cout<<endl;
// Write remaining bits, if any, padding them with 0s
if (bit_count > 0) {
buffer <<= (8 - bit_count);
compressed_csv.put(buffer);
}
cout << "Compression done. Output written to compressed_csv.huff"<<endl;
float compression_ratio=(float)csv_txt.tellg()/compressed_csv.tellp();
cout<<"Compression ratio is :- "<<compression_ratio<<":1"<<endl;
compressed_csv.close();
huffman_map.clear();
auto end_single = chrono::high_resolution_clock::now();
auto duration_single = chrono::duration_cast<chrono::milliseconds>(end_single - start_single).count();
cout << "Single-threaded time: " << duration_single << " ms" << endl;
//====================================================================================================================
/* Now i will use multi threading in order to fasten up the compression and
not messing with the compression ratio either. First i will split the file
in chunks and will make frequency_map for each chunk then i will merge the maps
after that will build a single huffmann tree from it and encoding will be again done
parallely */
//====================================================================================================================
auto start_multi = chrono::high_resolution_clock::now();
vector<string> c;
ifstream csv_ttxt(csv_txt_filepath);
ofstream compressed_csv_threaded("compressed_csv_threaded.huff",ios::binary);
c=split(csv_ttxt);
vector<map<char,int>>mp=chunk_map(c);
map<char,int> final_map=merging_map(mp);
vector<node*> element=element_creation(final_map);
node* root=huffmann_tree(element);
map<char, string> huffman_maps;
vector<pair<char,string>> ans_threaded;
encoded(ans_threaded, root);
for (auto& p : ans_threaded){
huffman_maps[p.first] = p.second;
}
// Progress monitoring
atomic<int> completed_chunks(0);
bool stop_monitoring = false;
thread monitor_thread([&](){
auto start_time = chrono::high_resolution_clock::now();
while (!stop_monitoring){
int done = completed_chunks.load();
float percent = (float)done / c.size();
auto now = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<chrono::seconds>(now - start_time).count();
int eta = (percent > 0) ? int((elapsed / percent) - elapsed) : 0;
cout << "\rEncoding Chunks: " << int(percent * 100) << "% | ETA: " << eta << "s" << flush;
this_thread::sleep_for(chrono::milliseconds(500));
}
});
cout<<endl;
vector<string> encoded_chunks(c.size());
auto encode_chunk = [&](int index) {
for (char ch : c[index]) {
encoded_chunks[index] += huffman_maps[ch];
}
};
vector<thread> encode_threads;
for (int i = 0; i < c.size(); ++i) {
encode_threads.emplace_back(encode_chunk, i);
}
for (auto& t : encode_threads) {
t.join();
}
stop_monitoring = true;
monitor_thread.join();
//converting all the encoded codes form diff chunks to a single string
string final_encodeds;
for (const auto& encoded : encoded_chunks) {
final_encodeds += encoded;
}
//Conversion of 0 and 1 in bits
for (char bit : final_encodeds) {
buffer <<= 1;
if (bit == '1') buffer |= 1;
bit_count++;
if (bit_count == 8) {
compressed_csv_threaded.put(buffer);
buffer = 0;
bit_count = 0;
}
}
if (bit_count > 0) {
buffer <<= (8 - bit_count);
compressed_csv_threaded.put(buffer);
}
cout << "Compression done. Output written to compressed_csv_threaded.huff"<<endl;
compression_ratio=(float)csv_ttxt.tellg()/compressed_csv_threaded.tellp();
cout<<"Compression ratio is :- "<<compression_ratio<<":1"<<endl;
csv_ttxt.close();
auto end_multi = chrono::high_resolution_clock::now();
auto duration_multi = chrono::duration_cast<chrono::milliseconds>(end_multi - start_multi).count();
cout << "Multithreaded time: " << duration_multi << " ms" << endl;
char request;
cout<<"Do you want to encrypt the compressed file"<<endl;
cin>>request;
if(request=='y'||request=='Y'){
cout<<"Encrypting.."<<endl;
encryption("compressed_csv_threaded.huff");
}
string pwd;
cout<<"Enter Password for decryption"<<endl;
cin>>pwd;
if(pwd=="Abcd@1234"){
cout<<"Decrpting.."<<endl;
decryption("encrypted_file.enc");
}
else{
cout<<"Wrong Password";
}
compressed_csv_threaded.close();
}
//====================================================================================================================
/* *****NOTE***** */
//No of threads = no of cores if extra thread are created then they will be time sliced
//Since one thread can run in a core at A POINT OF TIME no two cores can run simultaneously on a same core
//====================================================================================================================
/*g++ -std=c++17 read_file.cpp -o read_fileension -I/opt/homebrew/include -L/opt/homebrew/lib -lpoppler-cpp
./read_fileension
# Compile
g++ -std=c++17 read_file.cpp -o read_fileension -I/opt/homebrew/include -L/opt/homebrew/lib -lpoppler-cpp -lcryptopp
# Run
./read_fileension
*/