decode.cpp (2141B)
1 #include "../include/chunk.h" 2 #include "iostream" 3 #include "../include/debug.h" 4 #include "filesystem" 5 #include <algorithm> 6 7 8 // this does not respect chunk numbers 9 10 void writeSingleFile(std::string filename){ 11 Chunk chunk = Chunk(filename); 12 chunk.writeChunk(); 13 14 } 15 16 // This loads each file from the directory into memory. 17 // it then orders them by file name then chunk number and writes 18 // them back to the original file. 19 20 void writeDirectory(std::string dirName){ 21 22 LOG("WRITING DIRECTORY") 23 24 std::vector<std::string> filenames; 25 26 for (const auto & entry : std::filesystem::directory_iterator(dirName)){ 27 28 std::string current = entry.path(); 29 30 31 if(current.find(".pbm") != std::string::npos){ 32 filenames.push_back(current); 33 } 34 } 35 36 std::vector<Chunk> chunks; 37 38 39 for (std::string filename : filenames){ 40 Chunk chunk (filename); 41 42 LOG("UNSORTED") 43 LOG(std::to_string(chunk.getChunkNumber()) + " - " + chunk.getFilename()) 44 45 chunks.push_back(chunk); 46 } 47 48 49 // sort chunks first by filename then by chunk # 50 51 std::sort(chunks.begin(), chunks.end()); 52 53 LOG("SORTED") 54 for (Chunk chunk : chunks){ 55 LOG(std::to_string(chunk.getChunkNumber()) + " - " + chunk.getFilename()); 56 } 57 58 // write in order 59 60 for (Chunk chunk : chunks){ 61 LOG("WRITING CHUNK ") 62 LOG(std::to_string(chunk.getChunkNumber()) + " - " + chunk.getFilename()); 63 chunk.writeChunk(); 64 } 65 66 } 67 68 int main(int argc, char* argv[]) { 69 70 if (argc < 2) { 71 std::cout << "usage: [filename] OR -d [directory]" << std::endl; 72 return 10; 73 } 74 75 std::string filename; 76 std::string directory; 77 bool useDirectory = false; 78 79 if (std::string(argv[1]) == "-d") { 80 if (argc < 3) { 81 std::cout << "usage: -d [directory]" << std::endl; 82 return 11; 83 } 84 directory = argv[2]; 85 useDirectory = true; 86 } 87 else{ 88 filename = argv[1]; 89 } 90 91 if (useDirectory){ 92 writeDirectory(directory); 93 } 94 else{ 95 writeSingleFile(filename); 96 } 97 98 99 return 0; 100 } 101