summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/compression/compressed-format-target.cxx
blob: b4a8a859bb88d3b822e571d71d4887ce360844e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// file      : examples/cxx/tree/compression/compressed-format-target.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <ostream>
#include <cstring> // std::memcpy

#include "compressed-format-target.hxx"

using namespace std;

//
// compression_failure
//

const char* compression_failure::
what () const throw ()
{
  return "compression failure";
}

//
// compressed_format_target
//

compressed_format_target::
compressed_format_target (ostream& os, compression_type t)
    : os_ (os), closed_ (false), n_ (0)
  {
    zs_.zalloc = Z_NULL;
    zs_.zfree  = Z_NULL;
    zs_.opaque = Z_NULL;

    int window = 0;

    switch (t)
    {
    case raw:
      {
        window = -15;
        break;
      }
    case zlib:
      {
        window = 15;
        break;
      }
    case gzip:
      {
        window = 16 + 15;
        break;
      }
    }

    int r (deflateInit2 (&zs_,
                         Z_DEFAULT_COMPRESSION,
                         Z_DEFLATED,
                         window,
                         8,
                         Z_DEFAULT_STRATEGY));
    if (r != Z_OK)
      throw compression_failure (r);
  }

compressed_format_target::
~compressed_format_target ()
{
  try
  {
    // Close the free the compression stream.
    //
    if (!closed_)
      close ();
  }
  catch (...)
  {
  }

  deflateEnd (&zs_);
}

void compressed_format_target::
writeChars (const XMLByte* const buf,
#if _XERCES_VERSION >= 30000
            const XMLSize_t size,
#else
            const unsigned int size,
#endif
            xercesc::XMLFormatter* const)
{
  // Flush the buffer if the block is too large or if we don't have
  // any space left.
  //
  if ((size >= buf_size_ / 8 || n_ + size > buf_size_) && n_ != 0)
  {
    write (in_, n_);
    n_ = 0;
  }

  if (size < buf_size_ / 8)
  {
    memcpy (in_ + n_, reinterpret_cast<const char*> (buf), size);
    n_ += size;
  }
  else
    write (reinterpret_cast<const char*> (buf), size);
}


void compressed_format_target::
flush ()
{
  if (n_ != 0)
  {
    write (in_, n_);
    n_ = 0;
  }

  if (!os_.fail ())
    os_.flush ();
}

void compressed_format_target::
close ()
{
  write (in_, n_, true);
  n_ = 0;

  if (!os_.fail ())
    os_.flush ();

  closed_ = true;
}

void compressed_format_target::
write (const char* buf, size_t size, bool flush)
{
  zs_.next_in = reinterpret_cast<Bytef*> (const_cast<char*> (buf));
  zs_.avail_in = static_cast<uInt> (size);

  do
  {
    zs_.next_out = reinterpret_cast<Bytef*> (out_);
    zs_.avail_out = buf_size_;

    int r (deflate (&zs_, flush ? Z_FINISH : Z_NO_FLUSH));

    if (r != Z_OK && r != Z_BUF_ERROR && r != Z_STREAM_END)
      throw compression_failure (r);

    size_t n (buf_size_ - zs_.avail_out);

    if (!os_.fail () && n > 0)
      os_.write (out_, static_cast<streamsize> (n));

  } while (zs_.avail_out == 0);
}