1 | // |
---|
2 | // StreamConverter.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/StreamConverter.cpp#5 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Text |
---|
8 | // Module: StreamConverter |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/StreamConverter.h" |
---|
38 | #include "Poco/TextEncoding.h" |
---|
39 | |
---|
40 | |
---|
41 | namespace Poco { |
---|
42 | |
---|
43 | |
---|
44 | StreamConverterBuf::StreamConverterBuf(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): |
---|
45 | _pIstr(&istr), |
---|
46 | _pOstr(0), |
---|
47 | _inEncoding(inEncoding), |
---|
48 | _outEncoding(outEncoding), |
---|
49 | _defaultChar(defaultChar), |
---|
50 | _sequenceLength(0), |
---|
51 | _pos(0), |
---|
52 | _errors(0) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | StreamConverterBuf::StreamConverterBuf(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): |
---|
58 | _pIstr(0), |
---|
59 | _pOstr(&ostr), |
---|
60 | _inEncoding(inEncoding), |
---|
61 | _outEncoding(outEncoding), |
---|
62 | _defaultChar(defaultChar), |
---|
63 | _sequenceLength(0), |
---|
64 | _pos(0), |
---|
65 | _errors(0) |
---|
66 | { |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | StreamConverterBuf::~StreamConverterBuf() |
---|
71 | { |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | int StreamConverterBuf::readFromDevice() |
---|
76 | { |
---|
77 | poco_assert_dbg (_pIstr); |
---|
78 | |
---|
79 | if (_pos < _sequenceLength) return _buffer[_pos++]; |
---|
80 | |
---|
81 | _pos = 0; |
---|
82 | _sequenceLength = 0; |
---|
83 | int c = _pIstr->get(); |
---|
84 | if (c == -1) return -1; |
---|
85 | |
---|
86 | poco_assert (c < 256); |
---|
87 | int uc; |
---|
88 | _buffer [0] = (unsigned char) c; |
---|
89 | int n = _inEncoding.queryConvert(_buffer, 1); |
---|
90 | int read = 1; |
---|
91 | |
---|
92 | while (-1 > n) |
---|
93 | { |
---|
94 | poco_assert_dbg(-n <= sizeof(_buffer)); |
---|
95 | _pIstr->read((char*) _buffer + read, -n - read); |
---|
96 | read = -n; |
---|
97 | n = _inEncoding.queryConvert(_buffer, -n); |
---|
98 | } |
---|
99 | |
---|
100 | if (-1 >= n) |
---|
101 | { |
---|
102 | uc = _defaultChar; |
---|
103 | ++_errors; |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | uc = n; |
---|
108 | } |
---|
109 | |
---|
110 | _sequenceLength = _outEncoding.convert(uc, _buffer, sizeof(_buffer)); |
---|
111 | if (_sequenceLength == 0) |
---|
112 | _sequenceLength = _outEncoding.convert(_defaultChar, _buffer, sizeof(_buffer)); |
---|
113 | if (_sequenceLength == 0) |
---|
114 | return -1; |
---|
115 | else |
---|
116 | return _buffer[_pos++]; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | int StreamConverterBuf::writeToDevice(char c) |
---|
121 | { |
---|
122 | poco_assert_dbg (_pOstr); |
---|
123 | |
---|
124 | _buffer[_pos++] = (unsigned char) c; |
---|
125 | if (_sequenceLength == 0 || _sequenceLength == _pos) |
---|
126 | { |
---|
127 | int n = _inEncoding.queryConvert(_buffer, _pos); |
---|
128 | if (-1 <= n) |
---|
129 | { |
---|
130 | int uc = n; |
---|
131 | if (-1 == n) |
---|
132 | { |
---|
133 | ++_errors; |
---|
134 | return -1; |
---|
135 | } |
---|
136 | int n = _outEncoding.convert(uc, _buffer, sizeof(_buffer)); |
---|
137 | if (n == 0) n = _outEncoding.convert(_defaultChar, _buffer, sizeof(_buffer)); |
---|
138 | poco_assert_dbg (n <= sizeof(_buffer)); |
---|
139 | _pOstr->write((char*) _buffer, n); |
---|
140 | _sequenceLength = 0; |
---|
141 | _pos = 0; |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | _sequenceLength = -n; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | return charToInt(c); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | int StreamConverterBuf::errors() const |
---|
154 | { |
---|
155 | return _errors; |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | StreamConverterIOS::StreamConverterIOS(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): |
---|
160 | _buf(istr, inEncoding, outEncoding, defaultChar) |
---|
161 | { |
---|
162 | poco_ios_init(&_buf); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | StreamConverterIOS::StreamConverterIOS(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): |
---|
167 | _buf(ostr, inEncoding, outEncoding, defaultChar) |
---|
168 | { |
---|
169 | poco_ios_init(&_buf); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | StreamConverterIOS::~StreamConverterIOS() |
---|
174 | { |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | StreamConverterBuf* StreamConverterIOS::rdbuf() |
---|
179 | { |
---|
180 | return &_buf; |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | int StreamConverterIOS::errors() const |
---|
185 | { |
---|
186 | return _buf.errors(); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | InputStreamConverter::InputStreamConverter(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): |
---|
191 | StreamConverterIOS(istr, inEncoding, outEncoding, defaultChar), |
---|
192 | std::istream(&_buf) |
---|
193 | { |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | InputStreamConverter::~InputStreamConverter() |
---|
198 | { |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | OutputStreamConverter::OutputStreamConverter(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): |
---|
203 | StreamConverterIOS(ostr, inEncoding, outEncoding, defaultChar), |
---|
204 | std::ostream(&_buf) |
---|
205 | { |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | OutputStreamConverter::~OutputStreamConverter() |
---|
210 | { |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | } // namespace Poco |
---|