First commit
[anna.git] / example / core / zBlock / main.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <iostream>
38
39 #include <anna/core/core.hpp>
40
41 #include <anna/io/BinaryReader.hpp>
42
43 using namespace std;
44
45 int main (int argc, const char** argv)
46 {
47    CommandLine& commandLine (CommandLine::instantiate ());
48    io::BinaryReader reader (32 * 1024);
49    DataBlock contain (true);
50    ZBlock zcompress, zuncompress;
51    const DataBlock* block;
52    ZBlock::Mode::_v mode;
53
54    try {
55       commandLine.add ("f", CommandLine::Argument::Mandatory, "Filename to be compressed");
56       commandLine.add ("m", CommandLine::Argument::Mandatory, "Mode (d,s,c)");
57
58       commandLine.initialize (argv, argc);
59       commandLine.verify ();
60
61       // Lee el contenido del fichero indicado y lo guarda en el buffer 
62       reader.open (commandLine.getValue ("f"));
63       while ((block = reader.fetch ()) != NULL)
64          contain += *block;
65
66       cout << "Contain: \t" << functions::asString (contain, 24) << endl << endl;
67
68       std::string hexString;
69       cout << "Contain (asHexString): " << (hexString = functions::asHexString (contain)) << endl << endl;
70
71       DataBlock aux (true);
72       functions::fromHexString (hexString, aux);
73       cout << "Contain: (fromHexString): \t" <<  functions::asString (aux, 24) << endl << endl;
74
75       const char* smode = commandLine.getValue ("m");
76
77       switch (*smode) {
78          case 'd': mode = ZBlock::Mode::Default; break;
79          case 's': mode = ZBlock::Mode::BestSpeed; break;
80          case 'c': mode = ZBlock::Mode::BestCompression; break;
81       }
82
83       cout << "Original: " << contain.getSize () << " bytes";
84       cout << functions::asString (DataBlock (contain.getData (), min (128, contain.getSize ()), false), 24) << " (and more)" << endl << endl;
85
86       zcompress.compress (contain, mode);
87       cout << "Compress: " << zcompress.getSize () << " bytes";
88       cout << functions::asString (DataBlock (zcompress.getData (), min (128, zcompress.getSize ()), false), 24) << " (and more)" << endl << endl;
89
90       const DataBlock& uncompress = zuncompress.uncompress (zcompress);
91       cout << "UnCompress: " << uncompress.getSize () << " bytes";
92       cout << functions::asString (DataBlock (uncompress.getData (), min (128, uncompress.getSize ()), false), 24) << " (and more)" << endl << endl;
93    }
94    catch (Exception& ex) {
95       cout << ex.asString () << endl;
96    }
97    
98    return 0;
99 }
100