summaryrefslogtreecommitdiffstats
path: root/ml/dlib/examples/file_to_code_ex.cpp
blob: ce49bde7a0afad49976c518e66846e9b9c5ad480 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
    This is an example illustrating the use of the compress_stream and
    base64 components from the dlib C++ Library.

    It reads in a file from the disk and compresses it in an in memory buffer and
    then converts that buffer into base64 text.  The final step is to output to
    the screen some C++ code that contains this base64 encoded text and can decompress
    it back into its original form.
*/


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <dlib/compress_stream.h>
#include <dlib/base64.h>


using namespace std;
using namespace dlib;

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        cout << "You must give a file name as the argument to this program.\n" << endl;
        cout << "This program reads in a file from the disk and compresses\n"
             << "it in an in memory buffer and then converts that buffer \n"
             << "into base64 text.  The final step is to output to the screen\n"
             << "some C++ code that contains this base64 encoded text and can\n"
             << "decompress it back into its original form.\n" << endl;

        return EXIT_FAILURE;
    }

    // open the file the user specified on the command line
    ifstream fin(argv[1], ios::binary);
    if (!fin) {
        cout << "can't open file " << argv[1] << endl;
        return EXIT_FAILURE;
    }

    ostringstream sout;
    istringstream sin;

    // this is the object we will use to do the base64 encoding
    base64 base64_coder;
    // this is the object we will use to do the data compression
    compress_stream::kernel_1ea compressor;

    // compress the contents of the file and store the results in the string stream sout
    compressor.compress(fin,sout);
    sin.str(sout.str());
    sout.clear();
    sout.str("");

    // now base64 encode the compressed data
    base64_coder.encode(sin,sout);

    sin.clear();
    sin.str(sout.str());
    sout.str("");

    // the following is a little funny looking but all it does is output some C++ code
    // that contains the compressed/base64 data and the C++ code that can decode it back
    // into its original form.
    sout << "#include <sstream>\n";
    sout << "#include <dlib/compress_stream.h>\n";
    sout << "#include <dlib/base64.h>\n";
    sout << "\n";
    sout << "// This function returns the contents of the file '" << argv[1] << "'\n";
    sout << "const std::string get_decoded_string()\n";
    sout << "{\n";
    sout << "    dlib::base64 base64_coder;\n";
    sout << "    dlib::compress_stream::kernel_1ea compressor;\n";
    sout << "    std::ostringstream sout;\n";
    sout << "    std::istringstream sin;\n\n";


    sout << "    // The base64 encoded data from the file '" << argv[1] << "' we want to decode and return.\n";
    string temp;
    getline(sin,temp);
    while (sin && temp.size() > 0)
    {
        sout << "    sout << \"" << temp << "\";\n";
        getline(sin,temp);
    }

    sout << "\n";
    sout << "    // Put the data into the istream sin\n";
    sout << "    sin.str(sout.str());\n";
    sout << "    sout.str(\"\");\n\n";
    sout << "    // Decode the base64 text into its compressed binary form\n";
    sout << "    base64_coder.decode(sin,sout);\n";
    sout << "    sin.clear();\n";
    sout << "    sin.str(sout.str());\n";
    sout << "    sout.str(\"\");\n\n";
    sout << "    // Decompress the data into its original form\n";
    sout << "    compressor.decompress(sin,sout);\n\n";
    sout << "    // Return the decoded and decompressed data\n";
    sout << "    return sout.str();\n";
    sout << "}\n";


    // finally output our encoded data and its C++ code to the screen
    cout << sout.str() << endl;
}