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
|
void encode(string& intitle,string& outtitle, map<char, string>& code, size_t bufsize){
ifstream infile;
ofstream outfile;
infile.open(intitle.c_str(), ios::in);
outfile.open(outtitle.c_str(),ios::out);
char buffer[bufsize];
unsigned long i,size, bit_len = 0L, b_pack = 0L;
map<char, string>::iterator iter;
string bit_temp = "", total = "";
infile.rdbuf()->pubsetbuf(buffer, bufsize);
vector<string> bit_str;
size_t bit_size = 0;
while(true){
infile.read(buffer, bufsize);
size = infile.gcount();
if(!infile.eof()){
total = "";
for (i = 0; i < size; i++) {
total += code[buffer[i]];
}
cout<<total<<endl;
bit_size += total.size();
bit_str.push_back(total);
}
else{
total = "";
for (i = 0; i < size; i++) {
total += code[buffer[i]];
}
cout<<total<<endl;
bit_size += total.size();
bit_str.push_back(total);
break;
}
}
int long_bit_len = sizeof(unsigned long) * 8;
int long_count = bit_size / long_bit_len + (bit_size % long_bit_len ? 1 : 0);
unsigned long* bits = new unsigned long[long_count];
unsigned long temp_buffer = 0L;
if (!bits) { cerr << "Error - allocation too big..." << endl; abort(); }
cout<<"variables!!!!!!!"<<endl;
cout<<long_bit_len << " " << long_count <<" "<<bit_size<< endl;
vector<string>::iterator it;
int bit_index = 0, long_index = 0;
for(it = bit_str.begin(); it != bit_str.end(); it++){
bit_temp = *it;
cout<<"bit_temp "<<bit_temp<<endl;
for(int i = 0; i < bit_temp.size(); i++){
temp_buffer <<= 1;
temp_buffer += ((bit_temp[i] == '1') ? 1 : 0);
bit_index++;
if(bit_index >= long_bit_len){
bits[long_index++] = temp_buffer;
bit_index = 0;
cout<<"temp buffer -- " << temp_buffer<<endl;
}
}
}
if(bit_size % long_bit_len) {
temp_buffer <<= long_bit_len - (bit_size % long_bit_len);
bits[long_index] = temp_buffer;
}
// make sure the bit cotents
for (int i = 0; i < long_count; i++) {
bitset<64> data(bits[i]);
cout << "bit[" << setw(3) << i << "] -- " << data << endl;
}
outfile.write((char*)bits, long_count * long_bit_len);
delete bits;
infile.close();
outfile.close();
}
|