| 1 | // | 
|---|---|
| 2 | // HexBinaryEncoder.cpp | 
| 3 | // | 
| 4 | // Library: Foundation | 
| 5 | // Package: Streams | 
| 6 | // Module: HexBinary | 
| 7 | // | 
| 8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. | 
| 9 | // and Contributors. | 
| 10 | // | 
| 11 | // SPDX-License-Identifier: BSL-1.0 | 
| 12 | // | 
| 13 | |
| 14 | |
| 15 | #include "Poco/HexBinaryEncoder.h" | 
| 16 | |
| 17 | |
| 18 | namespace Poco { | 
| 19 | |
| 20 | |
| 21 | HexBinaryEncoderBuf::HexBinaryEncoderBuf(std::ostream& ostr): | 
| 22 | _pos(0), | 
| 23 | _lineLength(72), | 
| 24 | _uppercase(0), | 
| 25 | _buf(*ostr.rdbuf()) | 
| 26 | { | 
| 27 | } | 
| 28 | |
| 29 | |
| 30 | HexBinaryEncoderBuf::~HexBinaryEncoderBuf() | 
| 31 | { | 
| 32 | try | 
| 33 | { | 
| 34 | close(); | 
| 35 | } | 
| 36 | catch (...) | 
| 37 | { | 
| 38 | } | 
| 39 | } | 
| 40 | |
| 41 | |
| 42 | void HexBinaryEncoderBuf::setLineLength(int lineLength) | 
| 43 | { | 
| 44 | _lineLength = lineLength; | 
| 45 | } | 
| 46 | |
| 47 | |
| 48 | int HexBinaryEncoderBuf::getLineLength() const | 
| 49 | { | 
| 50 | return _lineLength; | 
| 51 | } | 
| 52 | |
| 53 | |
| 54 | void HexBinaryEncoderBuf::setUppercase(bool flag) | 
| 55 | { | 
| 56 | _uppercase = flag ? 16 : 0; | 
| 57 | } | 
| 58 | |
| 59 | |
| 60 | int HexBinaryEncoderBuf::writeToDevice(char c) | 
| 61 | { | 
| 62 | static const int eof = std::char_traits<char>::eof(); | 
| 63 | static const char digits[] = "0123456789abcdef0123456789ABCDEF"; | 
| 64 | |
| 65 | if (_buf.sputc(digits[_uppercase + ((c >> 4) & 0xF)]) == eof) return eof; | 
| 66 | ++_pos; | 
| 67 | if (_buf.sputc(digits[_uppercase + (c & 0xF)]) == eof) return eof; | 
| 68 | if (++_pos >= _lineLength && _lineLength > 0) | 
| 69 | { | 
| 70 | if (_buf.sputc('\n') == eof) return eof; | 
| 71 | _pos = 0; | 
| 72 | } | 
| 73 | return charToInt(c); | 
| 74 | } | 
| 75 | |
| 76 | |
| 77 | int HexBinaryEncoderBuf::close() | 
| 78 | { | 
| 79 | sync(); | 
| 80 | return _buf.pubsync(); | 
| 81 | } | 
| 82 | |
| 83 | |
| 84 | HexBinaryEncoderIOS::HexBinaryEncoderIOS(std::ostream& ostr): _buf(ostr) | 
| 85 | { | 
| 86 | poco_ios_init(&_buf); | 
| 87 | } | 
| 88 | |
| 89 | |
| 90 | HexBinaryEncoderIOS::~HexBinaryEncoderIOS() | 
| 91 | { | 
| 92 | } | 
| 93 | |
| 94 | |
| 95 | int HexBinaryEncoderIOS::close() | 
| 96 | { | 
| 97 | return _buf.close(); | 
| 98 | } | 
| 99 | |
| 100 | |
| 101 | HexBinaryEncoderBuf* HexBinaryEncoderIOS::rdbuf() | 
| 102 | { | 
| 103 | return &_buf; | 
| 104 | } | 
| 105 | |
| 106 | |
| 107 | HexBinaryEncoder::HexBinaryEncoder(std::ostream& ostr): HexBinaryEncoderIOS(ostr), std::ostream(&_buf) | 
| 108 | { | 
| 109 | } | 
| 110 | |
| 111 | |
| 112 | HexBinaryEncoder::~HexBinaryEncoder() | 
| 113 | { | 
| 114 | } | 
| 115 | |
| 116 | |
| 117 | } // namespace Poco | 
| 118 | 
