void Compressor::compress(string in, string out) { size = Utils::getFileSize(in); //cout << "size : " << size << endl; char c; string temp = ""; ifstream infile; infile.open(in, ios::binary | ios::in); while(!infile.eof()) { infile.read(&c, 1); charCpt++; float f = (float)charCpt / (float)size; if( f * 100 > nbBar) { nbBar++; Utils::drawProgressBar("compressing : ", nbBar); } if(index >= pow(2, ENCODING_LENGTH)) { m->clear(); initDico(); index = 256; } temp = pair(w, c); if(m->find(temp) != m->end()) { w = temp; } else { m->insert(std::pair<string, long>(temp, (long)index)); index++; long n = m->at(w); //cout << "c : " << n << endl; if(huffman) { chars->push_back(n); computeOufman(n); } else { result += std::bitset<ENCODING_LENGTH>(n).to_string(); } w = pair("", c); } } if(huffman) { computeOufman(m->at(w)); chars->push_back(m->at(w)); Huffman *h = new Huffman(oufman); result = h->convertToString(chars); } else { result += std::bitset<ENCODING_LENGTH>(m->at(w)).to_string(); string encodingLength = std::bitset<8>(ENCODING_LENGTH).to_string(); result = encodingLength + result; } writeResult(out); }
Compressor::Compressor() { m = new unordered_map<string, long>; oufman = new map<long, int>; chars = new vector<int>; initDico(); w = ""; result = ""; index = 256; charCpt = 0; nbBar = 0; }
Twig *initTwig(const char *cachePath, bool debug) { Template *env; env = (Template*)malloc(sizeof(*env)); if(env==NULL) printf("Error env malloc\n"); env->debug=debug; env->cache=alignhash_init(Twig_Hash); env->filter=initDico(TWIG_ANY_ENCODING); addFilter(env, "nl2br", &interpret_nl2br); return env; }