| 1 | // |
| 2 | // HexBinaryEncoder.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Streams |
| 6 | // Module: HexBinary |
| 7 | // |
| 8 | // Definition of the HexBinaryEncoder 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_HexBinaryEncoder_INCLUDED |
| 18 | #define Foundation_HexBinaryEncoder_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/UnbufferedStreamBuf.h" |
| 23 | #include <ostream> |
| 24 | |
| 25 | |
| 26 | namespace Poco { |
| 27 | |
| 28 | |
| 29 | class Foundation_API HexBinaryEncoderBuf: public UnbufferedStreamBuf |
| 30 | /// This streambuf encodes all data written |
| 31 | /// to it in hexBinary encoding and forwards it to a connected |
| 32 | /// ostream. |
| 33 | /// In hexBinary encoding, each binary octet is encoded as a character tuple, |
| 34 | /// consisting of two hexadecimal digits ([0-9a-fA-F]) representing the octet code. |
| 35 | /// See also: XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/), |
| 36 | /// section 3.2.15. |
| 37 | /// |
| 38 | /// Note: The characters are directly written |
| 39 | /// to the ostream's streambuf, thus bypassing |
| 40 | /// the ostream. The ostream's state is therefore |
| 41 | /// not updated to match the buffer's state. |
| 42 | { |
| 43 | public: |
| 44 | HexBinaryEncoderBuf(std::ostream& ostr); |
| 45 | ~HexBinaryEncoderBuf(); |
| 46 | |
| 47 | int close(); |
| 48 | /// Closes the stream buffer. |
| 49 | |
| 50 | void setLineLength(int lineLength); |
| 51 | /// Specify the line length. |
| 52 | /// |
| 53 | /// After the given number of characters have been written, |
| 54 | /// a newline character will be written. |
| 55 | /// |
| 56 | /// Specify 0 for an unlimited line length. |
| 57 | |
| 58 | int getLineLength() const; |
| 59 | /// Returns the currently set line length. |
| 60 | |
| 61 | void setUppercase(bool flag = true); |
| 62 | /// Specify whether hex digits a-f are written in upper or lower case. |
| 63 | |
| 64 | private: |
| 65 | int writeToDevice(char c); |
| 66 | |
| 67 | int _pos; |
| 68 | int _lineLength; |
| 69 | int _uppercase; |
| 70 | std::streambuf& _buf; |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | class Foundation_API HexBinaryEncoderIOS: public virtual std::ios |
| 75 | /// The base class for HexBinaryEncoder. |
| 76 | /// |
| 77 | /// This class is needed to ensure the correct initialization |
| 78 | /// order of the stream buffer and base classes. |
| 79 | { |
| 80 | public: |
| 81 | HexBinaryEncoderIOS(std::ostream& ostr); |
| 82 | ~HexBinaryEncoderIOS(); |
| 83 | int close(); |
| 84 | HexBinaryEncoderBuf* rdbuf(); |
| 85 | |
| 86 | protected: |
| 87 | HexBinaryEncoderBuf _buf; |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | class Foundation_API HexBinaryEncoder: public HexBinaryEncoderIOS, public std::ostream |
| 92 | /// This ostream encodes all data |
| 93 | /// written to it in BinHex encoding and forwards it to |
| 94 | /// a connected ostream. |
| 95 | /// Always call close() when done |
| 96 | /// writing data, to ensure proper |
| 97 | /// completion of the encoding operation. |
| 98 | /// In hexBinary encoding, each binary octet is encoded as a character tuple, |
| 99 | /// consisting of two hexadecimal digits ([0-9a-fA-F]) representing the octet code. |
| 100 | /// See also: XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/), |
| 101 | /// section 3.2.15. |
| 102 | /// |
| 103 | /// Note: The characters are directly written |
| 104 | /// to the ostream's streambuf, thus bypassing |
| 105 | /// the ostream. The ostream's state is therefore |
| 106 | /// not updated to match the buffer's state. |
| 107 | { |
| 108 | public: |
| 109 | HexBinaryEncoder(std::ostream& ostr); |
| 110 | ~HexBinaryEncoder(); |
| 111 | }; |
| 112 | |
| 113 | |
| 114 | } // namespace Poco |
| 115 | |
| 116 | |
| 117 | #endif // Foundation_HexBinaryEncoder_INCLUDED |
| 118 | |