Simplify the SFINAE construct a bit
[libs/core.git] / examples / z.cpp
1 #include <msp/core/application.h>
2 #include <msp/core/getopt.h>
3 #include <msp/io/console.h>
4 #include <msp/io/file.h>
5 #include <msp/io/zlibcompressed.h>
6
7 using namespace std;
8 using namespace Msp;
9
10 class Z: public RegisteredApplication<Z>
11 {
12 private:
13         IO::ZlibCompressed *zlib;
14         IO::File *input_file;
15         IO::Base *input;
16         IO::Base *output;
17         bool decompress;
18
19 public:
20         Z(int, char **);
21         ~Z();
22
23         virtual int main();
24 };
25
26 Z::Z(int argc, char **argv):
27         input_file(0)
28 {
29         GetOpt getopt;
30         getopt.add_option('d', "decompress", decompress, GetOpt::NO_ARG);
31         getopt(argc, argv);
32
33         const vector<string> &args = getopt.get_args();
34         if(!args.empty())
35                 input_file = new IO::File(args[0]);
36
37         input = (input_file ? static_cast<IO::Base *>(input_file) : static_cast<IO::Base *>(&IO::cin));
38         output = &IO::cout;
39         if(decompress)
40         {
41                 zlib = new IO::ZlibCompressed(*input);
42                 input = zlib;
43         }
44         else
45         {
46                 zlib = new IO::ZlibCompressed(*output);
47                 output = zlib;
48         }
49 }
50
51 Z::~Z()
52 {
53         delete zlib;
54         delete input_file;
55 }
56
57 int Z::main()
58 {
59         char buffer[1024];
60         while(1)
61         {
62                 unsigned len = input->read(buffer, sizeof(buffer));
63                 if(!len)
64                         break;
65                 output->write(buffer, len);
66         }
67         return 0;
68 }