From b0846152c522c859bee2fbce55721d7b79264848 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 27 Nov 2009 18:17:22 +0200 Subject: Add XML compression example --- .../tree/compression/compressed-format-target.hxx | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 examples/cxx/tree/compression/compressed-format-target.hxx (limited to 'examples/cxx/tree/compression/compressed-format-target.hxx') diff --git a/examples/cxx/tree/compression/compressed-format-target.hxx b/examples/cxx/tree/compression/compressed-format-target.hxx new file mode 100644 index 0000000..5d12e81 --- /dev/null +++ b/examples/cxx/tree/compression/compressed-format-target.hxx @@ -0,0 +1,96 @@ +// file : examples/cxx/tree/compression/compressed-format-target.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef COMPRESSED_FORMAT_TARGET_HXX +#define COMPRESSED_FORMAT_TARGET_HXX + +#include + +#include +#include // std::size_t +#include + +#include + +struct compression_failure: std::exception +{ + explicit + compression_failure (int code) + : code_ (code) + { + } + + int + code () const + { + return code_; + } + + const char* + message () const + { + return zError (code_); + } + + virtual const char* + what () const throw (); + +private: + int code_; +}; + +// Xerces-C++ XMLFormatTarget interface implementation with on-the-fly, +// zlib-based compression. +// +class compressed_format_target: public xercesc::XMLFormatTarget +{ +public: + enum compression_type + { + raw, + zlib, + gzip + }; + + compressed_format_target (std::ostream&, compression_type); + + virtual + ~compressed_format_target (); + + virtual void + writeChars (const XMLByte* const buf, +#if _XERCES_VERSION >= 30000 + const XMLSize_t size, +#else + const unsigned int size, +#endif + xercesc::XMLFormatter* const); + + virtual void + flush (); + + // Close the compressed stream by writing out the zlib or gzip trailer. + // This function is automatically called from the destructor but you + // may want to call it explicitly to be able to catch any exceptions + // that it might throw. + // + void + close (); + +private: + void + write (const char* buf, std::size_t size, bool flush = false); + +private: + std::ostream& os_; + z_stream zs_; + bool closed_; + + static const std::size_t buf_size_ = 65536; + char in_[buf_size_]; + char out_[buf_size_]; + size_t n_; +}; + +#endif // COMPRESSED_FORMAT_TARGET_HXX -- cgit v1.1