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
|
void file_info::scanner::set_file_copy(std::string& file_loc) {
_scanner_file.open(file_loc, std::ios::in);
if (_scanner_file.is_open()) {
_buffer << _scanner_file.rdbuf();
_scanner_file.close();
}
else std::cout << "Unable to open the file for copy operation." << std::endl;
}
std::string file_info::scanner::get_file_copy() const {
return _buffer.str();
}
void decoder::decompressor::read_key(std::string& decoding_key_loc) {
_decode_key.set_file_copy(decoding_key_loc);
buffer = _decode_key.get_file_copy();
build_decoding_tree(_root, 0);
print(_root, 0);
std::cout << std::endl;
std::cout << buffer << std::endl;
}
void decoder::decompressor::build_decoding_tree(Node *&root, int index) {
if (buffer[index] == '0') {
if (root->_left != NULL)
build_decoding_tree(root->_left, index);
else {
root->_left = new Node;
++index;
build_decoding_tree(root->_left, index);
}
}
else if (buffer[index] == '1') {
if (root->_right != NULL)
build_decoding_tree(root->_right, index);
else {
root->_right = new Node;
++index;
build_decoding_tree(root->_right, index);
}
}
else if (buffer[index] == 32) {
++index;
if ((buffer[index + 1] == '0') || (buffer[index + 1 ] == '1')) {
root->_character = buffer[index];
_root = _root_cpy;
++index;
build_decoding_tree(_root, index);
}
else
_file_type = buffer.substr(index);
}
}
void decoder::decompressor::test(std::string& encoded_file_loc) {
Node *p = _root;
char byte;
_encode_file.open(encoded_file_loc, std::ios::in | std::ios::binary);
byte = _encode_file.get();
for (int count = 0; count < 8; count++) {
bool b = byte & (1 << (7 - count));
if (b)
p = p->_right;
else
p = p->_left;
if (p->_character) { std::cout << p->_character; p = _root; }
}
}
void decoder::decompressor::print(Node* root, unsigned k) {
if (root != NULL) {
print(root->_left, k + 3);
for (unsigned i = 0; i < k; i++) {
std::cout << " ";
}
if (root->_character) { std::cout << "-" << "(" << root->_character << ")" << std::endl; }
else { std::cout << "-" << std::endl; }
print(root->_right, k + 3);
}
}
|