Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Compressor.cpp
- // TextCompression
- //
- // Created by Viet Nguyen on 12/4/16.
- // Copyright © 2016 Viet Nguyen. All rights reserved.
- //
- #include "Compressor.hpp"
- Compressor::Compressor(string filename, string tofile, vector<wchar_t> symbols, vector<string> codes)
- {
- fileSourse = filename;
- fileTo = tofile;
- for(int i = 0; i < symbols.size(); ++ i)
- map.insert(pair<wchar_t, string>(symbols[i], codes[i]));
- }
- vector<bool> Compressor::getBites(wchar_t c)
- {
- string code = map[c];
- vector<bool> result = *new vector<bool>;
- for(int i = 0 ; i < code.size(); ++i)
- result.push_back( code[i]=='1');
- return result;
- }
- void Compressor::CompressText()
- {
- //Open sourse text file to get text
- setlocale(LC_ALL, "en_US.UTF-8");
- wifstream input (fileSourse);
- #ifdef PREFER_BOOST
- boost::locale::generator gen;
- std::locale loc = gen("en_US.UTF-8");
- #else
- std::locale loc("en_US.UTF-8");
- #endif
- input.imbue(loc);
- wcout.imbue(loc);
- //Create new file
- ofstream comp;
- comp.open(fileTo + ".haff", ios::out | ios::binary);
- comp << map.size(); //Write number of row in table
- for(auto i = map.begin() ; i != map.end(); ++i)
- {
- comp << (*i).first << endl;
- for (int j = 0; j < (*i).second.size(); ++j)
- {
- bool t = (*i).second[j] == '0';
- comp << t;
- }
- comp<<endl;
- }
- int count = 0;
- char buffer = 0;
- while (!input.eof())
- {
- wchar_t ch = input.get();
- vector<bool> bites = getBites(ch);
- for(int i = 0; i < bites.size(); ++i)
- {
- buffer = buffer | (bites[i]? 1 : 0)<<(7 - count);
- count++;
- if (count == 8)
- {
- count = 0;
- comp << buffer;
- buffer = 0;
- }
- }
- }
- input.close();
- comp.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement