X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=examples%2Fz.cpp;fp=examples%2Fz.cpp;h=54f0eb7d336249c010ccb5911103f8bb88b7ee4a;hp=0000000000000000000000000000000000000000;hb=8ed14b63ab6249e9fc2a3a691cf8ffbf49166deb;hpb=faeafc9d652ba6caa350ca95dff14408b036ccfb diff --git a/examples/z.cpp b/examples/z.cpp new file mode 100644 index 0000000..54f0eb7 --- /dev/null +++ b/examples/z.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class Z: public RegisteredApplication +{ +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 &args = getopt.get_args(); + if(!args.empty()) + input_file = new IO::File(args[0]); + + input = (input_file ? static_cast(input_file) : static_cast(&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; +}