summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/tree/compression/compressed-format-target.hxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-12-18 18:48:46 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2021-02-25 13:45:48 +0300
commit5e527213a2430bb3018e5eebd909aef294edf9b5 (patch)
tree94de33c82080b53d9a9e300170f6d221d89078f4 /xsd-examples/cxx/tree/compression/compressed-format-target.hxx
parent7420f85ea19b0562ffdd8123442f32bc8bac1267 (diff)
Switch to build2
Diffstat (limited to 'xsd-examples/cxx/tree/compression/compressed-format-target.hxx')
-rw-r--r--xsd-examples/cxx/tree/compression/compressed-format-target.hxx91
1 files changed, 91 insertions, 0 deletions
diff --git a/xsd-examples/cxx/tree/compression/compressed-format-target.hxx b/xsd-examples/cxx/tree/compression/compressed-format-target.hxx
new file mode 100644
index 0000000..0061cfa
--- /dev/null
+++ b/xsd-examples/cxx/tree/compression/compressed-format-target.hxx
@@ -0,0 +1,91 @@
+// file : cxx/tree/compression/compressed-format-target.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef COMPRESSED_FORMAT_TARGET_HXX
+#define COMPRESSED_FORMAT_TARGET_HXX
+
+#include <zlib.h>
+
+#include <iosfwd>
+#include <cstddef> // std::size_t
+#include <exception>
+
+#include <xercesc/framework/XMLFormatter.hpp>
+
+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,
+ const XMLSize_t size,
+ 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