1 | #define BOOST_ALL_DYN_LINK
|
---|
2 |
|
---|
3 | #include <tchar.h>
|
---|
4 | #include <windows.h>
|
---|
5 | #include <iostream>
|
---|
6 |
|
---|
7 | #include <boost/iostreams/stream.hpp>
|
---|
8 | #include <boost/iostreams/code_converter.hpp>
|
---|
9 | #include <boost/locale/generator.hpp>
|
---|
10 | #include <boost/program_options/detail/utf8_codecvt_facet.hpp>
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 | using namespace boost::iostreams;
|
---|
14 |
|
---|
15 | class ActualDevice
|
---|
16 | {
|
---|
17 | private:
|
---|
18 |
|
---|
19 | char* in;
|
---|
20 | char* pIn;
|
---|
21 | char* epIn;
|
---|
22 |
|
---|
23 | char out[ 1024 ];
|
---|
24 | char* pOut;
|
---|
25 | char* epOut;
|
---|
26 |
|
---|
27 | public:
|
---|
28 |
|
---|
29 | ActualDevice()
|
---|
30 | {
|
---|
31 | in = "123 abc";
|
---|
32 |
|
---|
33 | pIn = in;
|
---|
34 | epIn = pIn + strlen( in );
|
---|
35 |
|
---|
36 | pOut = out;
|
---|
37 | epOut = pOut + 1024;
|
---|
38 | }
|
---|
39 |
|
---|
40 | std::streamsize read( char* s, std::streamsize n )
|
---|
41 | {
|
---|
42 | if( pIn >= epIn )
|
---|
43 | return -1;
|
---|
44 |
|
---|
45 | char* ps = s;
|
---|
46 | char* eps = ps + n;
|
---|
47 | while( ps < eps && pIn < epIn )
|
---|
48 | *ps++ = *pIn++;
|
---|
49 |
|
---|
50 | return ps - s;
|
---|
51 | }
|
---|
52 |
|
---|
53 | std::streamsize write( const char* s, std::streamsize n )
|
---|
54 | {
|
---|
55 | if( pOut >= epOut )
|
---|
56 | throw std::ios_base::failure( "can't write past end" );
|
---|
57 |
|
---|
58 | const char* ps = s;
|
---|
59 | const char* eps = ps + n;
|
---|
60 | while( ps < eps && pOut < epOut )
|
---|
61 | *pOut++ = *ps++;
|
---|
62 |
|
---|
63 | return ps - s;
|
---|
64 | }
|
---|
65 |
|
---|
66 | };
|
---|
67 |
|
---|
68 | class SomeDevice :
|
---|
69 | public device<bidirectional>
|
---|
70 | {
|
---|
71 | private:
|
---|
72 | ActualDevice& d;
|
---|
73 |
|
---|
74 | public:
|
---|
75 |
|
---|
76 | SomeDevice( ActualDevice& d ) : d( d )
|
---|
77 | {}
|
---|
78 |
|
---|
79 | ActualDevice& getActualDevice()
|
---|
80 | {
|
---|
81 | return d;
|
---|
82 | }
|
---|
83 |
|
---|
84 | std::streamsize read( char* s, std::streamsize n )
|
---|
85 | {
|
---|
86 | return d.read( s, n );
|
---|
87 | }
|
---|
88 |
|
---|
89 | std::streamsize write( const char* s, std::streamsize n )
|
---|
90 | {
|
---|
91 | return d.write( s, n );
|
---|
92 | }
|
---|
93 |
|
---|
94 | };
|
---|
95 |
|
---|
96 | int _tmain(int argc, _TCHAR* argv[])
|
---|
97 | {
|
---|
98 |
|
---|
99 | std::locale utf8Locale( std::locale::classic(), new boost::program_options::detail::utf8_codecvt_facet );
|
---|
100 |
|
---|
101 | ActualDevice dev;
|
---|
102 | SomeDevice sd( dev );
|
---|
103 |
|
---|
104 | typedef code_converter<SomeDevice> cvt;
|
---|
105 |
|
---|
106 | cvt cd( sd );
|
---|
107 | cd.imbue( utf8Locale );
|
---|
108 |
|
---|
109 | boost::iostreams::stream<cvt> io( cd );
|
---|
110 | io << L"output";
|
---|
111 | io.flush();
|
---|
112 |
|
---|
113 | int i;
|
---|
114 | io >> i;
|
---|
115 | wcout << L"i = " << i << endl;
|
---|
116 |
|
---|
117 | wstring s;
|
---|
118 | io >> s;
|
---|
119 | wcout << L"s = " << s << endl;
|
---|
120 |
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|