Ticket #5525: test.cpp

File test.cpp, 2.2 KB (added by jensen.bo@…, 11 years ago)

Simple example

Line 
1#include <boost/iostreams/device/file.hpp>
2#include <boost/iostreams/device/file_descriptor.hpp>
3#include <boost/iostreams/filtering_streambuf.hpp>
4#include <boost/iostreams/filtering_stream.hpp>
5#include <boost/iostreams/copy.hpp>
6#include <boost/iostreams/filter/gzip.hpp>
7#include <boost/iostreams/categories.hpp>
8#include <boost/iostreams/code_converter.hpp>
9#include <cstdio>
10#include <iostream>
11
12//#define WORKS
13#define DO_NOT WORK
14
15/* Helper class for writing gzipped files */
16class gzip_sink
17{
18 public:
19 typedef char char_type;
20 typedef boost::iostreams::sink_tag category;
21
22 boost::iostreams::filtering_ostream sink_; /* Sink used
23to push filters */
24 std::ofstream &outifstr_; /* Write
25stream to be pushed into sink */
26
27 /* Constructor */
28 gzip_sink(std::ofstream &wrt) :
29 outifstr_(wrt)
30 {
31 #ifdef DO_NOT_WORK
32 /* Push gzip compressor */
33 sink_.push(boost::iostreams::gzip_compressor());
34
35 /* Push file writer */
36 sink_.push(outifstr_);
37 #endif
38 }
39
40 gzip_sink(const gzip_sink &rhs) :
41 outifstr_(rhs.outifstr_)
42 {
43 #ifdef DO_NOT_WORK
44 /* Push gzip compressor */
45 sink_.push(boost::iostreams::gzip_compressor());
46
47 /* Push file writer */
48 sink_.push(outifstr_);
49 #endif
50 }
51
52 /* Write function */
53 std::streamsize write( const char * s, std::streamsize n )
54 {
55 #ifdef WORKS
56 /* Push gzip compressor */
57 sink_.push(boost::iostreams::gzip_compressor());
58
59 /* Push file writer */
60 sink_.push(outifstr_);
61 #endif
62
63 /* Push write stream */
64 sink_.write(s,n);
65
66 #ifdef WORKS
67 sink_.pop();
68 sink_.pop();
69 #endif
70
71 return n;
72 }
73};
74
75/* Typedef */
76typedef boost::iostreams::code_converter<gzip_sink> convert_to_narrow_sink;
77
78int main(int argc, char *argv[])
79{
80 std::wcout <<"sizeof : "<<sizeof(MyTest)<< std::endl;
81 std::wcout <<"sizeof : "<<sizeof(MyTest2)<< std::endl;
82
83 std::ofstream strm("test.gz",std::ios_base::binary);
84
85 gzip_sink gzip(strm);
86 convert_to_narrow_sink convert(gzip,1000);
87
88 boost::iostreams::filtering_wostreambuf wbuf(convert,100);
89
90 std::wostream wout(&wbuf);
91
92 wout <<"Hello world!"<< std::endl;
93}
94