| 1 | // |
| 2 | // StreamConverter.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Text |
| 6 | // Module: StreamConverter |
| 7 | // |
| 8 | // Definition of the StreamConverter class. |
| 9 | // |
| 10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_StreamConverter_INCLUDED |
| 18 | #define Foundation_StreamConverter_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/TextEncoding.h" |
| 23 | #include "Poco/UnbufferedStreamBuf.h" |
| 24 | #include <istream> |
| 25 | #include <ostream> |
| 26 | |
| 27 | |
| 28 | namespace Poco { |
| 29 | |
| 30 | |
| 31 | class Foundation_API StreamConverterBuf: public UnbufferedStreamBuf |
| 32 | /// A StreamConverter converts streams from one encoding (inEncoding) |
| 33 | /// into another (outEncoding). |
| 34 | /// If a character cannot be represented in outEncoding, defaultChar |
| 35 | /// is used instead. |
| 36 | /// If a byte sequence is not valid in inEncoding, defaultChar is used |
| 37 | /// instead and the encoding error count is incremented. |
| 38 | { |
| 39 | public: |
| 40 | StreamConverterBuf(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
| 41 | /// Creates the StreamConverterBuf and connects it |
| 42 | /// to the given input stream. |
| 43 | |
| 44 | StreamConverterBuf(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
| 45 | /// Creates the StreamConverterBuf and connects it |
| 46 | /// to the given output stream. |
| 47 | |
| 48 | ~StreamConverterBuf(); |
| 49 | /// Destroys the StreamConverterBuf. |
| 50 | |
| 51 | int errors() const; |
| 52 | /// Returns the number of encoding errors encountered. |
| 53 | |
| 54 | protected: |
| 55 | int readFromDevice(); |
| 56 | int writeToDevice(char c); |
| 57 | |
| 58 | private: |
| 59 | std::istream* _pIstr; |
| 60 | std::ostream* _pOstr; |
| 61 | const TextEncoding& _inEncoding; |
| 62 | const TextEncoding& _outEncoding; |
| 63 | int _defaultChar; |
| 64 | unsigned char _buffer[TextEncoding::MAX_SEQUENCE_LENGTH]; |
| 65 | int _sequenceLength; |
| 66 | int _pos; |
| 67 | int _errors; |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | class Foundation_API StreamConverterIOS: public virtual std::ios |
| 72 | /// The base class for InputStreamConverter and OutputStreamConverter. |
| 73 | /// |
| 74 | /// This class is needed to ensure the correct initialization |
| 75 | /// order of the stream buffer and base classes. |
| 76 | { |
| 77 | public: |
| 78 | StreamConverterIOS(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
| 79 | StreamConverterIOS(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
| 80 | ~StreamConverterIOS(); |
| 81 | StreamConverterBuf* rdbuf(); |
| 82 | int errors() const; |
| 83 | |
| 84 | protected: |
| 85 | StreamConverterBuf _buf; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | class Foundation_API InputStreamConverter: public StreamConverterIOS, public std::istream |
| 90 | /// This stream converts all characters read from the |
| 91 | /// underlying istream from one character encoding into another. |
| 92 | /// If a character cannot be represented in outEncoding, defaultChar |
| 93 | /// is used instead. |
| 94 | /// If a byte sequence read from the underlying stream is not valid in inEncoding, |
| 95 | /// defaultChar is used instead and the encoding error count is incremented. |
| 96 | { |
| 97 | public: |
| 98 | InputStreamConverter(std::istream& istr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
| 99 | /// Creates the InputStreamConverter and connects it |
| 100 | /// to the given input stream. |
| 101 | |
| 102 | ~InputStreamConverter(); |
| 103 | /// Destroys the stream. |
| 104 | }; |
| 105 | |
| 106 | |
| 107 | class Foundation_API OutputStreamConverter: public StreamConverterIOS, public std::ostream |
| 108 | /// This stream converts all characters written to the |
| 109 | /// underlying ostream from one character encoding into another. |
| 110 | /// If a character cannot be represented in outEncoding, defaultChar |
| 111 | /// is used instead. |
| 112 | /// If a byte sequence written to the stream is not valid in inEncoding, |
| 113 | /// defaultChar is used instead and the encoding error count is incremented. |
| 114 | { |
| 115 | public: |
| 116 | OutputStreamConverter(std::ostream& ostr, const TextEncoding& inEncoding, const TextEncoding& outEncoding, int defaultChar = '?'); |
| 117 | /// Creates the OutputStreamConverter and connects it |
| 118 | /// to the given input stream. |
| 119 | |
| 120 | ~OutputStreamConverter(); |
| 121 | /// Destroys the CountingOutputStream. |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | } // namespace Poco |
| 126 | |
| 127 | |
| 128 | #endif // Foundation_StreamConverter_INCLUDED |
| 129 | |