]> git.tdb.fi Git - libs/core.git/blobdiff - examples/z.cpp
Add support for de/compression with zlib
[libs/core.git] / examples / z.cpp
diff --git a/examples/z.cpp b/examples/z.cpp
new file mode 100644 (file)
index 0000000..54f0eb7
--- /dev/null
@@ -0,0 +1,68 @@
+#include <msp/core/application.h>
+#include <msp/core/getopt.h>
+#include <msp/io/console.h>
+#include <msp/io/file.h>
+#include <msp/io/zlibcompressed.h>
+
+using namespace std;
+using namespace Msp;
+
+class Z: public RegisteredApplication<Z>
+{
+private:
+       IO::ZlibCompressed *zlib;
+       IO::File *input_file;
+       IO::Base *input;
+       IO::Base *output;
+       bool decompress;
+
+public:
+       Z(int, char **);
+       ~Z();
+
+       virtual int main();
+};
+
+Z::Z(int argc, char **argv):
+       input_file(0)
+{
+       GetOpt getopt;
+       getopt.add_option('d', "decompress", decompress, GetOpt::NO_ARG);
+       getopt(argc, argv);
+
+       const vector<string> &args = getopt.get_args();
+       if(!args.empty())
+               input_file = new IO::File(args[0]);
+
+       input = (input_file ? static_cast<IO::Base *>(input_file) : static_cast<IO::Base *>(&IO::cin));
+       output = &IO::cout;
+       if(decompress)
+       {
+               zlib = new IO::ZlibCompressed(*input);
+               input = zlib;
+       }
+       else
+       {
+               zlib = new IO::ZlibCompressed(*output);
+               output = zlib;
+       }
+}
+
+Z::~Z()
+{
+       delete zlib;
+       delete input_file;
+}
+
+int Z::main()
+{
+       char buffer[1024];
+       while(1)
+       {
+               unsigned len = input->read(buffer, sizeof(buffer));
+               if(!len)
+                       break;
+               output->write(buffer, len);
+       }
+       return 0;
+}