void Tree::rebuildOriginalFile(char *pathIn) { string info, aux_s,aux_s2; ifstream file; unsigned char* buffer; info.clear(); rebuildTree(tree_written); //printNo(root); equilibrar(tree_written); file.open(pathIn, ios::in | ios::binary | ios::ate); if(file.is_open()){ int size = file.tellg(); file.seekg(0, ios::beg); buffer = new unsigned char[size]; for(int i = 0; i < parada; i++){ file.get(); } for(int i = parada;i < size-1;i++){ unsigned char ch = file.get(); const unsigned char c = (const unsigned char)ch; info += charParaString(c); } aux_s = info; const char c = file.get(); aux_s2 = charParaString(c); removeTrash(aux_s2); aux_s += aux_s2; procurafinal(aux_s); } else { cout << "TRASH!" << endl; } file.close(); }
//Constrói o cabeçalho void Tree::buildHeader(std::string filename) { std::string aux_s; std::string aux_s2; bitArrays *bit = new bitArrays(); int aux_bit; int k; aux_s.clear(); writeTree(root, aux_s); aux_s.clear(); //cout << tree_written << endl; tree_written = tree_written.substr(1,tree_written.length()-2); sizeTree = tree_written.length(); equilibrar(tree_written); switch (sizeTrash) { case 1: aux_s = "001"; break; case 2: aux_s = "010"; break; case 3: aux_s = "011"; break; case 4: aux_s = "100"; break; case 5: aux_s = "101"; break; case 6: aux_s = "110"; break; case 7: aux_s = "111"; break; case 8: aux_s = "000"; default: break; } aux_bit = sizeTree/2; if(sizeTree%2 == 1) { aux_s2 = "1"; } else if(sizeTree%2 == 0) { aux_s2 = "0"; } /* for(int i=0; i < 13; i++) { if(aux_bit == 1) { aux_s2 += "1"; break; } if((aux_bit % 2) == 1) { aux_s2 += "1"; } else if((aux_bit % 2) == 0) { aux_s2 += "0"; } aux_bit = aux_bit / 2; } */ aux_bit = sizeTree; while(aux_bit > 1) { aux_bit = aux_bit/2; if(aux_bit % 2 == 1) { aux_s2 += "1"; } else if(aux_bit % 2 == 0) { aux_s2 += "0"; } } //cout << "string: " << aux_s2 << endl; aux_bit = 13 - aux_s2.length(); reverse(aux_s2.begin(), aux_s2.end()); //cout << "string2: " << aux_s2 << endl; for(int i=0; i < aux_bit; i++) { aux_s += "0"; } aux_s += aux_s2; //cout << "aux_s: " <<aux_s << endl; //std::string teste = aux_s.substr(0,8); //cout << teste << endl; codenode_to_bitarray(aux_s.substr(0,8), bit); //cout << "1: " << bit->getArray() << endl; codenode_to_bitarray(aux_s.substr(8,8), bit); aux_s.clear(); k = 3 + filename.length() + sizeTree; header = new unsigned char[k]; header = bit->getArray(); //cout << "header: " << header << endl; unsigned char c = filename.length(); header[2] = c; int w =0; for(int i =0; i<filename.length();i++) { w = i +3; header[w] = filename[i]; // cout << header[w] << " "; } //cout << endl; w = 0; int j = 3 + filename.length(); for(int i = 0;i< sizeTree;i++) { w = i+j; header[w] = tree_written[i]; } sizeHeader = k; sizeFileName = filename.length(); for(int i=0;i<k;i++) { //cout << "header: " << i << ":" << header[i] << endl; } }
void Tree::buildHeader(string filename){ string aux_s; string aux_s2; BitArray *bit = new BitArray(); int aux_bit; int k; aux_s.clear(); writeTree(root, aux_s); aux_s.clear(); tree_written = tree_written.substr(1,tree_written.length()-2); sizeTree = tree_written.length(); equilibrar(tree_written); switch (sizeTrash) { case 1: aux_s = "001"; break; case 2: aux_s = "010"; break; case 3: aux_s = "011"; break; case 4: aux_s = "100"; break; case 5: aux_s = "101"; break; case 6: aux_s = "110"; break; case 7: aux_s = "111"; break; case 8: aux_s = "000"; default: break; } aux_bit = sizeTree/2; if(sizeTree%2 == 1){ aux_s2 = "1"; } else if(sizeTree%2 == 0){ aux_s2 = "0"; } aux_bit = sizeTree; while(aux_bit > 1){ aux_bit = aux_bit/2; if(aux_bit % 2 == 1){ aux_s2 += "1"; } else if(aux_bit % 2 == 0){ aux_s2 += "0"; } } aux_bit = 13 - aux_s2.length(); reverse(aux_s2.begin(), aux_s2.end()); for(int i=0; i < aux_bit; i++){ aux_s += "0"; } aux_s += aux_s2; codenode_to_bitarray(aux_s.substr(0,8), bit); codenode_to_bitarray(aux_s.substr(8,8), bit); aux_s.clear(); k = 3 + filename.length() + sizeTree; header = new unsigned char[k]; header = bit->getArray(); unsigned char c = filename.length(); header[2] = c; int w =0; for(int i =0; i<filename.length();i++){ w = i +3; header[w] = filename[i]; } w = 0; int j = 3 + filename.length(); for(int i = 0;i< sizeTree;i++){ w = i+j; header[w] = tree_written[i]; } sizeHeader = k; sizeFileName = filename.length(); for(int i=0;i<k;i++); }
//Reconstrói o arquivo original void Tree::rebuildOriginalFile(char *pathIn) { std::string info, aux_s,aux_s2; std::ifstream file; std::string oux, oux2; int sobra2 = 0; unsigned char* buffer; int x = 0; info.clear(); rebuildTree(tree_written); //printNo(root); equilibrar(tree_written); file.open(pathIn, std::ios::in | std::ios::binary | std::ios::ate); if(file.is_open()) { int size = file.tellg(); file.seekg(0, std::ios::beg); buffer = new unsigned char[size]; for(int i = 0; i < parada; i++) { file.get(); } for(int i = parada;i < size-1;i++) { unsigned char ch = file.get(); const unsigned char c = (const unsigned char)ch; info += charParaString(c); //ajeitar /* if(info.length() == 1024) { //sobra2 = info.length() - 1024; //oux = info.substr(1024, sobra2); //oux2 = info.substr(0,1024); aux_s = procura(info,&x); cout << "aux_s size: " << aux_s.length() << endl; info.clear(); info += aux_s; //info += oux; aux_s.clear(); //oux.clear(); //oux2.clear(); } */ } //x = info.length(); int sobra = 1024 - info.length(); //aux_s = info.substr(x,sobra); aux_s = info; const char c = file.get(); aux_s2 = charParaString(c); removeTrash(aux_s2); aux_s += aux_s2; // cout << "aux_s: "; // for(int i = 0; i < aux_s.length();i++) // { // cout << aux_s[i] << " "; // if((i-1)%8 == 0 && i != 0) // { // cout << endl; // cout << "aux_s: "; // } // } procurafinal(aux_s); } else { cout << "eRRO!" << endl; } file.close(); }