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 275 276 277 278 279 280
|
#include <queue>
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include "bitChar.h"
const std::string magicNum="7771234777";
const int asciiSize = 256;
int lCount[asciiSize];
std::string str_code[asciiSize];
struct node{
char ch;
int count;
node* left;
node* right;
};
class cmp{
public:
bool operator()(const node* lhs, const node* rhs) const
{
return lhs->count > rhs->count;
}
};
node* makeNode(char ch, int count)
{
node* tmp = new node;
tmp->ch = ch;
tmp->count = count;
tmp->left = NULL;
tmp->right = NULL;
return tmp;
};
typedef std::priority_queue<node*, std::vector<node*>, cmp> mypq;
void trie(mypq& _X)
{
while(_X.size() > 1)
{
node* holder = new node;
holder->left = _X.top(); _X.pop();
holder->right = _X.top(); _X.pop();
holder->count = holder->left->count + holder->right->count;
holder->ch = -1;
_X.push(holder);
}
}
//Create bit codes by recursively traversing the trie, adding a 0 for left and 1 for right, the key is to remove the end char when the recursion breaks and you have to go up a level
void code(node* _X)
{
static std::string bits = "";
if (_X->right != NULL)
{
bits += "1";
code(_X->right);
bits = bits.substr(0, bits.size() - 1);
}
if (_X->left != NULL)
{
bits += "0";
code(_X->left);
bits = bits.substr(0, bits.size() - 1);
}
if(!_X->left && !_X->right)
{
str_code[_X->ch] = bits;
}
}
void count(std::string file, int& _X){
char letter;
std::ifstream inf(file.c_str());
inf >> std::noskipws;
//Clears array
for(int i = 0;i < asciiSize; ++i)
lCount[i] = 0;
//Goes through text and counts
while(inf >> letter){
if(letter >= 0 && letter < asciiSize)
{
++lCount[letter];
++_X;
}
}
inf.close();
}
//Generates a string of the bit codes in the order they appear in the file
//Used during encoding
std::string BITSstring(std::string inFile)
{
char input;
std::string BITS = "";
//Open input stream and create BITS string of entire file
std::ifstream inf(inFile.c_str());
inf >> std::noskipws;
while(inf >> input)
{
BITS += str_code[input];
}
inf.close();
//Append ascii 3 EOT character to signify end of text
BITS += str_code[3];
return BITS;
}
int main(int argc, char** argv)
{
int rc;
char choice;
unsigned char inChar;
std::string inFile = "", outFile = "", BITS = "", BITSsub = "", mn = "";
std::ofstream outf;
std::ifstream inf;
mypq pq;
bitChar bchar;
int origSize = 0;
std::cout << "Menu..." << std::endl << "e) Encode file" << std::endl << "d) Decode file" << std::endl;
std::cin >> choice;
switch(choice)
{
case 'e':
//Get input filename and set output filename
std::cout<<"Enter File Name to Encode: "<<std::endl;
std::cin>>inFile;
outFile = inFile + ".mpc";
std::cout << std::left << std::setw(17);
std::cout << "Input filename: " << inFile << std::endl;
std::cout << std::left << std::setw(17);
std::cout << "Output filename:" << outFile << std::endl;
std::cout << std::endl;
//Open output streams
outf.open(outFile.c_str());
//count and populate array of letter occurrences (lCount) and add one EOT char
count(inFile, origSize);
if(lCount[3] == 0)
lCount[3] = 1;
//Output compressed file header
outf<<magicNum<<std::endl;
outf<<inFile<<std::endl;
for(int i = 0; i < asciiSize; ++i)
{
outf << lCount[i] << " ";
}
outf << std::endl;
//Create nodes based on the available ascii characters and push them into the priority queue
for(int i = 0; i < asciiSize; ++i)
{
if(lCount[i] > 0)
{
node* tmp = makeNode(i, lCount[i]);
pq.push(tmp);
}
}
//Create trie and bit codes
trie(pq);
code(pq.top());
//Create string of bitcodes for actual huffman encoding and do it
BITS = BITSstring(inFile);
outf << '#';
bchar.setBITS(BITS);
outf << std::noskipws;
rc = bchar.insertBits(outf);
if(rc == BITS.length())
{
std::cout << "Encoding succsessful! :)" << std::endl;
std::cout << "The compression ration is: " << (float)rc / ((float)origSize * 8.0) * 100.0 << "%" << std::endl;
}
else
{
std::cout << "There was an error writing the bits! :(" << std::endl;
std::cout << "Expected: " << BITS.length() * 8 << " but got: " << rc << std::endl;
}
break;
case 'd':
//Get input filename and set output filename
std::cout<<"Enter File Name to Decode: "<<std::endl;
std::cin>>inFile;
inf.open(inFile.c_str());
inf >> mn;
if(mn != magicNum)
{
std::cout << "Magic number does not match, this is not a valid file..." << std::endl;
return 1;
}
inf >> outFile;
if(outFile != inFile.substr(0, inFile.length() - 4))
{
std::cout << outFile << " " << inFile.substr(0, inFile.length() - 4) << std::endl;
std::cout << "File names do not match but will attempt to decode anyway..." << std::endl;
}
outf.open(outFile.c_str());
//Read in the letter count and add valid one to the priority queue
for(int i = 0; i < asciiSize; ++i)
{
inf >> lCount[i];
if(lCount[i] > 0)
{
node* tmp = makeNode(i, lCount[i]);
pq.push(tmp);
}
}
//Create trie and bit codes
trie(pq);
code(pq.top());
while(inChar != '#')
{
inf >> inChar;
}
inf >> std::noskipws;
//Read in encoded chars and create BITS
while(inf >> inChar)
{
BITS += bchar.getBits(inChar);
}
inf.close();
for(int i = 0; i < BITS.length(); ++i)
{
BITSsub += BITS[i];
for(int j = 0; j < asciiSize; ++j)
{
if(BITSsub == str_code[j])
{
//End of text has been hit and file is over, write newline and exit
if(j == 3)
{
outf << "\n";
i = BITS.length();
break;
}
outf << (char)j;
BITSsub = "";
break;
}
}
}
break;
default:
std::cout << "Invalid choice...." << std::endl;
break;
}
outf.close();
return 0;
}
|