Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "Decompress.H"
#include <fstream>
#include <iostream>
#include <string>
#include <math.h>
#include <string.h>
using namespace std;
void Decompress::parseText(){
ifstream inpFile(_fname);
if (!inpFile.is_open()){
throw("[Error] Could not open file " + _fname + " in parseText()\n");
}
_header = make_shared<Header>(Header());
char c;
string val;
//Retrieve header Size
while(inpFile.get(c) && c != '/'){
val += string(1,c);
}
_header->preorderSize = stoi(val);
//Retrieve Body Size
val = "";
while (inpFile.get(c) && c != '/'){
val += string(1,c);
}
_header->bodySize = stoi(val);
//Getting PreOrder String
val = "";
for (int i = 0; i < ceil((_header->preorderSize)/8.0);i++){
val += inpFile.get();
}
_header->preorderStr = convertToString(val);
_header->preorderStr = string(_header->preorderStr.begin(),_header->preorderStr.begin()+_header->preorderSize);
//Getting Letters for each leaf in Tree
int counter = 0;
for (char c : _header->preorderStr){
if(c == '1') counter++;
}
for (int i = 0; i < counter;i++){
_header->letters.push(inpFile.get());
}
//Getting the body of compressed file and then converting it into string form
val = "";
while (inpFile.get(c)){
val += string(1,c);
}
inpFile.close();
_header->body = convertToString(val);
_header->body = string(_header->body.begin(),_header->body.begin()+_header->bodySize);
}
void Decompress::generateTree(){
_tree = generateTreeHelper(_header->preorderStr);
}
shared_ptr<Node> Decompress::generateTreeHelper(string& bin){
if (bin.size() < 1){
return NULL;
}
string bit = string(bin.begin(),bin.begin()+1); // First element in string
bin = string(bin.begin()+1,bin.end()); //The rest of the string
shared_ptr<Node> curr;
if (!bit.compare("1")){
curr = make_shared<Node>(Node(0,_header->letters.front(),NULL,NULL));
_header->letters.pop();
}
else if (!bit.compare("0")){
curr = make_shared<Node>(Node(0,'\0',generateTreeHelper(bin),generateTreeHelper(bin)));
}
return curr;
}
void Decompress::decompressFile(string output){
parseText();
generateTree();
generateBitMap();
shared_ptr<Node> curr = _tree;
string uncompress = "";
for (char c : _header->body){
curr = (c == '1') ? curr->right : curr->left;
if (curr->isLeaf()){
uncompress += string(1,curr->letter);
curr = _tree;
}
}
ofstream outFile(output);
if (!outFile.is_open()) throw("[Error] Could not open filename " + output + "in decompressFile()\n");
outFile << uncompress;
outFile.close();
cout << "Decompressed " + _fname + " and saved to " + output << endl;
}
/*
int main(int argc, char *argv[]){
using namespace std;
try{
Decompress tst = Decompress(argv[1]);
tst.decompressFile();
tst.printPreorder();
}
catch(string &e){cout << e;}
return 0;
}
*/