aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/src/StreamConverter.cpp
blob: aa626fbae47414725ad21637669a8919a08ed122 (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 
// StreamConverter.cpp 
// 
// Library: Foundation 
// Package: Text 
// Module:  StreamConverter 
// 
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 
// and Contributors. 
// 
// SPDX-License-Identifier:	BSL-1.0 
// 
 
 
#include "Poco/StreamConverter.h" 
#include "Poco/TextEncoding.h" 
 
 
namespace Poco { 
 
 
StreamConverterBuf::StreamConverterBuf(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): 
	_pIstr(&istr), 
	_pOstr(0), 
	_inEncoding(inEncoding), 
	_outEncoding(outEncoding), 
	_defaultChar(defaultChar), 
	_sequenceLength(0), 
	_pos(0), 
	_errors(0) 
{ 
} 
 
 
StreamConverterBuf::StreamConverterBuf(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar): 
	_pIstr(0), 
	_pOstr(&ostr), 
	_inEncoding(inEncoding), 
	_outEncoding(outEncoding), 
	_defaultChar(defaultChar), 
	_sequenceLength(0), 
	_pos(0), 
	_errors(0) 
{ 
} 
 
 
StreamConverterBuf::~StreamConverterBuf() 
{ 
} 
 
 
int StreamConverterBuf::readFromDevice() 
{ 
	poco_assert_dbg (_pIstr); 
 
	if (_pos < _sequenceLength) return _buffer[_pos++]; 
 
	_pos = 0; 
	_sequenceLength = 0; 
	int c = _pIstr->get(); 
	if (c == -1) return -1;	 
 
	poco_assert (c < 256); 
	int uc; 
	_buffer [0] = (unsigned char) c; 
	int n = _inEncoding.queryConvert(_buffer, 1); 
	int read = 1; 
 
	while (-1 > n) 
	{ 
		poco_assert_dbg(-n <= sizeof(_buffer)); 
		_pIstr->read((char*) _buffer + read, -n - read); 
		read = -n; 
		n = _inEncoding.queryConvert(_buffer, -n); 
	} 
 
	if (-1 >= n) 
	{ 
		uc = _defaultChar; 
		++_errors; 
	} 
	else 
	{ 
		uc = n; 
	} 
 
	_sequenceLength = _outEncoding.convert(uc, _buffer, sizeof(_buffer)); 
	if (_sequenceLength == 0) 
		_sequenceLength = _outEncoding.convert(_defaultChar, _buffer, sizeof(_buffer)); 
	if (_sequenceLength == 0) 
		return -1; 
	else 
		return _buffer[_pos++]; 
} 
 
 
int StreamConverterBuf::writeToDevice(char c) 
{ 
	poco_assert_dbg (_pOstr); 
 
	_buffer[_pos++] = (unsigned char) c; 
	if (_sequenceLength == 0 || _sequenceLength == _pos) 
	{ 
		int n = _inEncoding.queryConvert(_buffer, _pos); 
		if (-1 <= n) 
		{ 
			int uc = n; 
			if (-1 == n) 
			{ 
				++_errors; 
				return -1; 
			} 
			int number = _outEncoding.convert(uc, _buffer, sizeof(_buffer)); 
			if (number == 0) number = _outEncoding.convert(_defaultChar, _buffer, sizeof(_buffer)); 
			poco_assert_dbg (number <= sizeof(_buffer)); 
			_pOstr->write((char*) _buffer, number); 
			_sequenceLength = 0; 
			_pos = 0; 
		} 
		else 
		{ 
			_sequenceLength = -n; 
		} 
	} 
 
	return charToInt(c); 
} 
 
 
int StreamConverterBuf::errors() const 
{ 
	return _errors; 
} 
 
 
StreamConverterIOS::StreamConverterIOS(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):  
	_buf(istr, inEncoding, outEncoding, defaultChar) 
{ 
	poco_ios_init(&_buf); 
} 
 
 
StreamConverterIOS::StreamConverterIOS(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):  
	_buf(ostr, inEncoding, outEncoding, defaultChar) 
{ 
	poco_ios_init(&_buf); 
} 
 
 
StreamConverterIOS::~StreamConverterIOS() 
{ 
} 
 
 
StreamConverterBuf* StreamConverterIOS::rdbuf() 
{ 
	return &_buf; 
} 
 
 
int StreamConverterIOS::errors() const 
{ 
	return _buf.errors(); 
} 
 
 
InputStreamConverter::InputStreamConverter(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):  
	StreamConverterIOS(istr, inEncoding, outEncoding, defaultChar), 
	std::istream(&_buf) 
{ 
} 
 
 
InputStreamConverter::~InputStreamConverter() 
{ 
} 
 
 
OutputStreamConverter::OutputStreamConverter(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar):  
	StreamConverterIOS(ostr, inEncoding, outEncoding, defaultChar), 
	std::ostream(&_buf) 
{ 
} 
 
 
OutputStreamConverter::~OutputStreamConverter() 
{ 
} 
 
 
} // namespace Poco