]> git.tdb.fi Git - libs/core.git/blob - examples/z.cpp
Add a class for monitoring changes in files
[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         string input_fn;
30         GetOpt getopt;
31         getopt.add_option('d', "decompress", decompress, GetOpt::NO_ARG);
32         getopt.add_argument("filename", input_fn, GetOpt::OPTIONAL_ARG);
33         getopt(argc, argv);
34
35         if(!input_fn.empty())
36                 input_file = new IO::File(input_fn);
37
38         input = (input_file ? static_cast<IO::Base *>(input_file) : static_cast<IO::Base *>(&IO::cin));
39         output = &IO::cout;
40         if(decompress)
41         {
42                 zlib = new IO::ZlibCompressed(*input);
43                 input = zlib;
44         }
45         else
46         {
47                 zlib = new IO::ZlibCompressed(*output);
48                 output = zlib;
49         }
50 }
51
52 Z::~Z()
53 {
54         delete zlib;
55         delete input_file;
56 }
57
58 int Z::main()
59 {
60         char buffer[1024];
61         while(1)
62         {
63                 unsigned len = input->read(buffer, sizeof(buffer));
64                 if(!len)
65                         break;
66                 output->write(buffer, len);
67         }
68         return 0;
69 }