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
18namespace Poco {
19
20
21HexBinaryEncoderBuf::HexBinaryEncoderBuf(std::ostream& ostr):
22 _pos(0),
23 _lineLength(72),
24 _uppercase(0),
25 _buf(*ostr.rdbuf())
26{
27}
28
29
30HexBinaryEncoderBuf::~HexBinaryEncoderBuf()
31{
32 try
33 {
34 close();
35 }
36 catch (...)
37 {
38 }
39}
40
41
42void HexBinaryEncoderBuf::setLineLength(int lineLength)
43{
44 _lineLength = lineLength;
45}
46
47
48int HexBinaryEncoderBuf::getLineLength() const
49{
50 return _lineLength;
51}
52
53
54void HexBinaryEncoderBuf::setUppercase(bool flag)
55{
56 _uppercase = flag ? 16 : 0;
57}
58
59
60int 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
77int HexBinaryEncoderBuf::close()
78{
79 sync();
80 return _buf.pubsync();
81}
82
83
84HexBinaryEncoderIOS::HexBinaryEncoderIOS(std::ostream& ostr): _buf(ostr)
85{
86 poco_ios_init(&_buf);
87}
88
89
90HexBinaryEncoderIOS::~HexBinaryEncoderIOS()
91{
92}
93
94
95int HexBinaryEncoderIOS::close()
96{
97 return _buf.close();
98}
99
100
101HexBinaryEncoderBuf* HexBinaryEncoderIOS::rdbuf()
102{
103 return &_buf;
104}
105
106
107HexBinaryEncoder::HexBinaryEncoder(std::ostream& ostr): HexBinaryEncoderIOS(ostr), std::ostream(&_buf)
108{
109}
110
111
112HexBinaryEncoder::~HexBinaryEncoder()
113{
114}
115
116
117} // namespace Poco
118