Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / example / core / zBlock / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <iostream>
10
11 #include <anna/core/core.hpp>
12
13 #include <anna/io/BinaryReader.hpp>
14
15 using namespace std;
16
17 int main (int argc, const char** argv)
18 {
19    CommandLine& commandLine (CommandLine::instantiate ());
20    io::BinaryReader reader (32 * 1024);
21    DataBlock contain (true);
22    ZBlock zcompress, zuncompress;
23    const DataBlock* block;
24    ZBlock::Mode::_v mode;
25
26    try {
27       commandLine.add ("f", CommandLine::Argument::Mandatory, "Filename to be compressed");
28       commandLine.add ("m", CommandLine::Argument::Mandatory, "Mode (d,s,c)");
29
30       commandLine.initialize (argv, argc);
31       commandLine.verify ();
32
33       // Lee el contenido del fichero indicado y lo guarda en el buffer 
34       reader.open (commandLine.getValue ("f"));
35       while ((block = reader.fetch ()) != NULL)
36          contain += *block;
37
38       cout << "Contain: \t" << functions::asString (contain, 24) << endl << endl;
39
40       std::string hexString;
41       cout << "Contain (asHexString): " << (hexString = functions::asHexString (contain)) << endl << endl;
42
43       DataBlock aux (true);
44       functions::fromHexString (hexString, aux);
45       cout << "Contain: (fromHexString): \t" <<  functions::asString (aux, 24) << endl << endl;
46
47       const char* smode = commandLine.getValue ("m");
48
49       switch (*smode) {
50          case 'd': mode = ZBlock::Mode::Default; break;
51          case 's': mode = ZBlock::Mode::BestSpeed; break;
52          case 'c': mode = ZBlock::Mode::BestCompression; break;
53       }
54
55       cout << "Original: " << contain.getSize () << " bytes";
56       cout << functions::asString (DataBlock (contain.getData (), min (128, contain.getSize ()), false), 24) << " (and more)" << endl << endl;
57
58       zcompress.compress (contain, mode);
59       cout << "Compress: " << zcompress.getSize () << " bytes";
60       cout << functions::asString (DataBlock (zcompress.getData (), min (128, zcompress.getSize ()), false), 24) << " (and more)" << endl << endl;
61
62       const DataBlock& uncompress = zuncompress.uncompress (zcompress);
63       cout << "UnCompress: " << uncompress.getSize () << " bytes";
64       cout << functions::asString (DataBlock (uncompress.getData (), min (128, uncompress.getSize ()), false), 24) << " (and more)" << endl << endl;
65    }
66    catch (Exception& ex) {
67       cout << ex.asString () << endl;
68    }
69    
70    return 0;
71 }
72