1 | // |
---|
2 | // StreamConverter.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/StreamConverter.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Text |
---|
8 | // Module: StreamConverter |
---|
9 | // |
---|
10 | // Definition of the StreamConverter class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_StreamConverter_INCLUDED |
---|
40 | #define Foundation_StreamConverter_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/TextEncoding.h" |
---|
45 | #include "Poco/UnbufferedStreamBuf.h" |
---|
46 | #include <istream> |
---|
47 | #include <ostream> |
---|
48 | |
---|
49 | |
---|
50 | namespace Poco { |
---|
51 | |
---|
52 | |
---|
53 | class Foundation_API StreamConverterBuf: public UnbufferedStreamBuf |
---|
54 | /// A StreamConverter converts streams from one encoding (inEncoding) |
---|
55 | /// into another (outEncoding). |
---|
56 | /// If a character cannot be represented in outEncoding, defaultChar |
---|
57 | /// is used instead. |
---|
58 | /// If a byte sequence is not valid in inEncoding, defaultChar is used |
---|
59 | /// instead and the encoding error count is incremented. |
---|
60 | { |
---|
61 | public: |
---|
62 | StreamConverterBuf(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
---|
63 | /// Creates the StreamConverterBuf and connects it |
---|
64 | /// to the given input stream. |
---|
65 | |
---|
66 | StreamConverterBuf(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
---|
67 | /// Creates the StreamConverterBuf and connects it |
---|
68 | /// to the given output stream. |
---|
69 | |
---|
70 | ~StreamConverterBuf(); |
---|
71 | /// Destroys the StreamConverterBuf. |
---|
72 | |
---|
73 | int errors() const; |
---|
74 | /// Returns the number of encoding errors encountered. |
---|
75 | |
---|
76 | protected: |
---|
77 | int readFromDevice(); |
---|
78 | int writeToDevice(char c); |
---|
79 | |
---|
80 | private: |
---|
81 | std::istream* _pIstr; |
---|
82 | std::ostream* _pOstr; |
---|
83 | const TextEncoding& _inEncoding; |
---|
84 | const TextEncoding& _outEncoding; |
---|
85 | int _defaultChar; |
---|
86 | unsigned char _buffer[TextEncoding::MAX_SEQUENCE_LENGTH]; |
---|
87 | int _sequenceLength; |
---|
88 | int _pos; |
---|
89 | int _errors; |
---|
90 | }; |
---|
91 | |
---|
92 | |
---|
93 | class Foundation_API StreamConverterIOS: public virtual std::ios |
---|
94 | /// The base class for InputStreamConverter and OutputStreamConverter. |
---|
95 | /// |
---|
96 | /// This class is needed to ensure the correct initialization |
---|
97 | /// order of the stream buffer and base classes. |
---|
98 | { |
---|
99 | public: |
---|
100 | StreamConverterIOS(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
---|
101 | StreamConverterIOS(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
---|
102 | ~StreamConverterIOS(); |
---|
103 | StreamConverterBuf* rdbuf(); |
---|
104 | int errors() const; |
---|
105 | |
---|
106 | protected: |
---|
107 | StreamConverterBuf _buf; |
---|
108 | }; |
---|
109 | |
---|
110 | |
---|
111 | class Foundation_API InputStreamConverter: public StreamConverterIOS, public std::istream |
---|
112 | /// This stream converts all characters read from the |
---|
113 | /// underlying istream from one character encoding into another. |
---|
114 | /// If a character cannot be represented in outEncoding, defaultChar |
---|
115 | /// is used instead. |
---|
116 | /// If a byte sequence read from the underlying stream is not valid in inEncoding, |
---|
117 | /// defaultChar is used instead and the encoding error count is incremented. |
---|
118 | { |
---|
119 | public: |
---|
120 | InputStreamConverter(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
---|
121 | /// Creates the InputStreamConverter and connects it |
---|
122 | /// to the given input stream. |
---|
123 | |
---|
124 | ~InputStreamConverter(); |
---|
125 | /// Destroys the stream. |
---|
126 | }; |
---|
127 | |
---|
128 | |
---|
129 | class Foundation_API OutputStreamConverter: public StreamConverterIOS, public std::ostream |
---|
130 | /// This stream converts all characters written to the |
---|
131 | /// underlying ostream from one character encoding into another. |
---|
132 | /// If a character cannot be represented in outEncoding, defaultChar |
---|
133 | /// is used instead. |
---|
134 | /// If a byte sequence written to the stream is not valid in inEncoding, |
---|
135 | /// defaultChar is used instead and the encoding error count is incremented. |
---|
136 | { |
---|
137 | public: |
---|
138 | OutputStreamConverter(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
---|
139 | /// Creates the OutputStreamConverter and connects it |
---|
140 | /// to the given input stream. |
---|
141 | |
---|
142 | ~OutputStreamConverter(); |
---|
143 | /// Destroys the CountingOutputStream. |
---|
144 | }; |
---|
145 | |
---|
146 | |
---|
147 | } // namespace Poco |
---|
148 | |
---|
149 | |
---|
150 | #endif // Foundation_StreamConverter_INCLUDED |
---|