| 23 | using namespace dlib; |
| 24 | |
| 25 | int main(int argc, char** argv) |
| 26 | { |
| 27 | if (argc != 2) |
| 28 | { |
| 29 | cout << "You must give a file name as the argument to this program.\n" << endl; |
| 30 | cout << "This program reads in a file from the disk and compresses\n" |
| 31 | << "it in an in memory buffer and then converts that buffer \n" |
| 32 | << "into base64 text. The final step is to output to the screen\n" |
| 33 | << "some C++ code that contains this base64 encoded text and can\n" |
| 34 | << "decompress it back into its original form.\n" << endl; |
| 35 | |
| 36 | return EXIT_FAILURE; |
| 37 | } |
| 38 | |
| 39 | // open the file the user specified on the command line |
| 40 | ifstream fin(argv[1], ios::binary); |
| 41 | if (!fin) { |
| 42 | cout << "can't open file " << argv[1] << endl; |
| 43 | return EXIT_FAILURE; |
| 44 | } |
| 45 | |
| 46 | ostringstream sout; |
| 47 | istringstream sin; |
| 48 | |
| 49 | // this is the object we will use to do the base64 encoding |
| 50 | base64 base64_coder; |
| 51 | // this is the object we will use to do the data compression |
| 52 | compress_stream::kernel_1ea compressor; |
| 53 | |
| 54 | // compress the contents of the file and store the results in the string stream sout |
| 55 | compressor.compress(fin,sout); |
| 56 | sin.str(sout.str()); |
| 57 | sout.clear(); |
| 58 | sout.str(""); |
| 59 | |
| 60 | // now base64 encode the compressed data |
| 61 | base64_coder.encode(sin,sout); |
| 62 | |
| 63 | sin.clear(); |
| 64 | sin.str(sout.str()); |
| 65 | sout.str(""); |
| 66 | |
| 67 | // the following is a little funny looking but all it does is output some C++ code |
| 68 | // that contains the compressed/base64 data and the C++ code that can decode it back |
| 69 | // into its original form. |
| 70 | sout << "#include <sstream>\n"; |
| 71 | sout << "#include <dlib/compress_stream.h>\n"; |
| 72 | sout << "#include <dlib/base64.h>\n"; |
| 73 | sout << "\n"; |
| 74 | sout << "// This function returns the contents of the file '" << argv[1] << "'\n"; |
| 75 | sout << "const std::string get_decoded_string()\n"; |
| 76 | sout << "{\n"; |
| 77 | sout << " dlib::base64 base64_coder;\n"; |
| 78 | sout << " dlib::compress_stream::kernel_1ea compressor;\n"; |
| 79 | sout << " std::ostringstream sout;\n"; |
| 80 | sout << " std::istringstream sin;\n\n"; |
| 81 | |
| 82 | |