-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.cpp
96 lines (82 loc) · 2.03 KB
/
encode.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
#include <iostream>
#include <vector>
#include <fstream>
#include <bitset>
#include <string>
#include <map>
using namespace std;
int main(int argc, char* argv[]) {
int in_char;
vector<char> user_in;
map<int, string> the_code;
map<int,string>::iterator it = the_code.begin();
string word;
int ascii;
fstream in;
in.open(argv[1]);
//IF YOU INCORRECTLY ENTERED THE FILE NAME TO ARGV[] I WILL OPEN IT MANUALLY
if (!in.is_open()) {
in.open("codewords.txt");
}
//STORE HUFFMAN CODES IN MAP
string temp;
while(!in.eof()) {
in >> temp;
ascii = stoi(temp);
in >> word;
the_code.insert(pair<int,string>(ascii,word));
}
//GETLINE NOT WORKING!!!!
// string acum;
// for(string line; getline(cin, line) && line.compare("");) {
// line += "\n";
// for(int i = 0; i < line.length(); i++) {
// int index_char = line[i];
// it = the_code.find(index_char);
// string code = it->second;
//
// acum += code;
//
// }
// }
//CHAR AT A TIME
while ((in_char = getc(stdin)) != EOF) //EOF is a flag
user_in.push_back(static_cast<char>(in_char));
string acum;
for(int i = 0; i < user_in.size(); i++) {
int index_char = user_in[i];
it = the_code.find(index_char);
string code = it->second;
acum += code;
}
int char_blocks;
int padding = acum.length()%8;
if(padding == 0){
cout << "pad: ";
cout << padding <<endl;
char_blocks = acum.length()/8;
}
else {
padding = 8-acum.length()%8;
char_blocks = acum.length()/8 + 1;
cout << "pad: ";
cout << padding << endl;
for(int i =0; i < padding; i++)
acum += "0";
}
int i;
//vector<char> binary;
for(i = 0; i < char_blocks; i++) {
string temp = acum.substr(i*8,8);
// bitset<8> b(temp);
// char* c = (char*)&b;
// cout << *c;
//auto bitset = std::bitset<8>(temp);
bitset<8> bitset(temp);
char c= static_cast<char>(bitset.to_ulong());
cout << c;
//binary.push_back(c);
}
in.close();
return 0;
}