diff --git a/.gitignore b/.gitignore index 73fafe5..fed3380 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,15 @@ #Ignoring all .o file *.o + +#ignoring temp file +random + +#ignore swp files from vim +*.swp + +#ignore executables +compress +decompress + + diff --git a/Compress.H b/Compress.H index acd1764..edcb3dc 100644 --- a/Compress.H +++ b/Compress.H @@ -18,12 +18,7 @@ public: void generateTree(); void compressFile(string outFile); - void printPreorder(); }; - -template -string preOrder(Ptr curr); - #endif diff --git a/Compress.cpp b/Compress.cpp index 2900b0b..04a3a53 100644 --- a/Compress.cpp +++ b/Compress.cpp @@ -51,14 +51,6 @@ void Compress::generateTree(){ _tree = lst[0]; } -template -string preOrder(Ptr curr, string & letters){ - if (curr->isLeaf()){ - letters += string(1,curr->letter); - return "1"; - } - return preOrder(curr->left,letters) + "0" + preOrder(curr->right,letters); -} //NEED NUMBER OF BITS FOR BODY string Compress::generateBody(){ @@ -73,6 +65,7 @@ string Compress::generateBody(){ while(inpFile.get(c)){ uncompressed += _bitMap[c]; } + cout <<"UncomPressed binary: " << uncompressed << endl; inpFile.close(); _counter = uncompressed.size(); // cout << "Uncompressed string: " << uncompressed << endl; @@ -96,12 +89,6 @@ string Compress::generateHeader(){ return to_string(tmp.size()) + "/" + to_string(_counter) + "/" + comp + headerLetters; } -void Compress::printPreorder(){ - string letts = ""; - cout << preOrder(_tree,letts) << endl; -} - - int main(int argc, char *argv[]){ using namespace std; try{ diff --git a/Decompress.H b/Decompress.H index 6f8b44f..be65d84 100644 --- a/Decompress.H +++ b/Decompress.H @@ -4,19 +4,33 @@ #include "Huffman.H" #include +#include using namespace std; +struct FileParse; + class Decompress : public Huffman { - int bodySize; + shared_ptr _header; // Will hold parse Compressed file info + shared_ptr generateTreeHelper(string& bin); + void parseText(); + void generateTree(); + +public: + Decompress(string fname): Huffman(fname){} + void decompressFile(); + +}; + +struct FileParse{ + int bodySize; int headerSize; string preorderStr; queue letters; string body; -public: - Decompress(string fname): Huffman(fname){} - void parseText(); + FileParse(){} + ~FileParse(){} }; #endif diff --git a/Decompress.cpp b/Decompress.cpp index 742268f..dfaf200 100644 --- a/Decompress.cpp +++ b/Decompress.cpp @@ -15,6 +15,7 @@ void Decompress::parseText(){ throw("[Error] Could not open file " + _fname + " in parseText()\n"); } + _header = make_shared(FileParse()); char c; string val; @@ -22,39 +23,35 @@ void Decompress::parseText(){ while(inpFile.get(c) && c != '/'){ val += string(1,c); } - headerSize = stoi(val); + _header->headerSize = stoi(val); + + cout << "Passed Parsing\n" << endl; //Retrieve Body Size val = ""; while (inpFile.get(c) && c != '/'){ val += string(1,c); } - bodySize = stoi(val); + _header->bodySize = stoi(val); //Getting PreOrder String val = ""; - for (int i = 0; i < ceil(headerSize/8.0);i++){ + for (int i = 0; i < ceil((_header->headerSize)/8.0);i++){ val += inpFile.get(); } - preorderStr = convertToString(val); - cout << "Before Cutting: " << preorderStr << endl; - preorderStr = string(preorderStr.begin(),preorderStr.begin()+headerSize); + _header->preorderStr = convertToString(val); + _header->preorderStr = string(_header->preorderStr.begin(),_header->preorderStr.begin()+_header->headerSize); //Getting Letters for each leaf in Tree int counter = 0; - for (char c : preorderStr){ + for (char c : _header->preorderStr){ if(c == '1') counter++; } for (int i = 0; i < counter;i++){ - letters.push(inpFile.get()); + _header->letters.push(inpFile.get()); } - -/* while ( letters.size() > 0){ - cout << letters.front() << endl; - letters.pop(); - } //Print method to see letters in queue */ //Getting the body of compressed file and then converting it into string form val = ""; @@ -62,11 +59,47 @@ void Decompress::parseText(){ val += string(1,c); } inpFile.close(); - body = convertToString(val); - body = string(body.begin(),body.begin()+bodySize); - cout << headerSize << " - " << bodySize << " - " << preorderStr << " - " << letters.size() << " - "; - cout << "String size = " << val.length()<< " -- " << val << " : " << body << endl; - + _header->body = convertToString(val); + _header->body = string(_header->body.begin(),_header->body.begin()+_header->bodySize); +} + +void Decompress::generateTree(){ + _tree = generateTreeHelper(_header->preorderStr); +} + +shared_ptr 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 curr; + if (!bit.compare("1")){ + curr = make_shared(Node(0,_header->letters.front(),NULL,NULL)); + _header->letters.pop(); + } + else if (!bit.compare("0")){ + curr = make_shared(Node(0,'\0',generateTreeHelper(bin),generateTreeHelper(bin))); + } + return curr; +} + +void Decompress::decompressFile(){ + parseText(); + generateTree(); + generateBitMap(); + + shared_ptr 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; + } + } + cout << "uncompressed: " << uncompress << endl; } @@ -81,7 +114,8 @@ int main(int argc, char *argv[]){ cout << "String bit for A : " << convertToString("A") << endl; */ Decompress tst = Decompress(argv[1]); - tst.parseText(); + tst.decompressFile(); + tst.printPreorder(); } catch(string &e){cout << e;} return 0; diff --git a/Huffman.H b/Huffman.H index ea90ad5..ba0115e 100644 --- a/Huffman.H +++ b/Huffman.H @@ -25,6 +25,9 @@ public: virtual void generateTree(){};// Will be different for compress and decompress void generateBitMap(); + void printBitMap(); + + void printPreorder(); const map & getBitMap(){ return _bitMap;} string getFileName(){return _fname;} @@ -45,4 +48,7 @@ string convertToBits(string uncompressed);//Converts the string format bits into string convertToString(string compressed);//Converts the bit form into string form +template +string preOrder(Ptr curr,string &lett); + #endif diff --git a/Huffman.cpp b/Huffman.cpp index e303b96..75a470f 100644 --- a/Huffman.cpp +++ b/Huffman.cpp @@ -1,5 +1,7 @@ #include "Huffman.H" +#include + void Huffman::generateBitMapHelper(std::shared_ptr curr, std::string bin){ if(curr->isLeaf()){ _bitMap[curr->letter] = bin; @@ -12,6 +14,15 @@ void Huffman::generateBitMapHelper(std::shared_ptr curr, std::string bin){ void Huffman::generateBitMap(){ //Wrapper class to recursively call enumerateTreeHelper with DFS generateBitMapHelper(_tree,""); + printBitMap(); +} + +void Huffman::printBitMap(){ + cout << "MAP TABLE BEGIN\n" << endl; + for (auto it = _bitMap.begin();it != _bitMap.end();it++){ + cout << it->first << " : " << it->second << endl; + } + cout << "MAP TABLE END\n" << endl; } string convertToBits(string uncompressed){ //Converts the string format bits into char @@ -54,3 +65,17 @@ string convertToString(string compressed){//converts from binary form to string } return total; } + +template +string preOrder(Ptr curr, string & letters){ + if (curr->isLeaf()){ + letters += string(1,curr->letter); + return "1"; + } + return "0" + preOrder(curr->left,letters) + preOrder(curr->right,letters); +} + +void Huffman::printPreorder(){ + string letts = ""; + cout << preOrder(_tree,letts) << endl; +}